├── README.md ├── .gitignore ├── Main.java └── Play.java /README.md: -------------------------------------------------------------------------------- 1 | # Rock-paper-scissor 2 | CLI rock paper scissors in java 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | replay_pid* 25 | -------------------------------------------------------------------------------- /Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | Play play = new Play(); 7 | 8 | System.out.println("Welcome to Rock Paper Scissors!"); 9 | String choice = ""; 10 | do { 11 | if (choice.equals("yes")) 12 | System.out.println("\nOk, lets go again!"); 13 | 14 | play.playGame(); 15 | 16 | do { 17 | System.out.print("Do you want to continue playing[Yes/No]: "); 18 | choice = scanner.nextLine(); 19 | choice = choice.toLowerCase(); 20 | } while (!(choice.equals("yes")) && !(choice.equals("no"))); 21 | 22 | } while (choice.equals("yes")); 23 | 24 | scanner.close(); 25 | System.out.println("\nThank you for playing Rock Paper Scissors ^-^"); 26 | } 27 | } -------------------------------------------------------------------------------- /Play.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Random; 3 | 4 | public class Play { 5 | Scanner scanner = new Scanner(System.in); 6 | Random random = new Random(); 7 | private String[] rps = { "Rock", "Paper", "Scissor" }; 8 | private String[] rps2 = { "rock", "paper", "scissor" }; 9 | 10 | private boolean ifInRps(String str) { 11 | return (str.equals(rps2[0]) || str.equals(rps2[1]) || str.equals(rps2[2])); 12 | } 13 | 14 | private String getUserChoice() { 15 | String user; 16 | 17 | do { 18 | System.out.print("\nEnter your choice[Rock, Paper or Scissor]: "); 19 | user = scanner.nextLine(); 20 | user = user.toLowerCase(); 21 | } while (!ifInRps(user)); 22 | 23 | return user; 24 | } 25 | 26 | private int getChoiceIndex(String str) { 27 | int i; 28 | 29 | if (str.equals(rps2[0])) 30 | i = 0; 31 | else if (str.equals(rps2[1])) 32 | i = 1; 33 | else 34 | i = 2; 35 | 36 | return i; 37 | } 38 | 39 | public void playGame() { 40 | int compIndex = random.nextInt(3); 41 | String compChoice = rps2[compIndex]; 42 | String userChoice = getUserChoice(); 43 | int userIndex = getChoiceIndex(userChoice); 44 | 45 | System.out.println("You chose " + rps[userIndex] + ", and computer chose " + rps[compIndex]); 46 | if (userChoice.equals(rps2[0])) { 47 | if (compChoice.equals(rps2[0])) 48 | System.out.println("Its a Draw!"); 49 | else if (compChoice.equals(rps2[1])) 50 | System.out.println("Sorry, you lose!"); 51 | else 52 | System.out.println("You Win, yay!"); 53 | } else if (userChoice.equals(rps2[1])) { 54 | if (compChoice.equals(rps2[1])) 55 | System.out.println("Its a Draw!"); 56 | else if (compChoice.equals(rps2[2])) 57 | System.out.println("Sorry, you lose!"); 58 | else 59 | System.out.println("You Win, yay!"); 60 | } else { 61 | if (compChoice.equals(rps2[2])) 62 | System.out.println("Its a Draw!"); 63 | else if (compChoice.equals(rps2[1])) 64 | System.out.println("You Win, yay!"); 65 | else 66 | System.out.println("Sorry, you lose!"); 67 | } 68 | System.out.println(); 69 | } 70 | } --------------------------------------------------------------------------------