-
Notifications
You must be signed in to change notification settings - Fork 187
/
snake water gun game in java
53 lines (45 loc) · 1.73 KB
/
snake water gun game in 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
import java.util.Random;
import java.util.Scanner;
public class SnakeWaterGunGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("Welcome to the Snake-Water-Gun Game!");
System.out.println("Choose one of the following:");
System.out.println("1. Snake");
System.out.println("2. Water");
System.out.println("3. Gun");
System.out.print("Enter your choice (1/2/3): ");
int playerChoice = scanner.nextInt();
String playerChoiceName = getChoiceName(playerChoice);
int computerChoice = random.nextInt(3) + 1;
String computerChoiceName = getChoiceName(computerChoice);
System.out.println("Your choice: " + playerChoiceName);
System.out.println("Computer's choice: " + computerChoiceName);
String result = determineWinner(playerChoice, computerChoice);
System.out.println(result);
}
public static String getChoiceName(int choice) {
switch (choice) {
case 1:
return "Snake";
case 2:
return "Water";
case 3:
return "Gun";
default:
return "Invalid choice";
}
}
public static String determineWinner(int playerChoice, int computerChoice) {
if (playerChoice == computerChoice) {
return "It's a draw!";
} else if ((playerChoice == 1 && computerChoice == 2) ||
(playerChoice == 2 && computerChoice == 3) ||
(playerChoice == 3 && computerChoice == 1)) {
return "You win!";
} else {
return "Computer wins!";
}
}
}