Exemple #1
0
def test_game():
    game = Game()
    assert game.players[0].name == "Player X"
    assert game.players[1].name == "Player O"

    assert not game.make_move(0)  # player 1

    assert game.is_position_taken(0)
    with pytest.raises(ValueError):
        assert game.is_position_taken(200)

    assert game.current_player == game.players[1]
    assert not game.make_move(4)  # player 2

    assert not game.make_move(1)  # player 1
    assert not game.make_move(5)  # player 2
    assert game.make_move(2)  # player 1
    assert game.get_winner() == game.players[0]
    assert game.is_end_of_game()
    assert not game.is_array_full()

    game2 = Game()

    # Cannot make a move to a position outside the game array limit
    with pytest.raises(ValueError):
        game2.make_move(123)

    assert not game2.make_move(0)  # player 1
    assert not game2.make_move(3)  # player 2
    assert not game2.make_move(1)  # player 1
    assert not game2.make_move(4)  # player 2
    assert not game2.make_move(8)  # player 1
    assert game2.make_move(5)  # player 2
    assert game2.get_winner() == game2.players[1]
    assert game.is_end_of_game()
    assert not game.is_array_full()
Exemple #2
0
            if next_move_val < min_val:
                min_val = next_move_val
                best_move = m
                if next_move_val < 0:
                    break
    if i == 0:
        return best_move

    return max_val if i % 2 == 0 else min_val


if (x_first):
    print("valid moves are ", game.valid_moves)
    move = int(input("player one(X), make first move: "))
    game.make_move(move)

while not game.is_over():
    if game.player == X:
        print("valid moves are ", game.valid_moves)
        move = int(input("please make move: "))
    else:
        move = minimax(game, 0)
        print("computer is now making move ", move)
    game.make_move(move)
    print(game)

if game.winner_exists():
    print("You win!" if game.get_winner() == X else "You lose!")
else:
    print("player one and two ties")