-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.java
71 lines (60 loc) · 2.41 KB
/
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
import java.util.Scanner;
import java.util.Random;
public class game {
public static void main(String[] args) {
playGame();
}
public static void playGame() {
System.out.println("Let's play Rock-Paper-Scissors against the computer!");
Scanner scanner = new Scanner(System.in);
Random random = new Random();
while (true) {
String userChoice = getUserChoice(scanner);
String computerChoice = getComputerChoice(random);
System.out.println("\nYou chose " + userChoice + ".");
System.out.println("Computer chose " + computerChoice + ".\n");
String result = determineWinner(userChoice, computerChoice);
System.out.println(result);
System.out.print("Do you want to play again? (yes/no): ");
String playAgain = scanner.nextLine().toLowerCase();
if (!playAgain.equals("yes")) {
System.out.println("Thank you for playing! Have a great day!");
break;
}
}
scanner.close();
}
public static String getUserChoice(Scanner scanner) {
String userChoice;
while (true) {
System.out.print("Enter your choice (rock, paper, or scissors): ");
userChoice = scanner.nextLine().toLowerCase();
if (userChoice.equals("rock") || userChoice.equals("paper") || userChoice.equals("scissors")) {
return userChoice;
}
System.out.println("Invalid choice. Please try again.");
}
}
public static String getComputerChoice(Random random) {
int randomChoice = random.nextInt(3);
switch (randomChoice) {
case 0:
return "rock";
case 1:
return "paper";
default:
return "scissors";
}
}
public static String determineWinner(String userChoice, String computerChoice) {
if (userChoice.equals(computerChoice)) {
return "It's a tie!";
} else if ((userChoice.equals("rock") && computerChoice.equals("scissors")) ||
(userChoice.equals("paper") && computerChoice.equals("rock")) ||
(userChoice.equals("scissors") && computerChoice.equals("paper"))) {
return "Congratulations! You win!";
} else {
return "Computer wins. Better luck next time!";
}
}
}