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.
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...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
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...
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT