-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathRock_Paper_Scissors_Game.java
125 lines (114 loc) · 5 KB
/
Rock_Paper_Scissors_Game.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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();
String[] choices = {"rock", "paper", "scissors"};
System.out.println("Welcome to the game!");
System.out.println("Enter:");
System.out.println("r for rock");
System.out.println("p for paper");
System.out.println("s for scissors");
System.out.print("Enter your name: ");
String playerName = scanner.nextLine();
int userScoreTotal = 0;
int computerScoreTotal = 0;
int i = 1;
while (i == 1) {
String userChoice = userInputChecker(scanner);
while (userChoice.equals("")) {
userChoice = userInputChecker(scanner);
}
String computerChoice = choices[random.nextInt(choices.length)];
System.out.println("Computer chooses: " + computerChoice);
int[] scores = gameLogic(computerChoice, userChoice, userScoreTotal, computerScoreTotal);
i = scores[0];
userScoreTotal = scores[1];
computerScoreTotal = scores[2];
if (i == 0) {
System.out.println("Scores for this match are as follows:");
System.out.println(playerName + "'s score: " + userScoreTotal);
System.out.println("Computer's score: " + computerScoreTotal);
System.out.println("Thank you for playing the game.");
System.out.println("Have a nice day!");
} else if (i != 0 && i != 1) {
System.out.println("Invalid Input!");
System.out.print("Please enter 1 to continue or 0 to leave the game: ");
i = scanner.nextInt();
}
}
}
public static String userInputChecker(Scanner scanner) {
System.out.print("Enter your choice: ");
String userChoice = scanner.nextLine();
if (userChoice.equals("r") || userChoice.equals("p") || userChoice.equals("s")) {
return userChoice;
} else {
System.out.println("Wrong Input!!");
return "";
}
}
public static int[] gameLogic(String computerChoice, String userChoice, int userScore, int computerScore) {
int[] scores = new int[3];
if (computerChoice.equals("rock") && userChoice.equals("p")) {
System.out.println("Player Wins");
System.out.println("Enter 1 to continue and 0 to leave the game");
userScore += 1;
scores[0] = scanner.nextInt();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
} else if (computerChoice.equals("rock") && userChoice.equals("s")) {
System.out.println("Computer Wins");
System.out.println("Enter 1 to continue and 0 to leave the game");
computerScore += 1;
scores[0] = scanner.nextInt();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
} else if (computerChoice.equals("paper") && userChoice.equals("r")) {
System.out.println("Computer Wins");
System.out.println("Enter 1 to continue and 0 to leave the game");
computerScore += 1;
scores[0] = scanner.nextInt();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
} else if (computerChoice.equals("paper") && userChoice.equals("s")) {
System.out.println("Player Wins");
System.out.println("Enter 1 to continue and 0 to leave the game");
userScore += 1;
scores[0] = scanner.nextInt();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
} else if (computerChoice.equals("scissors") && userChoice.equals("r")) {
System.out.println("Player Wins");
System.out.println("Enter 1 to continue and 0 to leave the game");
userScore += 1;
scores[0] = scanner.nextInt();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
} else if (computerChoice.equals("scissors") && userChoice.equals("p")) {
System.out.println("Computer Wins");
System.out.println("Enter 1 to continue and 0 to leave the game");
computerScore += 1;
scores[0] = scanner.nextInt();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
} else if (computerChoice.equals(userChoice)) {
System.out.println("Draw");
System.out.println("Enter 1 to continue and 0 to leave the game");
userScore += 1;
computerScore += 1;
scores[0] = scanner.nextInt();
scores[1] = userScore;
scores[2] = computerScore;
return scores;
}
return scores;
}
}