This project was made to show my understanding of Python. It is simple command line interface game is a copy of a classic battleships game played against one of the toughest enemies - the computer.
Time frame to finish this project was about 15-20 hours.
Game is divided into 2 sections.
- Placing boats
- Shooting at target
5 ■■■■■, 4 ■■■■, 2 ■■, 1 ■, 1 ■
The user if first asked to place boat of size 5 then 4 and so on. The user has to type in x coordinate and then y coordinate with a comma in between. The example input is shown so the user knows exactly what to do. User can see their board on the left and computers board on the right. Computers boats are hidden but they are there.
After the user places all the boats available, the computer makes its first shot. The shot is marked by x on the game table. User can input where they want to shoot by using x and then y coordinates just like above example is provided. The game will continue asking user for input until someone wins. When user or computer hits a boat a ▢ appears so user knows if a boat was hit.
The game is made up of 4 screens:
1. Welcome screen
2. Game screen
3. Winner screen
4. Loser screen
Welcomes user and lets them starts the game when they are ready
The whole game takes place on this screen. The user places and shoots boats using coordinates.
This screen is shown to users when they win the game.
This screen is shown to users when they lose the game.
Boat size 5 ■■■■■
Input | Expected | Result |
---|---|---|
1,1 | Pass | ✓ |
9,9 | Fail | ✓ |
6,1 | Fail | ✓ |
a,b | Fail | ✓ |
0,1 | Fail | ✓ |
1,0 | Fail | ✓ |
11 | Fail | ✓ |
1,11 | Fail | ✓ |
asd | Fail | ✓ |
Boat size 1 ■
Input | Expected | Result |
---|---|---|
1,1 | Pass | ✓ |
9,9 | Pass | ✓ |
6,1 | Pass | ✓ |
a,b | Fail | ✓ |
0,1 | Fail | ✓ |
1,0 | Fail | ✓ |
11 | Fail | ✓ |
1,11 | Fail | ✓ |
asd | Fail | ✓ |
Attempt to place boat on already taken up square
Boat size | Input | Expected | Result |
---|---|---|---|
5 ■■■■■ | 1,1 | Pass | ✓ |
4 ■■■■ | 1,1 | Fail | ✓ |
4 ■■■■ | 2,1 | Fail | ✓ |
4 ■■■■ | 3,1 | Fail | ✓ |
2 ■■ | 6,1 | Pass | ✓ |
Attempt to place boat outside the grid
Boat size | Input | Expected | Result |
---|---|---|---|
5 ■■■■■ | 5,1 | Pass | ✓ |
5 ■■■■■ | 6,1 | Fail | ✓ |
5 ■■■■■ | 0,1 | Fail | ✓ |
5 ■■■■■ | 5,5 | Fail | ✓ |
4 ■■■■ | 6,1 | Pass | ✓ |
4 ■■■■ | 7,1 | Fail | ✓ |
Input | Expected | Result |
---|---|---|
1,1 | Pass | ✓ |
9,9 | Pass | ✓ |
6,1 | Pass | ✓ |
a,b | Fail | ✓ |
0,1 | Fail | ✓ |
1,0 | Fail | ✓ |
11 | Fail | ✓ |
1,11 | Fail | ✓ |
asd | Fail | ✓ |
Note - Shooting at the same already shoot square is a feature. In real life you can shoot multiple times at the same location so to keep my game realistic I have decided to let the user and the computer shoot at a already shot square if they choose to do so.
I have tested my python code with PEP8 validator to make sure it is up to highest standard and still easily readable. I have used pycodestyle library to help me find all the errors that I may have missed. I have decided to fix them all except a few that that would reduce the readability of the code if fixed. When choosing between readability and PEP8 guidelines I chose readability of the code. Those warnings are shown under the Issues and Fixes heading.
When choosing between readability and PEP8 guidelines I chose readability of the code.
Fallowing warnings appeared:
Same warning with appropriate lines:
# | User Story |
---|---|
1 | As a user I want to be welcomed by a start screen with name of the game. |
2 | As a user I want to see where my boat is placed after I enter coordinates. |
3 | As a user I want to see the table so I can plan placing my boats. |
4 | As a user I want to have more than one boat to place. |
5 | As a user I want to have boats of different lengths to place . |
6 | As a user I want to know why my input wasn't correct. |
7 | As a user I want to see what squares have been shot by computer and me. |
8 | As a user I want to know if I won or lost at the end. |
9 | As a user I want to know if Im placing boat on another boat by accident. |
10 | As a user I want to be warned that my boat is too long to be placed in certain positions. |
# | User Story |
---|---|
1 | As a user I want to hear sound when a boat is destroyed. |
2 | As a user I want to see after how many shots I have won or lost. |
3 | As a user I want to see some color on the terminal. |
4 | As a user I want to see a leader board to see how I did compared to my friends. |
The project is deployed on Heroku. Heroku makes the deployment process super easy.
-
Create Heroku account.
-
Create new project.
-
Go into settings -> Config Var and add the fallowing:
key value PORT 8000 -
Make sure to include fallowing buildpacks:
* Heroku/python * Heroku/nodejs
-
In your project: create Prockfile with type of app (web in this case) and file name of main file to be ran first.
-
In your project: Create requirements.txt file with
pip3 freeze > requirements.txt
-
Deploy the project to GitHub with the fallowing files included.
-
On Heroku for Deployment Method pick Github and find the repo with the project you want to deploy.
-
Pick which branch you want to deploy -- in my case it was main
-
Click deploy and wait until the project is build, watch out for any errors.