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

Based on the below code. How is the program organized? What major data structure

ID: 3588156 • Letter: B

Question

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 2: To turn right 3: To move 4: To exit Enter : ");
                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();
        }
    }
}

Explanation / Answer

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 2: To turn right 3: To move 4: To exit Enter : ");

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote