//**************************************************************
//
// CS 240B Winter Quarter 2003 Ohio University Travis Dillon
// Proj3 : Monte Carlo
// file : methods.cc
// started : 01-30-03
//
//**************************************************************
#include "prog3.h"
#include "methods.h"
int num_steps(int N)
{
int K(0);
int x(N/2);
int y(N/2);
int num(1);
while((x < N)&&(x >= 0)&&(y < N)&&(y >= 0)) //king still on board
{
K++;
num = rand() % 8;
switch (num)
{ // 6 7 0
case 0: x++; y++; break; // 5 K 1
case 1: x++; break; // 4 3 2
case 2: x++; y--; break;
case 3: y--; break;
case 4: x--; y--; break;
case 5: x--; break;
case 6: x--; y++; break;
case 7: y++; break;
}
}
return K;
}
void fill_num(char num_arr[])
{
for(int i(0); i < 10; ++i) //all integers
{
num_arr[i] = static_cast<char>(i+48);
}
for(int i(10); i < 36; ++i) //all lower case
{
num_arr[i] = static_cast<char>(i+87);
}
for(int i(36); i < 62; ++i) //all upper case
{
num_arr[i] = static_cast<char>(i+29);
}
}
void user_info()
{
char screen[SCR_ROWS][SCR_COLS];
int num_runs; //number of board sizes
int first; //first board size
int incr; //board size increment
int num_iters; //number of trials
char num[62]; //array of numbers and chars
int max_ele(0); //largest number of occurences
int scale; //scale to keep output on screen
fill_num(num); //fill array with chars and ints
cout<<"\nHow many board sizes? ";
cin >>num_runs;
cout<<"What is first board size? ";
cin >>first;
cout<<"What is the increment? ";
cin >>incr;
cout<<"How many trials for each board? ";
cin >>num_iters;
int **bin;
bin = new int*[num_runs];
for(int i(0); i<num_runs; ++i) bin[i] = new int [SCR_COLS];
for(int r(0); r < num_runs; ++r) //initialize to all 0's
{
for(int c(0); c < SCR_COLS; ++c)
{
bin[r][c] = 0;
}
}
for(int r(0); r < SCR_ROWS; ++r) //initialize to all blanks
{
for(int c(0); c < SCR_COLS; ++c)
{
screen[r][c] = ' ';
}
}
for(int i(0); i < num_runs; ++i)
{
int z = first + i * incr;
for(int j(0); j < num_iters; ++j)
{
int n = (num_steps(z)); //number of steps
if(n < SCR_COLS)
{
bin[i][n]++; //increment bin
if(bin[i][n] > max_ele) max_ele = bin[i][n];
}
}
}
scale = (max_ele + SCR_ROWS)/SCR_ROWS;
for(int i(0); i < num_runs; ++i) //scale bin to fin on screen
{
for(int j(0); j < SCR_COLS; ++j)
{
bin[i][j] = bin[i][j] / scale;
}
}
for(int i(0); i < num_runs; ++i) //fill screen with data from bin
{
for(int j(0); j < SCR_COLS; ++j)
{
if(bin[i][j] != 0)
{
screen[bin[i][j]][j] = num[first + i*incr];
}
}
}
print_graph(screen, num_runs, first, incr, num_iters,
scale, max_ele);
for(int i(0); i < num_runs; ++i) delete [] bin[i];
delete [] bin;
}
void print_graph(char scr_arr[SCR_ROWS][SCR_COLS], int num_runs, int first,
int incr, int num_iters, int scale, int max_ele)
{
ofstream fout;
fout.open("graph.txt");
cout <<"\n\nNumber of board sizes = " << num_runs //output user info
<<"\nFirst board size = " << first
<<"\nBoard increment size = " << incr
<<"\nNumber of trials = " << num_iters
<<"\nscale = " << scale
<<"\nmax_ele = " << max_ele << endl;
fout <<"\n\nNumber of board sizes = " << num_runs
<<"\nFirst board size = " << first
<<"\nBoard increment size = " << incr
<<"\nNumber of trials = " << num_iters
<<"\nscale = " << scale
<<"\nmax_ele = " << max_ele << endl;
for(int i(0); i < SCR_ROWS; ++i) //ouput screen in graph form
{
for(int j(0); j < SCR_COLS; ++j)
{
cout << scr_arr[SCR_ROWS - i - 1][j];
fout << scr_arr[SCR_ROWS - i - 1][j];
}
fout << endl;
}
fout.close();
}