-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
82 lines (70 loc) · 2.89 KB
/
view.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
import tkinter as tk
from PIL import Image, ImageTk
class GameBoard(tk.Frame):
def __init__(self, parent, rows=8, columns=8, size=84, color1="white", color2="white"):
'''size is the size of a square, in pixels'''
self.rows = rows
self.columns = columns
self.size = size
self.color1 = color1
self.color2 = color2
self.pieces = {}
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 addpiece(self, name, image, row=0, column=0):
'''Add a piece to the playing board'''
self.canvas.create_image(0,0, image=image, tags=(name, "piece"), anchor="c")
self.placepiece(name, row, column)
def placepiece(self, name, row, column):
'''Place a piece at the given row/column'''
self.pieces[name] = (row, column)
x0 = (column * self.size) + int(self.size/2)
y0 = (row * self.size) + int(self.size/2)
self.canvas.coords(name, x0, y0)
def refresh(self, event):
'''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")
color = self.color2
for row in range(self.rows):
color = self.color1 if color == self.color2 else self.color2
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=color, tags="square")
color = self.color1 if color == self.color2 else self.color2
for name in self.pieces:
self.placepiece(name, self.pieces[name][0], self.pieces[name][1])
self.canvas.tag_raise("piece")
self.canvas.tag_lower("square")
def showChromosome(chromosome = [], size = 8):
root = tk.Tk()
root.wm_title("Genetic 8 Queen")
board = GameBoard(root, rows=size, columns=size)
board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
im = Image.open("/Users/kiarash/PycharmProjects/8Queen/queenW.png")
player1 = ImageTk.PhotoImage(im)
for i in range(0, chromosome.__len__()):
board.addpiece("player" + str(i), player1, i, chromosome[i])
root.mainloop()
if __name__ == "__main__":
root = tk.Tk(screenName="Genetic 8 Queen", baseName="jehdk")
board = GameBoard(root)
board.pack(side="top", fill="both", expand="true", padx=4, pady=4)
im = Image.open("/Users/kiarash/PycharmProjects/8Queen/queenW.png")
player1 = ImageTk.PhotoImage(im)
board.addpiece("player1", player1, 0,0)
board.addpiece("player2", player1, 1, 1)
board.addpiece("player3", player1, 2, 3)
root.mainloop()