Пример #1
0
def test_draw(capsys):
    game = Game(4, "X", "MEOW", "KOTIK")
    game.move(0, 0, "X")
    game.move(0, 2, "O")
    game.move(1, 0, "O")
    game.move(1, 1, "X")
    game.move(2, 2, "O")
    assert (
        game.draw() ==
        "  | 1 | 2 | 3 | 4 \nA | X       O      \nB | O   X          \nC |         O      \nD |                \n"
    )
Пример #2
0
def make_step(sock: socket.socket, game: Game) -> bool:
    def make_move():
        cell = input("Ваш ход: ")
        while not re.match(r"[A-Z] \d", cell) or ord(
                cell.split(" ")[0]) - 65 >= game.field_size:
            print(
                "Введите клетку в формате: Буква Цифра. Клетка не должна выходить за пределы игрового поля."
            )
            cell = input("Ваш ход: ")
        own_x, own_y = cell.split(" ")
        is_move_success = False
        while not is_move_success:
            try:
                game.move(ord(own_x) - 65, int(own_y) - 1)
                sock.sendall(f"MOVE {cell}".encode("utf-8"))
            except Exception as e:
                print(e)
                make_move()

            is_move_success = True

    game_over = False
    print(f"Ожидание хода игрока {game.enemy_name}")

    data = sock.recv(BUFFER_SIZE)
    command, *arguments = data.decode("utf-8").split(" ")

    if command == "MOVE":
        enemy_x, enemy_y, *rest = arguments
        try:
            game.move(ord(enemy_x) - 65, int(enemy_y) - 1, ENEMY_SIDE)
        except Exception as e:
            print(e)

        if rest:
            game_over = True
            winner = rest[1]
            print(game.draw())

            if winner == "DRAW":
                print("Результат игры - ничья")
            else:
                print(
                    f"Выиграл игрок {game.gamer_name if winner == OWN_SIDE else game.enemy_name}"
                )

            return game_over
    elif command == "STOP":
        game_over = True
        winner = arguments[0]

        if winner == "DRAW":
            print("Результат игры - ничья")
        else:
            print(
                f"Выиграл игрок {game.gamer_name if winner == OWN_SIDE else game.enemy_name}"
            )

        return game_over
    else:
        raise WrongCommand

    print(game.draw())

    make_move()
    print(game.draw())

    return game_over