In: Computer Science
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"); } } } }
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: