Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
Connect_4
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Anurag Kumar
Connect_4
Commits
df3aafc2
You need to sign in or sign up before continuing.
Commit
df3aafc2
authored
Mar 15, 2021
by
Anurag Kumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload new file
parent
11756cb5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
361 additions
and
0 deletions
+361
-0
connect4_2.0.py
connect4_2.0.py
+361
-0
No files found.
connect4_2.0.py
0 → 100644
View file @
df3aafc2
import
numpy
as
np
import
pygame
import
sys
from
tkinter
import
*
from
tkinter
import
messagebox
from
gui
import
*
import
pygame_menu
#Constants
SQUARESIZE
=
70
ROW
=
6
COLUMN
=
7
WIDTH
,
HEIGHT
=
COLUMN
*
SQUARESIZE
,
(
ROW
+
2
)
*
SQUARESIZE
WINDOW
=
pygame
.
display
.
set_mode
((
WIDTH
,
HEIGHT
))
pygame
.
display
.
set_caption
(
"Connect 4"
)
FPS
=
60
#Global variable
player1_score
=
0
player2_score
=
0
player1_name
=
'Player1'
player2_name
=
''
difficulty_level
=
1
# score location of player-1 and player-2
SCORE_PLAYER1_X
=
SQUARESIZE
/
4
SCORE_PLAYER1_Y
=
SQUARESIZE
/
3
SCORE_PLAYER2_X
=
WIDTH
-
2
*
SQUARESIZE
SCORE_PLAYER2_Y
=
SQUARESIZE
/
3
# Color used
BLUE
=
(
0
,
0
,
255
)
WHITE
=
(
255
,
255
,
255
)
BLACK
=
(
0
,
0
,
0
)
RED
=
(
255
,
0
,
0
)
YELLOW
=
(
255
,
255
,
0
)
LIGHTWOOD
=
(
249
,
248
,
246
)
WOOD
=
(
202
,
164
,
114
)
DARKWOOD
=
(
184
,
134
,
69
)
BLOODRED
=
(
238
,
75
,
43
)
#flags
player1_winner_flag
=
False
player2_winner_flag
=
False
gameExit
=
False
def
playing_board
():
'''
:return: 2D matrix of ROW*COLUMN size
'''
return
(
np
.
zeros
((
ROW
,
COLUMN
)))
def
is_valid_location
(
board
,
column
):
'''
:param board: hold playing_board() result
:param column: represent number of column
:return: location is valid or not, It return true if location is valid otherwise return flase
'''
return
board
[
5
][
column
]
==
0
# This function will return the row number where Square will fill after clicking
def
get_block_filling_location
(
board
,
column
):
'''
:param board: hold the playing_board() matrix
:param column: represent the number of column
:return: It return the row number of correspoing column where mouse is clicked
'''
for
row
in
range
(
ROW
):
if
(
board
[
row
][
column
]
==
0
):
return
row
def
winner
(
board
,
squareBlock
):
'''
:param board: hold the playing_board() matrix
:param squareBlock: squareBlock = 1(Blue) for player-1 , and squareBlock = 2(Yellow) for player-2.
:return: True if 4 same color consecutive block is present in row or column or diagonally
'''
# horizontal checkup
for
j
in
range
(
COLUMN
-
3
):
for
i
in
range
(
ROW
):
if
(
board
[
i
][
j
]
==
squareBlock
and
board
[
i
][
j
+
1
]
==
squareBlock
and
board
[
i
][
j
+
2
]
==
squareBlock
and
board
[
i
][
j
+
3
]
==
squareBlock
):
return
True
# vertical checkup
for
j
in
range
(
COLUMN
):
for
i
in
range
(
ROW
-
3
):
if
(
board
[
i
][
j
]
==
squareBlock
and
board
[
i
+
1
][
j
]
==
squareBlock
and
board
[
i
+
2
][
j
]
==
squareBlock
and
board
[
i
+
3
][
j
]
==
squareBlock
):
return
True
# Diagonal checkup
for
j
in
range
(
COLUMN
-
3
):
for
i
in
range
(
ROW
-
3
):
if
(
board
[
i
][
j
]
==
squareBlock
and
board
[
i
+
1
][
j
+
1
]
==
squareBlock
and
board
[
i
+
2
][
j
+
2
]
==
squareBlock
and
board
[
i
+
3
][
j
+
3
]
==
squareBlock
):
return
True
for
j
in
range
(
COLUMN
-
3
):
for
i
in
range
(
3
,
ROW
):
if
(
board
[
i
][
j
]
==
squareBlock
and
board
[
i
-
1
][
j
+
1
]
==
squareBlock
and
board
[
i
-
2
][
j
+
2
]
==
squareBlock
and
board
[
i
-
3
][
j
+
3
]
==
squareBlock
):
return
True
def
playerOneActionOnMouseClick
(
board
,
event
):
'''
:param board: hold the playing_board() matrix
:param event: MOUSEBUTTONDOWN event
:Functinality: represent what happen when player one turn and mouse clicked
'''
global
player1_score
# player 1 score
global
player1_winner_flag
#player 1 winning flag, represent True if player one won
global
gameExit
mouse_pos
=
event
.
pos
[
0
]
column
=
int
(
mouse_pos
/
SQUARESIZE
)
if
is_valid_location
(
board
,
column
):
row
=
get_block_filling_location
(
board
,
column
)
board
[
row
][
column
]
=
1
print
(
board
)
if
winner
(
board
,
1
):
gameExit
=
True
print
(
board
)
print
(
"Player 1 wins"
)
player1_winner_flag
=
True
player1_score
=
player1_score
+
1
#
def
playerTwoActionOnMouseClick
(
board
,
event
):
'''
:param board: hold the playing_board() matrix
:param event: MOUSEBUTTONDOWN event
This function defined what happen when player Two turn is there and mouse clicked
'''
global
player2_score
global
player2_winner_flag
global
gameExit
mouse_pos
=
event
.
pos
[
0
]
column
=
int
(
mouse_pos
/
SQUARESIZE
)
if
is_valid_location
(
board
,
column
):
row
=
get_block_filling_location
(
board
,
column
)
board
[
row
][
column
]
=
2
print
(
board
)
if
winner
(
board
,
2
):
gameExit
=
True
print
(
board
)
print
(
"Player 2 wins"
)
player2_winner_flag
=
True
player2_score
=
player2_score
+
1
#This function defined what happen when player one won the match
def
playerOneWon
():
global
gameExit
global
player1_winner_flag
global
player1_name
global
difficulty_level
result
=
messagebox
.
askyesno
(
"Restart"
,
"{} Won !!!!
\n
Do you want to setPlayerMode again?"
.
format
(
player1_name
))
if
result
:
gameExit
=
False
player1_winner_flag
=
False
if
difficulty_level
==
1
:
manualPlayer
()
else
:
pass
#Add ComputerPlayer() here for difficulty_level = 2
else
:
menu
()
#This function defined what happend when player Two won the match
def
playerTwoWon
():
global
gameExit
global
player2_winner_flag
global
player2_name
global
difficulty_level
result
=
messagebox
.
askyesno
(
"Restart"
,
"{} Won !!!!
\n
Do you want to setPlayerMode again?"
.
format
(
player2_name
))
if
result
:
gameExit
=
False
player2_winner_flag
=
False
if
difficulty_level
==
1
:
manualPlayer
()
else
:
pass
#Add ComputerPlayer() here
else
:
menu
()
def
manualPlayer
():
'''
This function is for manual player
'''
global
player1_score
#score of 1st Player
global
player2_score
#score of 2nd Player
global
player1_name
#Name of 1st player
global
player2_name
#Name of 2nd player
global
player1_winner_flag
#Player1 won then this flag set True, Otherwise False
global
player2_winner_flag
#Player2 won then this flag set True, Otherwise False
global
gameExit
#This flag set True when we want to exit from game
board
=
playing_board
()
#return 2D matrix containing 0's, size is ROW * COLUMN
player2_name
=
'Player2'
#For manual player Player2 name is 'Player2', for computer set Player2 name is 'computer'
player_turn
=
0
#Indicate the turn of player, for player1 player_turn = 0, and for player2 player_turn = 1
clock
=
pygame
.
time
.
Clock
()
gui
=
Gui
(
WINDOW
)
# created object of Gui class defined in gui.py file
gui
.
draw_window
(
board
)
# calling draw_window method defined in gui.py file
gui
.
show_score
(
SCORE_PLAYER1_X
,
SCORE_PLAYER1_Y
,
SCORE_PLAYER2_X
,
SCORE_PLAYER2_Y
,
player1_score
,
player2_score
,
player1_name
,
player2_name
)
while
not
gameExit
:
#Infinite loop, till player want to exit
clock
.
tick
(
FPS
)
#For synchronization purpose, Bcoz different computer have different speed
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
#When player click on 'X' button
Tk
()
.
wm_withdraw
()
#Freez the pygame window, untill user reply for confirmation window
result
=
messagebox
.
askyesno
(
"Confirmation Window"
,
"Are you sure want to exit?"
)
#Confirmation window
if
result
:
sys
.
exit
()
#Exit from game
if
event
.
type
==
pygame
.
MOUSEMOTION
:
#This even take care of mousemotion
# print(event.pos)
pygame
.
draw
.
rect
(
WINDOW
,
BLACK
,
(
0
,
SQUARESIZE
+
1
,
WIDTH
,
SQUARESIZE
))
#moving Square row (i.e Black row)
mouse_pos
=
event
.
pos
[
0
]
#calculate the X coordinate of mouse motion
if
player_turn
==
0
:
pygame
.
draw
.
rect
(
WINDOW
,
BLUE
,
(
mouse_pos
,
SQUARESIZE
+
1
,
SQUARESIZE
,
SQUARESIZE
-
1
))
#If player1 turn then moving square will be BLUE
else
:
pygame
.
draw
.
rect
(
WINDOW
,
YELLOW
,
(
mouse_pos
,
SQUARESIZE
+
1
,
SQUARESIZE
,
SQUARESIZE
-
1
))
#If player2 turn then moving square will be YELLOW
pygame
.
display
.
update
()
#Update the pygame window
if
event
.
type
==
pygame
.
MOUSEBUTTONDOWN
:
#This even take care of mousemotion
if
player_turn
==
0
:
playerOneActionOnMouseClick
(
board
,
event
)
#This function will take care when player1 click to fill then SQUARE
else
:
playerTwoActionOnMouseClick
(
board
,
event
)
#This function will take care when player1 click to fill then SQUARE
gui
.
draw_window
(
board
)
#Drawing the pygame window board again and again
player_turn
+=
1
#Increase the Player_turn of every turn
player_turn
=
player_turn
%
2
gui
.
show_score
(
SCORE_PLAYER1_X
,
SCORE_PLAYER1_Y
,
SCORE_PLAYER2_X
,
SCORE_PLAYER2_Y
,
player1_score
,
player2_score
,
player1_name
,
player2_name
)
#Update the score
print
(
"Player 1 score: "
+
str
(
player1_score
))
print
(
"Player 2 score: "
+
str
(
player2_score
))
if
gameExit
:
Tk
()
.
wm_withdraw
()
#Freez the pygame window and popup the tkinter confirmation window
if
player1_winner_flag
:
playerOneWon
()
#This function defines what happen when player1 won the match
if
player2_winner_flag
:
playerTwoWon
()
#This function defines what happen when player2 won the match
def
set_difficulty
(
value
,
difficulty
):
'''
:param value: It return value of difficult, like easy, hard
:param difficulty: It return difficulty level, for easy difficulty = 1, for hard difficulty=2
This function will be called after difficulty level set by player from menu
This function will set the difficulty level in global variable difficulty_level
'''
global
difficulty_level
if
difficulty
==
1
:
difficulty_level
=
1
if
difficulty
==
2
:
difficulty_level
=
2
def
setPlayerMode
():
'''
Get the difficulty_level then start game according to difficulty level set by player
'''
global
difficulty_level
#reading difficulty level, byDefault Manual means difficulty_level = 1
if
difficulty_level
==
1
:
manualPlayer
()
elif
difficulty_level
==
2
:
Tk
()
.
wm_withdraw
()
result
=
messagebox
.
showinfo
(
"showinfo"
,
"Playing with computer not implemented"
)
def
menu
():
'''
This function is for menu
'''
global
player1_name
global
player2_name
menu
=
pygame_menu
.
Menu
(
250
,
400
,
'Welcome'
,
theme
=
pygame_menu
.
themes
.
THEME_BLUE
)
menu
.
add_selector
(
'Difficulty :'
,
[(
'Manual '
,
1
),(
'Computer'
,
2
)],
onchange
=
set_difficulty
)
menu
.
add_button
(
'Play'
,
setPlayerMode
)
menu
.
add_button
(
'Quit'
,
pygame_menu
.
events
.
EXIT
)
menu
.
mainloop
(
WINDOW
)
if
__name__
==
'__main__'
:
pygame
.
init
()
#initialize the pygame
menu
()
#calling menu function
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment