Skip to content

Instantly share code, notes, and snippets.

@flankerhqd
Last active December 18, 2015 13:39
Show Gist options
  • Save flankerhqd/5791919 to your computer and use it in GitHub Desktop.
Save flankerhqd/5791919 to your computer and use it in GitHub Desktop.
import Tkinter as tk
class GameBoard(tk.Frame):
def __init__(self, parent, rows=8, columns=8, size=32, color1="white", color2="black", color3="blue"):
'''size is the size of a square, in pixels'''
self.rows = rows + 1
self.columns = columns + 1
self.size = size
self.color1 = color1
self.color2 = color2
self.color3 = color3
self.pieces = {}
self.nulls = []
self.dists = []
self.begin = ()
self.paths = []
canvas_width = columns * size
canvas_height = rows * size
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,
width=canvas_width, height=canvas_height, background="bisque")
self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)
# this binding will cause a refresh if the user interactively
# changes the window size
self.canvas.bind("<Configure>", self.refresh)
def setBegin(self,c,r):
self.begin = (c,r)
def putCircle(self,column, row, color):
(x,y) = self.translate(column,row)
self.canvas.create_oval(x-5,y-5,x+5,y+5, fill=color, outline="black")
def addNull(self, column, row):
self.nulls.append((row,column))
def addDist(self, column, row):
self.dists.append((row,column))
def addPath(self, c1,r1, c2, r2):
(x0,y0) = self.translate(c1,r1)
(x1,y1) = self.translate(c2,r2)
self.paths.append(((c1,r1), (c2,r2)))
self.canvas.create_line(x0,y0,x1,y1, arrow="last", fill="red", smooth="true")
def refresh(self, event):
#self.canvas.get_tk_widget().delete("all")
print "test"
'''Redraw the board, possibly in response to window being resized'''
xsize = int((event.width-1) / self.columns)
ysize = int((event.height-1) / self.rows)
self.size = min(xsize, ysize)
self.canvas.delete("square")
for row in range(self.rows):
for col in range(self.columns):
x1 = (col * self.size)
y1 = (row * self.size)
x2 = x1 + self.size
y2 = y1 + self.size
self.canvas.create_rectangle(
x1, y1, x2, y2, outline="black", fill=self.color1, tags="square")
self.putCircle(self.begin[0], self.begin[1],"green")
for null in self.nulls:
(c,r) = null
self.putCircle(c,r,"black")
for dist in self.dists:
(c,r) = dist
self.putCircle(c,r,"blue")
self.canvas.tag_raise("piece")
self.canvas.tag_lower("square")
for pair in self.paths:
((c1,r1),(c2,r2)) = pair
(x0,y0) = self.translate(c1,r1)
(x1,y1) = self.translate(c2,r2)
self.canvas.create_line(x0,y0,x1,y1, arrow="last", fill="red", smooth="true")
def translate(self, column, row):
column = column + 1
row = row + 1
x0 = (column * self.size)
y0 = (row * self.size)
return (x0,y0)
def parse(s):
x = int(s.split(' ')[0])
y = int(s.split(' ')[1])
return (x,y)
if __name__ == "__main__":
# row column differs!
s = raw_input()#problem 1
s = raw_input()#dimension 1
s = raw_input()#6 5
(x,y) = parse(s)
root = tk.Tk()
board = GameBoard(root,x,y)
s = raw_input()# feed 1
s = raw_input()#0 0
(x,y) = parse(s)
board.setBegin(x,y)
s = raw_input()#antenna 2
cnt = int(s.split(' ')[1])
for i in range(0,cnt):
s = raw_input()#2 3
(x,y) = parse(s)
board.addDist(x,y)
s = raw_input()#null 7
cnt = int(s.split(' ')[1])
for i in range(0,cnt):
s = raw_input()
(x,y) = parse(s)
board.addNull(x,y)
board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
while True:
s = raw_input()
if s.find("Solution") != -1:#solution 4
break
#board.putCircle(5,5,"red")
s = raw_input()#line 17
cnt = int(s.split(' ')[1])
board.addPath(1,1,2,2)
for i in range(0,cnt):
s = raw_input()#segment 1 2 3 4
l = s.split(' ')
board.addPath(int(l[1]),int(l[2]),int(l[3]),int(l[4]))
board.update()
board.canvas.update()
root.update()
#print "main loop"
#board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
root.mainloop()
while True:
s = raw_input()
#board.setBegin(0,0)
#board.addNull(1,1)
#board.addDist(3,3)
#board.addPath(2,2,3,3)
#root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment