In: Computer Science
JAVA PROGRAM
Without changing changing main or PlayOneGame, you need to write the rest of the methods. Please complete "Rock, Scissor, Paper" game.
Main:
public static void main(String[] args ) { int wins = 0; int losses = 0; PrintWelcomeMessage(); // Just says hello and explains the rules while( PlayerWantsToPlay() ) // Asks the player if they want to play, and returns true or false { boolean outcome = PlayOneGame(); // One full game. We do NOT come back here on a tie. The game is not over on a tie. if( outcome == true ) wins++; else losses++; } PrintGoodByeMessage( wins, losses ); // Prints the outcome of the games }
Playonegame:
static boolean PlayOneGame() { int AI; int player; int outcome; do { AI = AIPick(); // 0,1,2 player = PlayerPick() // 0,1,2 outcome = ComputeWinner(AI, Player)// 0,1,2 } while( outcome == 0 );// while tied if( outcome == 1 ) return true; // they won else return false;// they lost }
Included all the other functions that are to be implemented. Simply Paste this code in your Class and run.
static int AIPick()
{
Random random = new Random();
return random.nextInt(3);
}
static int PlayerPick()
{
Scanner scanner = new Scanner(System.in);
int playerChoice;
System.out.print("Please enter your choice, 0 ==> Rock, 1 ==> Paper, 2 ==> Scissors: ");
playerChoice = scanner.nextInt();
while (playerChoice < 0 || playerChoice > 2)
{
System.out.println("Please enter a choice among 0, 1, 2");
System.out.print("Please enter your choice, 0 ==> Rock, 1 ==> Paper, 2 ==> Scissors: ");
playerChoice = scanner.nextInt();
}
return playerChoice;
}
static int ComputeWinner(int AIChoice, int playerChoice)
{
if (AIChoice == playerChoice) // Tie
{
System.out.println("Game tied");
return 0;
}
if (AIChoice < playerChoice)
{
if (AIChoice == 0 && playerChoice == 2) // Rock wins over Scissors
{
// System.out.println("Computer Won");
return -1;
}
// Paper wins over Rock and Scissors wins over Paper
// System.out.println("You Won");
return 1;
}
// AIChoice greater than playerChoice
if (AIChoice == 2 && playerChoice == 0) // Rock wins over Scissors
{
// System.out.println("You Won");
return 1;
}
// Paper wins over Rock and Scissors wins over Paper
// System.out.println("Computer Won");
return -1;
}
static void PrintWelcomeMessage()
{
System.out.println("Hi there!");
System.out.println("Please enter your choice when you are prompted to pick");
System.out.println("Rock beats Scissors\nPaper beats Rock\nScissors beat Paper");
}
static void PrintGoodByeMessage(int wins, int losses)
{
System.out.println("You won "+ wins + " games");
System.out.println("Computer won "+ losses + " games");
System.out.println("See You!");
}
static boolean PlayerWantsToPlay()
{
int wantToPlay;
Scanner sc = new Scanner(System.in);
System.out.print("Would you like to play ? Yes(1)/No(0): ");
wantToPlay = sc.nextInt();
return wantToPlay > 0;
}