Build a simple robot game where the robots

Assignment Help JAVA Programming
Reference no: EM131591397

Question No 1

The objective is to build a simple robot game where the robots are modeled as objects and move around in a 2- dimensional grid as shown in the diagram below. The position of the robot is expressed as x- and y- coordinates, which can take any value within the limits of the grid. The goal of the game is to move the robot so that reaches the grid number (8,8) from grid number (1,1). The robot also faces one of three directions (Up, Right or Diagonal). It can move one, two or three steps in the direction it faces. It can also collect points as it moves across the grid.

The program specification for the first question is long, but read on. The actual programming part is not too hard. You don't need to use 2-d arrays or any other data structures, but just x and y coordinates and random numbers.

Start by defining a robot object (Robot.java) with the following attributes:
- name (a String)
- x position (an integer)
- y position (an integer)
- direction (it faces) (you can decide on this data type)
- points collected (an integer)

Implement the following methods:
- Constructor that sets the name and the direction it faces to user-given values. The constructor should also set the x and y positions to (1,1) and the points collected to 0.
- Get and set methods
- move that moves the robot by the given number of steps in the direction it faces.

- toString method that returns the attributes of the robot
- amIAhead method that compares the x and y coordinates of this robot with the x and y coordinates of another robot and returns true if this robot is closer to (8,8). A simple test is to just add the x and y values and see which is larger. For example, a robot at (3,3) is ahead than the robot at (1,4). As another example, a robot at (7,1) and another at (4,4) are at the same distance from (8,8).

You may add other methods if you find them necessary to complete your program.

Now build a RobotGame.java class that contains the main method to test the above class and run a simple simulation game using two robots as follows:

Prompt the user to enter the name and direction for two robots and create the two robots.

Next, move each robot 1, 2 or 3 steps by generating random numbers. You can use Math.random() to do this.
(int)(Math.random() * (max-min)+1) + min
generates a random number between min and max, both inclusive. For example,
(int)(Math.random()*101
will generate a random number between 0 and 100, both inclusive. (int)(Math.random()*11 + 10)
will generate a random number between 10 and 20, both inclusive.

The Bug.java program in Lab No. 2 shows you how Math.random() works. You can also check out web resources on Math.random(). Change the x and y positions of the robot accordingly. For example, if the robot is facing up and it moves two steps from (1,1) which is the starting point for all robots, then its position changes to (1,3). If the robot is facing Diagonal and it moves three steps, its position changes from (1,1) to (4,4). Note that the robot cannot go beyond the grid. Therefore, if the robot is in box (1,7) and the random number that is generated is 2, it cannot make the move - it stays in its box for this round.

Collect points for each move according to the following rules:

1. At the start, each robot earns 1+1=2 points for being in the box (1,1).
2. If a robot moves into a box (x,y) collect x+y points for that robot and add it to the current number of points.
3. If a robot A's move falls into a box (x,y) which is already occupied by another robot, deduct x+y points from A and do not move A from its current position.

After each move, change the robot's direction by generating another random number from 1 to 3. (1 means direction is Up, 2 means Right and 3 means Diagonal).

After each move, print the attributes of each robot using the toString method. Also print which robot is ahead by comparing using the amIAhead method. If neither one is ahead, print that they are at the same distance.

If one robot reaches (8,8), remove it from further moves (by using a boolean variable or otherwise).

Play the game by putting the moves in a loop and see which robot gets to (8,8) first and which robot collects the most points!

A sample screen dialog would look something like this:
Welcome to the Robot Game Simulator! Enter the name of the first robot: Artoo
Enter the direction of the first robot: Up

Enter the name of the second robot: Detoo

Enter the direction of the second robot: Right Simulation started!

(this means that in the first move, Artoo's random number was 2 for the steps and 2 for the direction, so it moved up to (1,3) and turned right. The total points for Artoo now is 2 + 4 = 6. Detoo's random number was 1 for the steps and 3 for direction, so it moved to (2,1) and turned diagonal. The total points for Detoo is 2 + 3=5).

Move!
Artoo (2,3) Up 11 points
Detoo (4,3) Right 12 points Detoo ahead!
Move!
Artoo (2,6) Diag 19 points
Detoo (4,4) Right 20 points
Both are at the same distance from the goal!

Etc.

Move!
Artoo (8,8) Up 56 points
Detoo (7,8) Up 51 points
Artoo has reached the goal (56 points) Move!
Detoo (7,8) Right 51 points

Move!
Detoo (8,8) Up 66 points

Detoo has reached the goal (66 points) Game over!

Question No. 2

In this question, you are required to construct a class that could be used to play a game of hangman. For those of you who haven't played this game, check out web resources. There is an animation of the game at https://www.proprofs.com/games/word-games/hangman/animation/

We will simplify the game a bit. Rather than randomly generating a secret word from a large dictionary, we will use the words from a much smaller dictionary of 25 words. These will be the names of the first 25 Pokemon characters. These 25 words will be stored in a String array, and each time the game is played, a word is randomly selected from this array. The words are the following:

String[] secretWordDictionary = {"bulbasaur", "ivysaur", "venusaur", "charmander", "charmeleon", "charizard", "squirtle", "wartortle", "blastoise", "caterpie", "metapod", "butterfree", "weedle", "kakuna", "beedrill", "pidgey", "pidgeotto", "pidgeot","rattata", "raticate", "spearow", "fearow", "ekans", "arbok", "pikachu"};

The class has the following attributes:

a. The secretWordDictionary: the array that has been given above.
b. The secret word: private char[] word (that is, a char array)
c. The disguised word (another array of char), in which each unknown letter in the secret word is replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a and b have been guessed, the disguised word would be ab?a?a?ab?a.
d. The number of guesses made.
e. The number of incorrect guesses.

It will have the following methods:
- No-args constructor: creates an empty Hangman object
- Get methods for the disguised word, number of guesses made and number of incorrect guesses. (DO NOT add public get methods for the secretWordDictionary or the secretword since they must not be accessible from outside. If you need these get methods in your program, make them private).
- Set methods for secret word, disguised word, number of guesses made and number of incorrect guesses. You don't need a set method for the secretWordDictionary since it is hard-coded into the program.
- generateSecretWord(): Generate a random number from 0 to 24 and select the appropriate word from the secretWordDictionary. Convert it into a char array and store in char[] word.
- makeGuess(char c) guesses that character c is in the word..
- isFound returns true if the hidden word has been discovered.

Here's the skeleton outline for the program Hangman.java.

public class Hangman
{
private String[] secretWordDictionary = {"bulbasaur","ivysaur", "venusaur","charmander","charmeleon", "charizard", "squirtle", "wartortle", "blastoise", "caterpie", "metapod", "butterfree", "weedle", "kakuna", "beedrill", "pidgey", "pidgeotto", "pidgeot","rattata", "raticate", "spearow", "fearow", "ekans", "arbok", "pikachu"};
private char[] secretWord; private char[] disguisedWord; private int numGuesses; private int numIncorrect;

//continue the rest of the code

}

Test the above class to ensure that all the methods are functional.

Next implement a demo class by writing a demo program Hangmandemo.java and simulate the game of hangman.

Notes:

1. Keep a limit of six on the number of incorrect guesses, and quit the game if the user doesn't get the word right within the limit.
2. You can use two char arrays to keep track of the letters that were guessed, one for correct guesses and another for incorrect guesses. You can use this to prompt the user that they cannot pick a letter that was already guessed.
3. Note the conversions between a char array and a String.

String s = new String(word);
will convert a char array word into a String s.
Similarly,
char[] word = s.toCharArray();
will convert a String s into a char array word.

Here are two sample screen dialogs:

Welcome to the Hangman game.
I have a secret Pokemon character name. You have to guess it. You are allowed only six incorrect guesses.

Secret word: ??????? Enter the guess: a Correct.

Secret word: ?a??a?a Enter the guess: s
Incorrect. You have 5 incorrect guesses left.

Secret word: ?a??a?a Enter the guess: t Correct.

Secret word: ?attata Enter the guess: g
Incorrect. You have 4 incorrect guesses left.

Secret word: ?attata Enter the guess: a
You already guessed a. Choose another letter.

Secret word: ?attata Enter the guess: r Correct.

Secret word: rattata You win!

Another sample screen dialog:

Welcome to the Hangman game.
I have a secret Pokemon character name. You have to guess it. You are allowed only six incorrect guesses.

Secret word: ????? Enter the guess: t
Incorrect. You have 5 incorrect guesses left.

Secret word: ????? Enter the guess: s Correct.

Secret word: ????s Enter the guess: m
Incorrect. You have 4 incorrect guesses left.

Secret word: ????s Enter the guess: g
Incorrect. You have 3 incorrect guesses left.

Secret word: ????s Enter the guess: z
Incorrect. You have 2 incorrect guesses left.

Secret word: ????s Enter the guess: p
Incorrect. You have 1 incorrect guesses left.

Secret word: ????s Enter the guess: b
Incorrect. You have 0 incorrect guesses left.

The secret word was ekans. You lose!

Reference no: EM131591397

Questions Cloud

What the interviewee wants to accomplish to form a plan : You will use the information that you gained from the interview on what the interviewee wants to accomplish to form a "Client Service Plan."
Explain the relationship between the number of years : The Nickelodeon Manufacturing Co. has a series of $1,000 par value bonds outstanding. Explain the relationship between the number of years until a bond matures
Develop a health organization disaster planning : Develop a Health Organization Disaster Planning and Response Strategy that links federal, state, and local agency authorities and reflects.
Calculate apr for each loan given the specifics in each case : Calculate the APR for each loan given the specifics in each case.
Build a simple robot game where the robots : Build a simple robot game where the robots are modeled as objects and move around in a 2- dimensional grid as shown -
Determine the pension expense : Prepare the journal entries necessary to record the pension activity for the year. Determine the pension expense for 2017
Product ladder and hierarchy of needs : Assess the importance of the Ries and Trout's Product Ladder as a target marketing device within the health care industry.
Summary on the effects of globalization on health in russia : Post a brief summary on the effects of globalization on health in Russia. Discuss one change in quality of life in post-transition Russia.
Explain technologies that have frustrating glitches : Technology is changing so quickly that we are frequently using computers, software programs, and other technologies that have frustrating glitches and problems

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd