-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect4_GUI.py
More file actions
151 lines (126 loc) · 4.98 KB
/
connect4_GUI.py
File metadata and controls
151 lines (126 loc) · 4.98 KB
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
import numpy as np
import pygame
import sys
import math
ROWS = 6
COLUMNS = 7
SQUARESIZE = 100
RADIUS = int(SQUARESIZE/2 - 4)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
pygame.init()
msg_font = pygame.font.SysFont("monospace", 75)
def create_board():
board = np.zeros((ROWS, COLUMNS))
return board
def drop_piece(board, row, col, piece):
""" adds the pieces to the board """
board[row][col] = piece
def is_valid_location(board, col):
""" Checks if the selected column is full """
return board[ROWS - 1][col] == 0
def get_next_open_row(board, col):
""" Finds the first empty row in the selected column """
for r in range(ROWS):
if board[r][col] == 0:
return r
def print_board(board):
""" flips the board """
print(np.flip(board, 0))
def winning(board, piece):
""" Checks for winning condition"""
# checking horizontal neighbours for win
for c in range(COLUMNS-3):
for r in range(ROWS):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece:
return True
# checking vertical neighbours for win
for c in range(COLUMNS):
for r in range(ROWS - 3):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece:
return True
# checking positively sloped diagonal
for c in range(COLUMNS-3):
for r in range(ROWS - 3):
if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece:
return True
# checking negatively sloped diagonal
for c in range(COLUMNS-3):
for r in range(3, ROWS):
if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece:
return True
def draw_board(board):
for c in range(COLUMNS):
for r in range(ROWS):
pygame.draw.rect(window, BLUE, (c*SQUARESIZE, (r+1)
* SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(window, BLACK, (int(
c*SQUARESIZE + SQUARESIZE/2), int((r+1)*SQUARESIZE + SQUARESIZE/2)), RADIUS)
for c in range(COLUMNS):
for r in range(ROWS):
if board[r][c] == 1:
pygame.draw.circle(window, RED, (int(
c*SQUARESIZE + SQUARESIZE/2), height-int((r)*SQUARESIZE + SQUARESIZE/2)), RADIUS)
elif board[r][c] == 2:
pygame.draw.circle(window, YELLOW, (int(
c*SQUARESIZE + SQUARESIZE/2), height-int((r)*SQUARESIZE + SQUARESIZE/2)), RADIUS)
pygame.display.update()
board = create_board()
game_over = False
turn = 0
width = COLUMNS * SQUARESIZE
height = (ROWS + 1) * SQUARESIZE
window = pygame.display.set_mode((width, height))
draw_board(board)
pygame.display.update()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(window, BLACK, (0, 0, width, SQUARESIZE))
posx = event.pos[0]
if turn == 0:
pygame.draw.circle(
window, RED, (posx, int(SQUARESIZE/2)), RADIUS)
else:
pygame.draw.circle(
window, YELLOW, (posx, int(SQUARESIZE/2)), RADIUS)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
# # Check whose turn it is
if turn == 0:
# Player 1 input
posx = event.pos[0]
column = (posx // SQUARESIZE)
if is_valid_location(board, column):
row = get_next_open_row(board, column)
drop_piece(board, row, column, 1)
if winning(board, 1):
pygame.draw.rect(
window, BLACK, (0, 0, width, SQUARESIZE))
label = msg_font.render(
"Player 1 wins!", 1, (255, 255, 255))
window.blit(label, (40, 10))
game_over = True
else:
# Player 2 input
posx = event.pos[0]
column = (posx // SQUARESIZE)
if is_valid_location(board, column):
row = get_next_open_row(board, column)
drop_piece(board, row, column, 2)
if winning(board, 2):
pygame.draw.rect(
window, BLACK, (0, 0, width, SQUARESIZE))
label = msg_font.render(
"Player 2 wins!", 1, (255, 255, 255))
window.blit(label, (40, 10))
game_over = True
turn += 1
turn %= 2
draw_board(board)
if game_over:
pygame.time.wait(5000)