Skip to content

Commit b409ff7

Browse files
committed
water_gun_game
1 parent 3213b1a commit b409ff7

File tree

14 files changed

+113
-0
lines changed

14 files changed

+113
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
17 Bytes
Binary file not shown.
Binary file not shown.
1 Byte
Binary file not shown.
17 Bytes
Binary file not shown.

app/.gradle/7.4.2/gc.properties

Whitespace-only changes.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#Tue Oct 17 20:55:57 IST 2023
2+
gradle.version=7.4.2

app/.gradle/vcs-1/gc.properties

Whitespace-only changes.

water_gun_game/readme.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Water Gun Game in Java
2+
3+
A simple text-based two-player water gun game implemented in Java.
4+
5+
## Game Description
6+
7+
In this game, two players take turns choosing actions: filling their water guns or shooting each other. The game continues until one of the players runs out of water.
8+
9+
## Features
10+
11+
- Two-player game with basic actions: Fill water gun and Shoot.
12+
- Random water shot amounts between 0 and 20.
13+
14+
## Getting Started
15+
16+
1. Clone or download the project to your local machine.
17+
18+
2. Compile and run the game using a Java development environment or command line.
19+
20+
3. Follow the on-screen instructions to play the game.
21+
22+
4. Players can choose to "Fill water gun" (action 1) to restore their water or "Shoot" (action 2) to shoot the other player with a random water amount.
23+
24+
## Sample Gameplay
25+
26+
```plaintext
27+
Welcome to the Water Gun Game!
28+
Enter Player 1's name: Alice
29+
Enter Player 2's name: Bob
30+
31+
Alice's water: 100
32+
Bob's water: 100
33+
Alice, choose an action (1: Fill water gun, 2: Shoot): 2
34+
Alice shot Bob with 9 water!
35+
36+
Alice's water: 100
37+
Bob's water: 91
38+
Bob, choose an action (1: Fill water gun, 2: Shoot): 1
39+
Bob filled their water gun.
40+
41+
Alice's water: 100
42+
Bob's water: 100
43+
Alice, choose an action (1: Fill water gun, 2: Shoot): 2
44+
Alice shot Bob with 15 water!
45+
46+
...
47+
48+
Bob is out of water. Alice wins!

water_gun_game/water.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.Scanner;
2+
import java.util.Random;
3+
4+
public class WaterGunGame {
5+
public static void main(String[] args) {
6+
Scanner scanner = new Scanner(System.in);
7+
Random random = new Random();
8+
9+
System.out.println("Welcome to the Water Gun Game!");
10+
System.out.print("Enter Player 1's name: ");
11+
String player1Name = scanner.nextLine();
12+
System.out.print("Enter Player 2's name: ");
13+
String player2Name = scanner.nextLine();
14+
15+
int player1Water = 100;
16+
int player2Water = 100;
17+
boolean isPlayer1Turn = true;
18+
19+
while (player1Water > 0 && player2Water > 0) {
20+
System.out.println("\n" + player1Name + "'s water: " + player1Water);
21+
System.out.println(player2Name + "'s water: " + player2Water);
22+
23+
if (isPlayer1Turn) {
24+
System.out.print(player1Name + ", choose an action (1: Fill water gun, 2: Shoot): ");
25+
} else {
26+
System.out.print(player2Name + ", choose an action (1: Fill water gun, 2: Shoot): ");
27+
}
28+
29+
int action = scanner.nextInt();
30+
31+
if (action == 1) {
32+
if (isPlayer1Turn) {
33+
player1Water = 100;
34+
System.out.println(player1Name + " filled their water gun.");
35+
} else {
36+
player2Water = 100;
37+
System.out.println(player2Name + " filled their water gun.");
38+
}
39+
} else if (action == 2) {
40+
int shotAmount = random.nextInt(21); // Randomly shoot between 0 and 20
41+
if (isPlayer1Turn) {
42+
player2Water -= shotAmount;
43+
System.out.println(player1Name + " shot " + player2Name + " with " + shotAmount + " water!");
44+
} else {
45+
player1Water -= shotAmount;
46+
System.out.println(player2Name + " shot " + player1Name + " with " + shotAmount + " water!");
47+
}
48+
} else {
49+
System.out.println("Invalid action. Choose 1 to fill or 2 to shoot.");
50+
}
51+
52+
isPlayer1Turn = !isPlayer1Turn;
53+
}
54+
55+
if (player1Water <= 0) {
56+
System.out.println(player1Name + " is out of water. " + player2Name + " wins!");
57+
} else {
58+
System.out.println(player2Name + " is out of water. " + player1Name + " wins!");
59+
}
60+
61+
scanner.close();
62+
}
63+
}

0 commit comments

Comments
 (0)