Project #47328 - java HW

Rock Paper Scissors Game

 

Objective:

 

Write one interface and three classes which will make a rock paper scissors game.  First download the Driver.

The interface named RPSPlayerInterface will have the following methods

  • public int getPoints()
  • public String getGesture()
  • public void setPoints(int aPoints)
  • public void setGesture(String aGesture)
  • public void chooseGesture()

The next class RPSPlayer implements this interface

  • It has an instance variable gesture which is a string that corresponds to “rock”, “paper”, or “scissors”
  • It has another rinstance variable points that keeps track of how many times the player has won
  • Create only a default constructor that sets the gesture to “none” and points to 0
  • Create the accessors and mutators for the instance variables
    • The mutator for setGesture should make sure it is either “rock”, “paper”, or “scissors”.  If it is not then tell the user that was a wrong choice and set gesture to “none”
  • Write the chooseGesture method
    • In this case just set the instance variable gesture to “none”

Next write the class RPSHumanPlayer which inherits from RPSPlayer

  • It has an instance variable keyboard which is of type Scanner
    • Make sure to import java.util.Scanner
  • Create a default constructor
    • Calls the parent’s default constructor
    • Sets up the keyboard to System.in
  • Override the method chooseGesture
    • Call the parent’s setGesture method and pass the string gotten from the Scanner keyboard.
    • You do not have to prompt the user.

Next write the class RPSComputerPlayer which also inherits from RPSPlayer

  • It has an instance variable rand which is of type Random
    • Make sure to import java.util.Random
  • Create a default constructor
    • Calls the parent’s default constructor
    • Constructs the rand variable
  • Override the method chooseGesture
    • Using the random variable it should randomly select a number, which is either 0, 1, or 2
    • Correspond the number to a string which will either rock, paper, or scissors
      • For instance if rand equals 0 then it is a rock, else if 1 then paper, else if 2 then scissors
    • Use the parent’s setGesture to then assign the randomly picked gesture

 

 

Example Dialog:

Welcome to the rock paper scissors game!

Enter either "rock", "paper", or "scissors" to compete

rock

You picked rock

The computer picked paper

You lose!

You have 0 points

The computer has 1 points

Press enter to continue or enter "quit" to quit

 

Enter either "rock", "paper", or "scissors" to compete

paper

You picked paper

The computer picked scissors

You lose!

You have 0 points

The computer has 2 points

Press enter to continue or enter "quit" to quit

 

Enter either "rock", "paper", or "scissors" to compete

scissors

You picked scissors

The computer picked paper

You win!

You have 1 points

The computer has 2 points

Press enter to continue or enter "quit" to quit

 

Enter either "rock", "paper", or "scissors" to compete

dynamite

Incorrect choice

You picked none

The computer picked paper

You lose!

You have 1 points

The computer has 3 points

Press enter to continue or enter "quit" to quit

quit

Good bye

Finally:

Upload all the files to the dropbox

 

Driver:

 

import java.util.Scanner;
public class RPSGame {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean gameOver = false;
		RPSHumanPlayer human = new RPSHumanPlayer();
		RPSComputerPlayer computer = new RPSComputerPlayer();
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("Welcome to the rock paper scissors game!");
		while(gameOver == false)
		{
			System.out.println("Enter either \"rock\", \"paper\", or \"scissors\" to compete");
			human.chooseGesture();
			computer.chooseGesture();
			
			System.out.println("You picked "+human.getGesture());
			System.out.println("The computer picked "+computer.getGesture());
			
			if(human.getGesture().equals("rock"))
			{
				if(computer.getGesture().equals("paper"))
				{
					System.out.println("You lose!");
					computer.setPoints(computer.getPoints()+1);
				}
				else if(computer.getGesture().equals("scissors"))
				{
					System.out.println("You win!");
					human.setPoints(human.getPoints()+1);
				}
				else
				{
					System.out.println("Tie");
				}
			}
			else if(human.getGesture().equals("paper"))
			{
				if(computer.getGesture().equals("scissors"))
				{
					System.out.println("You lose!");
					computer.setPoints(computer.getPoints()+1);
				}
				else if(computer.getGesture().equals("rock"))
				{
					System.out.println("You win!");
					human.setPoints(human.getPoints()+1);
				}
				else
				{
					System.out.println("Tie");
				}
			}
			else if(human.getGesture().equals("scissors"))
			{
				if(computer.getGesture().equals("rock"))
				{
					System.out.println("You lose!");
					computer.setPoints(computer.getPoints()+1);
				}
				else if(computer.getGesture().equals("paper"))
				{
					System.out.println("You win!");
					human.setPoints(human.getPoints()+1);
				}
				else
				{
					System.out.println("Tie");
				}
			}
			else
			{
				System.out.println("You lose!");
				computer.setPoints(computer.getPoints()+1);
			}
			
			System.out.println("You have "+human.getPoints()+" points");
			System.out.println("The computer has "+computer.getPoints()+" points");
			System.out.println("Press enter to continue or enter \"quit\" to quit");
			String input = keyboard.nextLine();
			if(input.equalsIgnoreCase("quit"))
			{
				gameOver = true;
				System.out.println("Good bye");
			}
		}
		
	}

}

Subject Computer
Due By (Pacific Time) 11/12/2014 11:30 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy