BattleShip Part 1: Initial Review / Functional Requirements

Some initial observations.

It is on Github which is nice and there’s a README that explains some basics. Probably don’t need to mention all the internal variables of BS in the README. BS is also not a great name it’s too non-descriptive.

Before digging into the code, it’s important to understand how the project is supposed to work (Functional Requirements). I am familiar with the battleship game.

Try running python and creating the game object. Not clear what the initial output means.

>>> import BattleShipGame
>>> game = BattleShipGame.BS()
B 2 5 right 3

The grid is a little hard to make out.

>>> game.printGrid(True)
-ABCDEFGHIJ
0OOOOOOOOOO
1OOOOOOOOOO
2OOOOOOOOOO
3OOOOOOOOOO
4OOOOOOOOOO
5OOOOOOOOOO
6OOOOOOOOOO
7OOOOOOOOOO
8OOOOOOOOOO
9OOOOOOOOOO

It’s not very easy to actually use the game as-is, but luckily there is some sample code for how to play the game. This should ideally just be part of the project so people can play the game directly. Anyway, we’ll copy that and create a main.py file.

from os import system, name                                                                                                                                                                                 
                                                                                                                                                                                                            
from BattleShipGame import BS                                                                                                                                                                               
                                                                                                                                                                                                            
if __name__ == '__main__':                                                                                                                                                                                  
    def clear():                                                                                                                                                                                            
       # for windows                                                                                                                                                                                        
       if name == 'nt':                                                                                                                                                                                     
          _ = system('cls')                                                                                                                                                                                 
                                                                                                                                                                                                            
       # for mac and linux                                                                                                                                                                                  
       else:                                                                                                                                                                                                
          _ = system('clear')                                                                                                                                                                               
                                                                                                                                                                                                            
    game = BS()                                                                                                                                                                                             
    game.printGrid()                                                                                                                                                                                        
    while game.gameOver == False:                                                                                                                                                                           
        pos = input("Input Position (eg: A0): ")                                                                                                                                                            
        if pos.lower() == "show":                                                                                                                                                                           
            clear()                                                                                                                                                                                         
            print("Ships Left:", game.shipCount())                                                                                                                                                          
            game.printGrid(False)                                                                                                                                                                           
        else:                                                                                                                                                                                               
                                                                                                                                                                                                            
                clear()                                                                                                                                                                                     
                print([pos[0].capitalize(),pos[1]])                                                                                                                                                         
                if game.attackShip([pos[0].capitalize(),pos[1]]):                                                                                                                                           
                    print("HIT!")                                                                                                                                                                           
                else:                                                                                                                                                                                       
                    print("Miss!")                                                                                                                                                                          
                print("Ships Left:", game.shipCount())                                                                                                                                                      
                game.printGrid()                                                                                                                                                                            
                                                                                                                                                                                                            
                                                                                                                                                                                                            
    print("Game Over")  

The game seems to work now. One observation is it will crash if you enter an empty string or are missing a number. Ideally, the game should handle this gracefully.

Traceback (most recent call last):
  File "main.py", line 26, in <module>
    print([pos[0].capitalize(),pos[1]])
IndexError: string index out of range

I’m still not 100% sure this is all working correctly as I don’t want to manually go through and enter a bunch of coordinates. I’ll create a helper file that can help me just sanity check that the game at least completes.

if __name__ == '__main__':                                                                                                                                                                                  
    game = BS()                                                                                                                                                                                             
    game.printGrid()                                                                                                                                                                                        
                                                                                                                                                                                                            
    for c in range(65, 75):                                                                                                                                                                                 
        for i in range(0, 10):                                                                                                                                                                              
            pos = [chr(c), str(i)]                                                                                                                                                                          
            print(''.join(pos))                                                                                                                                                                             
            game.attackShip(pos)                                                                                                                                                                            
                                                                                                                                                                                                            
            if (game.gameOver):                                                                                                                                                                             
                break                                                                                                                                                                                       
    print('Game Over') 

This just iterates through all the possible positions and checks if the game is over.

Looks like the game does end successfully. So think we are at a good starting point.

Link to change set.

One response to “BattleShip Part 1: Initial Review / Functional Requirements”

  1. […] BattleShip Part 1: Initial Review / Functional Requirements […]

    Like

Leave a reply to BattleShip Project Review – Level up SE Cancel reply