-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyboard_agent.py
72 lines (66 loc) · 2.67 KB
/
keyboard_agent.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
60
61
62
63
64
65
66
67
68
69
70
71
72
from agent import Agent
from crazy_eights_game import Card
from game_rules import gameRules,Actions
class KeyboardAgent(Agent):
"""
An agent controlled by the keyboard.
"""
def __init__(self, index=0):
self.index = index
self.keys = []
def getAction(self, state):
print('Card on table: %s' % state.getCardOnTable())
print('Cards in hand: %s' % ['%s : %s' % (card, state.getHand().pile[card]) for
card in state.getHand().pile])
print('Number of cards in opponents hand: %s' % state.handsizes[(self.index+1)%2])
while True:
print('legal actions: %s ' % ['%s' % a + (' ' + ' '.join([ '%s %s' % (card.rank, card.suit) for card in cards])
if cards != None else '') for a,cards in state.getLegalActions()])
x = raw_input('Make action: ').split()
inputAction = x[0]
inputCards = x[1:]
cards = []
if inputAction.lower() in Actions.AllActions:
valid = True
for rank,suit in zip(inputCards[::2],inputCards[1::2]):
valid, rank, suit = self.isValidCard(rank,suit)
if valid:
cards.append(Card(rank,suit))
else:
print('Wrong input format!')
break
if not valid: continue
if cards == []: cards = None
for action in state.getLegalActions():
if self.compare((inputAction,cards),action):
return action
print('Wrong input format!')
continue
else:
print('Wrong input format!')
def compare(self, action1, action2):
a1, cs1 = action1
a2, cs2 = action2
if a1 == a2:
if cs1 == None or cs2 == None:
if a1 in [Actions.TAKE, Actions.PASS]:
return True
else:
return False
if len(cs1) != len(cs2):
return False
for card1, card2 in zip(cs1, cs2):
if card1.suit != card2.suit or \
card1.rank != card2.rank:
return False
return True
def isValidCard(self, rank, suit):
rank = int(rank)
if rank not in gameRules.ranks:
return False, None, None
suit = suit.lower()
if suit not in gameRules.suits:
suit = suit[:-1]
if suit not in gameRules.suits:
return False, None, None
return True, rank, suit