-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2_gui.py
executable file
·161 lines (128 loc) · 3.89 KB
/
p2_gui.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
from Tkinter import *
from p2_game import Game, State
# Default bots, overridden by redteam and blueteam
import first_bot as red_bot
import first_bot as blue_bot
BOTS = {'red': red_bot, 'blue': blue_bot}
master = Tk()
UNDO_STACK = []
RED_AI = IntVar(master)
BLUE_AI = IntVar(master)
AI_THOUGHTS = StringVar(master)
BOARD_SIZE = 4
master.title("Dots and Boxes")
w = 600
h = 600
canvas = Canvas(master, width=w, height=h)
def display(state):
canvas.delete(ALL)
square_width = min(int(canvas['width']),int(canvas['height']))
step = square_width/state.game.width
r = int(step/10.0)
w = int(step/15.0)
def make_callback(move):
def callback(event):
if state.whos_turn == 'red' and RED_AI.get():
print "Give the red guy a chance to think!"
return
if state.whos_turn == 'blue' and BLUE_AI.get():
print "The blue lady needs more time to think!"
return
make_move(state, move)
return callback
for i,j in state.game.h_lines:
x = (i+0.5)*step
y = (j+0.5)*step
if (i,j) in state.h_line_owners:
owner = state.h_line_owners[(i,j)]
canvas.create_line(x,y,x+step,y,width=w)
else:
line = canvas.create_line(x,y,x+step,y,width=w,dash=(w,w),fill=state.whos_turn)
canvas.tag_bind(line,"<Button-1>",make_callback(('h',(i,j))))
for i,j in state.game.v_lines:
x = (i+0.5)*step
y = (j+0.5)*step
if (i,j) in state.v_line_owners:
owner = state.v_line_owners[(i,j)]
canvas.create_line(x,y,x,y+step,width=w)
else:
line = canvas.create_line(x,y,x,y+step,width=w,dash=(w,w),fill=state.whos_turn)
canvas.tag_bind(line,"<Button-1>",make_callback(('v',(i,j))))
for i,j in state.game.boxes:
x = (i+0.5)*step
y = (j+0.5)*step
if (i,j) in state.box_owners:
owner = state.box_owners[(i,j)]
canvas.create_rectangle(x+r,y+r,x+step-r,y+step-r,fill=owner)
for i,j in state.game.dots:
x = (i+0.5)*step
y = (j+0.5)*step
canvas.create_oval(x-r,y-r,x+r,y+r,fill='black')
if not state.is_terminal():
if state.whos_turn == 'red' and RED_AI.get():
think(state)
elif state.whos_turn == 'blue' and BLUE_AI.get():
think(state)
def make_move(state, move):
moves = state.get_moves()
if move in moves:
UNDO_STACK.append(state)
next_state = state.copy()
next_state.apply_move(move)
display(next_state)
if next_state.is_terminal():
try:
from pygame import mixer
mixer.init()
bustin = mixer.Sound('bustin.wav')
bustin.play()
except ImportError:
print "For the full dots and boxes experience install pygame"
AI_THOUGHTS.set("BOXIN' MAKES ME FEEL GOOD!")
else:
print move, "not in legal moves!"
def think(state):
import threading
class ThinkingThread(threading.Thread):
def run(self):
def quip(line):
AI_THOUGHTS.set(line)
move = BOTS[state.whos_turn].think(state.copy(), quip)
make_move(state, move)
#AI_THOUGHTS.set("")
ThinkingThread().start()
def restart():
game = Game(BOARD_SIZE)
initial_state = State(game)
UNDO_STACK[:] = [initial_state]
display(initial_state)
def undo():
if len(UNDO_STACK) > 1:
UNDO_STACK.pop()
display(UNDO_STACK[-1])
def rungui(redteam=None, blueteam=None, boardSize=4):
# Override the bots if provided
global BOARD_SIZE, BOTS
if redteam:
BOTS['red'] = redteam
if blueteam:
BOTS['blue'] = blueteam
BOARD_SIZE = boardSize
toolbar = Frame(master, width=w, height=h+20)
toolbar.pack(side=BOTTOM)
undo_btn = Button(toolbar, text="Undo", command=undo)
undo_btn.pack(side=LEFT)
restart_btn = Button(toolbar, text="Restart", command=restart)
restart_btn.pack(side=LEFT)
red_ai_btn = Checkbutton(toolbar, text="Red AI", variable=RED_AI)
red_ai_btn.pack(side=LEFT)
blue_ai_btn = Checkbutton(toolbar, text="Blue AI", variable=BLUE_AI)
blue_ai_btn.pack(side=LEFT)
BLUE_AI.set(1)
ai_thoughts_ent = Entry(toolbar, textvariable=AI_THOUGHTS, state=DISABLED, width=50)
ai_thoughts_ent.pack(side=LEFT)
canvas.pack(side=RIGHT)
restart()
mainloop()
if __name__ == '__main__':
rungui()