-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRps_game.java
56 lines (45 loc) · 2.04 KB
/
Rps_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
package mini_project;
import java.util.Random;
import java.util.Scanner;
public class Rps_game {
public static void main(String[] args) {
int userScore = 0;
int computerScore = 0;
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("Let's play Rock Paper Scissors!");
while (true) {
//user's choice
System.out.print("Enter your choice (rock, paper, scissors): ");
String userChoice = scanner.nextLine().toLowerCase();
// computer's choice
String[] options = {"rock", "paper", "scissors"};
int index = random.nextInt(options.length);
String computerChoice = options[index];
System.out.println("Computer chose " + computerChoice + ".");
// Compare user's choice with computer's choice
if (userChoice.equals(computerChoice)) {
System.out.println("It's a tie!");
} else if (userChoice.equals("rock") && computerChoice.equals("scissors") ||
userChoice.equals("paper") && computerChoice.equals("rock") ||
userChoice.equals("scissors") && computerChoice.equals("paper")) {
System.out.println("You win!");
userScore++;
} else {
System.out.println("Computer wins!");
computerScore++;
}
// Show current score
System.out.println("Current score: You : " + userScore + ", Computer : " + computerScore);
// Ask if user wants to play again
System.out.print("Do you want to play again? (yes or no): ");
String playAgain = scanner.nextLine().toLowerCase();
if (!playAgain.equals("yes")) {
break;
}
}
// Show final score
System.out.println("Final score: You : " + userScore + ", Computer : " + computerScore);
scanner.close();
}
}