Git Product home page Git Product logo

battle-ship-2's Introduction

Battleships

Battleships is a Python terminal game, which runs in the Code Institute mock terminal on Heroku.

Users can try to beat the computer by finding all of the computer's battleships before the computer can find theirs. Each battleship occupies one square on the board.

Click here for the live version of this project

How to play

Battleships is a turn-based naval strategy game based on the classic pen-and-paper game. You read all about its history and evolution over the years on its Wikipedia Page.

In this version of the game, the player is able to create a username and choose a board size between 5 and 8 rows and columns. The boards are then generated, and the ship positions randomly chosen.

The user is able to see their ships on the board, indicated by the @ symbol. The computer's ships are hidden to provide the challenge. Spaces are indicated with a - symbol, incorrect guesses are marked with a o and direct hits will be marked with a x.

The player and the computer will take turns attempting to guess where their opponent's ship is to try and sink all of them. The winner is the first to sink their opponent's five ships.

Features

Implemented Features

  • User defined board size
    • The player chooses between 5 to 8 rows and columns per board
  • Randomly generated ship locations that can change each game

Battle Boards

  • Computer opponent that tries to defeat the user
  • Accepts user input and returns feedback

Input Feedback

  • Maintains score and displays the outcome of the previous guesses

Updated Boards

  • Input validation and error-checking
    • Username cannot be blank or Computer
    • User cannot input coordinates outside of the board size
    • User cannot input characters, symbols or decimal numbers
    • User cannot enter the same coordinates twice

Username Validation

Guess Validation 1

Guess Validation 2

  • Data maintained in class instances(Player and Board)

Future Features

  • Allow the player to choose their ships' position
  • Create ships that vary in size
  • Allow for two users to play the game
  • Change coordinates from 0 indexing for better user experience
  • Allow the user to choose "Computer" as a username

Data Model

For this project I have decided to create a Player class and a Board class as my models.

The game creates two instances of the Player class, one for the user and one for the computer. This class stores the username, score, current guess and the guesses already made.

The Player class has methods such as the new_player method which stores the username chosen. The get_player_answer method which stores the players guess and checks if it's a valid response and the check_answer method that provides feedback to the player based on their guess.

The game then creates two instances of the Board class, one for each player. This class stores the player that owns the board, the board size, the location of the ships and the board's display.

The Board class has methods such as the new_board method which gets the user's desired board size. The generate_ship_location method which creates five random coordinates for the battleships and the print_board method which prints the board and the player's score to the terminal.

Testing

I have manually tested this project by doing the following:

  • Passed the code through a PEP8 linter and confirmed there are no issues
  • Passed invalid inputs: decimal point numbers, chars, symbols and strings where only whole numbers are allowed.
  • Passed inputs that are outside of the board size and input the same coordinates more than once.
  • Passed inputs that are blank or "Computer" as a username.
  • Tested the game in both the Code Institute Heroku terminal and my local Gitpod terminal

Bugs

Solved Bugs

  • The display_result function was returning feedback for a guess that the player had not entered. I fixed this by accessing the correct data structure, as I was originally accessing the ship position array instead of the guesses made array.

  • The check_answer function was creating an infinite guessing loop if the player had already entered the chosen coordinates. I fixed this by adding a return True or return False condition to stop looping if the new coordinates have not been previously guessed.

Remaining Bugs

  • No bugs remaining

Validator Testing

  • PEP8
    • No errors were returned from PEP8online.com

Deployment

This project was deployed using Code Institute's mock terminal for Heroku.

  • Steps for deployment:
    • Fork or clone this repository
    • Create a new Heroku app
    • Set the buildbacks to Python and NodeJS in that order
    • Link the Heroku app to the repository
    • Click on Deploy.

Credits

  • Code Institute for the deployment terminal, a big part of this Read Me file and the code for the create_board function
  • Ascii Art for the Battleship ASCII art
  • Pator JK for the ASCII title
  • Stack Overflow for the countless doubts and questions I had
  • Wikipedia for the rules and history of the Battleships game

battle-ship-2's People

Contributors

afolabib 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.