예제 #1
0
def deterministic_game(qtable1, qtable2):
    wins = 0
    loses = 0
    ties = 0
    for game_num in range(6):
        starting_player = game_num % 2
        first_mv = game_num % 3
        mv_num = 2
        pos_num = first_mv
        end = False
        while (not end) and (mv_num <= 9):
            if mv_num % 2 == starting_player:
                pos_num = cf.move_against_player(mv_num, pos_num, qtable2)
                if boards.winner(BOARDS[mv_num][pos_num]) != 0:
                    end = True
                    loses += 1
            else:
                pos_num = cf.move_against_player(mv_num, pos_num, qtable1)
                if boards.winner(BOARDS[mv_num][pos_num]) != 0:
                    end = True
                    wins += 1
            mv_num += 1
        if not end:
            ties += 1
    return ties, loses, wins
예제 #2
0
def nondeterministc_game(qtable1, qtable2):
    gap1 = qtable1[0][0][:].max() - qtable1[0][0][:].min()
    gap2 = qtable2[0][0][:].max() - qtable2[0][0][:].min()
    starting_player = randint(0, 1)  # losowanie pierwszego gracza
    mv_num = 1
    pos_num = 0
    while mv_num <= 9:  # petla symulujaca gre
        if mv_num % 2 == starting_player:  # ruch pierwszego gracza
            pos_num = move_with_gap(mv_num, pos_num, qtable2, gap2)
            if boards.winner(BOARDS[mv_num][pos_num]) != 0:
                return -1
        else:  # ruch drugiego gracza
            pos_num = move_with_gap(mv_num, pos_num, qtable1, gap1)
            if boards.winner(BOARDS[mv_num][pos_num]) != 0:
                return 1
        mv_num += 1
    return 0
예제 #3
0
def make_action(prev_action, action, mv_num, curr_pos, prev_pos, qtable):
    new_pos = cf.position_after_move(mv_num, curr_pos, ACTIONS.index(action))
    # uaktualnianie qtabeli w zaleznosci od tego czy gra sie skonczyla
    if boards.winner(BOARDS[mv_num][new_pos]) != 0:
        next_act = q_update(prev_action, action, mv_num, new_pos, curr_pos,
                            prev_pos, REWARDS[1], qtable)
        end = True
    else:
        next_act = q_update(prev_action, action, mv_num, new_pos, curr_pos,
                            prev_pos, REWARDS[0], qtable)
        end = False
    return new_pos, curr_pos, action, next_act, end
예제 #4
0
            curr_pos, prev_pos, prev_action, action, end = make_action(
                prev_action, action, mv_num, curr_pos, prev_pos, QTABLE)
            mv_num += 1
    with open(file_name, 'wb') as f:
        pickle.dump(QTABLE, f)
else:
    with open(file_name, 'rb') as f:
        QTABLE = pickle.load(f)

# glowna czesc programu - gra z uzytkownikiem
print("Play as O: 1; Play as X: 2; End: 0")
new_game = int(input("Choose your symbol: "))
while new_game != 0:
    mv_num = 1
    pos_num = 0
    end = False  # incjacja zmiennych
    while (not end) and (mv_num <= 9):  # petla dla pojedynczej rozgrywki
        active_player = (mv_num + new_game) % 2
        # wybieranie i wykonanie akcji
        if active_player == 0:
            pos_num = cf.player_move(mv_num, pos_num)
        else:
            pos_num = cf.move_against_player(mv_num, pos_num, QTABLE)
        if boards.winner(BOARDS[mv_num][pos_num]) != 0:
            end = True
        boards.print_board(BOARDS[mv_num][pos_num])
        print("-----------------")  # wypisanie planszy po ruchu
        mv_num += 1
    print("Play as O: 1; Play as X: 2; End: 0")
    new_game = int(input("Choose your symbol: "))