Hello!! So I've tried looking around the forums and haven't been able to find exactly what i needed so here I am.
I have a homework assignment that is sort of a cat and mouse game on a 8 by 8 grid. The cat always starts at the far bottom left which is (7,0) on my grid, and there are 3 mice that are randomly put in.
So far I have created the 8 by 8 grid and have the cat starting at (7,0). The part that I am stuck on is figuring out how to randomly put in 3 mice when i start up the game. Does anyone have any tips that i can do or steps that I should do? The assignment itself has steps but they are so vague and since I am still new to programming I kind of need step by steps instructions. Too bad my book only covers 2d arrays for 3 pages :(.
I have been stuck on this for several hours so maybe I am just overlooking something or just missing something. Im going to get some sleep to rest my brain and come back and work on it in the morning. Hopefully you guys can help :).
// Cat and Mouse.cpp : main project file.
#include "stdafx.h"//Used with MS VC++ 2008 Express. Delete this line if using a different IDE
#include <ctime>
#include <iostream>
usingnamespace std;
int main()
{
time_t t;
srand((unsigned) time(&t));
char maze[8][8];
int x, y, count;
for(x=0;x<8;x++)
for (y=0;y<8;y++)
maze[x][y] = '.';
maze[7][0] = 'C'; // Cat
for (count =0;count<3;count++)
{
do
{
x=rand()%8; // random row
y=rand()%8; // random column
}while (maze[x][y]!='.'); // So we don't over-write a used space
maze[x][y]='M';// place mouse at maze[x][y]
}
for(x=0;x<8;x++)
{
for (y=0;y<8;y++)
{
cout << maze[x][y]<< " ";
}
cout << endl;
}
return 0;
}
You already have an x and y, variable, so you can just re-use them. x=7, y =0. Location in maze. Make a do/while loop. Do while count !=0. If the user presses, 'u' for up, check that x-1 is not less than zero. Check maze[x-1][y] to see if it contains a 'M'. Inform the user if he/she caught a mouse, and then have maze[x][y] equal '.', and maze[x-1][y] to equal 'C'. Also have the x to decrease by one, x--. Do the same for the other three directions, as well. Don't forget to decrease count after each mouse is caught, as well..
Thanks for the tip! ill try to implement it and see where it goes!
Oh and to move the mouse i have to use cordinates so i dont think that the 'u' for up works for this case, ( i should have mentioned that earlier)