def test_shoot():
    game = Battleship()

    game.generate_board(10)

    game.place_ship(1, '2d', 'vertical')
    game.place_ship(2, '5h', 'horizontal')

    assert game.shoot(1, 2, '8b') == 'Miss'
    assert game.shoot(1, 2, '8b') == False
    assert game.shoot(1, 2, '6h') == 'Hit'
def test_generate_board():
    game = Battleship()

    assert game.generate_board(-1) == True
    assert game.generate_board('F') == False
    assert game.generate_board(10) == True
    assert game.generate_board(7) == True

    game.generate_board(5)
    assert game.x_axis == [1, 2, 3, 4, 5]
    assert game.y_axis == ['a', 'b', 'c', 'd', 'e']

    game.generate_board(10)
    assert game.x_axis == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    assert game.y_axis == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Exemplo n.º 3
0
def test_get_location(monkeypatch):
  monkeypatch.setattr('builtins.input', lambda _: '6d')
  game = Battleship()
  game.generate_board(10)
  assert get_location(game, 2) == '6d'
  monkeypatch.setattr('builtins.input', lambda _: '16g')
  game.generate_board(20)
  assert get_location(game, 2) == '16g'
  monkeypatch.setattr('builtins.input', lambda _: 'none')
  game.generate_board(20)
  assert get_location(game, 2) == False
Exemplo n.º 4
0
def automated_main():
  game = Battleship()
  board_size = random.randrange(5, 100)
  print(board_size)
  game.generate_board(board_size)
  
  directions = ['vertical', 'horizontal']

  player_one_shot = game.get_random_shot(1)
  player_one_direction = random.choice(directions)
  print(f'Player one ship location: {player_one_shot}, {player_one_direction}')
  game.place_ship(1, player_one_shot, player_one_direction)
  
  player_two_shot = game.get_random_shot(2)
  player_two_direction = random.choice(directions)
  print(f'Player two ship location: {player_two_shot}, {player_two_direction}')
  game.place_ship(2, player_two_shot, player_two_direction)

  game_loop(game, True)
Exemplo n.º 5
0
def main():
  game = Battleship()
  
  size = get_board_size()
  game.generate_board(size)
  
  for player in range(1,3): # exclusive
    location = get_location(game)
    direction = get_direction()
    game.place_ship(player, location, direction)

  game_loop(game)
def test_random_shot():
    game = Battleship()

    game.generate_board(10)

    # Check a random shot is in the correct format and is in the axis choices
    shot = game.get_random_shot(1)
    assert re.match(r'\d+\D+', shot)
    letter = re.findall(r'\D+', shot)[0]
    number = int(re.findall(r'\d+', shot)[0])
    assert letter in game.y_axis
    assert number in game.x_axis

    shot = game.get_random_shot(2)
    assert re.match(r'\d+\D+', shot)
    letter = re.findall(r'\D+', shot)[0]
    number = int(re.findall(r'\d+', shot)[0])
    assert letter in game.y_axis
    assert number in game.x_axis
def test_game():
    game = Battleship()

    game.generate_board(10)

    game.place_ship(1, '2d', 'vertical')
    game.place_ship(2, '5h', 'horizontal')

    assert game.shoot(1, 2, '8b') == 'Miss'
    assert game.shoot(2, 1, '3g') == 'Miss'
    assert game.shoot(1, 2, '9g') == 'Miss'
    assert game.shoot(2, 1, '8c') == 'Miss'
    assert game.shoot(1, 2, '6h') == 'Hit'
    assert game.shoot(2, 1, '5i') == 'Miss'
    assert game.shoot(1, 2, '8h') == 'Miss'
    assert game.shoot(2, 1, '7e') == 'Miss'
    assert game.shoot(1, 2, '9g') == False
    assert game.shoot(2, 1, '2i') == 'Miss'
    assert game.shoot(1, 2, '1e') == 'Miss'
    assert game.shoot(2, 1, '1d') == 'Miss'
    assert game.shoot(1, 2, '5h') == 'Hit'
    assert game.shoot(2, 1, '4e') == 'Miss'
    assert game.shoot(1, 2, '4h') == 'Sunk'
def test_get_shots_taken():
    game = Battleship()

    game.generate_board(10)

    game.place_ship(1, '2d', 'vertical')
    game.place_ship(2, '5h', 'horizontal')

    assert game.shoot(1, 2, '8b') == 'Miss'
    assert game.get_shots_taken(1) == ['8b']
    assert game.shoot(1, 2, '9g') == 'Miss'
    assert game.get_shots_taken(1) == ['8b', '9g']
    assert game.shoot(1, 2, '8h') == 'Miss'
    assert game.get_shots_taken(1) == ['8b', '9g', '8h']
    assert game.shoot(1, 2, '1e') == 'Miss'
    assert game.get_shots_taken(1) == ['8b', '9g', '8h', '1e']
def test_place_ships():
    game = Battleship()

    game.generate_board(10)

    game.place_ship(1, '2d', 'vertical')
    game.place_ship(2, '5h', 'horizontal')
    assert game.players[1]['ship_location'] == ['2c', '2d', '2e']
    assert game.players[2]['ship_location'] == ['4h', '5h', '6h']

    game.place_ship(1, '6a', 'vertical')
    game.place_ship(2, '1h', 'horizontal')
    assert game.players[1]['ship_location'] == ['6c', '6a', '6b']
    assert game.players[2]['ship_location'] == ['3h', '1h', '2h']

    game.place_ship(1, '6j', 'vertical')
    game.place_ship(2, '10h', 'horizontal')
    assert game.players[1]['ship_location'] == ['6i', '6j', '6h']
    assert game.players[2]['ship_location'] == ['9h', '10h', '8h']