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.
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....
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...
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...
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...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
java For this assignment, you will create a Time class that holds an hour value and...
java For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 AM is 0001 and 1 PM is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 AM to 11:59 PM. In previous assignments, we had a requirement that your class be named Main. In this assignment, the...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT