Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
@@ -1,12 +1,93 @@
|
|
1 |
---
|
2 |
-
title: Battleship
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.14.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Battleship Game
|
3 |
+
emoji: 🚢
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.14.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
short_description: Play a simple Battleship game against an AI in a 5x5 grid.
|
11 |
---
|
12 |
|
13 |
+
# Battleship Game
|
14 |
+
|
15 |
+
A simple, interactive Battleship game built with Gradio, Matplotlib, and NumPy.
|
16 |
+
The game sets up two 5x5 grids: one for the user and one for an AI. Each grid has three single-cell ships. Enter row and column values to fire at the AI’s ships.
|
17 |
+
|
18 |
+
## Features
|
19 |
+
|
20 |
+
- **Random Ship Placement**: Both player and AI boards are randomized at the start.
|
21 |
+
- **Simple Firing Mechanics**: Input row and column to fire on the AI’s board.
|
22 |
+
- **AI Guessing**: AI randomly selects a cell on the user’s board to fire back.
|
23 |
+
- **Instant Feedback**: Real-time messages show hits, misses, and status updates.
|
24 |
+
- **Visual Boards**: Two Matplotlib-based grids visually render hits, misses, and your own ships.
|
25 |
+
|
26 |
+
## Getting Started
|
27 |
+
|
28 |
+
### 1. Install Dependencies
|
29 |
+
Make sure you have [Python 3.7+](https://www.python.org/downloads/) installed.
|
30 |
+
Install required libraries via:
|
31 |
+
|
32 |
+
```bash
|
33 |
+
pip install -r requirements.txt
|
34 |
+
```
|
35 |
+
|
36 |
+
### 2. Run the App
|
37 |
+
|
38 |
+
To run the app, use the following command:
|
39 |
+
|
40 |
+
```bash
|
41 |
+
python app.py
|
42 |
+
```
|
43 |
+
|
44 |
+
### 3. Open in Browser
|
45 |
+
Once the server starts, Gradio will print a local URL (e.g., http://127.0.0.1:7860).
|
46 |
+
Open it to start playing the game.
|
47 |
+
|
48 |
+
|
49 |
+
## How to Play
|
50 |
+
|
51 |
+
1. **Initial Load**
|
52 |
+
- You’ll see two boards: “Your Board” (with your ships) and “AI Board” (ships hidden).
|
53 |
+
- A “Color Key” plot also displays how ships, hits, and misses are represented.
|
54 |
+
|
55 |
+
2. **Firing**
|
56 |
+
- Enter a row (0–4) and column (0–4) in the input boxes.
|
57 |
+
- Press **Fire!** to attempt hitting an AI ship.
|
58 |
+
|
59 |
+
3. **Receiving Feedback**
|
60 |
+
- A **Game Messages** box updates each time you fire. It will show whether you hit or missed.
|
61 |
+
- The AI then fires back, and you’ll see if your ship got hit.
|
62 |
+
|
63 |
+
4. **Winning and Losing**
|
64 |
+
- If you sink all three of the AI’s ships, you win!
|
65 |
+
- If the AI sinks all three of your ships, you lose.
|
66 |
+
|
67 |
+
5. **Resetting the Game**
|
68 |
+
- Press the **Reset Game** button to start over with brand new boards.
|
69 |
+
|
70 |
+
## Code Overview
|
71 |
+
|
72 |
+
- **`init_game()`**: Initializes two 5x5 boards for user and AI, randomly placing three ships on each.
|
73 |
+
- **`plot_board(board, is_user_board=True)`**: Generates a Matplotlib plot of a board. It hides AI ships if `is_user_board=False`.
|
74 |
+
- **`plot_color_key()`**: Generates a legend explaining the colors for ships, hits, and misses.
|
75 |
+
- **`ai_guess(state)`**: Makes a random guess on the user’s board, marking hits and misses.
|
76 |
+
- **`make_move(state, row, col)`**: Validates user guess and updates boards accordingly.
|
77 |
+
- **`reset_game()`**: Resets the game state to start fresh.
|
78 |
+
- **`update_display(state)`**: Helper that returns the plots and game message for quick updates.
|
79 |
+
|
80 |
+
### Gradio Interface:
|
81 |
+
- The user can input row and column to fire; the interface displays live board updates.
|
82 |
+
- `demo.load(...)` ensures the boards show as soon as the app loads.
|
83 |
+
- The **Reset Game** button re-initializes the state and re-draws the boards.
|
84 |
+
|
85 |
+
## Requirements
|
86 |
+
|
87 |
+
The following dependencies are required to run the game:
|
88 |
+
|
89 |
+
```plaintext
|
90 |
+
gradio
|
91 |
+
matplotlib
|
92 |
+
numpy
|
93 |
+
```
|