Skip to content

Instantly share code, notes, and snippets.

@cmaspi
Last active February 12, 2022 17:03
Show Gist options
  • Save cmaspi/53ca75e97a582e51bb35bb14d540dd98 to your computer and use it in GitHub Desktop.
Save cmaspi/53ca75e97a582e51bb35bb14d540dd98 to your computer and use it in GitHub Desktop.
This is a pythom implementation of tic tac toe.
import pygame
import numpy as np
import sys
# initializing
pygame.init()
gameover = False
# board initialization
board = np.zeros((3,3), dtype=int)
# player initialization
player = 1
# mark colors
COLOR = ((0,0,0),(255,255,255))
# functions to draw the separating lines
def bars(screen, BG_COLOR, start, end, width):
pygame.draw.line( screen, BG_COLOR, start, end, width)
# function to draw the circle
def circle(screen, center, radius, width ):
center = tuple(i*200+100 for i in center)
pygame.draw.circle(screen, (255,255,255), center, radius, width)
# function to draw cross
def cross(screen, center, size, width ):
center = tuple(i*200+100 for i in center)
start_1 = tuple(i-j for i,j in zip(center,size))
end_1 = tuple(i+j for i,j in zip(center, size))
# size = tuple(i*j for i,j in zip(size,()))
start_2 = tuple(i-j*k for i,j,k in zip(center,size,(-1,1)))
end_2 = tuple(i+j*k for i,j,k in zip(center, size,(-1,1)))
bars(screen, (0,0,0), start_1, end_1, width)
bars(screen, (0,0,0), start_2, end_2, width)
# function to check if the cell is marked
def isMarked(pos):
if board[pos]:
return True
return False
# function to check if the board is filled
def isFilled(board):
if (board==0).any():
return False
return True
# to check if a player won
def win(board):
if ((board == 1).all(axis=1)).any():
return True
elif ((board == 2).all(axis=1)).any():
return True
elif ((board == 1).all(axis=0)).any():
return True
elif ((board == 2).all(axis=0)).any():
return True
elif (board.diagonal()==1).all():
return True
elif (board[::-1].diagonal()==1).all():
return True
elif (board.diagonal()==2).all():
return True
elif (board[::-1].diagonal()==2).all():
return True
else:
return False
# dimensions of board
DIMENSIONS = (600,600)
# purple
BG_COLOR = (179,102,255)
# dimensions are 600*600 fixed
screen = pygame.display.set_mode(DIMENSIONS)
# caption
pygame.display.set_caption("TIC TAC TOE")
# background color
screen.fill(BG_COLOR)
# making the horizontal and vertical separting bars
bars(screen, tuple(2*i//3 for i in BG_COLOR), (15,200), (595,200), 15)
bars(screen, tuple(2*i//3 for i in BG_COLOR), (15,400), (595,400), 15)
bars(screen, tuple(2*i//3 for i in BG_COLOR), (200,15), (200,585), 15)
bars(screen, tuple(2*i//3 for i in BG_COLOR), (400,15), (400,585), 15)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and not gameover:
pos = event.pos
pos = tuple(i//200 for i in pos)
if not isMarked(pos):
board[pos] = player
if player == 1: circle(screen, pos, 75, 15 )
if player == 2: cross(screen, pos, (60,60),20)
if win(board):
print("player {} won the game".format(player))
gameover = True
if isFilled(board):
print("Tie")
gmaeover = True
player = (player % 2) + 1
pygame.display.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment