Question: import java.util.*; import java.util.Scanner; //define main class class PacMan2 { //implement function for printing. public static void printing(char[][] Grid, int X, int Y) { System.out.println();
import java.util.*;
import java.util.Scanner;
//define main class
class PacMan2
{
//implement function for printing.
public static void printing(char[][] Grid, int X, int Y)
{
System.out.println();
for (int i = 0; i < X; i++)
{
for (int j =0; j System.out.println(Grid[i][j]); System.out.println(); } System.out.println(); } //implement function for the Pac-Man public static char PutPacMan(int CuurentDir) { if (CuurentDir == 0) return '>'; else if (CuurentDir == 1) return 'V'; else if (CuurentDir == 1) return '<'; return '^'; } //implement the play function public static void Play (char[][] Grid, int X, int Y) { Scanner scan = new Scanner(System.in); int CuurentDir = 0; int I = 0; int J = 0; while (true) { System.out.println("1. Turn Left"); System.out.println("2. Turn Right"); System.out.println("3. Move"); System.out.println("4. Exit"); System.out.println("Please Make Your Choice: "); int N = scan.nextInt(); if (N == 1) { if (CuurentDir == 0) CuurentDir = 3; else if (CuurentDir == 2) CuurentDir = 1; else if (CuurentDir == 3) CuurentDir = 2; else if (CuurentDir == 1) CuurentDir = 0; Grid[I][J] = PutPacMan(CuurentDir); printing(Grid, X, Y); } else if (N == 2) { if (CuurentDir == 1) CuurentDir = 2; else if (CuurentDir == 2) CuurentDir = 3; else if (CuurentDir == 3) CuurentDir = 0; else if (CuurentDir == 0) CuurentDir = 1; Grid[I][J] = PutPacMan(CuurentDir); printing(Grid, X, Y); } else if (N == 3) { if (CuurentDir == 0) { if (J+1 Grid[I][J] = ' '; Grid[I][J+1] = PutPacMan(CuurentDir); J+= 1; } else { System.out.println("Can't do that move!"); } } else if (CuurentDir == 1) { if (I+1 < X && Grid [I+1] [J] != 'O'){ Grid[I][J] = ' '; Grid[I][J+1] = PutPacMan(CuurentDir); I+= 1; } else { System.out.println("Can't do that move!"); } } else if (CuurentDir == 2) { if (J-1 >= 0 && Grid [I] [J-1] != 'O'){ Grid[I][J] = ' '; Grid[I][J-1] = PutPacMan(CuurentDir); J -= 1; } else { System.out.println("Can't do that move!"); } } else if (CuurentDir == 3) { if (I-1 >= 0 && Grid [I-1] [J] != 'O'){ Grid[I][J] = ' '; Grid[I-1][J] = PutPacMan(CuurentDir); I-= 1; } else { System.out.println("Can't do that move!"); } } printing(Grid, X, Y); } else break; } } public static boolean PacMoves(char [][] Grid, int X, int Y) { if (Grid[X][Y] == '<') return true; if (Grid[X][Y] == '>') return true; if (Grid[X][Y] == '^') return true; if (Grid[X][Y] == 'V') return true; return false; } //declare the main function public static void main(String[] args) { //User Input for size of game. Scanner scan = new Scanner(System.in); System.out.println ("Enter the number of rows and columns in your game, " + "separated by a space:"); int X = scan.nextInt(); int Y = scan.nextInt(); //create the grid char[][] Grid = new char[X][Y]; //define random variable Random Rand = new Random(); //Place 8% Cookies for (int i=0; i < (int) (0.08*X*Y); i++) { int A = Math.abs(Rand.nextInt() % X); int B = Math.abs(Rand.nextInt() % Y); Grid[A][B] = 'O'; } Grid[0][0] = '>'; for (int i = 0; i < X; i++) { for (int j=0; j < Y; j++) { if (Grid[i][j] != 'O' && PacMoves (Grid, i, j) == false) Grid[i][j] = '.'; } } //Call the printing function printing(Grid, X, Y); //Call the play function Play(Grid, X, Y); } } Hi! I have the following assignment: 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 8% 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) The following menu of commands must be provided and must be displayed when appropriate. The command number is what the user should enter to execute the command. Just display the command number and text (ex. 1: Menu), not the explanation of what the command does: 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. Adds one to the count of move commands if successful or not. If the move command is successful, the previous location is replaced with Visited Symbol (see above). If the move command results in the PacMan moving to a cell where a cookie is located, the cookie is eaten and the number of cookies eaten is increased by one. 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 number 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 move 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. 3. Observe the PacMan demonstration for an example of one implementation. I have written the code, I think it works perfectly other than for instead of my grid making a grid each line appears on a new line, can you help troubleshoot? Due tonight, please help.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
