-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathejmploexp.py
182 lines (154 loc) · 5.91 KB
/
ejmploexp.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from experta import *
from experta.fact import *
import random
NERD = True
class WinTotals(Fact):
human = Field(int, default=0)
computer = Field(int, default=0)
ties = Field(int, default=0)
class Results(Fact):
winner = Field(str, mandatory=True)
loser = Field(str, mandatory=True)
why = Field(str, mandatory=True)
class ValidAnswer(Fact):
answer = Field(str, mandatory=True)
key = Field(str, mandatory=True)
class Action(Fact):
pass
class HumanChoice(Fact):
pass
class ComputerChoice(Fact):
pass
class RockPaperScissors(KnowledgeEngine):
def yes_or_no(self, question):
return input(question).upper().startswith('Y')
@DefFacts()
def game_rules(self, is_nerd=False):
"""Declare game rules and valid input keys for the user."""
self.valid_answers = dict()
yield Results(winner='rock', loser='scissors', why='Rock smashes scissors')
yield Results(winner='paper', loser='rock', why='Paper covers rock')
yield Results(winner='scissors', loser='paper', why='Scissors cut paper')
yield ValidAnswer(answer='rock', key='r')
yield ValidAnswer(answer='paper', key='p')
yield ValidAnswer(answer='scissors', key='s')
if is_nerd:
yield Results(winner='rock', loser='lizard', why='Rock crushes lizard')
yield Results(winner='spock', loser='rock', why='Spock vaporizes rock')
yield Results(winner='spock', loser='scissors', why='Spock smashes scissors')
yield Results(winner='paper', loser='spock', why='Paper disproves Spock')
yield Results(winner='scissors', loser='lizard', why='Scissors decapitates lizard')
yield Results(winner='lizard', loser='paper', why='Lizard eats paper')
yield Results(winner='lizard', loser='spock', why='Lizard poisons Spock')
yield ValidAnswer(answer='spock', key='k')
yield ValidAnswer(answer='lizard', key='l')
@Rule()
def startup(self):
print("Lets play a game!")
print("You choose rock, paper, or scissors,")
print("and I'll do the same.")
self.declare(WinTotals(human=0, computer=0, ties=0))
self.declare(Action('get-human-move'))
@Rule(NOT(Action()),
ValidAnswer(answer=MATCH.answer,
key=MATCH.key))
def store_valid_answers(self, answer, key):
self.valid_answers[key] = answer
#
# HUMAN MOVE RULES
#
@Rule(Action('get-human-move'))
def get_human_move(self):
question = ", ".join(
"{name} ({key})".format(
name=a[1].title(), key=a[0].upper())
for a in self.valid_answers.items()) + '? '
res = input(question).lower()
self.declare(HumanChoice(res))
@Rule(AS.f1 << HumanChoice(MATCH.choice),
ValidAnswer(answer=MATCH.answer,
key=MATCH.choice),
AS.f2 << Action('get-human-move'))
def good_human_move(self, f1, f2, answer):
self.retract(f1)
self.retract(f2)
self.declare(HumanChoice(answer))
self.declare(Action('get-computer-move'))
@Rule(AS.f1 << HumanChoice(MATCH.choice),
NOT(ValidAnswer(key=MATCH.choice)),
AS.f2 << Action('get-human-move'))
def bad_human_move(self, f1, f2, choice):
print("Sorry %s is not a valid answer" % choice)
self.retract(f1)
self.retract(f2)
self.declare(Action('get-human-move'))
#
# COMPUTER MOVE RULES
#
@Rule(AS.f1 << Action('get-computer-move'))
def get_computer_move(self, f1):
choice = random.choice(list(self.valid_answers.values()))
self.retract(f1)
self.declare(ComputerChoice(choice))
self.declare(Action('determine-results'))
#
# WIN DETERMINATION RULES
#
@Rule(AS.f1 << Action('determine-results'),
AS.f2 << ComputerChoice(MATCH.cc),
AS.f3 << HumanChoice(MATCH.hc),
AS.w << WinTotals(computer=MATCH.cw),
Results(winner=MATCH.cc,
loser=MATCH.hc,
why=MATCH.explanation))
def computer_wins(self, f1, f2, f3, w, cw, explanation):
self.retract(f1)
self.retract(f2)
self.retract(f3)
self.modify(w, computer=cw + 1)
print("Computer wins!", explanation)
self.declare(Action('determine-play-again'))
@Rule(AS.f1 << Action('determine-results'),
AS.f2 << ComputerChoice(MATCH.cc),
AS.f3 << HumanChoice(MATCH.hc),
'w' << WinTotals(human=MATCH.hw),
Results(winner=MATCH.hc,
loser=MATCH.cc,
why=MATCH.explanation))
def humans_wins(self, f1, f2, f3, w, hw, explanation):
self.retract(f1)
self.retract(f2)
self.retract(f3)
self.modify(w, human=hw + 1)
print("You win!", explanation)
self.declare(Action('determine-play-again'))
@Rule(AS.f1 << Action('determine-results'),
AS.f2 << ComputerChoice(MATCH.cc),
AS.f3 << HumanChoice(MATCH.cc),
AS.w << WinTotals(ties=MATCH.nt))
def tie(self, f1, f2, f3, w, nt):
self.retract(f1)
self.retract(f2)
self.retract(f3)
self.modify(w, ties=nt + 1)
print("Tie! Ha-ha!")
self.declare(Action('determine-play-again'))
#
# PLAY AGAIN RULE
#
@Rule(AS.f1 << Action('determine-play-again'),
WinTotals(computer=MATCH.ct,
human=MATCH.ht,
ties=MATCH.tt))
def play_again(self, f1, ct, ht, tt):
self.retract(f1)
if not self.yes_or_no("Play again?"):
print("You won", ht, "game(s).")
print("Computer won", ct, "game(s).")
print("We tied", tt, "game(s).")
self.halt()
else:
self.declare(Action('get-human-move'))
rps = RockPaperScissors()
rps.reset()
rps.run()