Giter Club home page Giter Club logo

ships-that-battle's Introduction

Ships that Battle

For my third Portfolio Project submitted as part of the Code Institute's Diploma in full-stack software development course, I created a board game, commonly known as battleship, called ' Ships that Battle'. This is a Python terminal that runs using the Code Institute mock terminal on Heroku.

Responsivity on multiple screens

Summary

This interactive game provides users with an easy way to 'fire cannonballs' at a computer ‘enemy’s fleet of ships’. The game is based on the well-known board game ‘Battleship’, to learn more about this game Click Here.

  1. How to Play
  2. Features
  3. Features Left to Implement
  4. Data Model
  5. Testing
  6. Bugs
  7. Deployment
  8. Credits
  9. Acknowledgements

In this version of the classic Battleship game, the player enters their name and an empty board is displayed. Five randomly located ships are generated which the player cannot see.

The player must guess the coordinates of the hidden ships by choosing a row number and a column letter. The player has ten ‘cannonballs’ or turns to take in order to ‘hit’ the hidden ships.

Hits are indicted by ‘X’ and misses ‘-‘

If the player hits all of the computer’s ships they win the game. If they fire all of their cannonballs and fail to do so, they lose the game.

Existing features

  • Random board generation

    • Ships are randomly placed on the board by the computer so that the player cannot see where they are.

    player board

  • Accepts player’s input.

  • Validates coordinates input by player.

  • Tells player if they input invalid values or the same values more than once.

  • Tells player how many turns or 'cannon balls' they have left.

    input validation

Featuers left to implement

  • HTML and CSS is required to enhance UX
  • An option for the User to decide on the size of the game board and how many ships
  • Functions are used on throughout the code to avoid repetitive code as much as possible.

  • Methods are used throughout the game to print and/or return instructions and responses to the player’s input of name and the coordinates for the player to advance in the game and win or lose.

  • The randint()Method was imported to generate the ships at various locations on the game board.

  • The special built-in Python variable name is used to execute the main module directly.

PEP8 Online was used to validate the Python code to ensure there were no errors present, such as issues with indentation or whitespaces.

PEP8 validation

Two bugs were encountered in developing this project:

  • The computer’s board with the random location of the ships was being printed for the player to see. This defeats the object of the game and had to be fixed.

secret board printed

This was happening for testing purposes to make sure that the player could win and/or lose. This bug was fixed by removing Line 124.

remove secret board

  • Another initial bug was that the board was not printing out correctly.

incorrect board layout

The simple fix for the board bug was proper indentation on Line 62 (board[ship_row][ship_column] = 'X').

‘Bugged’ code:

def random_ship_location(board):
    '''
    Places 5 ships at random on given board
    '''
    for ship in range(5):
        ship_row, ship_column = randint(0, 7), randint(0, 7)
        while board[ship_row][ship_column] == 'X':
            ship_row, ship_column = randint(0, 7), randint(0, 7)
    board[ship_row][ship_column] = 'X'

debugged code:

def initialize_board(board, max_ships):
    """
    Places `max_ships` ships at random on given board.
    """
    for _ in range(max_ships):
        ship_row, ship_column = randint(0, 7), randint(0, 7)
        while board[ship_row][ship_column] == "X":
            ship_row, ship_column = randint(0, 7), randint(0, 7)

        board[ship_row][ship_column] = "X"

Heroku

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

Steps for deployment:

  1. Fork or clone this repository.
  2. Create a new Heroku app.
  3. Set the buildbacks to python and NodeJS in that order.
  4. Link the Heroku app to the repository.
  5. Click on Deploy.

The following online resources were used for a range of supports such as getting correct syntax and learning more about functions and generating random coordinates for the ships:

• Code Institute LMS, in particular the Portfolio Project Scope

W3Schools

Stack Overflow

MDN Web Docs

Geeks for Geeks

Code used to develop the game board was based on battleship code developed by Garrett Broughton:

  def get_letters_to_numbers():
    letters_to_numbers = {"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7}
    return letters_to_numbers

  def print_board(self):
      print("  1 2 3 4 5 6 7 8")
      print("  A B C D E F G H")
      print("  +-+-+-+-+-+-+-+")
      row_number = 1
      for row in self.board:
          print("%d|%s|" % (row_number, "|".join(row)))
          row_number += 1

Click Here to return to Contents

ships-that-battle's People

Contributors

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