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;
}
| |