Question

In: Computer Science

Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently....

Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently.

Problem 5: Player Move Dungeon (10 points) (Game Development)

You're the lead programmer at a AAA studio making a sequel to the big hit game, Zeldar 2. You've been challenged to implement player movement in dungeons. The game is top-down, with dungeons modeled as a 2d grid with walls at the edges. The player's location is tracked by x,y values correlating to its row and column positions. Given the current position of the player and a sequence of input commands: w,a,s,d you must determine the new position of the player. The player must not be able to move outside the walls of the dungeon (i.e. grid)

Facts

● the player's position is modeled using two integer values (x, y)

● x represents the column position, left-right axis ● top-left corner is (0,0)

● y represents the row position, up-down axis

● “w” move up by decreasing y by 1

● “a” move left by decreasing x by 1

● “s” move down by increasing y by 1

● “d” move right by increasing x by 1

● if an input attempts to move player off grid, then ignore that move.

Input

The first input is the number of test cases. Each test case contains three lines of inputs. The first line is two positive integers that represent the dungeon's grid size, rows (length) columns (width). The second line is two non-negative integers representing the player's position in the dungeon grid, x,y. The third line represents the sequence of player movements "w", "s", "a", "d".

Output

The program should print the final location of the player in the form of , where “x” and “y” are the coordinates within the dungeon grid.

Sample Input

2

4 4

2 3

s s s w

10 10 9 4

s d w a

Sample Output

2 2 8 4

Solutions

Expert Solution

Java Code:

import java.io.Console;

import java.util.Scanner;

public class Dungeon {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int numberOfTestCases;

        numberOfTestCases = sc.nextInt();

        int[][] resultArray = new int[numberOfTestCases][2];

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

            int rows, columns;

            int playerXPos, playerYPos;

            String moves;

            rows = sc.nextInt();

            columns = sc.nextInt();

            playerXPos = sc.nextInt();

            playerYPos = sc.nextInt();

            Console con = System.console();

            moves = con.readLine();

            for (int j = 0; j < moves.length(); j++) {

                switch (moves.charAt(j)) {

                case 's':

                    System.out.println("Move down");

                    if(playerYPos != columns-1)

                    {

                        playerYPos += 1;

                    }

                    break;

                case 'w':

                    System.out.println("Move up");

                    if(playerYPos != 0)

                    {

                        playerYPos -= 1;

                    }

                    break;

                case 'a':

                    System.out.println("Move Left");

                    if(playerXPos != 0)

                    {

                        playerXPos -= 1;

                    }

                    break;

                case 'd':

                    System.out.println("Move Right");

                    if(playerXPos != rows-1)

                    {

                        playerXPos += 1;

                    }

                    break;

                }

            }

            int[] answer = new int[]{playerXPos , playerYPos};

            resultArray[i] = answer ;

        }

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

        {

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

            {

                System.out.println(resultArray[i][j] + " ");

            }

        }

    }

}

pseudocode:

Pseudocode

function main
{
read howManytestCases
for each test case
{
read totalRows
read totalColumns
read playerXPosition
read playerYPosition
read totalMoves

for each move in totalMoves
{
case 's'
if playerYPosition is not at the wall
move playerYPosition by 1 to downwards by adding 1
playerYPosition <- playerYPosition + 1

case 'w'
if playerYPosition is not at the wall   
move playerYPosition by 1 to upwards by subtracting 1
playerYPosition <- playerYPosition - 1

case 'a'
if playerXPosition is not at the wall
move playerXPosition by 1 to left by subtracting 1
playerXPosition <- playerXPosition - 1

case 'd'
if playerXPosition is not at the wall
move playerXPosition by 1 to right by adding 1
playerXPosition <- playerXPosition + 1
}

save the playerXPosition and playerYPosition in 2D array
}

display the playerXPosition and playerYPosition for each test cases
}

Thank You!


Related Solutions

This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem 7: Simple Calculator (10 points) (General) Calculators represent the most basic, general-purpose of computing machines. Your task is to reduce your highly capable computer down into a simple calculator. You will have to parse a given mathematical expression, and display its result. Your calculator must support addition (+), subtraction (-), multiplication (*), division (/), modulus(%), and exponentiation (**). Facts ● Mathematical expressions in the...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA Private Key (10 points) (Cyber Security) In the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. In this task, you must calculate the modular multiplicative inverse for given set of values. What is the Modular Multiplicative Inverse? In mathematics, each operation typically has an inverse. For example,...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for a program for Hansel's Housecleaning Service. The program prompts the user for a customer's last name only. While the last name is not “zzz” your program will ask for the number of bathrooms and the number of other rooms to be cleaned and display the cleaning charge. You should use a sentinel-controlled while loop in your main loop logic. A customer name of “zzz”...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please (in your...
How Heapify is done (theory, pseudocode, and examples) the examples used Java code please (in your own words)
Please select all of the correct answers in this question to test your understanding of Type...
Please select all of the correct answers in this question to test your understanding of Type 4 hypersensitivity. Type 4 hypersensitivity is also referred to as delayed hypersensitivity. The severity of the reaction depends upon whether IgG, IgM, or IgE are involved. Type 4 hypersensitivity is mediated by T-cells and the effector cells that they recruit. Type 4 hypersensitivity is most appropriately treated with antihistamines. Type 4 hypersensitivity may result from mismatched blood transfusions.
briefly provide your understanding to one of the principles in FPA Code of Ethics. please state...
briefly provide your understanding to one of the principles in FPA Code of Ethics. please state the name of the practice standards and your understanding
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT