/*
 * Solution to Problem 4 (C++)
 * 14th Annual CS Programming Contest
 * WCU Dept of Math and CS
 * Copyright 2003 , All Rights Reserved
 *
 * Problem Statement:
 * Random Walk
 * This program simulates a random walk around a board.  Each step
 * around the board is recorded on a 10 by 10 grid.  A step can be 
 * in any of eight directions (n,s,e,w,ne,se,nw,sw) and can 
 * "wrap around" the board.
 */
#include <stdlib.h>
#include <iostream.h>
main()
{

  int rowDirec, colDirec; //variables to indicate direction
  int row, col; //variables to hold current location on grid
  //Create and initialize grid
  int grid[10][10];
  for (int j=0; j<10; j++)
     for (int k=0; k<10; k++)
         grid[j][k] = 0;
  

  // Seed the random number generator 
  // by calling srand with the time
  srand((unsigned)time(0));

  //Determine random placement of walker on grid
  row = random()%10;
  col = random()%10;
      
  //Walk around the board n**4 steps
  for (int i=0; i<10000; i++)
  {
     grid[row][col]++;

     // Select directions for row and column movement.
     // Choose again if no move chosen (that is, if row and col 
     // directions are both 0).
     do {
        rowDirec = (random()%3) - 1;
        colDirec = (random()%3) - 1;
       } while ((rowDirec == 0) && (colDirec == 0));

     //Move in new direction
     row = (row + rowDirec+ 10)%10; //mod operator ensures "wrap"
     col = (col + colDirec+ 10)%10;
  }

  //Output grid
  cout <<"Here is a record of the random walk:"<<endl;
  for (int m=0; m<10; m++)
  {
     for (int n=0; n<10; n++)
       cout << grid[m][n] << "\t" ;
     cout << endl;        
   }
}




