Battleships
Welcome to my Battleships game.
Battleships is a strategy type guessing game whereby two players attempt to destroy the opposing players fleet of ships. They do this by alternating guesses of the coordinates of the concealed ships set out on a grid.
The object of this project was to build a game which could be interactive, where the player is able to play against the computer, obtain feedback on progress throughout the game and understand who has won and lost the game at the end.
This project uses the programming language of Python.
A live website can be found here
Site Design Considerations
User Expectations
- The game should be engaging for the player, providing fun and joy.
- It should define a clear goal for the player.
- It should clearly display a winner and a loser after each turn.
- It should be challenging and give incentive to the player to want to return to play again.
Colour Scheme and Font
To provide an improved user experience and a point of difference I decided to implement a colour scheme to the game. I hoped that this would provide a slightly more interesting visual experience for the player and also give a better definition to the functions when there are run.
- Player input requests and messages in Purple colour (\033[1;35;10m)
- Win messages in Cyan colour (\033[1;36;10m)
- Lose messages in Red colour(\033[1;31;10m)
Site Skeleton
Lucidchart was used to help create a flow chart diagram for the game. It was useful as it enabled me to clearly play out all the logic and possible scenarios to plan for the game
Features
Player welcome message and game instructions
Player board
Computer Board
Player sunk ship message
Computer sunk ship message
It's a draw message
Player winner message
Play again message
Technologies Used
During the making of this game I used the following:
-
Was the language used to create the game
-
For storing project code written in gitpod
-
For use as the code editor for the project code
-
For creating flow chart for game planning
-
To test code for errors or problems
-
For checking python code best practices and validation
-
To deploy project into live environment
Testing
Code was first tested using the PEP8 Online validator. This threw up some errors with the length of certain lines within the code being too long as detailed below:
After this was corrected no further issues were found in the code as detailed in the screenshot below:
For player name input, fields were tested using numbers, special characters and blank inputs.
For letter inputs, fields were tested using letters outside of the range, special characters, blank and number inputs.
For number inputs, fields were tested using numbers outside of the range, special characters, blank and letter inputs.
For same guess inputs, fields were tested using a guess which had already been inputed.
For yes or no play again inputs, fields were tested using letters and numbers outside of the range, blanks and special characters inputs.
Developement
I played the game incrementally throughout development to test the features and ensure that they responded how I expected. If a feature was not responding how I expected it to then I would go back and check the code to resolve the issue.
Bugs
-
When testing the game I noticed that the computer guess was mirroring the player input guess therefore not drawing a guess randomly. This was caused by the code checking whether the computer has hit or miss having the incorrect variables passed in for the array indices. It should have been computer row guess, rather than player row guess. This was corrected and code worked as expected.
-
During coding, an issue arose with the line length on certain lines. Upon checking the PEP8 guidelines, I discovered the 80 character advisory limit. Where possible, print statement strings were split with the use of double quotation marks to denote the continuation of the line. Backslashes were not used in string statements as this resulted in spacing errors on the displayed result.
-
Upon adding colours I noticed that the background colour was displaying as a dark grey and not black as intended. This was resolved by changing the background colour from 40m to 10m.
-
When testing the game I found that the "Play Again" function was not working properly, the game would not let you exit, instead just looping back round to begin the game again. This was corrected by breaking the while loop by adding a new global variant and setting this to true. Code then worked as expected.
Deployment
To deploy my final project to the cloud I used Heroku. To do this I had to:
- Push the latest code to GitHub.
- Go to Heroku.
- Select new in the top right corner.
- Create new app.
- Enter the app name and select Europe as the region.
- Connect to GitHub.
- Search for repo-name.
- Select connect to the relevant repo you want to deploy.
- Select the settings tab.
- Go to Config Vars and add PORT 8000.
- Add buildpack.
- Select Python, then save changes.
- Select Nodejs, then save changes.
- Make sure that they are in this order, Heroku/Python is at top followed by Heroku/Nodejs.
- Navigate to the deploy tab.
- Scroll down to Automatic Deploys and select.
- Go to Manual Deploy and select deploy branch.
The live link can be found here - (https://battleships-saramorrin.herokuapp.com/)
Credits
-
An article on Stack Abuse helped me to understand how to implement colours into my code.
-
This article on Real Python helped my understanding of how to write Python code following best practices and guidelines.
-
I used Stack Overflow to further my knowledge of classes and functions from previous questions asked by the community.