Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import java.util.Random;
- public class RPS
- { // Start of Class
- static String getPlayerName()
- { // Start of getPlayerName method
- Scanner inPut = new Scanner(System.in);
- System.out.print("Enter your first name: ");
- String playerName = inPut.nextLine();
- while (playerName.length() == 0)
- { // Start of If
- System.out.println("No name entered.");
- System.out.print("Please enter your name: ");
- playerName = inPut.nextLine();
- } // End of If
- return playerName ;
- } // End of getPlayerName method
- static void welcomeMessage(String playerName)
- {
- System.out.println("Hello "+playerName+". Welcome to the Rock, Paper, Scissors game.");
- }
- static String playerChoiceM()
- {
- Scanner inPut = new Scanner(System.in);
- System.out.print("Enter Rock, Paper, Scissors, or Done: ");
- String playerChoice = inPut.nextLine();
- while (!playerChoice.equalsIgnoreCase("Rock") && !playerChoice.equalsIgnoreCase("Scissors") && !playerChoice.equalsIgnoreCase("Paper") && !playerChoice.equalsIgnoreCase("Done"))
- {
- System.out.print("Enter Rock, Paper, Scissors, or Done: ");
- playerChoice=inPut.nextLine();
- }
- System.out.println("You selected "+playerChoice);
- return playerChoice;
- }
- static String computerChoiceM()
- {
- Random rand = new Random();
- String[] computerChoiceArray ={"Rock","Paper","Scissors"};
- int randomIndex = rand.nextInt(3);
- String computerChoice = computerChoiceArray[randomIndex];
- System.out.println("The computer chose "+computerChoice);
- return computerChoice;
- }
- static int[] determineWinner(String playerChoice , String computerChoice)
- {
- int[] scores;
- scores = new int[3];
- if (playerChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Paper"))
- { scores[1] +=1;
- System.out.println("You lose!"); }
- else if (playerChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Scissors"))
- { scores[0] +=1;
- System.out.println("You win!");}
- else if (playerChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Rock"))
- {scores[2] +=1;
- System.out.println("You tie!"); }
- else if (playerChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Rock"))
- {scores[0] +=1;
- System.out.println("You win!");}
- else if (playerChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Paper"))
- {scores[2] +=1;
- System.out.println("You tie!");}
- else if (playerChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Scissors"))
- {scores[1] +=1;
- System.out.println("You lose!");}
- else if (playerChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Scissors"))
- {scores[2] +=1;
- System.out.println("You tie!");}
- else if (playerChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Paper"))
- {scores[0] +=1;
- System.out.println("You win!");}
- else if (playerChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Rock"))
- {scores[1] +=1;
- System.out.println("You lose!");}
- return scores;
- }
- static void winnerBoard(int playerWins , int computerWins , int gameCounter , int ties)
- {
- System.out.println();
- System.out.println("Thank you for playing! Here are the results:");
- System.out.println("You won "+playerWins+" time(s)!");
- System.out.println("Computer won "+computerWins+" time(s)!");
- System.out.println("It was a tie "+ties+" time(s)!");
- System.out.println("You played "+gameCounter+" game(s)!");
- }
- public static void main(String[] args)
- {
- Scanner inPut = new Scanner(System.in);
- String playerName = getPlayerName();
- welcomeMessage(playerName);
- System.out.println();
- String anotherGame = "yes";
- int[] scores={0,0,0};
- int playerWins =0, computerWins=0 , ties=0 , gameCounter=1;
- while (anotherGame.equalsIgnoreCase("Yes"))
- {
- String playerChoice=playerChoiceM();
- if (playerChoice.equalsIgnoreCase("Done"))
- {
- winnerBoard(playerWins,computerWins,ties,gameCounter);
- System.exit(0);
- }
- String computerChoice=computerChoiceM();
- scores = determineWinner(playerChoice , computerChoice);
- playerWins += scores[0];
- computerWins += scores[1];
- ties += scores[2];
- System.out.println();
- System.out.print("Would you like to play another game? yes or no: ");
- anotherGame = inPut.nextLine();
- System.out.println();
- if (anotherGame.equalsIgnoreCase("yes"))
- {
- ++gameCounter;
- }
- else if (!anotherGame.equalsIgnoreCase("yes") || !anotherGame.equalsIgnoreCase("no"))
- {
- while (!anotherGame.equalsIgnoreCase("no") && !anotherGame.equalsIgnoreCase("yes"))
- {
- System.out.print("Would you like to play another game? yes or no: ");
- anotherGame = inPut.nextLine();
- if (anotherGame.equalsIgnoreCase("yes"))
- {
- ++gameCounter;
- }
- }
- }
- }
- winnerBoard(playerWins,computerWins,gameCounter,ties);
- }
- } // End of Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement