Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class rock_paper_scissor
- {
- private int winsToWinTheMatch;
- public rock_paper_scissor(int winsToWinTheMatch)
- {
- this.winsToWinTheMatch = winsToWinTheMatch;
- }
- public void start()
- {
- System.out.println("WELCOME TO ROCK, PAPER AND SCISSOR!");
- gameplay();
- }
- private void gameplay()
- {
- int score = 0, enemyScore = 0, enemyChoice, userChoice;
- Random random = new Random();
- Scanner scanner = new Scanner(System.in);
- do
- {
- enemyChoice = random.nextInt(1, 4);
- System.out.println("Please Choose \n[1] ROCK\n[2] Paper\n[3] SCISSOR\n");
- System.out.print("Enter: ");
- String choice = scanner.nextLine();
- String winner = checkWinner(getWeapon(Integer.valueOf(choice)), getWeapon(Integer.valueOf(enemyChoice)));
- if(winner.equals("PLAYER"))
- {
- System.out.println("Player is the winner");
- score++;
- }
- else if(winner.equals("ENEMY"))
- {
- System.out.println("Enemy is the winner");
- enemyScore++;
- }
- else {
- System.out.println("TIE");
- }
- System.out.println("\nCURENT SCORE ");
- System.out.println("Player: " + score);
- System.out.println("Enemy: " + enemyScore);
- System.out.println("==========================================\n");
- if(winsToWinTheMatch <= score)
- {
- System.out.println("PLAYER IS THE WINNER OF THE MATCH");
- break;
- }
- else if(winsToWinTheMatch <= enemyScore)
- {
- System.out.println("ENEMY IS THE WINNER OF THE MATCH");
- break;
- }
- } while(true);
- }
- private String getWeapon(int weapon)
- {
- if(weapon == 1)
- return "ROCK";
- else if(weapon == 2)
- return "PAPER";
- else if(weapon == 3)
- return "SCISSOR";
- else
- return "ERROR";
- }
- private String checkWinner(String playerChoice, String enemyChoice)
- {
- if(playerChoice.equalsIgnoreCase(enemyChoice))
- return "TIE";
- else if(playerChoice.equals("ROCK") && enemyChoice.equals("PAPER"))
- return "ENEMY";
- else if(playerChoice.equals("SCISSOR") && enemyChoice.equals("ROCK"))
- return "ENEMY";
- else if(playerChoice.equals("PAPER") && enemyChoice.equals("SCISSOR"))
- return "ENEMY";
- else
- return "PLAYER";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement