-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
61 lines (46 loc) · 2.22 KB
/
RockPaperScissors.java
File metadata and controls
61 lines (46 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
/* STEPS TO CREATE GAME :
1.WELCOME MESSAGE
2.DECLARE VARS
3.CREATE RANDOM CLASS
4.TAKE USER INPUT FOR MOVE
5.TAKE COMPUTERS INPUT USING RANDOM CLASS
6.COMPARE USER'S INPUT & COMPUTER'S INPUT
7.DISPLAY RESULT (WIN/LOOSE)
8.CREATE LOOP TO PLAY AGAIN OR NO
9.If no print an "Exit" message.
*/
System.out.println("*****WELCOME TO THE GAME!*****\n");
String[] moves = {"rock", "paper", "scissors"};
String choice, comChoice;
String playAgain = "Y";
do {
System.out.print("\nEnter your move (rock,paper,scissors) : ");
choice = scanner.nextLine().toLowerCase(); //"to lowerCase() in case a user types in an upper case "rock".
if (!choice.equals("rock") && !choice.equals("paper") && !choice.equals("scissors")) {
System.out.println("Invalid Choice!Choose again");
continue;
}
comChoice = moves[random.nextInt(0, 3)];
System.out.printf("Computer's choice = %s", comChoice);
if (choice.equals(comChoice)) {
System.out.println("\nTIE!");
} else if ((choice.equals("rock") && comChoice.equals("scissors")) ||
(choice.equals("paper") && comChoice.equals("rock")) ||
(choice.equals("scissors") && comChoice.equals("paper"))) {
System.out.println("\nYOU WIN!");
} else {
System.out.println("\nYOU LOSE!");
}
System.out.print("\nDo you want to play again (Y/N)? : ");
playAgain = scanner.nextLine().toLowerCase();
} while (playAgain.equals("y"));
System.out.println("\n*****THANKS FOR PLAYING!*****");
scanner.close();
}
}