In: Computer Science
Description For this part of the assignment, you will create a Grid representing the pacman’s game grid by using a 2D array of Strings as shown in Section 1. Also, you will design the functionality of the pacman as described in Section 2 and represent it in the grid. 1 The Grid The dimensions of the grid is 15 x 15 and the gird is composed of 4 boundaries: north, south, east, and west boundaries as shown in the Figure. The grid has the following characteristics: 1. The boundaries are represented with “X”, and this will block the pacman. 2. There may be obstacles that obstruct the pacman’s movement. 3. There are 4 gates represented with “ ”, and these gates communicates with the opposite gate. E.g., the north gate communicates with the south gate, and the east gate communicates with the west. This means that if the pacman is located in the north gate, next time it moves up will appear in the south gate, similar with the east and west gate. 4. The grid contains cookies represented with “.”. The idea is to collect all the cookies from the grid in order to win the game. Every time the the pacman eats a cookie, the cookie disappears. The Grid has the following fields • grid: is a 2D array of Strings • x-pos: the x-coordinate where the pacman is located • y-pos: the y-coordinate where the pacman is located • counter: a counter that represents the total number of cookies consumed by the pacman In addition, the Grid has the following methods: • initializeGrid(): This method will print a “fresh” new grid with the pacman located in the middle of the grid and the rest of the grid will contain cookies “.”. Except the boundaries of the grid. Your maze shall have four boundaries i.e., north, south, east, and west. The boundaries are represented with an “X”. Each boundary has a gate in the middle that allows the pacman to communicate to the opposite gate. • updateGrid(): will happen after the selection of moving “a”,“s”,“d” or “w” is done. Since these four options will move to west, south, east, or north, then you need to “update” the grid. The way to do this is by passing the x-coordinate and the y-coordinate as arguments to this method. The method then, will update the new position of the pacman (that is, the x and y coordinate from the arguments). Here is where the previous x and y position of the pacman will “disappear” (which is now a blank space “ ”). This method will reflect the new position of the pacman in the grid in case it moved. This method will also reflect the number of cookies consumed. • checkBoundaries(): Before moving the pacman to the new position, this method will check if is possible according the current coordinates. In case is a valid movement, the method will return true. In case the movement is invalid, due to a boundary or through something else, then your method will return false. The Pacman The pacman has the following characteristics: • The pacman has the ability to move through the gates (i.e., north to south, east to west). • The maze is full of cookies (e.g., “.”). The pacman must eat all the cookies to finish the game. Every time the pacman eats a cookie, it disappears from the maze the counter increases by 1. • If pacman movesUp by typing the key w, the pacman’s y-coordinate must increase by one unit. If the pacman’s y-coordinate exceeds the maze’s upper boundary, then the pacman’s y-coordinate AND the maze’s position shall not be updated. • If pacman movesDown by typing the key s, the pacman’s y-coordinate must decrease by one unit. If the pacman’s y-coordinate exceeds the maze’s lower boundary, then the pacman’s y-coordinate AND the maze’s position shall not be updated. • If pacman movesRight, by typing the key d, the pacman’s x-coordinate must increase by one unit. If the pacman’s x-coordinate exceeds the maze’s right boundary, then the pacman’s x-coordinate AND the maze’s position shall not be updated. • If pacman movesLeft, by typing the key a, the pacman’s x-coordinate must decrease by one unit. If the pacman’s x-coordinate exceeds the maze’s left boundary, then the pacman’s x-coordinate AND the maze’s position shall not be updated. Notice that by moving through the y-coordinate the program deals with the rows of the array, similar when dealing with the x-coordinate, the program deals with the columns of array. 3 The Game Engine In order to simulate a game, you must have an engine that keeps looping the position. This is exactly what happens in the old movie theater when they have the 35mm projectors. Here, we will simulate the same idea with a loop: 1. import java.util.Scanner; 2. public class Engine{ 3. public static void main(String [] args){ 4. String[][] grid = new String[10][10]; 5. int col = 5; 6. initializeGrid(grid); 7. grid[5][col] = "P"; 8. Scanner input = new Scanner(System.in); 9. while(true){ 10. print(grid); 11. String option = input.nextLine(); 12. if(option.equals("d")){ 13. // code goes here 14. } 15. // more code goes here 16. } 17. } 18. // methods go here 19. } In Line 4, will create a grid of 10 x 10 of Strings. In Line 7 will store the pacman (’P’) in the middle of the maze. In Line 9 will allow to run your program “forever” until you decide to finish your program. Line 11 will wait for the input from the user to move the pacman (i.e., “a”, “s”, “d”, or “w”). A complete engine can be found in Grid.java.
Answer:
#include<graphics.h>
#include<stdlib.h>
#include<ctype.h>
#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3
void initialise(void);
char endscreen(void);
void startscreen(void);
void music(int);
void vline(unsigned char ,int,int,int,int);
void hline(unsigned char,int,int,int,int);
void drawbox (int,int,int,int,char);
void gamescreen(void);
void size (int,int);
void writechar(char,int,int,int);
void monitor(void);
void getkeyhit(void);
int testkeys(void);
void readchar(int,int,unsigned char*);
void bug(int *,int *,int *,unsigned char *);
void movebugright(int *,int *,unsigned char *);
void movebugleft(int *,int *,unsigned char *);
void movebugup(int *,int *,unsigned char *);
void movebugdown(int *,int *,unsigned char *);
void killeater(void);
int
maze[25][80],score,row,col,ascii,scan,liveslost,delayfactor;
int gd=DETECT,gm,midx,midy,maxx,maxy;
int bugnumber , r[5],c[5],dir[5];
unsigned char charbelow[5];
char far *vid_mem= (char far *) 0xB8000000L ;
void main(void)
{
char ans ;
clrscr();
/* intialize randonm number generator wih a random value */
randomize ();
startscreen();
while (1)
{
/* Initialize variables at thee4 start of each game*/
/*create opening screen*/
initialise();
/* Draw ther screen for the game*/
gamescreen();
/* monitor the movement of the bugs and the eater*/
monitor();
/*create ending screen*/
ans=endscreen();
/*check whether the user wishes to continue playing*/
if(ans== 'N')
break;
}
}
void startscreen(void)
{
char ch;
int i;
/* Intialize the graphices system*/
initgraph (&gd,&gm,"d:\tc\bgi");
/* get maximum x and y screen coordinates*/
maxx = getmaxx();
maxy= getmaxy();
/* calculate the center of the screen*/
midx= maxx/2;
midy=maxy/2;
/* draw a double-lined box*/
setcolor(GREEN);
rectangle (0,0,maxx,midy);
setcolor(BLUE);
rectangle (2,2,maxx-2,maxy-2);
setcolor(YELLOW);
/* Draw two vertical lines*/
line (55,1,55,maxy-2);
line (maxx-55,1,maxx-55,maxy-2);
/*display the string "EATER" ,horizontally */
setcolor(6);
settextjustify (CENTER_TEXT,CENTER_TEXT);
settextstyle (4,HORIZ_DIR,8);
outtextxy (midx,midy,"The PacMan");
/* Place the eater character at random on the start screen*/
int loop;
randomize();
for(loop=0; loop<40;loop++)
{
gotoxy(rand() % 80,rand()%25);
music(1);
printf("%c",2);
delay(100);
}
nosound();
/*play the starting music*/
/* Clear the area enclosed by the double-lined boundry */
setviewport (0,0,maxx,maxy,1);
clearviewport();
/* Draw the screen for display instructions*/
setcolor(BLUE);
rectangle(30,0,maxx-33,maxy);
setcolor(YELLOW);
for (i=15;i<=maxy-15;i+=15)
{
ellipse(15,i,0,360,6,3);
ellipse(maxx-15,i,0,360,6,3);
}
setcolor(5);
settextjustify (CENTER_TEXT,TOP_TEXT);
settextstyle(4,HORIZ_DIR,5);
outtextxy (midx,60,"INSTRUCTION");
/*display instruction*/
settextstyle(2,HORIZ_DIR,5);
outtextxy(midx,110,"You goal:- To eatup all the CRUNCHY MUNCHY
");
outtextxy(midx,130," UNDAY scattered throughtout the BHOOL
BHULLYIAN ");
outtextxy(midx,170,"Your task is not a HALWA! There are 5
Rascals
chasing
you ");
outtextxy(midx,210,"The Eater.You have to watch out for the
bugs
");
outtextxy(midx,250,"To help you ,we have bestowed the PacMan with
3
Janums");
outtextxy(midx,290,"You can move around the BHOOL BHULLYIAN
using
arrow
keys ");
outtextxy(midx,330,"If you are ready to start, press any
key");
setcolor(GREEN);
settextstyle (DEFAULT_FONT,HORIZ_DIR,3);
outtextxy(midx,400,"We wish you Best of luck");
/*Wait for key press*/
while(!kbhit());
/*Flush the keyboard buffer*/
if (getch()==0)
getch();
/*Draaw the screen for asking user level*/
setviewport (0,0,maxx,maxy,1);
clearviewport();
rectangle(midx-200,midy-60,midx+200,midy+60);
settextstyle (DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(midx,60,"Select Speed");
settextstyle (DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(midx,90,"Slow(S)");
outtextxy(midx,105,"Medium(M)");
outtextxy(midx,120,"Fast(F)");
/*Get user level*/
/*flush the keyboard buffer*/
if ((ch=getch())==0)
getch();
/*Set the value of variable delayfactor according to level selected*/
switch(toupper(ch))
{
case'S':
delayfactor=100;
break;
case'M':
delayfactor=50;
break;
case'F':
delayfactor=20;
break;
default:
delayfactor=100;
}
if(delayfactor==100)
outtextxy(midx,midy,"Slow(S)");
if(delayfactor==50)
outtextxy(midx,midy,"Medium(M)");
if(delayfactor==20)
outtextxy(midx,midy,"Fast(F)");
/*Change over to text mode*/
while(!kbhit());
closegraph();
restorecrtmode();
}
void music(int type)
{
float octave[7]={130.81,146.83,164.81,174.61,196,220,246.94};
int n,i;
switch(type)
{
case 1:
n=random(6);
sound(octave[n]*4);
delay(50);
break;
case 2:
for(i=6;i>=0;i--){
sound(octave[i]);
delay(54);}
nosound();
break;
case 3:
sound(octave[6]*2);
delay(50);
nosound();
}}
char endscreen()
{
char ans;
initgraph(&gd,&gm,"d:\tc\bgi");
rectangle(0,0,maxx,maxy);
rectangle(2,2,maxx-2,maxy-2);
settextjustify(CENTER_TEXT,CENTER_TEXT);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
if(liveslost==3)
{
outtextxy(midx,midy-30,"Bad Luck!!");
outtextxy(midx,midy,"Try Again");}
else
{
outtextxy(midx,midy/2,"You really are a");
settextstyle(DEFAULT_FONT,HORIZ_DIR,6);
outtextxy(midx,midy,"GENIUS!!");
}
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
outtextxy(midx,midy+midy/2,"Another
game(Y?N)...");
while(!(ans=='Y'||ans=='N'))
{
fflush(stdin);
ans=getch();
ans=toupper(ans);
}
closegraph();
restorecrtmode();
return(ans);
}
void initialise(void)
{
int j;
/*initialise row and column of EATere*/
row=12;
col=40;
r[0]=3;c[0]=76;
r[1]=3;c[1]=12;
r[2]=12;c[2]=4;
r[3]=14;c[3]=62;
score=0;
liveslost=0;
bugnumber=0;
for(j=0;j<4;j++)
{
dir[j]=0;
charbelow[j]=250;
}
}
void gamescreen(void)
{
int i ;
size(32,0); /*hide cursor*/
/*Fill the screen with the specified color*/
drawbox (0,0,16,79,12);
//draw the horizontal lines of the maze
/*Draw the titbits*/
hline(205,1,0,0,79);
hline(205,1,2,2,20);
hline(205,1,2,22,38);
hline(205,1,2,42,61);
hline(205,1,2,63,77);
hline(205,1,4,2,20);
hline(205,1,4,22,61);
hline(205,1,4,63,77);
hline(205,1,17,0,79);
hline(205,1,15,2,38);
hline(205,1,15,42,77);
hline(205,1,13,0,12);
hline(205,1,13,65,78);
hline(205,1,13,16,61);
hline(205,1,11,2,14);
hline(205,1,11,62,77);
hline(205,1,8,16,61);
hline(205,1,6,2,38);
hline(205,1,6,42,77);
hline(205,1,7,63,77);
hline(205,1,9,63,78);
hline(205,1,7,2,14);
hline(205,1,9,1,14);
/*Draw the columns of the titbits */
vline(186,1,0,0,16);
vline(186,1,79,0,16);
vline(186,1,40,0,2);
vline(186,1,40,4,6);
vline(186,1,14,12,14);
vline(186,1,40,13,15);
vline(186,1,63,12,14);
vline(186,1,40,9,11);
vline(202,1,14,15,15);
vline(202,1,63,15,15);
vline(203,1,40,0,0);
vline(203,1,40,4,4);
vline(203,1,40,8,8);
vline(203,1,40,13,13);
vline(187,1,79,0,0);
vline(201,1,0,0,0);
vline(188,1,79,17,17);
vline(200,1,0,17,17);
vline(204,1,0,9,9);
vline(204,1,0,13,13);
vline(185,1,79,9,9);
vline(185,1,79,13,13);
for(int vlin=16;vlin<=60;vlin+=2)
vline(186,1,vlin,10,11);
gotoxy(51,24);
printf("Press Esc to stop the game");
gotoxy(2,24);
printf("Point: %3d",score);
/*Place the five bugs at strategic Positions*/
writechar(2,3,76,15);
writechar(2,3,12,15);
writechar(2,12,4,15);
writechar(2,14,62,15);
/*Play Music*/
music(2);
}
/*Draw a box filling the required area*/
void drawbox (int sr,int sc,int er,int ec,char attr)
{
int r,c;
char far *v;
for(r=sr;r<=er;r++)
{for(c=sc;c<=ec;c++){
v=vid_mem+(r*160)+(c*2);
*v=250 ;
v++;
*v=attr;}}}
/* Write character and its attributes into memeory */
void writechar(char ch,int r,int c,int attr)
{
char far *v ;
/*calculate address*/
v=vid_mem+(r*160)+c*2;
*v=ch;/*store ascii code*/
v++;
*v=attr;/*store attribute */
}
/*draws horizantal line*/
void hline(unsigned char ch,int attr,int r,int c1,int c2)
{
int c;
for(c=c1;c<=c2;c++)
{
writechar (ch,r,c,attr);
/*if tibit placed at row r and column c, set corresponding element
of
array maez[][]to 1*/
maze[r][c]=1;
}
}
/*draws vertical line */
void vline(unsigned char ch,int attr,int c,int r1,int r2)
{
int r;
for(r=r1;r<=r2;r++)
{
writechar(ch,r,c,attr);
/*if tibit placed at row r and column c, set corresponding element
of
array maez[][]to 1*/
maze[r][c]=1;
}
}
void size(int ssl,int esl)
{
union REGS i,o;
i.h.ah=1;
i.h.ch=ssl;
i.h.cl=esl;
i.h.bh=0;
int86(16,&i,&o);
}
void monitor(void)
{
int key ;
unsigned char ch;
while(1)
{
/*place eater at specified row and column */
writechar(1,row,col,14);
/*move bugs around until a key is hit */
getkeyhit();
/*if all 3 lives of the eater are lost */
if(liveslost==3 )
break;
/*place a space in the position currently occuped by the
eater*/
writechar(' ',row,col,1);
/*update the position of the eater*/
key =testkeys();
/*if invalid key pressed*/
if(key==0)
{
/*write backeater in its orignal position */
writechar(1,row,col,14);
}
else
{
/*read character at the position whixh the eater is to occupy
*/
readchar(row,col,&ch);
/*if character read is tibit ,increment score and sound
music*/
if(ch==250)
{
score++;
music(3);
}
if(ch==2)
killeater();
/*if character read is bug , kill the eater */
if(liveslost==3)
break;
/*print the latest score*/
gotoxy(2,24);
printf("points : %3d",score);
/*if all the tibits are eaten up*/
if(score>=692)
{
/*erase the last tibit*/
writechar(' ',row,col,1);
break;
}
}
}
}
/*moves the bugs aroubnd until a key is hit */
void getkeyhit()
{
union REGS i,o;
int count;
/*unti a player hits a key, move each bug in turn*/
while(!kbhit())
{
/*introduce delay*/
delay(delayfactor);
bug(&r[bugnumber],&c[bugnumber],&dir[bugnumber],&charbelow[bugnumber]);
/*if all 3 lives of the eater are lost*/
if (liveslost==3)
return;
/*goto next bug*/
bugnumber++;
/*start with the first bug if allfive bugs have been moved*/
if(bugnumber==4)
bugnumber=0;
}
/*issue interrupt to read the ascii code and scan code od the
kay
pressed*/
i.h.ah=0;/*store service no*/
int86(22,&i,&o);/*issue interrupt*/
ascii=o.h.al;
scan=o.h.ah;
}
/*reports which key has ben hit*/
int testkeys(void)
{
switch(scan)
{
case 72:/*up arrow*/
/*if path is not present in the specified direction */
if(maze[row-1][col]==1)
return(0);
/*update row of eater*/
row--;
break;
case 80:/*down arrow*/
if(maze[row+1][col]==1)
return(0);
row++;
break;
case 77:/*right arrow*/
if(maze[row][col+1]==1)
return(0);
col++;
break;
case 75:/*left arrow*/
if(maze[row][col-1]==1)
return(0);
col--;
break;
case 1:/*esc key*/
exit(0);/*terminate the programm*/
default:
return(0);
}
}
/*reads the character presennt at the row r and volumn c into
ch*/
void readchar(int r,int c,unsigned char *ch)
{
char far *v;
/*calculate addresss*/
v=vid_mem+(r*160) +c*2;
*ch=*v;
}
/*moves the specefy bug in the appropriate direction*/
void bug(int *r,int *c,int* dir,unsigned char *ch)
{
int trials=1,flag=0;
char temp;
/*select a valid direction which takes the bug closer to
eater
in each if statement the 1st condition checks whether the movement
in
that directionwould move the bug closer to the eater , whereas the
2nd
condition checks if the maze permits a movement in that
direction*/
if(abs(*r-1-row)<abs(*r-row)&&maze[*r-1][*c]!=1)
*dir=UP;
else
if(abs(*r+1-row)<abs(*r-row)&&maze[*r+1][*c]!=1)
*dir=DOWN;
else
if(abs(*c+1-col)<abs(*c-col)&&maze[*r][*c+1]!=1)
*dir=RIGHT;
else
if(abs(*c-1-col)<abs(*c-col)&&maze[*r][*c-1]!=1)
*dir=LEFT;
/*check whether the direction chosen contains another bug , if
so find
an
alternate direction*/
while(1)
{
switch(*dir)
{
case RIGHT:
/*if there is a path to right of bug*/
if(maze[*r][*c+1]!=1)
{
/* read the character to the right of the bug*/
readchar(*r,*c+1,&temp);
if(temp==2)
{
/*if the character is again a bug find alternate direction*/
if(maze[*r][*c-1]!=1)
*dir=LEFT;
else
{
/*if patch exists to the top of the bug*/
if (maze[*r-1][*c]!=1)
*dir=UP;
else
{
/*if path exist below the bug*/
if(maze[*r+1][*c]!=1)
*dir=DOWN;
}
}
}
else
{
/*if there is no bug to the right of the bug being
considered , move the bug right*/
movebugright(r,c,ch);
flag=1;
}
}
else
{
/*since there is no pat in the RIGHT directio ,
try another path*/
*dir=random(4);
}
break;
case LEFT:
/*if there is a path to the LEFT of the bug*/
if(maze[*r][*c-1]!=1)
{
/*read the character to the ;eft of the bug*/
readchar(*r,*c-1,&temp);
if(temp==2)
{
if(maze[*r][*c+1]!=1)
*dir=RIGHT;
else
{
if(maze[*r-1][*c]!=1)
*dir=UP;
else
{
if(maze[*r+1][*c]!=1)
*dir=DOWN;
}
}
}
else
{
movebugleft(r,c,ch);
flag=1;
}
}
else
{
*dir=random(4);
}
break;
case UP:
if(maze[*r-1][*c]!=1)
{
readchar(*r-1,*c,&temp);
if(temp==2)
{
if(maze[*r][*c+1]!=1)
*dir=RIGHT;
else
{
if(maze[*r][*c-1]!=1)
*dir=LEFT;
else
{
if(maze[*r+1][*c]!=1)
*dir=DOWN;
}
}
}
else
{
movebugup(r,c,ch);
flag=1;
}
}
else
*dir=random(4);
break;
case DOWN:
if(maze[*r+1][*c]!=1)
{
readchar(*r+1,*c,&temp);
if (temp==2)
{
if(maze[*r][*c+1]!=1)
*dir=RIGHT;
else
{
if(maze[*r][*c-1]!=1)
*dir=LEFT;
else
{
if(maze[*r-1][*c]!=1)
*dir=UP;
}
}
}
else
{
movebugdown(r,c,ch);
flag=1;
}
}
else
*dir=random(4);
break;
}
if(flag==1)
break;
trials++;
if(trials>15)
break;
}
if(*r==row&&*c==col)
killeater();
}
void movebugleft( int *row,int * colm,unsigned char *ch)
{
if( *ch==0)
*ch=0;
writechar(*ch,*row,*colm,12);
*colm=*colm-1;
readchar(*row,*colm,ch);
if(*ch==1)
*ch=' ';
writechar(2,*row,*colm,15);
}
void movebugright(int *row,int *colm,unsigned char *ch)
{
if( *ch==0)
*ch=0;
writechar(*ch,*row,*colm,12);
*colm=*colm+1;
readchar(*row,*colm,ch);
if(*ch==1)
*ch=' ';
writechar(2,*row,*colm,15);
}
void movebugup(int *row,int *col,unsigned char *ch)
{
if( *ch==0)
*ch=0;
writechar(*ch,*row,*col,12);
*row=*row-1;
readchar(*row,*col,ch);
if(*ch==1)
*ch=' ';
writechar(2,*row,*col,15);
}
void movebugdown(int *row,int *col,unsigned char *ch)
{
if( *ch==0)
*ch=0;
writechar(*ch,*row,*col,12);
*row=*row+1;
readchar(*row,*col,ch);
if(*ch==1)
*ch=' ';
writechar(2,*row,*col,15);
}
void killeater(void){
int r,c;
r=row;
c=col;
writechar(1,row,col,112);
writechar(1,23,15+liveslost*3,14);
music(2);
liveslost++;
if(liveslost==3)
return;
row=12;
col=40;
writechar(1,row,col,14);
}