//casestudy 1 the drunken cockroach/*start of the program*/#include<stdio. h>#include<conio. h>#include<stdlib. h>#include<dos. h>void main(){int iteration,sizerow,sizecol,imove[8],jmove[8],k,ibug,jbug,a,b,legal=0,boundaryrow,boundarycol,move=0,count[40][40]={0};char tile[40][40];clrscr();printf(“The Drunken Cockroach Simulator! n”);printf(“Note:
Sometimes this cockroach steps on the already stepped tiles because its drunken”);printf(”
‘S’ – stands for the starting point
‘C’ – stands for the stepped tiles
‘Q’ – stands for unwalkable tiles”);printf(”
Input the size of the room you want (row,column): “);scanf(“%d,%d”,&sizerow,&sizecol);printf(“Input the cockroach’s starting point (row,column): “);scanf(“%d,%d”,&jbug,&ibug);printf(“Input the number of cockroach’s move you want: “);scanf(“%d”,&iteration);clrscr();boundaryrow=sizerow;boundarycol=sizecol;for(a=-1;a<=sizerow;a++){for(b=-1;b<=sizecol;b++){if(a==jbug&&b==ibug)tile[a][b]=’S’;else if(a<0||b<0||a>=boundarycol||b>=boundaryrow)tile[a][b]=’Q’;elsetile[a][b]=’ ‘;}}do{move++;clrscr();imove[0]=ibug-1;imove[1]=ibug;imove[2]=ibug+1;imove[3]=ibug+1;imove[4]=ibug+1;imove[5]=ibug;imove[6]=ibug-1;imove[7]=ibug-1;jmove[0]=jbug+1;jmove[1]=jbug+1;jmove[2]=jbug+1;jmove[3]=jbug;jmove[4]=jbug-1;jmove[5]=jbug-1;jmove[6]=jbug-1;jmove[7]=jbug;randomize();k=rand()%8;if(jmove[k]<0||jmove[k]>=boundarycol||imove[k]<0||imove[k]>=boundaryrow||jmove[k]>boundaryrow||imove[k]>boundarycol){randomize();ibug=rand()%boundarycol+1;jbug=rand()%boundaryrow+1;}else{jbug=jmove[k];ibug=imove[k];tile[jbug][ibug]=’C’;legal++;}count[jbug][ibug]++;for(a=-1;a<=boundaryrow;a++){for(b=-1;b<=boundarycol;b++) {if(tile[a][b]==’C’){delay(30);printf(“%c”,tile[a][b]);tile[a][b]=’*’;delay(15);}else if(tile[a][b]==’*’)printf(“%c”,tile[a][b]); else if(a<0||b<0||a>=boundaryrow||b>=boundarycol){tile[a][b]=’Q’;printf(“%c”,tile[a][b]);}elseprintf(“%c”,tile[a][b]);}printf(”
“);}printf(”

Legal moves count = %d “,legal);printf(”
Cockcroach’s move count =

Categories: Articles

x

Hi!
I'm Iren!

Would you like to get a custom essay? How about receiving a customized one?

Check it out