Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c71bfed

Browse files
Add files via upload
This is a client side game. where 2 players can play one after the other. Game Rules: 1. Get ready :) 2. Four consecutive balls should match of the same colour horizontally, vertically or diagonally ( either of both) just like tic-tat-toe. 3. That's it now u r ready to play the game :) New ideas are welcome always!!
1 parent bb6442d commit c71bfed

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

‎Connect-4 game/Connect-4.py

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /