In: Computer Science
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
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: