Question

In: Computer Science

Programmed In Java In this assignment you will create a simple game. You will put all...

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.

Solutions

Expert Solution

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.


Related Solutions

Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters...
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters and then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks: for example, G* T*** (for Go Team). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again or tell the user the guessed letter is not in the...
in java Create simple number guessing game as follows. First, prompt the user to enter a...
in java Create simple number guessing game as follows. First, prompt the user to enter a min and max value. In your code, declare and assign a variable with a random integer in this range (inclusive). Then, ask the user to guess the number. Input the user's guess. And at the end output "Correct!" if the user's guess was right on spot, or "Sorry, the answer is not corrcet.” The number I was looking for was xxx. Replace xxx with...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a message if they try to divide by 0. I have this so far. What code should I put? divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (number1.getText().length() != 0 && number2.getText().length() != 0) { double n1= Double.parseDouble(number1.getText().toString()); double n2= Double.parseDouble(number2.getText().toString()); double res= n1 / n2; result.setText(String.valueOf(res)); } else { Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show(); } } });
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
For this assignment you will create a set of simple classes that model a cookbook. The...
For this assignment you will create a set of simple classes that model a cookbook. The cookbook will consist of set or recipes. Each recipe will consist of a set of ingredients and instructions. Your submission will consist of the below three classes and a test driver (the only class with a main()) that exercises the functionality of your system. You will need to implement the following classes based on the description of their attributes and operations: Ingredient name -...
The Project is: 1. Create a new Java program which implements a simple PacMan-type text game...
The Project is: 1. Create a new Java program which implements a simple PacMan-type text game which contains the following functionality: A) At program startup, constructs and displays a 2-dimensional grid using standard array(s) (no collection classes allowed) with the size dynamically specified by the user (X and Y sizes can be different). Places the PacMan in the upper-left corner of the grid facing left All grid cells should have the empty cell character of ‘.’ except for the start...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living neighbors, it...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living neighbors, it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT