Skip to content

Commit 626dcb8

Browse files
authored
added connect_four.py file
1 parent 95db993 commit 626dcb8

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import numpy as np
2+
import pygame
3+
import sys
4+
import math
5+
6+
BLUE = (0, 0, 255)
7+
BLACK = (0, 0, 0)
8+
RED = (255, 0, 0)
9+
YELLOW = (255, 255, 0)
10+
11+
ROW_COUNT = 6
12+
COLUMN_COUNT = 7
13+
14+
15+
def create_board():
16+
board = np.zeros((ROW_COUNT, COLUMN_COUNT))
17+
return board
18+
19+
20+
def drop_piece(board, row, col, piece):
21+
board[row][col] = piece
22+
23+
24+
def is_valid_location(board, col):
25+
return board[ROW_COUNT - 1][col] == 0
26+
27+
28+
def get_next_open_row(board, col):
29+
for r in range(ROW_COUNT):
30+
if board[r][col] == 0:
31+
return r
32+
33+
34+
def print_board(board):
35+
print(np.flip(board, 0))
36+
37+
38+
def winning_move(board, piece):
39+
# Check horizontal locations for win
40+
for c in range(COLUMN_COUNT - 3):
41+
for r in range(ROW_COUNT):
42+
if board[r][c] == piece and board[r][c + 1] == piece and board[r][c + 2] == piece and board[r][
43+
c + 3] == piece:
44+
return True
45+
46+
# Check vertical locations for win
47+
for c in range(COLUMN_COUNT):
48+
for r in range(ROW_COUNT - 3):
49+
if board[r][c] == piece and board[r + 1][c] == piece and board[r + 2][c] == piece and board[r + 3][
50+
c] == piece:
51+
return True
52+
53+
# Check positively sloped diaganols
54+
for c in range(COLUMN_COUNT - 3):
55+
for r in range(ROW_COUNT - 3):
56+
if board[r][c] == piece and board[r + 1][c + 1] == piece and board[r + 2][c + 2] == piece and board[r + 3][
57+
c + 3] == piece:
58+
return True
59+
60+
# Check negatively sloped diaganols
61+
for c in range(COLUMN_COUNT - 3):
62+
for r in range(3, ROW_COUNT):
63+
if board[r][c] == piece and board[r - 1][c + 1] == piece and board[r - 2][c + 2] == piece and board[r - 3][
64+
c + 3] == piece:
65+
return True
66+
67+
68+
def draw_board(board):
69+
for c in range(COLUMN_COUNT):
70+
for r in range(ROW_COUNT):
71+
pygame.draw.rect(screen, BLUE, (c * SQUARESIZE, r * SQUARESIZE + SQUARESIZE, SQUARESIZE, SQUARESIZE))
72+
pygame.draw.circle(screen, BLACK, (
73+
int(c * SQUARESIZE + SQUARESIZE / 2), int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)), RADIUS)
74+
75+
for c in range(COLUMN_COUNT):
76+
for r in range(ROW_COUNT):
77+
if board[r][c] == 1:
78+
pygame.draw.circle(screen, RED, (
79+
int(c * SQUARESIZE + SQUARESIZE / 2), height - int(r * SQUARESIZE + SQUARESIZE / 2)), RADIUS)
80+
elif board[r][c] == 2:
81+
pygame.draw.circle(screen, YELLOW, (
82+
int(c * SQUARESIZE + SQUARESIZE / 2), height - int(r * SQUARESIZE + SQUARESIZE / 2)), RADIUS)
83+
pygame.display.update()
84+
85+
86+
board = create_board()
87+
print_board(board)
88+
game_over = False
89+
turn = 0
90+
91+
pygame.init()
92+
93+
SQUARESIZE = 100
94+
95+
width = COLUMN_COUNT * SQUARESIZE
96+
height = (ROW_COUNT + 1) * SQUARESIZE
97+
98+
size = (width, height)
99+
100+
RADIUS = int(SQUARESIZE / 2 - 5)
101+
102+
screen = pygame.display.set_mode(size)
103+
draw_board(board)
104+
pygame.display.update()
105+
106+
myfont = pygame.font.SysFont("monospace", 75)
107+
108+
while not game_over:
109+
110+
for event in pygame.event.get():
111+
if event.type == pygame.QUIT:
112+
sys.exit()
113+
114+
if event.type == pygame.MOUSEMOTION:
115+
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))
116+
posx = event.pos[0]
117+
if turn == 0:
118+
pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE / 2)), RADIUS)
119+
else:
120+
pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE / 2)), RADIUS)
121+
pygame.display.update()
122+
123+
if event.type == pygame.MOUSEBUTTONDOWN:
124+
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))
125+
# print(event.pos)
126+
# Ask for Player 1 Input
127+
if turn == 0:
128+
posx = event.pos[0]
129+
col = int(math.floor(posx / SQUARESIZE))
130+
131+
if is_valid_location(board, col):
132+
row = get_next_open_row(board, col)
133+
drop_piece(board, row, col, 1)
134+
135+
if winning_move(board, 1):
136+
label = myfont.render("Player 1 wins!!", 1, RED)
137+
screen.blit(label, (40, 10))
138+
game_over = True
139+
140+
141+
# # Ask for Player 2 Input
142+
else:
143+
posx = event.pos[0]
144+
col = int(math.floor(posx / SQUARESIZE))
145+
146+
if is_valid_location(board, col):
147+
row = get_next_open_row(board, col)
148+
drop_piece(board, row, col, 2)
149+
150+
if winning_move(board, 2):
151+
label = myfont.render("Player 2 wins!!", 1, YELLOW)
152+
screen.blit(label, (40, 10))
153+
game_over = True
154+
155+
print_board(board)
156+
draw_board(board)
157+
158+
turn += 1
159+
turn = turn % 2
160+
161+
if game_over:
162+
pygame.time.wait(3000)

0 commit comments

Comments
 (0)