Question

Based on the below code. How is the program organized? What major data structures were used?...

Based on the below code. How is the program organized? What major data structures were used? How are commands processed? How is the PacMan’s state maintained? Etc. This section should take about ½ to 2/3 of the paper content. Do not repeat the project specifications (assume the reader is knowledgeable of the project specifications). 2. What alternative approaches were considered and why were they rejected? 3. What did you learn from doing this project and what would you do differently?

import java.util.Scanner;
public class MyPacman
{
    static void displayModule(char array[][])
    {
        for(int i = 0; i < array.length; i++)
        {
            for(int j = 0; j < array[i].length; j++)
            {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
    }
    public static void main(String args[])
    {
        int rows, columns;
        int curRow = 0, curCol = 0, noOfEats = 0, noOfCommands = 0;
        Scanner myIn = new Scanner(System.in);
        System.out.print("Give the no. of rows & columns: ");
        rows = myIn.nextInt();
        columns = myIn.nextInt();
        char array[][] = new char[rows][columns];
        int noOfCookies = (int)(0.2 * rows * columns);
        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < columns; j++)
            {
                array[i][j] = '.';
            }
        }
        array[0][0] = '>';
        for(int i = 0; i < noOfCookies; i++)
        {
            int myR = (int)(Math.random() * rows);
            int myC = (int)(Math.random() * columns);
            if(array[myR][myC] == '.') array[myR][myC] = 'O';
            else i--;
        }
        while(true)
        {
            int moves;
            displayModule(array);
            do
            {
                System.out.print("1: To turn left\n2: To turn right\n3: To move\n4: To exit\nEnter : ");
                moves = myIn.nextInt();
                if(moves < 1 || moves > 4)
                    System.out.println("Command Invalid");
            }
            while(moves < 1 || moves > 4);
            switch(moves)
            {
                case 1:
                    if(array[curRow][curCol] == '<')
                        array[curRow][curCol] = 'v';
                    else if(array[curRow][curCol] == 'v')
                        array[curRow][curCol] = '>';
                    else if(array[curRow][curCol] == '>')
                        array[curRow][curCol] = '^';
                    else if(array[curRow][curCol] == '^')
                        array[curRow][curCol] = '<';
                        noOfCommands++;
                    break;
                case 2:
                    if(array[curRow][curCol] == '<')
                        array[curRow][curCol] = '^';
                    else if(array[curRow][curCol] == 'v')
                        array[curRow][curCol] = '<';
                    else if(array[curRow][curCol] == '>')
                        array[curRow][curCol] = 'v';
                    else if(array[curRow][curCol] == '^')
                        array[curRow][curCol] = '>';
                        noOfCommands++;
                        displayModule(array);
                        break;
                case 3:
                    if(array[curRow][curCol] == '<')
                    {
                        if(array[curRow].length > curCol + 1)
                        {
                            array[curRow][curCol] = ' ';
                            if(array[curRow][curCol + 1] == 'o')
                                noOfCookies++;
                                array[curRow][curCol + 1] = '<';
                                curCol++;
                                noOfCommands++;
                        }
                    }
                    else if(array[curRow][curCol] == 'v')
                    {
                        if(curRow > 0)
                        {
                            array[curRow][curCol] = ' ';
                            if(array[curRow - 1][curCol] == 'o')
                                noOfCookies++;
                                array[curRow - 1][curCol] = 'v';
                                curRow--;
                                noOfCommands++;
                        }
                    }
                    else if(array[curRow][curCol] == '>')
                    {
                        if(curCol > 0)
                        {
                            array[curRow][curCol] = ' ';
                            if(array[curRow][curCol - 1] == 'o')
                                noOfCookies++;
                                array[curRow][curCol - 1] = '>';
                                curCol--;
                                noOfCommands++;
                        }
                    }
                    else if(array[curRow][curCol] == '^')
                    {
                        if(array.length > curRow + 1)
                        {
                            array[curRow][curCol] = ' ';
                            if(array[curRow + 1][curCol] == 'o')
                                noOfCookies++;
                                array[curRow + 1][curCol] = '^';
                                curRow++;
                                noOfCommands++;
                        }
                    }
                    break;
                case 4:
                    System.out.println("No. of Moves: " + noOfCommands);
                    System.out.println("No. of cookies: " + noOfCookies);
                    System.out.println("No. of moves / cookie: " + noOfCommands / noOfCookies);
                    return;
                default:
                    break;
            }
            System.out.println();
        }
    }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class MyPacman {

// to print the current state of the pacman game

static void displayModule(char array[][]) {

for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[i].length; j++) {

System.out.print(array[i][j]);

}

System.out.println();

}

}

public static void main(String args[]) {

int rows, columns; // size of the grid

int curRow = 0, curCol = 0; // current position

int noOfEats = 0, noOfCommands = 0; // to store the points

// scanner to take user input

Scanner myIn = new Scanner(System.in);

System.out.print("Give the no. of rows & columns: ");

rows = myIn.nextInt();

columns = myIn.nextInt();

// Create a 2-D array of characters to represent the game grid

char array[][] = new char[rows][columns];

// 20% of the cells will be filled with cookies

int noOfCookies = (int) (0.2 * rows * columns);

// we are flling the cells with '.' character, which shows they are

// empty at start

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

array[i][j] = '.';

}

}

// The below line shows the starting position of pacman

array[0][0] = '>';

// Generating cookies at random positions

for (int i = 0; i < noOfCookies; i++) {

int myR = (int) (Math.random() * rows);

int myC = (int) (Math.random() * columns);

if (array[myR][myC] == '.')

array[myR][myC] = 'O';

else

i--;

}

// keep asking user choice till game is over or user want to exit

while (true) {

int moves;

displayModule(array);

// ask for a valid choice

do {

System.out

.print("1: To turn left\n2: To turn right\n3: To move\n4: To exit\nEnter : ");

moves = myIn.nextInt();

if (moves < 1 || moves > 4)

System.out.println("Command Invalid");

} while (moves < 1 || moves > 4);

// decide the functionality based on user choice

switch (moves) {

case 1:

// based on current direction, we need to make a left turn of pacman

if (array[curRow][curCol] == '<')

array[curRow][curCol] = 'v';

else if (array[curRow][curCol] == 'v')

array[curRow][curCol] = '>';

else if (array[curRow][curCol] == '>')

array[curRow][curCol] = '^';

else if (array[curRow][curCol] == '^')

array[curRow][curCol] = '<';

noOfCommands++;

break;

case 2:

// based on current direction, we need to make a right turn of pacman

if (array[curRow][curCol] == '<')

array[curRow][curCol] = '^';

else if (array[curRow][curCol] == 'v')

array[curRow][curCol] = '<';

else if (array[curRow][curCol] == '>')

array[curRow][curCol] = 'v';

else if (array[curRow][curCol] == '^')

array[curRow][curCol] = '>';

noOfCommands++;

displayModule(array);

break;

case 3:

// based on current position and direction, increase the position by one.

if (array[curRow][curCol] == '<') {

// left direction

// dont let it go outside the grid

if (array[curRow].length > curCol + 1) {

array[curRow][curCol] = ' ';

// check if we got the cookie

if (array[curRow][curCol + 1] == 'o')

noOfEats++;

array[curRow][curCol + 1] = '<';

curCol++;

noOfCommands++;

}

} else if (array[curRow][curCol] == 'v') {

// down direction

if (curRow > 0) {

array[curRow][curCol] = ' ';

if (array[curRow - 1][curCol] == 'o')

noOfEats++;

array[curRow - 1][curCol] = 'v';

curRow--;

noOfCommands++;

}

} else if (array[curRow][curCol] == '>') {

// right direction

if (curCol > 0) {

array[curRow][curCol] = ' ';

if (array[curRow][curCol - 1] == 'o')

noOfEats++;

array[curRow][curCol - 1] = '>';

curCol--;

noOfCommands++;

}

} else if (array[curRow][curCol] == '^') {

// Up direction

if (array.length > curRow + 1) {

array[curRow][curCol] = ' ';

if (array[curRow + 1][curCol] == 'o')

noOfEats++;

array[curRow + 1][curCol] = '^';

curRow++;

noOfCommands++;

}

}

break;

case 4:

System.out.println("No. of Moves: " + noOfCommands);

System.out.println("No. of cookies: " + noOfEats);

System.out.println("No. of moves / cookie: " + noOfCommands

/ noOfEats);

return;

default:

break;

}

System.out.println();

myIn.close();

}

}

}

=======================================

How is the program organized?
We are taking a character 2-D array as the game grid where pacman is at one particular positon with some direction.. It can move and eat cookies.

What major data structures were used?
A charcter 2-D array

How are commands processed?
We read the command and based on current direction, we process the command in a switch statement

How is the PacMan’s state maintained?
We have got <,>,v,^ symbols for maintaing the direction, and 2 variables for maintaining the current positon.

Add a comment
Know the answer?
Add Answer to:
Based on the below code. How is the program organized? What major data structures were used?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. What is the output of the following code segment? int array[] = { 8, 6,...

    1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • I need help with this method (public static int computer_move(int board[][])) . When I run it...

    I need help with this method (public static int computer_move(int board[][])) . When I run it and play the compute.The computer enters the 'O' in a location that the user has all ready enter it sometimes. I need to fix it where the computer enters the 'O' in a location the user has not enter the "X' already package tictactoe; import java.util.Scanner; public class TicTacToeGame { static final int EMPTY = 0; static final int NONE = 0; static final...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT