Skip to content

Build a simplified, one-player version of the classic board game Battleship with Python 2. There will be a single ship hidden in a random location on a 5x5 grid. The player will have 4 guesses to try to sink the ship. Codecademy practice project.

Notifications You must be signed in to change notification settings

candytale55/Battleship_Py_2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 

Repository files navigation

Battleship_Py_2

Build a simplified, one-player version of the classic board game Battleship with Python 2. There will be a single ship hidden in a random location on a 5x5 grid. The player will have 10 guesses to try to sink the ship. Codecademy practice project.

General info

The purpose of the project is just personal learning.

Description

Creating the board:

  • A variable board is set it equal to an empty list. This will represent the board. "O" will mean "Ocean", "X" will mean missed shot.

  • Function create_board takes one int parameter board_size. Using the built-in range() Python function, looping the number of times indicated by board_size will generate the board, a grid of all "O"s, for “ocean.”. Right now it will have an equal size of rows and columns.

  • Function print_board takes a single argument, board_in. It prints the board out like a grid with each row on its own line using a for loop to iterates through each row in board and print it to the screen. To get rid of the quote marks and commas part of the list structure we use the .join method with a " " string.

Hiding the battleship:

  • Since we have a 2-dimensional list, we’ll use two variables to store the ship’s location, ship_row and ship_col. We'll use randint so that the location is random in two functions random_col and random_row that will return a random index.
def random_col(board_in):   
  return randint(0, len(board_in) - 1) 

Note that we could just call randint(0, 4), but we use len(board) - 1 in case we want to change the board size later.

Playing the game:

  • Function play_battleship() plays a game of battleship. The user has 4 chances to "sink the battleship". At the beginning of each iteration, it prints "Turn", turn + 1 to let the player know what turn they are on.

    Seeking the batleship:
    • Using raw_input asks the user for input and returns it as a string, so we wrap the raw_inputs with int() to convert them to int, to get the player’s guess. The zero-based indexed list may confuse the user, who may be expecting the coordinates to start at 1, so to the int number we substract 1.
    Determine a win
    • We have the actual location of the ship and the player’s guess so we can check to see if the player guessed right. The function checks if guess_row equals ship_row and guess_col equals ship_col. If that is the case, prints out a note to the user stating he won and breaks the loop.
    Handle incorrect guesses:
    • The function will repeat the guessing and checking part of the game for 4 turns. The user can guess the battleship coordinates and win, or may miss the spot the user guessed will be changed to "X" on the board.
    • There are two other incorrect guesses that will be treated differently:
      • If they enter a guess that’s off the board - meaning it exceeds the size of the board or is a negative number (not in range(board_size)) the user will get a specific message.
      • If they enter a guess for a spot they’ve already guessed (handled by an elif that checkes if guessed location already has an ‘X’), they will get a specific message.
    Game Over:
    • If the user guesses wrong on their last turn it prints "Game Over". It must print no matter what the cause of the misses. So, under the else that accounts for misses and after the if/elif/else statements that check for the reason the player missed we set an if statement. Our turn variable starts at 0 and goes to 3, we will want to end the game when turn equals 3 (4 turns).

To-do list:

  • Make custom-boards with different number of rows vs columns.
  • Make a two-player game.
  • Make multiple battleships: Make sure that you don’t place battleships on top of each other on the game board and make to balance the size of the board with the number of ships so the game is still challenging and fun to play.
  • Make battleships of different sizes: this is trickier than it sounds. All the parts of the battleship need to be vertically or horizontally touching and you’ll need to make sure you don’t accidentally place part of a ship off the side of the board.

Table of contents

Screenshots

Example screenshot

Technologies

  • Python 2

Setup

NA - It's only coding examples, there's no setup.

Status

Project is: in progress, finished, - may come back and improve it, or not.

References

Based on Codecademy's Learn Python 2 course, Battleship!

Relevant discussion links

About

Build a simplified, one-player version of the classic board game Battleship with Python 2. There will be a single ship hidden in a random location on a 5x5 grid. The player will have 4 guesses to try to sink the ship. Codecademy practice project.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages