Question

In: Computer Science

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 position of the player and a sequence of input commands: w,a,s,d you must determine the new position of the player.

Facts

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

● x represents the column position, left-right axis

● 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

Input

The input will begin with a single line containing the number of test cases to execute. The next line should consist of the starting (x,y) position of the player. The next line is the sequence of moves represented with "w", "a", "s", or "d". This sequence can be empty string to any number of letters long. Each input is separated by a space with the last terminated with a new line.

Output

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

Sample Input

2 0 0

w w a a a

9 4

s d w a

Sample Output

-3 -2

9 4

Solutions

Expert Solution

Code:

import java.util.Scanner;

public class Zeldar {
   // Coordinate class for coordinates.
   class Coordinate
   {
   int x;
   int y;
   Coordinate(int x,int y ) { this.x = x; this.y=y;}
   Coordinate() { x = 0; y=0;}
   }
  
   //on pressing w
   Coordinate w(Coordinate c){
       c.y=c.y-1;
       return c;
   }
  
   //on pressing a
   Coordinate a(Coordinate c){
       c.x=c.x-1;
       return c;
   }
  
   //on pressing s
   Coordinate s(Coordinate c){
       c.y=c.y+1;
       return c;
   }
  
   //on pressing d
   Coordinate d(Coordinate c){
       c.x=c.x+1;
       return c;
   }
  
   //run game
   void runGame(){
       //scanner to take iput
       Scanner input=new Scanner(System.in);
       //taking number of test cases as input
       int testCases=Integer.parseInt(input.nextLine());
       //creating array of coordinates to store for each test case
       Coordinate[] arr=new Coordinate[testCases];
       String initial,movements;
       //looping for each test case
       for(int i=0;i<testCases;i++){
           //take initial position as input
           initial=input.nextLine();
           //taking movements as input
           movements=input.nextLine();
           //splitting initial position in x and y
           String[] init = initial.split("\\s+");
           //creating coordinate with x and y
           arr[i]=new Coordinate(Integer.parseInt(init[0]),Integer.parseInt(init[1]));
           //splitting movements
           String[] moves = movements.split("\\s+");
           //based on each movement calling function
           for(String move:moves){
               if(move.equalsIgnoreCase("w")){
                   arr[i]=w(arr[i]);
               }else if(move.equalsIgnoreCase("a")){
                   arr[i]=a(arr[i]);
               }
               else if(move.equalsIgnoreCase("s")){
                   arr[i]=s(arr[i]);
               }else{
                   arr[i]=d(arr[i]);
               }
           }
       }
       //displaying results
       for(int i=0;i<testCases;i++){
           System.out.println(arr[i].x+" "+arr[i].y);
       }
       input.close();
   }
  
   public static void main(String[] args){
       //creating Zeldar game and running
       Zeldar z=new Zeldar();
       z.runGame();
   }

}

Output:


Related Solutions

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...
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,...
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...
(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)
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: Person. Define Class Person Write the class header Class Variables Class Person is to have the following data members: firstName of type String, lastName of type String representing a person’s first and last names and ssn of type int representing a social security number. Each data member has an access specifier of type private. Constructors Class Person...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: TV. Define Class TV Write the class header Class Variables Class TV is to have the following instance variables: 1.channel of type int 2. volumeLevel of type int 3. isOn of type boolean Each data member is to have an access specifier of private. Constructor Class TV is to have a single, no-arg constructor that initializes the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT