-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbox.py
86 lines (71 loc) · 2.39 KB
/
box.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
# box game
# import the modules we need, for creating a GUI...
import tkinter as tk
from tkinter import messagebox as messagebox
board = [[None] * 10 for _ in range(10)]
counter = 0
root = tk.Tk()
root.geometry("275x250+100+300")
root.title("Box Game")
# method for drawing the game
def check_board():
freespaces = 0
redspaces = 0
greenspaces = 0
for i, row in enumerate(board):
for j, column in enumerate(row):
if board[i][j] == "red":
redspaces += 1
elif board[i][j] == "green":
greenspaces += 1
elif board[i][j] == None:
freespaces += 1
if freespaces == 0:
if greenspaces > redspaces:
winner = "green"
elif greenspaces < redspaces:
winner = "red"
else:
winner = "draw"
if winner != "draw":
messagebox.showinfo("Game Over!", winner + " wins!")
else:
messagebox.showinfo("Game Over!", "The game was a draw!")
# method to color the particular box
def on_click(i, j, event):
global counter
if counter < 100:
if board[i][j] == None:
color = "green" if counter % 2 else "red"
enemycolor = "red" if counter % 2 else "green"
event.widget.config(bg=color)
board[i][j] = color
for k in range(-1, 2):
for l in range(-1, 2):
try:
if board[i + k][j + l] == enemycolor:
board[i + k][j + l] = color
except IndexError:
pass
counter += 1
global gameframe
gameframe.destroy()
redraw()
root.wm_title(enemycolor + "'s turn")
else:
messagebox.showinfo("Alert", "This square is already occupied!")
check_board()
# call the main game everytime after one_click method
def redraw():
global gameframe
gameframe = tk.Frame(root)
gameframe.pack()
for i, row in enumerate(board):
for j, column in enumerate(row):
name = str(i) + str(j)
L = tk.Label(gameframe, text=' ',
bg="grey" if board[i][j] == None else board[i][j])
L.grid(row=i, column=j, padx='3', pady='3')
L.bind('<Button-1>', lambda e, i=i, j=j: on_click(i, j, e))
redraw()
root.mainloop()