Battleship

10/03/2017 - Zoe Junginger & Jens Hahn

1. Initialise

* Define ships (length and number of ships)    
* Define playing field    
Set playing field 10x10 ```np.chararray((10,10), unicode=True)```     
Put ```'O'``` on field 0x0 ```field[0,0] = 'O'```    

2. Let computer put the ships on the playing field

* Use random number generator to place ships and orientated them (not diagonal)    
Get a random number 0 <= x < 11 ```np.random.randint(0,11)```    

3. Play the game

* Put your ships on the field ```input('where to place submarine?')```    
* Let the computer shoot    
* Shoot on the computer field

In [ ]:
import numpy as np
# which ships and how long are they?
# battleship, carrier, cruiser, submarine, destroyer
ships = {'B': 4, 'T': 5, 'C': 3, 'S':3, 'D': 2}

In [ ]:
def place_ship(ship_length, abbreviation):
    """Function to place ships on pitch"""
    pos_ship = (np.random.randint(0,10), np.random.randint(0,10-ship_length))  # random position
    # check if ships are on a free position
    for i in range(ship_length):
        if pitch[pos_ship[0], pos_ship[1]+i] != '~':
            place_ship(ship_length, abbreviation)
            return None
    # put abbreviation on pitch
    for i in range(ship_length):
        pitch[pos_ship[0], pos_ship[1]+i] = abbreviation

In [ ]:
# pitch for the computers ships
pitch = np.chararray((10,10), unicode=True)
for i in range(10):
    for j in range(10):
        pitch[i,j] = '~'
print(pitch)

# pitch for the players ships
pitch_player = np.chararray((10,10), unicode=True)
for i in range(10):
    for j in range(10):
        pitch_player[i,j] = '~'
        
# pitch for the player to see the player shots 
pitch_computer = np.chararray((10,10), unicode=True)
for i in range(10):
    for j in range(10):
        pitch_computer[i,j] = '~'

In [ ]:
for ship in ships:
    place_ship(ships[ship], ship)

In [ ]:
positions = {}
positions['S'] = input ("where to place submarine (3)? ")
positions['T'] = input ("where to place Carrier (5)? ")
positions['B'] = input ("where to place Battleship (4)? ")
positions['C'] = input ("where to place Cruiser (3)? ")
positions['D'] = input ("where to place destroyer (2)? ")

In [ ]:
# test positions
positions = {'S': '0,0', 'T': '1,0', 'B': '2,0', 'C': '3,0', 'D':'4,0'}

In [ ]:
for ship in positions:
    positions[ship] = positions[ship].split(',')
    for i in range(ships[ship]):
        pitch_player[int(positions[ship][0]), int(positions[ship][1])+i] = ship

In [ ]:
print(pitch_player)

In [ ]:
def computer_round():
    shots = []
    target = (np.random.randint(0,10), np.random.randint(0,10))
    while target in shots:
        target = (np.random.randint(0,10), np.random.randint(0,10))
    print('I shoot at ' + str(target))
    if pitch_player[target[0], target[1]] != '~':
        pitch_player[target[0], target[1]] = 'X'
    shots.append(target)
    destroyed = []
    for ship in ships:
        if not ship in pitch_player:
            destroyed.append(ship)
    if len(destroyed) == len(ships):
        print("YOU LOST!! MUHAHA")
        return True

In [ ]:
def player_round():
    print(pitch_computer)
    target = input('Target?')
    target = target.split(',')
    if pitch[int(target[0]), int(target[1])] == '~':
        print('You missed!')
        pitch_computer[int(target[0]), int(target[1])] = 'o'
    else:
        print('HIT!')
        pitch_computer[int(target[0]), int(target[1])] = 'X'
        pitch[int(target[0]), int(target[1])] = 'X'
    destroyed = []
    for ship in ships:
        if not ship in pitch:
            destroyed.append(ship)
    if len(destroyed) == len(ships):
        print("YOU WON... :.|")   
        return True

In [ ]:
game = True
while game:
    pc = computer_round()
    if pc == True:
        break
    player = player_round()
    if player_round() == True:
        break
print('Computers pitch:\n', pitch)
print('Players pitch:\n', pitch_player)

In [ ]: