In: Computer Science
Programmed In Java
In this assignment you will create a simple game.
You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y
ou will start by welcoming the user.
1. You should print "Welcome! Your starting coordinates are (0, 0).”
2. On the next line, you will tell the user the acceptable list of commands. This should look like “Acceptable commands are 'up', 'down', 'left', 'right' or 'exit'.”
3. On a new line you will ask the user to enter a move by printing “Next move?”. Use the println method. You should now process the user’s command. The coordinate of the user is represented by (x, y). The user always starts at (0, 0). You MUST use a switch statement to process the user’s command. I will check the code for a switch statement. Think of a coordinate moving on a grid.
1. The up command should increment y. 2. The down command should decrement y. 3. The left command should decrement x. 4. The right command should increment x. 5. Exit should end the game. a. If the user types “exit”, you should print “Goodbye. You ended on (x, y).” where x and y are the coordinates the user ended on. After you process the command, if the user did not type “exit”, you should print the new coordinates by printing “You are now on (x, y). Next?” where x and y are the current coordinates. The “Next?” part in that print statement is asking the user for the next command. The trick to this assignment is that you will need a loop to keep asking the user for input, until they type “exit”.
Helpful Hints ● A while loop will be needed to keep asking the user for commands. The condition for the while loop will be that the loop will continue until the user types “exit”. ● The increment and decrement operators will be helpful to change the values of x and y.
As per your question, The Java 8 program is written perfectly and correctly as stated in the question. Please have a look on the source code(refer in-line comments for better understanding) and run output below:-
import java.util.Scanner;
public class Game {
//defining Scanner object for taking user input
private static Scanner scan= new Scanner(System.in);
//the main driver method
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("Welcome! Your starting coordinates are (0, 0).");
System.out.println("Acceptable commands are 'up', 'down', 'left', 'right' or 'exit'");
System.out.println("Next move?");
int x = 0; //defining and initializing the x coordinate
int y = 0; //defining and initializing the y coordinate
boolean start = true; // defining a start boolean for checking exit command
while(start) // loop will execute till start is true
{
String nextMove = scan.nextLine(); //taking next command user input
switch(nextMove)
{
case "up":
y = y + 1; // increasing y coordinate on up command
System.out.println("You are now on (" + x + ", " + y + "). Next?");
break;
case "down":
y = y - 1; // decreasing y coordinate on down command
System.out.println("You are now on (" + x + ", " + y + "). Next?");
break;
case "left":
x = x - 1; // decreasing x coordinate on left command
System.out.println("You are now on (" + x + ", " + y + "). Next?");
break;
case "right":
x = x + 1; // increasing x coordinate on right command
System.out.println("You are now on (" + x + ", " + y + "). Next?");
break;
case "exit":
start = false; // making start is false for loop end for Game end
System.out.println("Goodbye. You ended on (" + x + ", " + y + ").");
break;
default: //for any other user input
System.out.println("not a valid command");
}
}
scan.close();
}
}
=> The Run Output of the above program exactly as per your question statement:-
Thank You.. Please don't forget to like it.