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 222d353

Browse files
Merge pull request avinashkranjan#634 from syamala27/$connect-4-game
$connect 4 game
2 parents 5bffac6 + 12ce7b5 commit 222d353

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

‎Connect-4 game/Connect-4.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
#CHECK NEGATIVE DIAGONALS
51+
for c in range(COLUMN_COUNT-3):
52+
for r in range(3,ROW_COUNT):
53+
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:
54+
return True
55+
56+
def draw_board(board):
57+
for c in range(COLUMN_COUNT):
58+
for r in range(ROW_COUNT):
59+
pygame.draw.rect(screen,BLUE,(c*SQUARESIZE,r*SQUARESIZE+SQUARESIZE,SQUARESIZE,SQUARESIZE))
60+
pygame.draw.circle(screen,BLACK,(int(c*SQUARESIZE+SQUARESIZE/2),int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)),RADIUS)
61+
62+
for c in range(COLUMN_COUNT):
63+
for r in range(ROW_COUNT):
64+
if board[r][c]==1:
65+
pygame.draw.circle(screen,RED,(int(c*SQUARESIZE+SQUARESIZE/2),height-int(r*SQUARESIZE+SQUARESIZE/2)),RADIUS)
66+
67+
elif board[r][c]==2:
68+
pygame.draw.circle(screen,YELLOW,(int(c*SQUARESIZE+SQUARESIZE/2),height-int(r*SQUARESIZE+SQUARESIZE/2)),RADIUS)
69+
70+
pygame.display.update()
71+
72+
73+
board=create_board()
74+
print_board(board)
75+
game_over=False
76+
turn=0
77+
78+
pygame.init()
79+
80+
SQUARESIZE=100
81+
82+
width=COLUMN_COUNT*SQUARESIZE
83+
height=(ROW_COUNT+1)*SQUARESIZE
84+
85+
size=(width,height)
86+
87+
RADIUS=int(SQUARESIZE/2 -5)
88+
89+
pygame.display.set_caption("CONNECT FOUR GAME")
90+
screen=pygame.display.set_mode(size)
91+
draw_board(board)
92+
pygame.display.update()
93+
94+
myfont=pygame.font.SysFont("monosoace",75)
95+
96+
while not game_over:
97+
for event in pygame.event.get():
98+
if event.type==pygame.QUIT:
99+
sys.exit()
100+
101+
if event.type==pygame.MOUSEMOTION:
102+
pygame.draw.rect(screen,BLACK,(0,0,width,SQUARESIZE))
103+
posx=event.pos[0]
104+
if turn==0:
105+
pygame.draw.circle(screen,RED,(posx,int(SQUARESIZE/2)),RADIUS)
106+
else:
107+
pygame.draw.circle(screen,YELLOW,(posx,int(SQUARESIZE/2)),RADIUS)
108+
pygame.display.update()
109+
110+
if event.type==pygame.MOUSEBUTTONDOWN:
111+
#print(event.pos)
112+
pygame.draw.rect(screen,BLACK,(0,0,width,SQUARESIZE))
113+
114+
if turn==0:
115+
posx=event.pos[0]
116+
col=int(math.floor(posx/SQUARESIZE))
117+
118+
if is_valid_location(board,col):
119+
row=get_next_open_row(board,col)
120+
drop_piece(board,row,col,1)
121+
122+
123+
if winning_move(board,1):
124+
#print("Player 1 wins CONGRATULATIONS!!")
125+
label=myfont.render("Player 1 wins!!",1,RED)
126+
screen.blit(label,(40,10))
127+
game_over=True
128+
129+
else:
130+
posx=event.pos[0]
131+
col=int(math.floor(posx/SQUARESIZE))
132+
133+
if is_valid_location(board,col):
134+
row=get_next_open_row(board,col)
135+
drop_piece(board,row,col,2)
136+
137+
if winning_move(board,2):
138+
#print("Player 2 wins CONGRATULATIONS!!")
139+
label=myfont.render("Player 2 wins!!",1,YELLOW)
140+
screen.blit(label,(40,10))
141+
game_over=True
142+
143+
print_board(board)
144+
draw_board(board)
145+
146+
turn+=1
147+
turn=turn%2
148+
149+
if game_over:
150+
pygame.time.wait(3000)

‎Connect-4 game/READme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This is a client side game.
2+
where 2 players can play one after the other.
3+
4+
Game Rules:
5+
1. Get ready :)
6+
2. Four consecutive balls should match of the same colour horizontally, vertically or diagonally ( either of both) just like tic-tat-toe.
7+
3. That's it now u r ready to play the game :)
8+
New ideas are welcome always!!

0 commit comments

Comments
(0)

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