HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Python Battleship Game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamepythonbattleship

Problem

I have a 2-player game of Battleship written in Python, but I've reused a lot of code for the second player's decisions (particularly in the play_game function). I'm not sure if my question is specific enough for this website, but if so would appreciate some guidance on how this redundancy could be minimised.

```
__author__ = 'admin'
import sys

def initialise_board(): #create a 10x10 board
game_board = []
opponent_board = []
letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
for x in range(len(letters)):
game_board.append([])
opponent_board.append([])
for y in range(1, 11):
game_board[x].append(str(letters[x])+str(y))
opponent_board[x].append(str(letters[x])+str(y))

choose_ships(game_board, opponent_board) #call function to choose ships

def choose_ships(game_board, opponent_board):
Ships = {'Carrier': 5, 'Battleship': 4, 'Cruiser': 3, 'Submarine': 3, 'Destroyer': 2} #size of each ship
P1_Ships = [['Carrier', 1], ['Battleship', 1], ['Cruiser', 1], ['Submarine', 1], ['Destroyer', 1]] #number of ships to place
P2_Ships = [['Carrier', 1], ['Battleship', 1], ['Cruiser', 1], ['Submarine', 1], ['Destroyer', 1]]

for x in P1_Ships: #place ships
r = 0

while x[1] > 0: #check there's ships available
r += 1
type = x[0]
ship_size = Ships[x[0]]
position = input("Player 1, enter start position of {0}: ".format(x[0])) #choose position (i.e, A1)
check = place_ship(game_board, ship_size, position, type)
if check is True:
x[1] -= 1
else:
print("Can't place ship here.")

for z in P2_Ships: #place ships
r = 0

while z[1] > 0: #check there's ships available
r += 1
type = z[0]
ship_size = Ships[z[0]]
position = input("Player 2, enter start position of {0}: ".format(z[0])) #choose position (i.e

Solution

You can use loops to reduce repetition between players, here is an example:

P1_Ships = [['Carrier', 1], ['Battleship', 1], ['Cruiser', 1], ['Submarine', 1], ['Destroyer', 1]] #number of ships to place
P2_Ships = [['Carrier', 1], ['Battleship', 1], ['Cruiser', 1], ['Submarine', 1], ['Destroyer', 1]]

for x in P1_Ships: #place ships
    r = 0

    while x[1] > 0: #check there's ships available
        r += 1
        type = x[0]
        ship_size = Ships[x[0]]
        position = input("Player 1, enter start position of {0}: ".format(x[0])) #choose position (i.e, A1)
        check = place_ship(game_board, ship_size, position, type)
        if check is True:
            x[1] -= 1
        else:
            print("Can't place ship here.")

for z in P2_Ships: #place ships
    r = 0

    while z[1] > 0: #check there's ships available
        r += 1
        type = z[0]
        ship_size = Ships[z[0]]
        position = input("Player 2, enter start position of {0}: ".format(z[0])) #choose position (i.e, A1)
        check = place_ship(opponent_board, ship_size, position, type)
        if check is True:
            z[1] -= 1
        else:
            print("Can't place ship here.")


Becomes:

for player, board, ships in ( ("1", game_board, p1_ships), ("2", opponent_board, p2_ships) ):
    # Exercise for the reader


(Please note that the ships are the same for each player so you can just set one equal to the copy of the other.)

Code Snippets

P1_Ships = [['Carrier', 1], ['Battleship', 1], ['Cruiser', 1], ['Submarine', 1], ['Destroyer', 1]] #number of ships to place
P2_Ships = [['Carrier', 1], ['Battleship', 1], ['Cruiser', 1], ['Submarine', 1], ['Destroyer', 1]]

for x in P1_Ships: #place ships
    r = 0

    while x[1] > 0: #check there's ships available
        r += 1
        type = x[0]
        ship_size = Ships[x[0]]
        position = input("Player 1, enter start position of {0}: ".format(x[0])) #choose position (i.e, A1)
        check = place_ship(game_board, ship_size, position, type)
        if check is True:
            x[1] -= 1
        else:
            print("Can't place ship here.")

for z in P2_Ships: #place ships
    r = 0

    while z[1] > 0: #check there's ships available
        r += 1
        type = z[0]
        ship_size = Ships[z[0]]
        position = input("Player 2, enter start position of {0}: ".format(z[0])) #choose position (i.e, A1)
        check = place_ship(opponent_board, ship_size, position, type)
        if check is True:
            z[1] -= 1
        else:
            print("Can't place ship here.")
for player, board, ships in ( ("1", game_board, p1_ships), ("2", opponent_board, p2_ships) ):
    # Exercise for the reader

Context

StackExchange Code Review Q#152708, answer score: 4

Revisions (0)

No revisions yet.