Esempio n. 1
0
def play_game(is_learning):
    game.setup(1)

    rAll = 0
    d = False
    j = 0

    race_timer = 0
    is_race_over = False

    game.players[0].racers[1].draw_hand()

    while j < 99:
        j += 1
        # choose an action by greedily (with noise) picking from Q table
        s = game.current_observation()
        a1 = RL.choose_action(str(s))
        game.players[0].racers[1].select_move(a1)

        game.players[0].racers[0].draw_hand()
        s2 = game.current_observation()
        a2 = RL.choose_action(str(s2))
        game.players[0].racers[0].select_move(a2)

        result = game.make_move()

        if not is_learning:
            racers = []
            for player in game.players:
                racers = [*racers, *player.racers]
            racers.sort()
            racers.reverse()
            game.print_cards()
            game.draw_course(racers)
            if result:
                print(j, 'turns taken')

        r = 0
        if result:
            r = 1
            s3 = 'terminal'
        else:
            game.players[0].racers[1].draw_hand()
            s3 = game.current_observation()

        if is_learning:
            RL.learn(str(s2), a2, r, str((s3)))
            RL.learn(str(s), a1, r, str((s2)))

        rAll += r
        if result:
            break
    rList.append(rAll)
Esempio n. 2
0
import game

print('Random selection, 2 player game')

game.setup(2)
counter = 0
race_timer = 0
race_over = False
while not race_over:
    if counter == 5:
        counter = 0
        game.print_cards()

    for player in game.players:
        for racer in player.racers:
            racer.draw_hand()
            racer.select_move(0)

    race_over = game.make_move()
    print('Next Round')
    counter += 1
    race_timer += 1
    # sleep(2)

game.print_cards()
print('Finished in', race_timer, 'turns')
Esempio n. 3
0
 def test_clean_print_cards(self):
     self.bob.add_card(Card('9', 'hearts'))
     expected = '{}: {}'.format(self.bob.name, "Your hand: ['9 of Hearts']")
     reality = game.print_cards(self.bob)
     self.assertEqual(expected,reality)
def play_game(is_learning):
    game.setup(2)

    if not is_learning:
        racers = []
        for player in game.players:
            racers = [*racers, *player.racers]
        racers.sort()
        racers.reverse()
        game.draw_course(racers)

    rAll = 0
    d = False
    j = 0

    race_timer = 0
    is_race_over = False

    while j < 99:
        j += 1

        s, a1 = play_racer_RL(game.players[0].racers[1], is_learning)

        s2, a2 = play_racer_RL(game.players[0].racers[0], is_learning)

        play_racer_max(game.players[1].racers[0])
        play_racer_max(game.players[1].racers[1])

        if not is_learning:
            game.print_cards_for_racer(game.players[1].racers[1])
            game.print_cards_for_racer(game.players[1].racers[0])

        result = game.make_move()

        if not is_learning:
            racers = []
            for player in game.players:
                racers = [*racers, *player.racers]
            racers.sort()
            racers.reverse()
            game.draw_course(racers)
            if result:
                print(result.name, ' won,', j, 'turns taken')

        r = 0
        if result and (result == game.players[0].racers[0]
                       or result == game.players[0].racers[1]):
            r = 1
        elif result:
            r = -1

        s3 = game.current_observation()

        if is_learning:
            RL.store_transition(s, a1, r, s2)
            RL.store_transition(s2, a2, r, s3)
            if (counter > 500) and (counter % 10 == 0):
                RL.learn()

        rAll += r
        if result:
            break

    if rAll == 0:
        racers = []
        for player in game.players:
            racers = [*racers, *player.racers]
        racers.sort()
        racers.reverse()
        game.print_cards()
        game.draw_course(racers)
        print(j)
        print()
        pass

    rList.append(rAll)