Question

In: Computer Science

JAVA for DUMMIES (Game Development) You're the lead programmer at a AAA studio making a sequel...

JAVA for DUMMIES (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 <x> <y>, where “x” and
“y” are the coordinates within the dungeon grid.
Sample Input Sample Output
2


4 4 2 2
2 3
s s s w


10 10 8 4
9 4
s d w a

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.util.Scanner;

public class Game{

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        int grid_x, grid_y, x, y, t;

        char direction;

        t = scnr.nextInt();

        while(t>0){

            grid_x = scnr.nextInt();

            grid_y = scnr.nextInt();

            x = scnr.nextInt();

            y = scnr.nextInt();

            scnr.nextLine();

            String sequence[] = scnr.nextLine().split(" ");

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

                direction = sequence[i].charAt(0);

                if(direction=='w'){

                    if(y-1>=0){

                        y--;

                    }

                }

                else if(direction=='a'){

                    if(x-1>=0)

                        x--;

                }

                else if(direction=='s'){

                    if(y+1<grid_y)

                        y++;

                }

                else if(direction=='d'){

                    if(x+1<grid_x)

                        x++;

                }

            }

            System.out.println(x+" "+y);

            t--;

        }

    }

}


Related Solutions

Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java SE version only). If you do not have the latest Java installation to match, Netbeans will direct you to download the needed version of Java. If needed, download and install the latest Oracle Official Java Software Development Kit Standard Edition (Java SDK SE), and JUnit. I suggest you do this via the bundle referred to as The Java Development Kit (JDK). Download the architecture...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT