forked from datamllab/rlcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_rummy_random.py
59 lines (48 loc) · 2.34 KB
/
gin_rummy_random.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
File name: rlcard.examples.gin_rummy_random.py
Author: William Hale
Date created: 2/12/2020
A toy example of playing GinRummy with random agents / novice agents
'''
import rlcard
from rlcard import models
from rlcard.utils import set_global_seed
from rlcard.games.gin_rummy.player import GinRummyPlayer
from rlcard.games.gin_rummy.utils.move import DealHandMove
# Make environment
env = rlcard.make('gin-rummy', config={'seed': 0})
episode_num = 1
env.game.settings.print_settings()
# Set a global seed
set_global_seed(0)
# Set up agents
agents = models.load("gin-rummy-novice-rule").agents # use novice agents rather than random agents
env.set_agents(agents)
for episode in range(episode_num):
# Generate data from the environment
trajectories, _ = env.run(is_training=False)
# Print out the trajectories
print('\nEpisode {}'.format(episode))
for ts in trajectories[0]:
print('State: {}, Action: {}, Reward: {}, Next State: {}, Done: {}'.format(ts[0], ts[1], ts[2], ts[3], ts[4]))
# print move sheet
print("\n========== Move Sheet ==========")
move_sheet = env.game.round.move_sheet
move_sheet_count = len(move_sheet)
for i in range(move_sheet_count):
move = move_sheet[i]
print("{}".format(move))
if i == 0 and isinstance(move, DealHandMove):
player_dealing_id = move.player_dealing.player_id
leading_player_id = GinRummyPlayer.opponent_id_of(player_dealing_id)
shuffle_deck = move.shuffled_deck
leading_player_hand_text = [str(card) for card in shuffle_deck[-11:]]
dealing_player_hand_text = [str(card) for card in shuffle_deck[-21:-11]]
stock_pile_text = [str(card) for card in shuffle_deck[:31]]
short_name_of_player_dealing = GinRummyPlayer.short_name_of(player_id=player_dealing_id)
short_name_of_player_leading = GinRummyPlayer.short_name_of(player_id=leading_player_id)
print("player_dealing is {}; leading_player is {}.".format(short_name_of_player_dealing,
short_name_of_player_leading))
print("leading player hand: {}".format(leading_player_hand_text))
print("dealing player hand: {}".format(dealing_player_hand_text))
print("stock_pile: {}".format(stock_pile_text))