-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicpoker.py
216 lines (188 loc) · 7.3 KB
/
graphicpoker.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from graphics import *
from dieview import *
from button import Button
class GraphicInterface:
def __init__(self):
self.win = GraphWin("Dice Poker", 600, 400)
self.showHighScore()
self.win.setBackground("green3")
self.showIntro()
self.choosePlay()
banner = Text(Point(300,30),"Python Poker Parlor")
banner.setSize(24)
banner.setFill("yellow2")
banner.setStyle("bold")
banner.draw(self.win)
self.msg = Text(Point(300,380), "Welcom to the Dice Table")
self.msg.setSize(18)
self.msg.draw(self.win)
self.createDice(Point(300,100), 75)
self.buttons = []
self.addDiceButtons(Point(300,170), 75, 30)
b = Button(self.win, Point(300, 230), 400, 40, "Roll Dice")
self.buttons.append(b)
b = Button(self.win, Point(300, 280), 150, 40, "Score")
self.buttons.append(b)
b = Button(self.win, Point(570, 375), 40, 30, "Quit")
self.buttons.append(b)
b = Button(self.win, Point(570, 30), 40, 30, "Help")
self.buttons.append(b)
self.money = Text(Point(300,325), "$100")
self.money.setSize(18)
self.money.draw(self.win)
def createDice(self, center, size):
center.move(-3*size,0)
self.dice = []
for i in range(5):
view = DieView(self.win, center, size)
self.dice.append(view)
center.move(1.5*size, 0)
def addDiceButtons(self, center, width, height):
center.move(-3*width,0)
for i in range(1,6):
label = "Die {0}".format(i)
b = Button(self.win, center, width, height, label)
self.buttons.append(b)
center.move(1.5*width, 0)
def setMoney(self, amt):
self.money.setText("${}".format(amt))
def showResult(self, msg, score):
if score > 0:
text = "{0}! you win ${1}".format(msg,score)
else:
text = "you rolled {0}".format(msg)
self.msg.setText(text)
def setDice(self, values):
for i in range(5):
self.dice[i].setValue(values[i])
def wantToPlay(self):
while True:
ans = self.choose(["Roll Dice", "Quit", "Help"])
self.msg.setText("")
if ans == "Help":
self.help()
else:
break
return ans == "Roll Dice"
def chooseDice(self):
# choices is a list of the indexes of teh selected dice
choices = [] # no dice choosen yet
while True:
# wait for user to click a valid button
b = self.choose(["Die 1", "Die 2", "Die 3", "Die 4", "Die 5",
"Roll Dice", "Score", "Help"])
if b[0] == "D": # User Clicked a die button
i = int(b[4]) - 1 # Translate label to die index
if i in choices: # Currently selected, unselect it
choices.remove(i)
self.dice[i].setColor("black")
else: # Currentely deselected, select it
choices.append(i)
self.dice[i].setColor("gray")
else: # User clicked roll or score
for d in self.dice: # Revert appearance of all dice
d.setColor("black")
if b == "Score": # Score clicked, ignore choices
return []
elif b == "Help":
self.help()
elif choices !=[]: # Don't accept roll unless some
return choices # dice are actually selected
def close(self):
self.win.close()
def choose(self, choices):
buttons = self.buttons
# activate choice buttons, deactivate others
for b in buttons:
if b.getLabel() in choices:
b.activate()
else:
b.deactivate()
# get mouse clicks until an active button is clicked
while True:
p = self.win.getMouse()
for b in buttons:
if b.clicked(p):
return b.getLabel() # function exit here.
def setValue(self, value):
# Turn all the pips off
self.value = value
for pip in self.pips:
pip.setFill(self.background)
# Turn the appropriate pips back on
for i in self.onTable[value]:
self.pips[i].setFill(self.forground)
def showIntro(self):
self.intro = Text(Point(300,200), "Lets Play Some Poker with Dice!!")
self.intro.draw(self.win)
def choosePlay(self):
letsPlay = Button(self.win, Point(300,100), 100, 60, "Lets Play")
letsPlay.activate()
while True:
p = self.win.getMouse()
if letsPlay.clicked(p):
break
self.intro.undraw()
letsPlay.undraw()
def help(self):
helpWin = GraphWin("help", 400, 200)
helpWin.setBackground("blue1")
helpmsg = Text(Point(200,100), "hand \t pay\n\
--------------------- \n\
Two Pairs \t $5 \n\
Three of a Kind \t $8 \n\
Full House \t $12\n\
Four of a Kind \t $15\n\
Straight \t $20\n\
Five of a Kind \t $30 ")
helpmsg.draw(helpWin)
def showHighScore(self):
highscores = open("high_score.txt", "r")
high_score = highscores.readlines()
y = 170
scores = []
scorText = Text(Point(300,y-30), "High Score name")
scorText.draw(self.win)
for line in high_score:
s = Text(Point(300,y), line).draw(self.win)
scores.append(s)
y = y + 18
highscores.close()
cbut = Button(self.win, Point(300, 100), 400, 40, "Start Game")
cbut.activate()
while True:
p = self.win.getMouse()
if cbut.clicked(p):
scorText.undraw()
cbut.undraw()
for obj in scores:
obj.undraw()
break
def askUsername(self):
nameWin = GraphWin("player name", 300, 200)
name = Entry(Point(150,100),10)
name.draw(nameWin)
okbut = Button(nameWin, Point(150, 150), 30, 40, "OK")
okbut.activate()
while True:
p = nameWin.getMouse()
if okbut.clicked(p):
break
player = name.getText()
nameWin.close()
return player
def updateScore(self, money):
highscores = open("high_score.txt", "r")
high_score = highscores.readlines()
i = 0
for line in high_score:
if money > int(line.split("\t")[0]):
name = self.askUsername()
high_score.insert(i,"{}\t{}\n".format(money,name))
high_score.pop(10)
highscores = open("high_score.txt", "w")
for line in high_score:
print(line, file = highscores, end = "")
highscores.close()
break
i = i + 1