You are working for a computer gaming company and have been asked to look at a s
ID: 3640356 • Letter: Y
Question
You are working for a computer gaming company and have been asked to look at a survival game. You will be simulating a rabbit stranded on an island, collecting a large number of simulations of their efforts to get off the island. The island will be simulated by a 7-by-7 grid of uniform squares. It is surrounded, on all four sides, by water, 1 square wide. Only problem is that the water is full of piranhas, but on each side there three randomly-placed bridges to freedom. The corners are “reachable” so there can be bridges there and the rabbit can escape through the corners. It could be depicted as this (remember that the “bridges” will be randomlyplaced differently each time the program is executed):
The goal is for the survivor rabbit to get off the island by “hopping” onto one of the bridges to the mainland. If the rabbit hops into the water it is eaten. If the rabbit takes more than 20 hops and is still on the island then it starves. In all cases, either successfully “escaping” or dying in the effort, the program will print the number of hops that particular rabbit took.
(1) Need to generate random numbers
(2) Equate a random number to a direction of hop, for example, 1=up, 2=right
(3) Keep track of the rabbit's location (row, column). If the rabbit
hops onto a bridge then it has escaped. If it hops into water it dies
(4) Keep track of total number of hops. If hops=20 and it's still on the island, the rabbit dies
(5) Once above is working then put into a loop
Explanation / Answer
please rate - thanks
this is not totally debugged. I was changing an old program I had, but don't have the time. If you'd like I can finish tomorrow
hope this gets you started
#include<stdio.h>
#include<conio.h>
void move(int*,int*);
void drawisland(int[][9],int,int); //for debugging
int main()
{int isle[9][9]={-1,-1,-1,-1, -1,-1,-1,-1,-1, //-1 is water
-1, 0, 0,0, 0, 0, 0,0,-1,
-1, 0, 0, 0, 0, 0, 0,0,-1, //0 is land
-1, 0, 0, 0, 0, 0, 0,0,-1, //1 is bridge
-1, 0, 0, 0, 0, 0, 0,0,-1,
-1, 0, 0, 0, 0, 0, 0,0,-1,
-1, 0, 0, 0, 0, 0, 0,0,-1,
-1, 0, 0, 0, 0, 0, 0,0,-1,
-1,-1,-1,-1,-1,-1-1,-1,-1};
int i,j,tryy,iloc,jloc,n;
int livecounter[9][9]={0};
/*for(i=1;i<=4;i++) //isle[i][j] is where to start
for(j=1;j<=4;j++)
for(tryy=0;tryy<100;tryy++) //start each square 100 times*/
{for(i=0;i<4;i++)
{for(j=0;j<3;j++)
{n=rand()%7;
if(i==0)
if(isle[0][n+1]==1)
j--;
else
isle[0][n+1]=1;
else if(i==1)
if(isle[8][n+1]==1)
j--;
else
isle[8][n+1]=1;
else if(i==2)
if(isle[n+1][0]==1)
j--;
else
isle[n+1][0]=1;
else if(i==3)
if(isle[n+1][8]==1)
j--;
else
isle[n+1][8]=1;
}
}
}
iloc=1;jloc=1; //i and j to change
while(isle[iloc][jloc]>=0) //do while on land or bridge
{if(isle[iloc][jloc]==1) //at bridge
{livecounter[i][j]++; //lived-count
printf("rabbit crosses bridge %d %d %d ",i,j,livecounter[i][j]); //for debugging
getch(); //for debugging
break;
} //and go to next iteration
drawisland(isle,iloc,jloc); //for debugging
move(&iloc,&jloc);
drawisland(isle,iloc,jloc); //for debugging
}/*
printf("rabbit dies, starting over "); //get rid of later for debugging
}
printf("probability of living starting at location [%d][%d] is: %d%% ",i,j,livecounter[i][j]);
}*/
getch();
return 0;
}
void move(int* i,int* j)
{int direction; //0=left, 1=right, 2=up, 3=down
direction=rand()%4;
printf("started at %d %d ",*i,*j); //for debugging
switch(direction){
case 0: *j=*j-1; break;
case 1: *j=*j+1; break;
case 2: *i=*i-1; break;
case 3: *i=*i+1; return;
}
printf("moves to %d %d ",*i,*j); //for debugging
return;
}
//get rid of all remnants of drawisland after know it works, entire function for debugging
void drawisland(int isle[][9],int m,int n)
{int i,j; //l=land, m=rabbit, w=water, b=bridge
printf("mouse at %d %d ",m,n);
for(i=0;i<9;i++)
{for(j=0;j<9;j++)
{ if(i==m && j==n) //if rabbit is there put him and ignore others
{printf(" r ");
continue;
}
if(isle[i][j]==-1)
printf(" w ");
else if(isle[i][j]==0)
printf(" l ");
else if(isle[i][j]==1)
printf(" b ");
}
printf(" ");
}
printf("____________________");
printf(" ");
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.