0

my program is supposed to be an 11 X 11 grid where a mouse is placed on the center square. I am to count all the squares touched (or untouched) until the mouse has occupied every square. I also need to count the number of times the mouse touches the wall. The program will stop when all the squares have been touched.

When i run the program I get the size of the grid =1936 and the number of squares both touched and untouched are 21 and it stops. I was told to set the rand test to 15 until the program is "debugged". I am basically not sure if I have initialized i and j correctly. It is my understanding that to put the mouse down on the center square they would be need to be 5 but i am not sure that's right.

Any suggestions on what I should change in my code in order to get the correct output would be greatly appreciated.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <conio.h>


using namespace std;
// constants

  const int MYMAXROW = 11;
  const int MYMAXCOL = 11;

//header

struct squaretype
{
    int row;     //the row location of this square
    int col;     //the column location of this square
    int frequency;  // number of times touched by mouse
    int order; //to be explained in class
}; 


//function prototype

int allcovered(squaretype grid[][MYMAXCOL]); 
    
void printgrid(squaretype grid[][MYMAXCOL],
              int mouseRow, int mouseCol);
                             
squaretype findmaxLocation (squaretype grid[][MYMAXCOL]);
  

// end of header

//start of main
int main ()
{  
   
  int table [11][11];
  squaretype grid[MYMAXROW][MYMAXCOL];
  int i = 5; //where mouse starts
  int j = 5; //where mouse starts
  
   
  //rand number test
  //const int arraySize = 10;
  
  //srand(time(0));
  srand(15);
  
  grid[i][j].row = i;
  grid[i][j].col = j;
  grid[i][j].frequency = 0;
  
  //double for loop
  //declaration of two-dimensional array
   
  int location;
  int numberofmoves;
  int move;
   
   
   for (i = 0; i < 11; i++) //initialize and set counter to zero
   {
       for (j = 0; j < 11; j++)
       {
           grid[i][j].frequency = (i * j);
       }
   }
   
   cout << grid << endl;
   cout << "sizeof grid = " << sizeof(grid) << endl; //showing 1936?
   
      
   location = (i * (5 * sizeof(int))) + (j + sizeof(int));
   
   int row, col, wallhit;
   int currentRow = MYMAXROW/2;
   int currentCol = MYMAXCOL/2;
     cout << allcovered(grid) << endl; //gives squares not touched (showing 21)
   grid[currentRow][currentCol].frequency++; //place mouse down
     cout << allcovered(grid) << endl; //squares not touched (showing 21)
  getch();
  
   //still in main  and inside while loop
   
   while (allcovered(grid) && numberofmoves < 100000) //safety variable
   {
    move = rand() % 8; //switch on this
    
    switch (move) //all moving from m generating possible moves
    {
        case 0:
          row = -1;
          col = -1;
          break;
        case 1:
          row = -1;
          col = 0;
          break;
        case 2:
           row = -1;
           col = 1;
           break;
        case 3:
           row = 0;
           col = 1;
           break;
        case 4:
           row = 1;
           col = 1;
           break;
        case 5:
           row = 1;
           col = 0;
           break;
        case 6:
           row = 1;
           col = -1;
           break;
        case 7:
           row = 0;
           col = -1;
           break;
           
      
    }
       
            //in main still in while after switch
            // row hits
            
        if ((currentRow + row) < 0)
        {
            wallhit++;
        }
        else if ((currentRow + row) > MYMAXROW)
        {
            wallhit++;
        }
        else //upate row
        {
            currentRow = currentRow = row;
        }
        
            //column hits
         
        if (((currentCol + col) < 0))
        {
            wallhit++;
        }                     
        else if ((currentCol + col) > MYMAXCOL)
        {
            wallhit++;
        }
        else //update row
        {
            currentCol = currentCol = col;
        }    
        
        grid[currentRow][currentCol].frequency++; //update grid will go to 0
        
        numberofmoves++; //counter growing while grid shrinks
        
        if (numberofmoves % 100) //shows location of mouse
        {
             printgrid(grid, currentRow, currentCol);
            //possibly print out # of moves
            
            cout << allcovered;
        }    
   }    //end of while - did mouse touch all squares
   
   //report responses - max, how many times did mouse hit wall
   //how many times squares hit
   
   if(allcovered(grid)==0)
     {
        cout << printgrid; //print results
     }   
   else
     {
        //notify user
     }   
      
   
  getch();
  return 0;

}// end of main

//footer

int allcovered(squaretype grid[][MYMAXCOL]) //get to 0
{
     int retval;
     int i, j; //need to assign value
     retval = 0; //counter counting # of squares
     for (i=0; i < MYMAXROW; i++)
     {
         for (j = 0; j < MYMAXCOL; j++)
         {
            if(grid[i][j].frequency==0)
            {    
              retval++;
            }
         }
     }
 
 return(retval); //1st time thru 121,120 all true til it hits 0
 
 //get to 0 - done
 
}                      
void printgrid(squaretype grid[][MYMAXCOL],
               int mouseRow, int mouseCol) //has mouse been here or not
                                        //not necessarily needed/prints grid
                                       //doesnt need return stmt                                        
{
    int i, j;
    for (i = 0; i < MYMAXROW; i++)
    {
        for(j = 0; j < MYMAXCOL; j++)
        {
          if((i==mouseRow)&&(j==mouseCol))
          {
            cout << "Mouse is here";
          }
          else if(grid[i][j].frequency > 0)
          {
            cout << "X";
          }   
          else
          {
            cout << "O";
          }
          
        } 
      
   } //end outer forloop
   //cout << endl;
}//end print grid

squaretype findmaxLocation (squaretype grid[][MYMAXCOL])
{
   int retval = 0; //counter counting # of squares
   int i, j; //need to assign value
   
   for (i=0; i < MYMAXCOL; j++)
   {
       for (j=0; j < MYMAXCOL; j++)
       {
           if(grid[i][j].frequency==0)
           {
               retval++;
           }
       }
   } 
}
1
Contributor
1
Reply
2
Views
8 Years
Discussion Span
Last Post by wcmport
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.