View the deployed version of the Battleships application here.
This game is a Python implementation of the classic board game Battleships. The game involves two players, each of whom hides a number of ships on a grid. The other player then attempts to guess the locations of the ships by calling out coordinates on the grid. The game continues until all of one player's ships have been sunk.
- Clone the repository: git clone
https://github.com/Sirak-K/my-project-3.git
- Navigate to the
my-project-3
directory: cdmy-project-3
- Run the game:
python run.py
The objective of a Battleships game is to sink all of your opponent's ships before they sink yours.
The game will prompt you to enter the board size at the start of each game. Choose from small (s
), medium (m
), or large (l
) board sizes.
- Each player gets their ships placed on their own grid without the other player seeing.
- Ships can be placed horizontally or vertically.
- Players take turns guessing the location of the other player's ships by calling out a coordinate on the grid.
- If the guessed coordinate contains a ship, it is a hit. If not, it is a miss.
- If a ship is hit, the attacking player gets to take another turn.
- When a ship is hit in all of its squares, it is sunk.
- The game continues until all of one player's ships have been sunk.
- Use the keyboard to input your guesses.
- Row numbers range from 1 to the length of the board (e.g. 1-8 for a large board).
- Column letters range from A to the last letter of the alphabet corresponding to the length of the board e.g. A-H for a large board and A-D for a small board.
- Visual feedback system to keep track of gameplay information e.g turns left.
- Option to instantly restart the application when a game has ended.
This game has been thoroughly tested using the unittest
framework. The testing code can be found in the test_battleships.py
file and cover various aspects of the game's functionality.
To run the tests, follow these steps:
-
Make sure you have
unittest
installed. If not, you can install it using the following command:pip install unittest
-
Navigate to the project directory where the
test_battleships.py
file is located. -
Run the tests using the following command:
python -m unittest test_battleships.py
If all tests are successful, it will display "OK".
The testing code includes the following test cases:
- TEST-1: Verifies the
show_board
function does not raise any unexpected exceptions. - TEST-2: Checks if the
create_ship
function places 5 ships on the board. - TEST-3: Validates that the
get_board_coordinates
function returns correct coordinates for valid input. - TEST-4: Tests if the
count_hits
function returns 0 for an empty board. - TEST-5: Ensures the
create_ship
function places 5 ships on a small board. - TEST-6: Confirms the
reset_game_state
function initializes the small board state correctly. - TEST-7: Validates the
reset_game_state
function initializes the medium board state correctly. - TEST-8: Verifies the
reset_game_state
function initializes the large board state correctly. - TEST-9: Checks if the
BOARD_SIZES_DICT
has the correct keys. - TEST-10: Asserts the
reset_game_state
function raises aValueError
for an invalid board size.
It is recommended to run the tests after any modifications to the codebase to ensure that all the functionality is working correctly.
Here's a table for the expected outcomes of the 10 test suites:
Test Suite | Description | Expected Outcome |
---|---|---|
TEST-1 | Verifies the show_board function does not raise any unexpected exceptions. |
The show_board function should execute without raising any exceptions. |
TEST-2 | Checks if the create_ship function places 5 ships on the board. |
The create_ship function should place exactly 5 ships on the game board. |
TEST-3 | Validates that the get_board_coordinates function returns correct coordinates for valid input. |
The get_board_coordinates function should return the correct coordinates. |
TEST-4 | Tests if the count_hits function returns 0 for an empty board. |
The count_hits function should return 0 when called with an empty board. |
TEST-5 | Ensures the create_ship function places 5 ships on a small board. |
The create_ship function should place 5 ships on the small game board. |
TEST-6 | Confirms the reset_game_state function initializes the small board state correctly. |
The reset_game_state function should correctly initialize the small board. |
TEST-7 | Validates the reset_game_state function initializes the medium board state correctly. |
The reset_game_state function should correctly initialize the medium board. |
TEST-8 | Verifies the reset_game_state function initializes the large board state correctly. |
The reset_game_state function should correctly initialize the large board. |
TEST-9 | Checks if the BOARD_SIZES_DICT has the correct keys. |
The BOARD_SIZES_DICT should contain the keys "s", "m", and "l". |
TEST-10 | Asserts the reset_game_state function raises a ValueError for an invalid board size. |
The reset_game_state function should raise a ValueError for invalid size. |
Tool used: PEP-8 Linting Validator
Here are the steps to deploy the game on Heroku:
1. Create a Heroku account: If you don't have a Heroku account, go to Heroku and sign up for an account.
2. Install the Heroku CLI: Download and install the Heroku CLI appropriate for your operating system.
3. Log in to the Heroku CLI: Open a terminal or command prompt and log in to the Heroku CLI using the following command:
heroku login
4. Create a new Heroku app: Navigate to the project directory and create a new Heroku app using the following command:
heroku create <app-name>
- Replace
<app-name>
with the name for your Heroku app.
5. Deploy the app: Push the code to the Heroku remote repository to deploy the app using the following command:
git push heroku main
The Battleships game has now been successfully deployed to Heroku.
- Inspired by Knowledge Mavens https://github.com/gbrough/battleship/blob/main/single_player.py