Ejemplo n.º 1
0
def test_process_my_move(mocker):
    mocker.patch('seaBattle.game.SeaBattle.make_move')
    mocker.patch('seaBattle.game.SeaBattle.print_map')
    mocker.patch('builtins.input', return_value='a1')
    mocker.patch('socket.socket.send')
    mocker.patch('socket.socket.recv', result_value=strings.MISSED)
    game = SeaBattle(3)
    import socket
    assert game.process_my_move(socket.socket()) is None
    game.make_move.assert_called_once()
    game.print_map.assert_called_with(game.enemy_map)
    mocker.patch('builtins.input', return_value='quit')
    assert game.process_my_move(socket.socket())
Ejemplo n.º 2
0
def test_delete_marks(mocker):
    mocker.patch('seaBattle.game.SeaBattle.generate_map')
    import string
    game = SeaBattle(10)
    game.map = {
        x: ['*' for _ in range(game.size)]
        for x in string.ascii_uppercase[:game.size]
    }
    game.delete_marks()
    assert game.map == {
        x: [' ' for _ in range(game.size)]
        for x in string.ascii_uppercase[:game.size]
    }
Ejemplo n.º 3
0
def test_process_enemy_move(mocker):
    mocker.patch('socket.socket.send')
    mocker.patch('socket.socket.recv', result_value='a1')
    mocker.patch('seaBattle.game.SeaBattle.print_map')
    mocker.patch('seaBattle.game.SeaBattle.make_enemy_move',
                 return_value=strings.MISSED)
    game = SeaBattle(3)
    import socket
    assert game.process_enemy_move(socket.socket(), 2) is None
    game.make_enemy_move.assert_called_once()
    game.print_map.assert_called_with(game.map)
    mocker.patch('seaBattle.game.SeaBattle.make_enemy_move',
                 return_value=strings.ENEMY_WON)
    assert game.process_enemy_move(socket.socket(), 2)
Ejemplo n.º 4
0
def test_init(mocker, small_game, standart_game, big_game):
    mocker.patch('seaBattle.game.SeaBattle.generate_map')
    game = SeaBattle(10)
    game.generate_map.assert_called_once()
    assert len(small_game.map) == len(small_game.enemy_map) == 3
    assert len(standart_game.map) == len(standart_game.enemy_map) == 10
    assert len(game.map) == len(game.enemy_map) == 10
    assert len(big_game.map) == len(big_game.enemy_map) == 25
Ejemplo n.º 5
0
def client():
    with socket.socket() as s:
        s.connect(connection.get_addres())
        field_size = s.recv(1024).decode()
        game = SeaBattle(int(field_size))
        game.my_turn = False
        game.print_map(game.map)
        exit = False
        while not exit:
            while game.my_turn and not exit:
                exit = game.process_my_move(s)
            while not game.my_turn and not exit:
                exit = game.process_enemy_move(s, 1)
Ejemplo n.º 6
0
def test_mark_neighbour_cells(mocker):
    mocker.patch('seaBattle.game.SeaBattle.generate_map')
    game = SeaBattle(10)
    game.ships.append([('A', 1), ('A', 2)])
    game.map['A'][1] = 'S'
    game.map['A'][2] = 'S'
    game.mark_neighbour_cells(0)
    assert game.map['B'][1] == '*'
    assert game.map['C'][1] != '*'
    assert game.map['A'][3] == '*'
    assert game.map['A'][1] != '*'
    assert game.map['A'][0] == '*'
Ejemplo n.º 7
0
def server():
    field_size = get_field_size()
    game = SeaBattle(field_size)
    with socket.socket() as s:
        s.bind(connection.get_addres())
        print('Ожидаем подключения второго игрока...')
        s.listen(1)
        conn, addr = s.accept()
        with conn:
            print('Connected by', addr)
            conn.send(str(field_size).encode())
            game.print_map(game.map)
            exit = False
            while not exit:
                while game.my_turn and not exit:
                    exit = game.process_my_move(conn)
                while not game.my_turn and not exit:
                    exit = game.process_enemy_move(conn, 2)
Ejemplo n.º 8
0
def test_check_ship_true(mocker):
    mocker.patch('seaBattle.game.SeaBattle.generate_map')
    game = SeaBattle(10)
    assert game.check_horizontal_ship(4, ('A', 1))
    assert game.check_vertical_ship(4, ('A', 1))
Ejemplo n.º 9
0
def test_make_move():
    game = SeaBattle(10)
    game.make_move('a1', strings.MISSED)
    assert game.enemy_map['A'][0] == '.'
    assert not game.my_turn
    game.make_move('a1', strings.ALREADY_BEEN_HERE)
    assert not game.my_turn
    game.make_move('a1', strings.WRONG_MOVE)
    assert not game.my_turn
    game.make_move('a5', strings.HIT)
    assert game.enemy_map['A'][4] == '+'
    assert game.my_turn
    game.make_move('a6', strings.KILL)
    assert game.enemy_map['A'][5] == '+'
    assert game.my_turn
Ejemplo n.º 10
0
def test_make_enemy_move(mocker):
    mocker.patch('seaBattle.game.SeaBattle.generate_map')
    game = SeaBattle(3)
    game.ships_count = 2
    game.map = {
        'A': ['+', 'S', '.'],
        'B': [' ', ' ', '.'],
        'C': ['S', 'S', ' ']
    }
    game.ships.append([('A', 0), ('A', 1)])
    game.ships_cells[('A', 0)] = 0
    game.ships_cells[('A', 1)] = 0
    game.ships.append([('C', 0), ('C', 1)])
    game.ships_cells[('C', 0)] = 1
    game.ships_cells[('C', 1)] = 1
    assert game.make_enemy_move('a1') == strings.ALREADY_BEEN_HERE
    assert game.make_enemy_move('a3') == strings.ALREADY_BEEN_HERE
    assert game.make_enemy_move('B2') == strings.MISSED
    assert game.make_enemy_move('B2') == strings.ALREADY_BEEN_HERE
    assert game.make_enemy_move('h2') == strings.WRONG_MOVE
    assert game.make_enemy_move('A10') == strings.WRONG_MOVE
    assert game.make_enemy_move('A2') == strings.KILL
    assert game.make_enemy_move('C2') == strings.HIT
    assert game.make_enemy_move('C1') == strings.ENEMY_WON
    assert game.make_enemy_move('po1') == strings.WRONG_MOVE
    game.map['B'][0] = 'smth'
    with pytest.raises(RuntimeError) as e_info:
        assert game.make_enemy_move('B1') == Exception
Ejemplo n.º 11
0
def small_game():
    return SeaBattle(3)
Ejemplo n.º 12
0
def big_game():
    return SeaBattle(25)
Ejemplo n.º 13
0
def standart_game():
    return SeaBattle(10)