Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Connect4 game #153

Merged
merged 5 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Scripts/Miscellaneous/Connect4Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Connect 4 Game

:heart_eyes: **Connect Four** (also known as **Four Up**, **Plot Four**, **Find Four**, **Four in a Row**, **Four in a Line**, **Drop Four**, and **Gravitrips** in the Soviet Union) is a two-player connection board game , in which the players choose a color and then take turns dropping colored discs into a seven-column, six-row vertically suspended grid. The pieces fall straight down, occupying the lowest available space within the column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. Connect Four is a "Solved game". The first player can always win by playing the right moves.

The game was first sold under the _Connect Four_ trademark by "Milton Bradley Company") in February 1974.

## How To Play the Game ?

:exclamation: **NOTE:** Play this game along with your friends

python3 connect4game.py

## ScreenShots

![add names](addNames.png)
![gameplay](gameplay.png)

## Creator

:heart: Suvam Prasad
Binary file added Scripts/Miscellaneous/Connect4Game/addNames.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
179 changes: 179 additions & 0 deletions Scripts/Miscellaneous/Connect4Game/connect4game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import numpy as np
from tkinter import *
import pygame
import sys
import math

BLACK = (33, 33, 33)
GREY = (235, 235, 235)
RED = (255,0,0)
YELLOW = (255,255,0)

ROW_COUNT = 6
COLUMN_COUNT = 7

player_names = []

#adding players
def submit():
player_names.append(player_one.get())
player_names.append(player_two.get())
root.destroy()

#creating connect 4 board 6 X 7 common board size
def create_board():
board = np.zeros((ROW_COUNT,COLUMN_COUNT))
return board

def drop_piece(board, row, col, piece):
board[row][col] = piece

def is_valid_location(board, col):
return board[ROW_COUNT-1][col] == 0

def get_next_open_row(board, col):
for r in range(ROW_COUNT):
if board[r][col] == 0:
return r


def winning_move(board, piece):
# Check horizontal locations for win
for c in range(COLUMN_COUNT-3):
for r in range(ROW_COUNT):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece:
return True

# Check vertical locations for win
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT-3):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece:
return True

# Check positively sloped diaganols
for c in range(COLUMN_COUNT-3):
for r in range(ROW_COUNT-3):
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:
return True

# Check negatively sloped diaganols
for c in range(COLUMN_COUNT-3):
for r in range(3, ROW_COUNT):
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:
return True

def draw_board(board):
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
pygame.draw.rect(screen, BLACK, (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(screen, GREY, (int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)), RADIUS)

for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
if board[r][c] == 1:
pygame.draw.circle(screen, RED, (int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS)
elif board[r][c] == 2:
pygame.draw.circle(screen, YELLOW, (int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS)
pygame.display.update()

try:

root = Tk()
root.resizable(0,0)
player_one = StringVar(master=root)
player_two = StringVar(master=root)
root.geometry("500x300")
root.title("Connect 4 Game")
Label(master=root, text="Connect 4 Game", font=('Poppins', 30, 'normal')).place(x=80, y=20)
Label(master=root, text="Enter player 1 name: ").place(x=120, y=100)
Entry(master=root, textvariable = player_one,font=('calibre',10,'normal')).place(x=250, y=100)
Label(master=root, text="Enter player 2 name: ").place(x=120, y=140)
Entry(master=root, textvariable=player_two, font=('calibre', 10, 'normal')).place(x=250, y=140)
Button(master=root, text="ADD", width=10, command=submit).place(x=210,y=190)
root.mainloop()

if player_names[0] != '' and player_names[1] != '':

board = create_board()

game_over = False
turn = 0

pygame.init()

SQUARESIZE = 100

width = COLUMN_COUNT * SQUARESIZE
height = (ROW_COUNT+1) * SQUARESIZE

size = (width, height)

RADIUS = int(SQUARESIZE/2 - 5)

screen = pygame.display.set_mode(size)
draw_board(board)
pygame.display.update()

myfont = pygame.font.SysFont("monospace", 30)

while not game_over:

for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(screen, GREY, (0,0, width, SQUARESIZE))
posx = event.pos[0]
if turn == 0:
pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE/2)), RADIUS)
else:
pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE/2)), RADIUS)
pygame.display.update()

if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.rect(screen, GREY, (0,0, width, SQUARESIZE))

# Ask for Player 1 Input
if turn == 0:
posx = event.pos[0]
col = int(math.floor(posx/SQUARESIZE))

if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 1)
turn += 1
turn = turn % 2

if winning_move(board, 1):
label = myfont.render(player_names[0] + " wins the game", 1, RED)
screen.blit(label, (40,10))
game_over = True


# # Ask for Player 2 Input
else:
posx = event.pos[0]
col = int(math.floor(posx/SQUARESIZE))

if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 2)
turn += 1
turn = turn % 2

if winning_move(board, 2):
label = myfont.render(player_names[1] + " wins the game", 1, YELLOW)
screen.blit(label, (40,10))
game_over = True


draw_board(board)



if game_over:
pygame.time.wait(3000)

except IndexError as e:
print('Game closed')
Binary file added Scripts/Miscellaneous/Connect4Game/gameplay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Scripts/Miscellaneous/Connect4Game/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.19.2
pygame==1.9.6