Giter VIP home page Giter VIP logo

you-sunk-my-battleship's Introduction

Welcome to You Sunk My Battleship

You Sunk My Battleshp is a tribute to the popular traditional game of Battleships, the ultimate in simple strategy gaming.

Battleships can be played with just a paper and pen but it has grown into much more since it's inception during World War I. Now expanded to board games, electronic games and even a film, it is an enduring classic. This version offers basic game play in which the player must try and "sink" the ships on the board by guessing their position with a limited number of turns.

This game is a demonstration of my Python skills as required by Portfolio 3 Assessment as part of the Code Institute Diploma in Full Stack Software Development.


The deployed game can be found here.


screenshot of the game running as a Heroku app


How to play

Battleships are placed on a board and hidden from the player. They must guess where on the board they think the battelship is placed. First, typing a number corresponding to the x axis then hitting enter on their keyboard. Then followed by a letter corresponding to the y axis and the enter key. If the guess is correct, they have hit and sunk a battleship!

Players are limited to 8 torpedoes (or turns) in which to try and destroy all the battleships and win the game.

If they are successful, they will be rewarded with an ASCII display. If they run out of torpedoes, they will be prompted to try again in a new game.


Wireframing & Planning

Before development began, online chart builder Lucid was used to map out the game and how best to approach it. It helped to work through the steps needed to make the game make sense to the user and what tasks the code should execute.


battleships game play plan using lucid chart


Features

Existing

  • Generates board
  • Places battleships in random positions on board
  • Keeps score
  • Limits player turns
  • Offers rematch

Future

  • Boat number/board size choice
  • Two player mode
  • Custom player naming

Data Model

For this game, two classes were used; one for the board and one for the ships. Using two classes for main elements of the game could help additional features being added later without disrupting the way the game currently runs.


BattleshipBoard

This class is used to generate the board during the gameplay. As the user adds their guesses to the board it's used up to 8 times in one game.


Battleships

This class handles the position of the ships on the board and the users entries including validation and checking if the turn was a "hit". Similar to the BattleshipBoard class, Battleship is used at least 8 times, more if the user repeatedly enters an incorrect value when guessing.


Bugs

Fixed

  • Formatting: Although all the characters were working, the layout of the board was broken when using an f-string to format it. Using formatting placeholders may not actually be the optimal approach but were the best resolution at the time of submission. This should be reviewed.
  • Run game: After putting together the elements of the game, it would not run. Definitions and functions were checked by printing to the terminal but did not uncover any issues. Reviewing this solution demonstrated that the main function to run the script was missing.
  • Line 118: The original approach compared the two possibilities of a placement on the board being occupied by a hit (X) or a miss (-) within a while loop. This caused a line length error so variables were added to declare what a hit or miss is. Unfortunately this broke the loop and the user was caught in the loop, even if their guess was valid. The original code was put back in place and then broken up using parenthesis to address the line length error, this should be reviwed in the next iteration of this game.

Unfixed

  • Rematch: The game offers the user a rematch; the first time the user is asked, if they type "yes" they are brought back into the game through the rematch code. The second time they are asked they are brought back in through the initial game play. Attempts to fix this resulted in broken loops. The decision was made to use the initial buggy code as the user isn't impacted. However, it could effect future iterations that include new features so it should be reviewed.

Testing

This project was validated using PEP8 for Python 3. PEP8 is the industry standard that provides guidelines for styling Python code. It helps to keep programmes written in Python legible and maintained.

It should be noted that as the ships on the boards are randomly generated, it is difficult to test real game play of a winning game, especially with real users. To get around this, this section was tested by using print statements to the terminal.

  • Gitpod: Using Gitpod provided additional support when building this game. It flags errors and potential areas to improve as the code is written. This was particularly useful for ensuring indentation and line length were to PEP8 standard as the overall project developed.
  • Online PEP8 checkers: In addition to Gitpod, several online tools such as Extends Class were used to back up Gitpod validation during development. However, some of them were checking against older Python versions. This initially caused some confusion as they flagged code that was currently correct, such as the use of f-strings. Once the error was realised, the project was rechecked using an appropriate version and found no significant errors.
  • UAT: The game was tested in person with two users using a different device for each user. Without access to Apple devices, both used devices running Windows 10 using the Gitpod terminal. Users were encouraged to enter incorrect data. There were no issues found with the game play or how it was executed at this point.
  • Code Institute Python Linter: The Code Institute Python Linter was used as the final phase of testing before deployment. It showed some line lengths that could be improved and new variables or split lines were used to shorten them.
  • After deployment: Final testing was carried out after deployment both using the CI Python Linter and with a user. Although no errors were found with the code, the user uncovered that when they did not type "yes" or "no" successfully when asked for a rematch, it exited anyway. This was due to it being set up as "yes" or everything else with an if/else statement. By adding an elif for "no", this issue was resolved.

Deployment

This project was deployed using Heroku Cloud Platform. It uses the Code Institute Python template repo to assist with successful deployment, as per project recommendations. This included tasks that are unique to this template that prepared the project for deployment to Heroku, such as adding \n to the end of input strings (please note that not all were required and were removed during after deployment testing).

To replicate the deployment, please follow the below steps.

Steps for Deployment

  • Log in to yoour Heroku account
  • In the top right corner, click "New" then "New App"
  • Name the app with a unique name
  • Ensure the location is set to the relevant one
  • Click create
  • Go to settings
  • Add relevant ConfigVars and Buildpacks; for this app it includes PORT: 8000 and python & node.js
  • Go to deploy and choose a deployment method; for this app it is GitHub
  • When using GitHub, type the name in the text field to find the repo
  • Check the branch is correct
  • Click connect
  • App is deployed when the "View app" button appears

Credits

Background Support & Referencing:


Direct Sourcing:

  • Python ASCII art using Pyfiglet Module by Ankthon through Geek for Geeks - This is a straight forward method of inserting ASCII art into the project and there didn't appear to be justification for changing or complicating it.
  • Python Battleship with Object Oriented Programming by Knowledge Mavens through Youtube - This tutorial helped resolve two bugs referenced in the Bugs section of this document; the f-string board bug and the gameplay bug
  • Exit a Python Programm in 3 Easy Ways by AskPython.com - a break was used to stop game play after offering a rematch but this didn't acknowledge the user saying no to the new game. In looking for an alternative method to employ, this resource was discovered. The "quit" function was used as it is built in and doesn't require importing anything else to the game (compared to sys.exit for example). It is my understanding that this should only be used in scenarios such as student projects, where the code is not part of a larger, commercial project.

you-sunk-my-battleship's People

Contributors

somekindofdesign avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.