diff --git a/Scripts/Miscellaneous/Connect4Game/README.md b/Scripts/Miscellaneous/Connect4Game/README.md new file mode 100644 index 000000000..0725d809a --- /dev/null +++ b/Scripts/Miscellaneous/Connect4Game/README.md @@ -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 diff --git a/Scripts/Miscellaneous/Connect4Game/addNames.png b/Scripts/Miscellaneous/Connect4Game/addNames.png new file mode 100644 index 000000000..17b20010f Binary files /dev/null and b/Scripts/Miscellaneous/Connect4Game/addNames.png differ diff --git a/Scripts/Miscellaneous/Connect4Game/connect4game.py b/Scripts/Miscellaneous/Connect4Game/connect4game.py new file mode 100644 index 000000000..7b3a037b3 --- /dev/null +++ b/Scripts/Miscellaneous/Connect4Game/connect4game.py @@ -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') diff --git a/Scripts/Miscellaneous/Connect4Game/gameplay.png b/Scripts/Miscellaneous/Connect4Game/gameplay.png new file mode 100644 index 000000000..24597f260 Binary files /dev/null and b/Scripts/Miscellaneous/Connect4Game/gameplay.png differ diff --git a/Scripts/Miscellaneous/Connect4Game/requirements.txt b/Scripts/Miscellaneous/Connect4Game/requirements.txt new file mode 100644 index 000000000..014aeb86f --- /dev/null +++ b/Scripts/Miscellaneous/Connect4Game/requirements.txt @@ -0,0 +1,2 @@ +numpy==1.19.2 +pygame==1.9.6 \ No newline at end of file