Using 2d arrays to make a game

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 :).

Thanks,
Ben
Could you use a simple loop and random system, incrementing some counter that records each mouse placed. Counter = 3 then stop adding mouse
@bubbles

This program places 3 mice, char 'M', into random spaces in the maze grid, with a char 'C' at 7,0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 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>

using namespace 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;
}
Last edited on
Thanks a lot white, this code was somewhat similar to mine, except yours ofcourse worked :P

now to just figure out how move the cat to eat the mice.
@bubbles

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..
Last edited on
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)
Last edited on
Topic archived. No new replies allowed.