Ejemplo n.º 1
0
def test_no_winner():
    dims = (5, 5)
    s1 = {Ship(1, 1, 3, "v")}
    p1 = Player(Board(dims, s1))
    s2 = {Ship(1, 1, 3, "v")}
    p2 = Player(Board(dims, s2))
    game = Game(p1, p2, dims)

    result = game.get_winner()

    assert result is None
Ejemplo n.º 2
0
def test_game_text():
    dims = (3, 3)
    s1 = {Ship(0, 0, 3, "v")}
    p1 = Player(Board(dims, s1))
    s2 = {Ship(0, 0, 3, "v")}
    p2 = Player(Board(dims, s2))
    game = Game(p1, p2, dims)

    p1_lines = game.p1_lines()
    p2_lines = game.p2_lines()

    assert list(p1_lines) == [["s", ".", "."], ["s", ".", "."], ["s", ".", "."]]
    assert list(p2_lines) == [["s", ".", "."], ["s", ".", "."], ["s", ".", "."]]
Ejemplo n.º 3
0
def test_p2_winner():
    dims = (5, 5)
    s1 = {Ship(1, 1, 3, "v")}
    p1 = Player(Board(dims, s1))
    s2 = {Ship(1, 1, 3, "v")}
    p2 = Player(Board(dims, s2))
    game = Game(p1, p2, dims)

    game.p2_move(1, 1)
    game.p2_move(1, 2)
    game.p2_move(1, 3)
    result = game.get_winner()

    assert result == 2
Ejemplo n.º 4
0
def test_opponent_move_guessed():
    dims = (5, 5)
    s1 = {Ship(1, 1, 3, "v")}
    p1 = Player(Board(dims, s1))

    with pytest.raises(RuntimeError, match="already guessed"):
        p1.opponent_move(1, 1)
        p1.opponent_move(1, 1)
Ejemplo n.º 5
0
def test_player_is_defeated():
    dims = (5, 5)
    s1 = {Ship(1, 1, 3, "v")}
    p1 = Player(Board(dims, s1))

    for i in range(3):
        p1.opponent_move(1, 1 + i)

    assert p1.is_defeated
Ejemplo n.º 6
0
def test_player():
    dims = (5, 5)
    s1 = {Ship(1, 1, 3, "v")}
    p1 = Player(Board(dims, s1))

    for i in range(3):
        p1.opponent_move(1, 1 + i)

    assert list(p1.text) == [
        [".", ".", ".", ".", "."],
        [".", "h", ".", ".", "."],
        [".", "h", ".", ".", "."],
        [".", "h", ".", ".", "."],
        [".", ".", ".", ".", "."],
    ]
Ejemplo n.º 7
0
from battleship.ship import Ship


def print_game(p1_lines, p2_lines):
    print("Player 1:")
    for line in p1_lines:
        print("".join(line))
    print("Player 2:")
    for line in p2_lines:
        print("".join(line))


if __name__ == "__main__":
    dimensions = (10, 10)
    p1_board = Board(dimensions, {Ship(1, 1, 9, "h"), Ship(3, 5, 4, "v")})
    p1 = Player(p1_board)

    p2_board = Board(dimensions, {Ship(1, 2, 2, "v"), Ship(5, 6, 2, "h")})
    p2 = Player(p2_board)

    game = Game(p1, p2, dimensions)
    game.p1_move(3, 1)
    game.p1_move(3, 7)
    #
    game.p2_move(1, 1)
    # win
    game.p1_move(1, 2)
    game.p1_move(1, 3)
    game.p1_move(5, 6)
    game.p1_move(6, 6)
    print(f"winner: {game.get_winner()}")