Skip to content

Commit 0f3a984

Browse files
FinMacDovprateekiiest
authored andcommitted
Connect four (#135)
* Issue #124. Not change the contents of the code, but I have put in the pep8 style. * added extra space to please pep8 bot. * Issue #41. Adding 2 player connect four game. * Issue #41. Added a simplistic version of connect 4 that doesnt use pygames. * Added readme to give credit to the tutorial I followed to make the game. * Requiremts to run connect 4 with pygame. * Fixed pep8 issues * Fixed pep8 issues * Update simple_connect_4.py
1 parent c53cc05 commit 0f3a984

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This connect four game was created using the following tutorial:
2+
3+
https://www.youtube.com/watch?v=UYgyRArKDEs&list=PLFCB5Dp81iNV_inzM-R9AKkZZlePCZdtV
+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import numpy as np
2+
import math
3+
import pygame
4+
import sys
5+
6+
NB_COL = 7
7+
NB_ROW = 6
8+
BLUE = (0, 0, 255) # RGB value
9+
BLACK = (0, 0, 0)
10+
RED = (255, 0, 0)
11+
YELLOW = (255, 255, 0)
12+
13+
14+
def make_board():
15+
board = np.zeros((NB_ROW, NB_COL))
16+
return board
17+
18+
19+
def drop_piece(board, row, col, piece):
20+
board[row][col] = piece
21+
22+
23+
def is_valid_location(board, col):
24+
return board[NB_ROW-1][col] == 0
25+
26+
27+
def get_next_open_row(board, col):
28+
for r in range(NB_ROW):
29+
if board[r][col] == 0:
30+
return r
31+
32+
33+
def print_board(board):
34+
print(np.flip(board, 0))
35+
36+
37+
def winning_move(baord, piece):
38+
# Check hozizontal locations for win
39+
for c in range(NB_COL-3):
40+
for r in range(NB_ROW):
41+
if (
42+
board[r][c] == piece and board[r][c+1] == piece and
43+
board[r][c+2] == piece and board[r][c+3] == piece
44+
):
45+
return True
46+
# Check verticle locations for win
47+
for c in range(NB_COL):
48+
for r in range(NB_ROW-3):
49+
if (
50+
board[r][c] == piece and board[r+1][c] == piece and
51+
board[r+2][c] == piece and board[r+3][c] == piece
52+
):
53+
return True
54+
# check for positively sloped diags
55+
for c in range(NB_COL-3):
56+
for r in range(NB_ROW-3):
57+
if(
58+
board[r][c] == piece and board[r+1][c+1] == piece and
59+
board[r+2][c+2] == piece and board[r+3][c+3] == piece
60+
):
61+
return True
62+
# check for negitively sloped diags
63+
for c in range(NB_COL-3):
64+
for r in range(3, NB_ROW):
65+
if(
66+
board[r][c] == piece and board[r-1][c+1] == piece and
67+
board[r-2][c+2] == piece and board[r-3][c+3] == piece
68+
):
69+
return True
70+
71+
72+
def draw_board(board):
73+
for c in range(NB_COL):
74+
for r in range(NB_ROW):
75+
pygame.draw.rect(screen, BLUE,
76+
(c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE,
77+
SQUARESIZE, SQUARESIZE))
78+
pygame.draw.circle(screen, BLACK,
79+
(int(c*SQUARESIZE+SQUARESIZE/2),
80+
int(r*SQUARESIZE+1.5*SQUARESIZE)), RADIUS)
81+
82+
for c in range(NB_COL):
83+
for r in range(NB_ROW):
84+
if board[r][c] == 1:
85+
pygame.draw.circle(screen, RED,
86+
(int(c*SQUARESIZE+SQUARESIZE/2),
87+
height-int(r*SQUARESIZE+SQUARESIZE/2)),
88+
RADIUS)
89+
elif board[r][c] == 2:
90+
pygame.draw.circle(screen, YELLOW,
91+
(int(c*SQUARESIZE+SQUARESIZE/2),
92+
height-int(r*SQUARESIZE+SQUARESIZE/2)),
93+
RADIUS)
94+
pygame.display.update()
95+
96+
97+
board = make_board()
98+
game_over = False
99+
turn = 0
100+
101+
pygame.init()
102+
103+
SQUARESIZE = 100 # in pixels
104+
105+
width = NB_COL*SQUARESIZE
106+
height = (NB_ROW+1)*SQUARESIZE # extra row for piece
107+
108+
size = (width, height)
109+
RADIUS = int(SQUARESIZE/2-5)
110+
screen = pygame.display.set_mode(size)
111+
draw_board(board)
112+
pygame.display.update()
113+
myfont = pygame.font.SysFont('monospace', 75)
114+
115+
while not game_over:
116+
for event in pygame.event.get():
117+
if event.type == pygame.QUIT:
118+
sys.exit()
119+
if event.type == pygame.MOUSEMOTION:
120+
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))
121+
pos_x = event.pos[0]
122+
if turn == 0:
123+
pygame.draw.circle(screen, RED,
124+
(pos_x, int(SQUARESIZE/2)), RADIUS)
125+
else:
126+
pygame.draw.circle(screen, YELLOW,
127+
(pos_x, int(SQUARESIZE/2)), RADIUS)
128+
pygame.display.update()
129+
if event.type == pygame.MOUSEBUTTONDOWN:
130+
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))
131+
if turn is 0:
132+
pos_x = event.pos[0]
133+
col = int(math.floor(pos_x/SQUARESIZE))
134+
if is_valid_location(board, col):
135+
row = get_next_open_row(board, col)
136+
drop_piece(board, row, col, 1)
137+
# Adds winning condition
138+
if winning_move(board, 1):
139+
label = myfont.render('player 1 wins!', 1, RED)
140+
screen.blit(label, (40, 10))
141+
game_over = True
142+
# ask for player 2 input
143+
else:
144+
pos_x = event.pos[0]
145+
col = int(math.floor(pos_x/SQUARESIZE))
146+
if is_valid_location(board, col):
147+
row = get_next_open_row(board, col)
148+
drop_piece(board, row, col, 2)
149+
if winning_move(board, 2):
150+
label = myfont.render('player 2 wins!', 1, YELLOW)
151+
screen.blit(label, (40, 10))
152+
game_over = True
153+
draw_board(board)
154+
turn += 1
155+
turn = turn % 2 # resets turn to zero to allow player 1 to go
156+
157+
if game_over:
158+
pygame.time.wait(3000)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy==1.13.3
2+
pygame==1.9.3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import numpy as np
2+
# Creates grid to be filled
3+
4+
NB_COL = 7
5+
NB_ROW = 6
6+
7+
8+
def make_board():
9+
board = np.zeros((NB_ROW, NB_COL))
10+
return board
11+
12+
13+
def drop_piece(board, row, col, piece):
14+
board[row][col] = piece
15+
16+
17+
def is_valid_location(board, col):
18+
return board[NB_ROW-1][col] == 0
19+
20+
21+
def get_next_open_row(board, col):
22+
for r in range(NB_ROW):
23+
if board[r][col] == 0:
24+
return r
25+
26+
27+
def print_board(board):
28+
print(np.flip(board, 0))
29+
30+
31+
def winning_move(baord, piece):
32+
# Check hozizontal locations for win
33+
for c in range(NB_COL-3):
34+
for r in range(NB_ROW):
35+
if (
36+
board[r][c] == piece and board[r][c+1] == piece and
37+
board[r][c+2] == piece and board[r][c+3] == piece
38+
):
39+
return True
40+
# Check verticle locations for win
41+
for c in range(NB_COL):
42+
for r in range(NB_ROW-3):
43+
if (
44+
board[r][c] == piece and board[r+1][c] == piece and
45+
board[r+2][c] == piece and board[r+3][c] == piece
46+
):
47+
return True
48+
# check for positively sloped diags
49+
for c in range(NB_COL-3):
50+
for r in range(NB_ROW-3):
51+
if (
52+
board[r][c] == piece and board[r+1][c+1] == piece and
53+
board[r+2][c+2] == piece and board[r+3][c+3] == piece
54+
):
55+
return True
56+
# check for negitively sloped diags
57+
for c in range(NB_COL-3):
58+
for r in range(3, NB_ROW):
59+
if (
60+
board[r][c] == piece and board[r-1][c+1] == piece and
61+
board[r-2][c+2] == piece and board[r-3][c+3] == piece
62+
):
63+
return True
64+
65+
board = make_board()
66+
game_over = False
67+
turn = 0
68+
69+
print_board(board)
70+
while not game_over:
71+
# ask player 1 input
72+
if turn is 0:
73+
col = int(input('Player 1 make your Selection (0-6):'))
74+
# ask for player 2 input
75+
if is_valid_location(board, col):
76+
row = get_next_open_row(board, col)
77+
drop_piece(board, row, col, 1)
78+
# Adds winning condition
79+
if winning_move(board, 1):
80+
print('player 1 wins!')
81+
game_over = True
82+
print_board(board)
83+
break
84+
else:
85+
col = int(input('Player 2 make your Selection (0-6):'))
86+
if is_valid_location(board, col):
87+
row = get_next_open_row(board, col)
88+
drop_piece(board, row, col, 2)
89+
if winning_move(board, 2):
90+
print('player 2 wins!')
91+
game_over = True
92+
print_board(board)
93+
break
94+
95+
print_board(board)
96+
turn += 1
97+
turn = turn % 2 # resets turn to zero to allow player 1 to go

0 commit comments

Comments
 (0)