Question

In: Computer Science

The Project is: 1. Create a new Java program which implements a simple PacMan-type text game...

The Project is:

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 using standard array(s) (no

collection classes allowed) 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 15% of your grid (rounded down if

necessary) should contain cookies randomly located on the grid except for the initial PacMan

position. The grid must be displayed after each command.

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 (the command number is what

the user should enter to execute the command):

1. Menu – Display the menu of commands.

2. Turn Left – turns the PacMan left (counter-clockwise) 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

3. Turn Right – turns the PacMan right (clockwise) 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

4. Move – Moves the PacMan one grid location in the facing direction if possible.

5. 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 a move

command.

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 solution on CHEGG does not solve the problem. The user has to input the rows and columns. PacMan needs to turn left, right, and move forward only. I am posting what I have so far below. I still need to figure out the turn, the move forward, the disappear dots/ cookies, and the statistics on exit. import java.util.Scanner; import java.util.Random; public class AssignmentMP1_dspicer83 { public static void main(String[] args) { // Creation of variables int play = 0; int columns = 0; int rows = 0; int cookies = 0; Random rnd = new Random(); Scanner scn = new Scanner(System.in); boolean quit = false; String [][] playArea; int area = 0; String menu = "Select from the following Menu\n" + "\t1. Display Menu\n" + "\t2. Turn Left\n" + "\t3. Turn Right\n" + "\t4. Move Forward\n" + "\t5. Exit"; //User input starts here while (!quit) { System.out.println("Welcome to my PacMan game \n" + "Would you like to play? \n" + "Press 1 to play and 2 to quit"); play = scn.nextInt(); if (play == 1) { // Game Play //columns is an x value System.out.println("How many columns would you like?"); columns = scn.nextInt(); //rows is an y value System.out.println("How many rows would you like?"); rows = scn.nextInt(); //Array created area = rows * columns; playArea = new String[rows][columns]; //calculate amount of cookies cookies = (int) ((columns * rows) * 0.15); //Any size and type String selectedIndex = ""; //create random variable Random rand = new Random(); //populates play area with PacMan and dots for(int i=0;i"; } else playArea[i][j]="."; } } //Any number< totalEmenent for(int i=0; i< cookies; i++) { int selectIndex = rand.nextInt(area); //generate random until its unique while(selectedIndex.indexOf(String.valueOf(selectIndex))> 0) { selectIndex = rand.nextInt(area); } //test if is original value selectedIndex = selectedIndex+selectIndex; int xCord = (int)selectIndex/playArea[0].length; int yCord = selectIndex%playArea[0].length; if(xCord == 0 && yCord == 0) { i--; } else playArea[xCord][yCord]="O"; } //Options Menu System.out.println(menu); //Main Game loop while (!quit) { //Print Grid for(int i=0;i")) { } break; case 3: //Turn Right break; case 4: //Move Forward break; case 5: //Exit System.out.println("Thanks for playing STATS"); quit = true; break; default: System.out.println("Please select options 1 - 5"); } } } else if (play ==2) { //User exit out of game System.out.println("Thank you for checking out my PacMan game. Please come back and play."); quit = true; } else { //in input is not 1 or 2, loops back to input start System.out.println("Not a valid option"); } } } }

Solutions

Expert Solution

Working code:

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PacMan{
   static char grid[][];
   static int x;
   static int y;
   static int pacx;
   static int pacy;
   static int total_moves;
   static int cookies_ate;
   static int ch;
  
   public static void main(String[] args){
      
       /*
       * Grid layout
       *   
       * X coordinate
       * +--------------->
       * |
       * |Y coordinate
       * |
       * V
       */
       try{
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           /*
           * getting grid size
           */
           System.out.println("Enter the size of grid:");
           System.out.print("X value: ");
           x = Integer.parseInt(br.readLine());
           System.out.print("Y value: ");
           y = Integer.parseInt(br.readLine());
          
           /*
           * initializing grid
           */
           initGrid();
          
           /*
           * draw initial grid
           */
           drawGrid();
          
           do{
               /*
               * iterate through user inputs until exit command
               */
               System.out.print("1. Turn left\t2. Turn right\t3. Move\t0. Exit\tChoice: ");
               ch = Integer.parseInt(br.readLine());
              
               switch(ch){
               case 1:
                   turnLeft();
                   drawGrid();
                   break;
               case 2:
                   turnRight();
                   drawGrid();
                   break;
               case 3:
                   move();
                   drawGrid();                  
                   break;
               case 0:
                   break;
               default:
                   System.out.println("Invalid command !!!");
               }
           }while(ch != 0);
          
           /*
           * displaying statistics
           */
           System.out.println("Total valid move commands: " + total_moves);
           System.out.println("Total cookies ate: " + cookies_ate);
           System.out.println("Average number of commands per cookie: " + (total_moves / cookies_ate));
       }
       catch(Exception e){
           e.printStackTrace();
       }
   }
  
   public static void initGrid(){
       /*
       * initialize grid to X Y values
       */
       grid = new char[y][x];
       int rx, ry;
       for(int i = 0; i < x; i++){
           for(int j = 0; j < y; j++){
               grid[j][i] = '.';
           }
       }
      
       /*
       * placing initial pacman
       */
       grid[0][0] = '>';
       //finding total cookies
       int cookies = (int) Math.ceil(x * y * 15.0 / 100.0);
      
       for(int i = 0; i < cookies; i++){
           /*
           * inserting cookies
           */
           do{
               rx = (int)(Math.random() * 100) % x;
               ry = (int)(Math.random() * 100) % y;
           }while(grid[ry][rx] != '.');
          
           grid[ry][rx] = 'O';
       }
      
       pacx = 0;
       pacy = 0;
       total_moves = 0;
       cookies_ate = 0;
   }
  
   public static void drawGrid(){
       /*
       * display grid
       */
       System.out.println();
       for(int i = 0; i < x; i++){
           for(int j = 0; j < y; j++){
               System.out.print(grid[j][i] + " ");
           }
           System.out.println();
       }
       System.out.println();
   }
  
   public static void turnLeft(){
       /*
       * turn left
       */
       switch(grid[pacy][pacx]){
       case '>':
           grid[pacy][pacx] = '^';
           break;
       case '^':
           grid[pacy][pacx] = '<';
           break;
       case '<':
           grid[pacy][pacx] = 'v';
           break;
       case 'v':
           grid[pacy][pacx] = '>';
       }
   }
  
   public static void turnRight(){
       /*
       * turn right
       */
       switch(grid[pacy][pacx]){
       case '>':
           grid[pacy][pacx] = 'v';
           break;
       case 'v':
           grid[pacy][pacx] = '<';
           break;
       case '<':
           grid[pacy][pacx] = '^';
           break;
       case '^':
           grid[pacy][pacx] = '>';
       }
   }
  
   public static boolean move(){
       /*
       * making single position move
       */
       switch(grid[pacy][pacx]){
       case '>':
           /*
           * facing left
           */
           if(pacy == 0){
               System.out.println("Impossible move - Grid Boundary reached !!!");
               return false;
           }
          
           total_moves++;
          
           if(grid[pacy - 1][pacx] == 'O'){
               cookies_ate++;
           }
          
           grid[pacy][pacx] = ' ';
           grid[pacy - 1][pacx] = '>';
           pacy--;
           return true;
       case 'v':
           /*
           * facing top
           */
           if(pacx == 0){
               System.out.println("Impossible move - Grid Boundary reached !!!");
               return false;
           }
          
           total_moves++;
              
           if(grid[pacy][pacx - 1] == 'O'){
               cookies_ate++;
           }
          
           grid[pacy][pacx] = ' ';
           grid[pacy][pacx - 1] = 'v';
           pacx--;
           return true;
       case '<':
           /*
           * facing right
           */
           if(pacy == y - 1){
               System.out.println("Impossible move - Grid Boundary reached !!!");
               return false;
           }
          
           total_moves++;
      
           if(grid[pacy + 1][pacx] == 'O'){
               cookies_ate++;
           }
          
           grid[pacy][pacx] = ' ';
           grid[pacy + 1][pacx] = '<';
           pacy++;
           return true;
       case '^':
           /*
           * facing bottom
           */
           if(pacx == x - 1){
               System.out.println("Impossible move - Grid Boundary reached !!!");
               return false;
           }
          
           total_moves++;
          
           if(grid[pacy][pacx + 1] == 'O'){
               cookies_ate++;
           }
          
           grid[pacy][pacx] = ' ';
           grid[pacy][pacx + 1] = '^';
           pacx++;
           return true;
       }
       return false;
   }
}


Code screenshot:

Output:


Related Solutions

Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that...
Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that a player must traverse to win. 3. The maze must be text-based and adjustable from 5x5 to 20x20. • Player gets to choose size either as: 1) any size in the range from 5-20 by 5-20. 2) selects from 4 set size options [5x5,10x10,15x15, and 20x20] • the player can choose among different mazes. • You will need to figure out how to denote...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters...
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters and then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks: for example, G* T*** (for Go Team). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again or tell the user the guessed letter is not in the...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create the code so that when the program is run the user will see in the console the following: We are going to play rock paper scissors. Please choose 1 for Rock 2 for Paper or 3 for scissors. The program will have your choice which is the integer-valued typed in by the user and compChoice which will be the randomly generated value of either...
JAVA Overview This program implements a simple, interactive calculator. Major topics writing a program from scratch...
JAVA Overview This program implements a simple, interactive calculator. Major topics writing a program from scratch multiple methods testing In this document program logic & program structure (class, methods, variables) input / output assumptions and limitations documentation & style suggested schedule cover letter discussion questions grading, submission, and due date additional notes challenges sample output test plan Program Logic The program prompts the user to select a type of math problem (addition, subtraction, multiplication, or division), enter the two operands,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT