-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
140 lines (110 loc) · 3.75 KB
/
utils.py
File metadata and controls
140 lines (110 loc) · 3.75 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import numpy as np
from rules import Rules
# encodes a card using 8 (for number) + 4 (for color) bits
def two_hot_encode_card(card):
encoding = np.zeros(12)
encoding[card[1]] = 1
encoding[8 + card[0]] = 1
return encoding
# encodes a game using 3 (for gametype) + 4 (for color) bits
def two_hot_encode_game(game):
encoding = np.zeros(7)
if game[1] is not None:
encoding[game[1]] = 1
if game[0] is not None:
encoding[3 + game[0]] = 1
return encoding
def one_hot_encode_game(game):
one_hot_game = np.zeros(10)
one_hot_game[Rules().games.index(game)] = 1
return one_hot_game
def one_hot_encode_game_chosen(game):
one_hot_game = np.zeros(9)
one_hot_game[Rules().games.index(game) - 1] = 1 # None is not possible anymore here
return one_hot_game
def one_hot_encode_position(position):
one_hot_position = np.zeros(4)
one_hot_position[position] = 1
return one_hot_position
def one_hot_encode_protocol(protocol):
enc_protocol = np.zeros((4, 10))
for game, i in zip(protocol, range(4)):
enc_protocol[i] = one_hot_encode_game(game)
return enc_protocol
def one_hot_encode_card(card):
single_one_hot_card = np.zeros(32)
single_one_hot_card[Rules().cards.index(card)] = 1
return single_one_hot_card
def one_hot_encode_cards(cards, game_type=None):
hand = sort_hand(cards, game_type)
enc_cards = np.zeros((8, 32))
for card, i in zip(hand, range(8)):
enc_cards[i] = one_hot_encode_card(card)
return enc_cards
def one_hot_encode_result(result):
enc_result = np.zeros(2)
enc_result[result] = 1
return enc_result
def sort_hand(cards, game_type):
if not game_type:
return sorted(cards, key=lambda card: Rules().cards.index(card))
sorted_trumps = Rules().get_sorted_trumps(game_type=game_type)
trumps = sorted([card for card in cards if card in sorted_trumps], key= lambda card : sorted_trumps.index(card))
non_trumps = sorted([card for card in cards if card not in sorted_trumps], key=lambda card: Rules().cards.index(card))
return non_trumps + trumps
"""
self.games = [[None, None], # no game
[0, 0], [2, 0], [3, 0], # sauspiel
[None, 1], # wenz
[0, 2], [1, 2], [2, 2], [3, 2]] # solo
# schelle # herz # gras # eichel
"""
def translate_cards_to_str(cards):
cards_str = []
for card in cards:
if card[0] == 0:
card_str = 'Schellen-'
elif card[0] == 1:
card_str = 'Herz-'
elif card[0] == 2:
card_str = 'Gras-'
else:
card_str = 'Eichel-'
if card[1] == 0:
card_str += '7'
elif card[1] == 1:
card_str += '8'
elif card[1] == 2:
card_str += '9'
elif card[1] == 3:
card_str += 'Unter'
elif card[1] == 4:
card_str += 'Ober'
elif card[1] == 5:
card_str += 'Koenig'
elif card[1] == 6:
card_str += '10'
else:
card_str += 'Ass'
cards_str.append(card_str)
return cards_str
def translate_games_to_str(game):
if game == [None, None]:
card_str = "Kein Spiel"
elif game == [0, 0]:
card_str = "auf die Schellen"
elif game == [2, 0]:
card_str = "auf die Gras"
elif game == [3, 0]:
card_str = "auf die Eichel"
elif game == [None, 1]:
card_str = "Wenz"
elif game == [0, 2]:
card_str = "Schellen-Solo"
elif game == [1, 2]:
card_str = "Herz-Solo"
elif game == [2, 2]:
card_str = "Gras-Solo"
elif game == [3, 2]:
card_str = "Eichel-Solo"
return card_str