Cat and mouse Problem

Hi, I've been trying to do this program for a while now but i have a problem for some reason.
I am supposed to make a program where I have a 8*8 2D array with a mouse situated in the center of an island surrounded with by 1*1 2D array of water and a cat that is located in another fixed location (away from the mouse) The mouse is allowed to move one tile per move and can not move diagonally.
The movement is determined by a random number generator that is limited to 0,1,2 and 3 {each number represents a direction). The mouse is allowed 100 moves and then the game stops, it also stops when the mouse bumps into the cat (death), crosses a bridge that is situated in the middle of the right side(escape)or goes into water (drown) the program is supposed to run a couple of times and record the number of deaths, escapes and drowns.but I keep getting zero deaths, zero escapes and zero drowns every time i try to run the program.
here is my code:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<stdlib.h>
#include <time.h>
using namespace std; 
#define N 9
#define M 9
int direction(int x, int A[M][N], int row, int col)
{
	int m;
	if (x == 0)
		m = A[row - 1][col]; //North
	if (x == 1)
		m = A[row+1][col]; //South
	if (x == 2)
		m = A[row][col - 1]; //West
	if (x == 3)
		m = A[row][col + 1]; //East
	return m;
}

int main()
{

	int v, S1, S2, S3, S4;
	int Mouse, Cat, Bridge;
	int death=0, escape=0, drown=0;
	int A[M][N];
	for (int i = 0;i < M;i++)
		S1 = A[i][0] = 4;
	for (int i = 0;i < M;i++)
		S2 = A[i][8] = 4;
	for (int j = 0;j < N;j++)
		S3 = A[0][j] = 4;
	for (int j = 0;j < N;j++)
		S4 = A[8][j] = 4;
	for (int i = 1;i < M;i++)
	{
		for (int j = 1;j < N;j++)
			A[i][j] = 0;
	}
	Mouse = A[4][4] = 1; //mouse
	Cat = A[5][2] = 2; //cat
	Bridge = A[4][8] = 3; //bridge
	for (int b = 0;b < 100;b++)
	{
		srand((unsigned)time(NULL));
		v = rand() % 4;
		if (direction(v, A, 4, 4) == Cat)
		{
			death++;
			return 0;
		}
		else if (direction(v, A, 4, 4) == Bridge)
		{
			escape++;
			return 0;
		}
		else if (direction(v, A, 4, 4) == S1 || direction(v, A, 4, 4) == S2 || direction(v, A, 4, 4) == S3 || direction(v, A, 4, 4) == S4)
		{
			drown++;
			return 0;
		}
	}
	cout << "The mouse died " << death << " times and escaped " << escape << " times and drowned " << drown << " times" << endl;
	return 0;
}
Last edited on
First, please use code tags. See http://www.cplusplus.com/articles/jEywvCM9/


Second, mouse, cat, bridge and water are not integers. Each of them has position -- an {x,y} pair. Movement of the mouse updates its position.

You can have an array A for testing:
1
2
3
4
5
switch ( A[mouse.y][mouse.x] ) {
  case 1: // cat ...
  case 2: // bridge ...
  // ...
}


Alternatively, you test without any array:
1
2
3
if ( mouse == cat ) ...
else if ( mouse == bridge ) ...
elsel if ( inwater( mouse ) ) ...
Thanks for your reply, @Keskiverto. I am still new here sorry.
If I can't initialize mouse,cat, bridge and water as integers, how can I declare them?
Also, Is there any other problem with the code?
Much Appreciated
Last edited on
Topic archived. No new replies allowed.