Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a new Java program which implements a simple PacMan-type text game whi

ID: 3674311 • Letter: 1

Question

1. Create a new Java program which implements a simple PacMan-type text game which contains the
following functionality:


A) At program startup, constructs and displays a 2-dimensional grid with the size dynamically specified by the user (X and Y sizes can be different). Places the PacMan in the upper-left corner of the grid facing left All grid cells should have the empty cell character of ‘.’ except for the start position of the PacMan which will have the appropriate PacMan symbol (see below). Also 20% of your grid should contain cookies randomly located on the grid except for the initial PacMan position. A menu of commands then the grid must be displayed.

B) Use these symbols for the grid:

1. Cookie symbol – shows were cookies are in the grid ('O')
2. Empty symbol – shows empty unvisited grid cells ('.') (dot)
3. Visited symbol – shows grid cells where the PacMan has visited (' ') (space)
4. PacMan symbol depends on the current PacMan facing direction.

1. Left ‘>’
2. Up ‘V’
3. Right ‘<’
4. Down ‘^’

C) A menu of commands must be provided and must be displayed when appropriate. At a minimum the menu should consists of the following commands:

1. Turn Left – turns the PacMan left but the PacMan stays in its current location

1. Current: up, new: left
2. Current: right, new up
3. Current: down, new right
4. Current: left, new down

2. Turn Right – turns the PacMan right but the PacMan stays in its current location

1. Current: up, new: right
2. Current: right, new down
3. Current: down, new left
4. Current: left, new up

3. Move – Moves the PacMan one grid location in the facing direction if possible.
4. Exit – exits the program displaying the game statistics of the number of total moves and the
average number of moves per cookie obtained.

2. The main processing cycle is the following:
A) The grid must be displayed after each command showing the effects of the command.
B) Optionally display the list of commands
C) Display the grid
D) Accept user input. Code will be provided for reading user input.

1. If an invalid command is entered, an appropriate error message should be displayed and the
menu of commands and grid gets redisplayed. An invalid command does not count as a
command in the statistics.
2. Process the command and add one to the number of commands entered if it is an action
command (move or turn).
3. If the user enters the Exit command, the program will display the number of commands and
the average number of commands per cookie.

E) If the resulting move places the PacMan over a cookie, indicate the cookie was eaten and add
one to the number of cookies eaten for the program statistics.

The program show ask user input for rows, and columns. then display 20% of cookies

Example:

x = 10;

y = 10;

comment:

1: turn left

2: turn right

3: move

4: exit

>...o.....

......o...

....o.....

...oo.....

.....o....

..........

..........

..........

..........

..........

Explanation / Answer

import java.util.Scanner;

public class Pacman{
   static void display(char arr[][]){
       for(int i = 0; i < arr.length; i++){
           for(int j = 0; j < arr[i].length; j++){
               System.out.print(arr[i][j]);
           }
           System.out.println();
       }
   }
   public static void main(String args[]){
       int row, col;
       int cr = 0, cc = 0, numEaten = 0, numCommands = 0;
       Scanner in = new Scanner(System.in);
       System.out.print("Enter the number of rows and columns: ");
       row = in.nextInt();
       col = in.nextInt();
       char arr[][] = new char[row][col];
       int numCookies = (int)(0.2 * row * col);
       for(int i = 0; i < row; i++){
           for(int j = 0; j < col; j++){
               arr[i][j] = '.';
           }
       }
       arr[0][0] = '>';
       for(int i = 0; i < numCookies; i++){
           int r = (int)(Math.random() * row);
           int c = (int)(Math.random() * col);
           if(arr[r][c] == '.') arr[r][c] = 'o';
           else i--;
       }
       while(true){
           int move;
           display(arr);
           do{
               System.out.print("1: turn left 2: turn right 3: move 4: exit Enter your move: ");
               move = in.nextInt();
               if(move < 1 || move > 4) System.out.println("Invalid command");
           }while(move < 1 || move > 4);
          
           switch(move){
           case 1:
               if(arr[cr][cc] == '<') arr[cr][cc] = 'v';
               else if(arr[cr][cc] == 'v') arr[cr][cc] = '>';
               else if(arr[cr][cc] == '>') arr[cr][cc] = '^';
               else if(arr[cr][cc] == '^') arr[cr][cc] = '<';
               numCommands++;
               break;
           case 2:
               if(arr[cr][cc] == '<') arr[cr][cc] = '^';
               else if(arr[cr][cc] == 'v') arr[cr][cc] = '<';
               else if(arr[cr][cc] == '>') arr[cr][cc] = 'v';
               else if(arr[cr][cc] == '^') arr[cr][cc] = '>';
               numCommands++;
               display(arr);
               break;
           case 3:
               if(arr[cr][cc] == '<'){
                   if(arr[cr].length > cc + 1){
                       arr[cr][cc] = ' ';
                       if(arr[cr][cc + 1] == 'o') numCookies++;
                       arr[cr][cc + 1] = '<';
                       cc++;
                       numCommands++;
                   }
               }
               else if(arr[cr][cc] == 'v'){
                   if(cr > 0){
                       arr[cr][cc] = ' ';
                       if(arr[cr - 1][cc] == 'o') numCookies++;
                       arr[cr - 1][cc] = 'v';
                       cr--;
                       numCommands++;
                   }
               }
               else if(arr[cr][cc] == '>'){
                   if(cc > 0){
                       arr[cr][cc] = ' ';
                       if(arr[cr][cc - 1] == 'o') numCookies++;
                       arr[cr][cc - 1] = '>';
                       cc--;
                       numCommands++;
                   }
               }
               else if(arr[cr][cc] == '^'){
                   if(arr.length > cr + 1){
                       arr[cr][cc] = ' ';
                       if(arr[cr + 1][cc] == 'o') numCookies++;
                       arr[cr + 1][cc] = '^';
                       cr++;
                       numCommands++;
                   }
                  
               }
               break;
           case 4:
               System.out.println("Number of Moves: " + numCommands);
               System.out.println("Number of cookies: " + numCookies);
               System.out.println("Number of moves per cookie: " + numCommands / numCookies);
               return;
           default:
               break;
           }
           System.out.println();
       }
   }
}