-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect 4.py
171 lines (139 loc) · 6.44 KB
/
Connect 4.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
# import tkinter module
import tkinter as tk
from PIL import Image, ImageTk
from connect_4_game import Connect4
from constants import *
import os
import sys
class GUI:
def __init__(self):
self._connect_4 = Connect4()
self._game_over = False
self._window = tk.Tk()
self._window.geometry("500x750")
self._window.minsize(500, 750)
self._window.maxsize(500, 750)
self._window.title("Connect 4")
self._starting_frame = tk.Frame(self._window)
self._column_button_frame = tk.Frame(self._window)
self._board_frame = tk.Frame(self._window)
self.GREEN_BUTTON_IMG = self.__get_image(r"./images/green_button.png", 20, 20)
self.EMPTY_CELL_IMG = self.__get_image(r"./images/empty_cell.png", 50, 50)
self.GREY_CELL_IMG = self.__get_image(r"./images/grey_cell.png", 50, 50)
self.BLACK_CELL_IMG = self.__get_image(r"./images/black_cell.png", 50, 50)
self.info_text = tk.Label(self._starting_frame, text="", font=("Arial", 15), foreground="green", width=100)
self.cell_UIs = []
self.__include_initial_UI()
self._starting_frame.pack(padx=1, pady=20)
self._column_button_frame.pack(padx=20, pady=10)
self._board_frame.pack(padx=40, pady=10)
self._window.mainloop()
def __include_initial_UI(self):
heading = tk.Label(self._starting_frame, text="Connect 4",
font=("Arial", 20),
foreground="blue",
width=20)
heading.pack(padx=20, pady=10)
new_game_button = tk.Button(self._starting_frame, text="New Game", font=("Arial", 13),
width=20, foreground="purple", background="yellow",
command=self.on_new_game_button_clicked)
new_game_button.pack(padx=20, pady=10)
exit_button = tk.Button(self._starting_frame, text="Exit", font=("Arial", 13),
width=20, foreground="white", background="brown",
command=self.on_exit_button_clicked)
exit_button.pack(padx=20, pady=20)
self.info_text.pack(padx=20, pady=20)
def __get_image(self, file_path, width, height):
img = Image.open(file_path)
img = img.resize((width, height))
img = ImageTk.PhotoImage(img)
return img
def __include_column_select_buttons(self):
for j in range(NO_COLUMNS):
ct1 = tk.Label(self._column_button_frame, text="Rack\n" + str(j + 1),
font=("Arial", 7),
foreground="black",
width=5)
c1 = tk.Button(self._column_button_frame, image=self.GREEN_BUTTON_IMG, borderwidth=0,
command=self.__get_column_function(j))
ct1.grid(row=0, column=j, padx=12.5, pady=0)
c1.grid(row=1, column=j, padx=12.5, pady=0)
def __get_column_function(self, column):
if column == 0:
return self.on_column_1_button_clicked
elif column == 1:
return self.on_column_2_button_clicked
elif column == 2:
return self.on_column_3_button_clicked
elif column == 3:
return self.on_column_4_button_clicked
elif column == 4:
return self.on_column_5_button_clicked
elif column == 5:
return self.on_column_6_button_clicked
else:
return self.on_column_7_button_clicked
def __include_initial_grid(self):
for i in range(NO_ROWS):
cell_UI_row = []
for j in range(NO_COLUMNS):
cell = tk.Label(self._board_frame, image=self.EMPTY_CELL_IMG, width=50, height=50)
cell.grid(row=i, column=j, padx=1, pady=1)
cell_UI_row.append(cell)
self.cell_UIs.append(cell_UI_row)
self.cell_UIs.reverse()
def __make_move(self, column):
if not self._game_over:
self.update_info_text("", "green")
status = self._connect_4.update_on_player_move(column)
if status:
self.update_cell_UI(self._connect_4.stack_next_cells[column] - 1, column, GREY_CELL_TYPE)
if self._connect_4.game_over:
self.update_info_text("Player won the game!", "green")
self._game_over = True
else:
next_column = self._connect_4.make_AI_bot_move()
if next_column >= 0:
self.update_cell_UI(self._connect_4.stack_next_cells[next_column] - 1, next_column,
BLACK_CELL_TYPE)
if self._connect_4.game_over:
self.update_info_text("Computer won the game!", "red")
self._game_over = True
if not self._game_over and self._connect_4.no_moves >= 42:
self._game_over = True
self.update_info_text("Game was drawn!", "brown")
else:
self.update_info_text(f"Rack {column + 1} is completely filled.\nPlease make another valid move",
"red")
print("No of Moves: " + str(self._connect_4.no_moves))
def on_new_game_button_clicked(self):
self._game_over = False
self.cell_UIs = []
self.update_info_text("", "green")
self.__include_column_select_buttons()
self.__include_initial_grid()
self._connect_4.on_start_new_game()
def on_exit_button_clicked(self):
self._window.destroy()
def on_column_1_button_clicked(self):
self.__make_move(0)
def on_column_2_button_clicked(self):
self.__make_move(1)
def on_column_3_button_clicked(self):
self.__make_move(2)
def on_column_4_button_clicked(self):
self.__make_move(3)
def on_column_5_button_clicked(self):
self.__make_move(4)
def on_column_6_button_clicked(self):
self.__make_move(5)
def on_column_7_button_clicked(self):
self.__make_move(6)
def update_info_text(self, text, color):
self.info_text.config(text=text, foreground=color)
def update_cell_UI(self, row, column, cell_type):
if cell_type == GREY_CELL_TYPE:
self.cell_UIs[row][column].config(image=self.GREY_CELL_IMG)
else:
self.cell_UIs[row][column].config(image=self.BLACK_CELL_IMG)
gui = GUI()