Skip to content

Commit d18e30d

Browse files
Add initial files
1 parent 04a2dfe commit d18e30d

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

TicTacToe-GUI/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Tic Tac Toe
2+
<img width="300" src="https://media.giphy.com/media/ChzovjKPuEiYe8ePih/giphy.gif" />
3+
4+
### How To Play
5+
6+
1. The game is played on a grid that's 3 squares by 3 squares.
7+
2. You are X, your friend (or the computer in this case) is O.
8+
3. Players take turns putting their marks in empty squares.
9+
4. The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.
10+
5. When all 9 squares are full, the game is over.
11+
12+
### How The Game Works
13+
14+
1. Our Tic Tac Toe is programmed in other to allow two users or players to play the game in the same time.
15+
2. It is GUI game & gives an instant alert when players wins or losses or draw the game basically tells us to restart.
16+
17+
### Details
18+
Libraries Required
19+
20+
| Library | Command To Install | Documentation |
21+
| :------------ | :------------ | :------------ |
22+
| Pygame | `pip install pygame` | https://pypi.org/project/pygame/ |
23+
| Sys | `pip install os-sys` | https://pypi.org/project/os-sys/ |
24+
25+
### Steps Required To Make This Awesome Game Live:
26+
- First of all, create a folder named any in your PC & drag it to your code editor.
27+
- Secondly, open your command prompt(CMD) & install the Pygame package by typing pip install pygame command and os-sys package by typing pip install os-sys .(I used windows10 OS)
28+
- Thirdly, make a file called TicTacToeGame.py . It will you to store all your code needed for the game.
29+
- Set your codes which are given below to your respective files.
30+
- Lastly just run this code and enjoy the game.
31+
32+
### How It Will Look
33+
<a href="https://imgbb.com/"><img width="350" src="https://i.ibb.co/0ZKQrW4/download.png" alt="download" border="0"></a>
34+
35+
### Author : [Lakhan Kumawat](https://github.com/Lakhankumawat)
36+
37+

TicTacToe-GUI/TicTacToe.py

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Tic Toe Using pygame , numpy and sys with Graphical User Interface
4+
5+
"""
6+
7+
import pygame, sys
8+
from pygame.locals import *
9+
import numpy as np
10+
11+
#------
12+
#constants
13+
#-------
14+
width=800
15+
height=800
16+
#row and columns
17+
board_rows=3
18+
board_columns=3
19+
cross_width=25
20+
21+
square_size=width//board_columns
22+
#colors in RGB format
23+
line_Width=15
24+
red=(255, 0, 0)
25+
bg_color=(28, 170, 156)
26+
line_color=(23, 145, 135)
27+
circle_color=(239,231,200)
28+
cross_color=(66,66,66)
29+
30+
space=square_size//4
31+
#circle
32+
circle_radius=square_size//3
33+
circle_width=14
34+
35+
pygame.init()
36+
screen = pygame.display.set_mode((height,width))
37+
pygame.display.set_caption('Tic Tac Toe!')
38+
screen.fill( bg_color )
39+
40+
#color to display restart
41+
white = (255, 255, 255)
42+
green = (0, 255, 0)
43+
blue = (0, 0, 128)
44+
45+
font = pygame.font.Font('freesansbold.ttf', 25)
46+
47+
# create a text suface object,
48+
# on which text is drawn on it.
49+
text = font.render('Press R to restart', True, green, blue)
50+
51+
# create a rectangular object for the
52+
# text surface object
53+
textRect = text.get_rect()
54+
textRect.center = (width-150, height-13)
55+
56+
board=np.zeros( (board_rows,board_columns))
57+
#print(board)
58+
#pygame.draw.line( screen ,red ,(10,10),(300,300),10)
59+
60+
def draw_figures():
61+
for row in range(board_rows):
62+
for col in range(board_columns):
63+
if board[row][col]==1:
64+
pygame.draw.circle(screen,circle_color,( int(col*square_size + square_size//2 ),int(row*square_size +square_size//2)),circle_radius,circle_width)
65+
elif board[row][col]==2:
66+
pygame.draw.line( screen ,cross_color ,(col*square_size + space,row*square_size +square_size -space),(col*square_size+square_size -space,row*square_size +space),cross_width)
67+
pygame.draw.line( screen ,cross_color ,(col*square_size +space,row*square_size +space),(col*square_size +square_size -space,row*square_size +square_size -space),cross_width)
68+
69+
def draw_lines():
70+
pygame.draw.line( screen ,line_color ,(0,square_size),(width,square_size),line_Width)
71+
#2nd horizontal line
72+
pygame.draw.line( screen ,line_color ,(0,2*square_size),(width,2*square_size),line_Width)
73+
#1st verticle
74+
pygame.draw.line( screen ,line_color ,(square_size,0),(square_size,height),line_Width)
75+
#2nd verticle
76+
pygame.draw.line( screen ,line_color ,(2*square_size,0),(2*square_size,height),line_Width)
77+
78+
79+
#To mark which square player has chosen
80+
def mark_square(row,col,player):
81+
board[row][col]=player
82+
83+
84+
# TO check the availablity of a square
85+
def available_square(row,col):
86+
return board[row][col]==0
87+
88+
#check board full or not
89+
def is_board_full():
90+
for row in range(board_rows):
91+
for col in range(board_columns):
92+
if board[row][col]==0:
93+
return False
94+
else:
95+
return True
96+
97+
98+
def check_win(player):
99+
#check verticle win
100+
for col in range(board_columns):
101+
if board[0][col]==player and board[1][col]==player and board[2][col]==player:
102+
draw_vertical_winning_line(col, player)
103+
return True
104+
#check Horizontal win
105+
for row in range(board_rows):
106+
if board[row][0]==player and board[row][1]==player and board[row][2]==player:
107+
draw_horizontal_winning_line(row, player)
108+
return True
109+
#check for asc win
110+
if board[2][0]==player and board[1][1]==player and board[0][2]==player:
111+
draw_asc_diagonal(player)
112+
return True
113+
if board[0][0]==player and board[1][1]==player and board[2][2]==player:
114+
draw_des_diagonal(player)
115+
return True
116+
117+
def draw_horizontal_winning_line(row,player):
118+
posY=row*square_size +square_size//2
119+
120+
if(player==1):
121+
color=circle_color
122+
else:
123+
color=cross_color
124+
125+
pygame.draw.line(screen, color, (15,posY), (width-15,posY),15)
126+
127+
128+
129+
def draw_vertical_winning_line(col,player):
130+
posX=col*square_size +square_size//2
131+
if(player==1):
132+
color=circle_color
133+
else:
134+
color=cross_color
135+
136+
pygame.draw.line(screen, color, (posX,15), (posX,width-15),15)
137+
def draw_asc_diagonal(player):
138+
if(player==1):
139+
color=circle_color
140+
else:
141+
color=cross_color
142+
143+
pygame.draw.line(screen,color,(15,height-15),(width-15,15),15)
144+
def draw_des_diagonal(player):
145+
if(player==1):
146+
color=circle_color
147+
else:
148+
color=cross_color
149+
150+
pygame.draw.line(screen,color,(15,15),(width-15,height-15),15)
151+
152+
153+
154+
def restart():
155+
screen.fill(bg_color)
156+
draw_lines()
157+
player = 1
158+
159+
for row in range(board_rows):
160+
for col in range(board_columns):
161+
board[row][col]=0
162+
163+
164+
draw_lines()
165+
#player
166+
player=1
167+
game_over=False
168+
169+
while True: # main game loop
170+
for event in pygame.event.get(): #constantly looks for the event
171+
if event.type == pygame.QUIT: #if user clicks exit pygame.QUIT and sys exits
172+
pygame.quit()
173+
sys.exit()
174+
175+
if event.type==pygame.MOUSEBUTTONDOWN and not game_over:
176+
mouseX= event.pos[0] #x
177+
mouseY= event.pos[1] #y
178+
179+
clicked_row=int(mouseY // square_size)
180+
181+
clicked_column=int(mouseX // square_size)
182+
183+
if available_square(clicked_row, clicked_column):
184+
mark_square(clicked_row,clicked_column, player)
185+
if(check_win(player)):
186+
game_over=True
187+
screen.blit(text, textRect)
188+
player=player%2 +1
189+
190+
191+
192+
draw_figures()
193+
#to restart the game
194+
if event.type==pygame.KEYDOWN:
195+
if event.key==pygame.K_r:
196+
restart()
197+
game_over=False
198+
#print(board)
199+
pygame.display.update()
200+

0 commit comments

Comments
 (0)