forked from laoyuan/LearnPythonTheHardWay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex43_hand.py
44 lines (37 loc) · 1.07 KB
/
ex43_hand.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
from random import choice
class HandRules(object):
def __init__(self):
self.hands = ['rock', 'scissors', 'paper']
def hand_random(self):
"""return a random hand."""
return choice(self.hands);
def hand_win(self, hand):
"""return what can win hand."""
if not (hand in self.hands):
print "wrong hand: %s" % hand
exit(1)
if hand == 'rock':
return 'paper'
elif hand == 'scissors':
return 'rock'
else:
return 'scissors'
def hand_lose(self, hand):
"""return what can lose hand."""
if not (hand in self.hands):
print "wrong hand: %s" % hand
exit(1)
if hand == 'rock':
return 'scissors'
elif hand == 'scissors':
return 'paper'
else:
return 'rock'
def hand_compare(self, h1, h2):
"""win lose draw: 1 -1 0"""
if h1 == self.hand_win(h2):
return 1
elif h1 == h2:
return 0
else:
return -1