-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
161 lines (132 loc) · 4.83 KB
/
tictactoe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pygame,sys
import numpy as np
pygame.init()
width=600
height=600
line_width=15
board_rows=3
board_cols=3
circle_radius=60
circle_width=15
cross_width=25
space=55
# colors are in rgb format : red blue green
red=(255,0,0)
bg_color=(28,170,156)
line_color=(23,145,135)
circle_color=(239,231,200)
cross_color=(66,66,66)
screen= pygame.display.set_mode((width,height) )
pygame.display.set_caption('TIC TAC TOE')
screen.fill(bg_color)
#console board
board=np.zeros((board_rows,board_cols))
def draw_lines():
#1st horizontal
pygame.draw.line(screen ,line_color,(0,200),(600,200),line_width)
#2nd horizontal
pygame.draw.line(screen ,line_color,(0,400),(600,400),line_width)
#1st vertical
pygame.draw.line(screen ,line_color,(200,0),(200,600),line_width)
#2nd vertical
pygame.draw.line(screen ,line_color ,(400,0),(400,600),line_width)
def draw_figures():
for rows in range(board_rows):
for cols in range(board_cols):
if board[rows][cols]==1:
pygame.draw.circle(screen,circle_color,(int(cols * 200 + 100),int (rows * 200 + 100)),circle_radius,circle_width)
elif board[rows][cols]==2:
pygame.draw.line(screen ,cross_color ,(cols * 200 + space ,rows * 200 + 200 - space ),(cols * 200 + 200 - space ,rows * 200 + space ),cross_width)
pygame.draw.line(screen ,cross_color ,(cols * 200 + space ,rows * 200 + space ),(cols * 200 + 200 - space ,rows * 200 + 200 - space ),cross_width)
def mark_squares(row,col,player):
board[row][col]=player
def available_square(row,col):
return board[row][col]==0
def is_board_full():
for row in range(board_rows):
for col in range(board_cols):
if board[row][col]==0:
return False
return True
def check_win(player):
#vertical win check
for col in range(board_cols):
if board[0][col]==player and board[1][col]==player and board[2][col]==player:
draw_vertical_winning_line(col,player)
return True
# horizontal win check
for row in range(board_rows):
if board[row][0]==player and board[row][1]==player and board[row][2]==player:
draw_horizontal_winning_line(row,player)
return True
#ascending win check
if board[2][0]==player and board[1][1]==player and board[0][2]==player:
draw_ascending_winning_line(player)
return True
# descending win check
if board[0][0]==player and board[1][1]==player and board[2][2]==player:
draw_descending_winning_line(player)
return True
return False
def draw_vertical_winning_line(col,player):
pos_x=col * 200 + 100
if player==1:
color=circle_color
elif player==2:
color=cross_color
pygame.draw.line(screen ,color,(pos_x,15),(pos_x,height-15),15)
def draw_horizontal_winning_line(row,player):
pos_y=row * 200 + 100
if player==1:
color=circle_color
elif player==2:
color=cross_color
pygame.draw.line(screen ,color,(15,pos_y),(width - 15 , 15 ),15)
def draw_ascending_winning_line(player):
if player==1:
color=circle_color
elif player==2:
color=cross_color
pygame.draw.line(screen ,color,(15,height - 15 ),(width - 15 , 15 ),15)
def draw_descending_winning_line(player):
if player==1:
color=circle_color
elif player==2:
color=cross_color
pygame.draw.line(screen ,color,(15,15),(width - 15 ,height - 15 ),15)
def restart():
screen.fill(bg_color)
draw_lines()
for row in range(board_rows):
for col in range(board_cols):
board[row][col]=0
draw_lines()
player=1
game_over=False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type==pygame.MOUSEBUTTONDOWN and not game_over:
mouse_x=event.pos[0] # x co-ordinate
mouse_y=event.pos[1] # y co-ordiante
clicked_row=int(mouse_y//200)
clicked_col=int(mouse_x//200)
if available_square(clicked_row,clicked_col):
if player == 1 :
mark_squares(clicked_row,clicked_col,1)
if check_win(player):
game_over=True
player=2
elif player ==2:
mark_squares(clicked_row,clicked_col,2)
if check_win(player):
game_over=True
player=1
draw_figures()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
restart()
player=1
game_over=False
pygame.display.update()