-
Notifications
You must be signed in to change notification settings - Fork 1
/
toctacgame.py
234 lines (195 loc) · 8.47 KB
/
toctacgame.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import pygame, sys
import numpy as np
# variable
pygame.init()
WIDTH = 600
HEIGHT = 600
board_row = 3
board_col = 3
background_color = (28, 170, 0)
player=1
# screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('TIC TAC TOE')
screen.fill(background_color)
# board
board = np.zeros((board_row, board_col))
print(board)
# drawboard
def drawborderline(start, end):
pygame.draw.line(screen, (255, 255, 255), start, end, 5)
vertical_line_start = ((0, 200), (0, 400))
vertical_line_end = ((600, 200), (600, 400))
horizontal_line_start = ((200, 0), (400, 0))
horizontal_line_end = ((200, 600), (400, 600))
for start in range(len(vertical_line_start)):
for end in range(len(vertical_line_end)):
if start == end:
drawborderline(vertical_line_start[start], vertical_line_end[end])
for start in range(len(horizontal_line_start)):
for end in range(len(horizontal_line_end)):
if start == end:
drawborderline(horizontal_line_start[start], horizontal_line_end[end])
#Mark when player click
def mark_when_player_click(row,col):
board[row][col]=player
# drawo
def drawo_out(cen, ra):
pygame.draw.circle(screen, (255, 255, 255), cen, ra)
def drawo_in(cen, ra, colr):
pygame.draw.circle(screen, colr, cen, ra)
# drawx
def drawx(start, end):
pygame.draw.line(screen, (0, 0, 0), start, end, 10)
pygame.draw.line(screen, (0, 0, 0), start, end, 10)
#draw after player click
def drawo_after_click(pos):
global player
#drawo in row 1
if pos[0] > 0 and pos[1] > 0 and pos[0] <= 197 and pos[1]<= 197 and board[0][0] == 0:
drawo_out((197 / 2, 197 / 2), 190 / 2)
drawo_in((197 / 2, 197 / 2), 180 / 2, background_color)
mark_when_player_click(0,0)
print(board)
player=2
if pos[0] > 203 and pos[1] > 0 and pos[0] <= 397 and pos[1]<= 197 and board[0][1] == 0:
drawo_out((397 - 190 / 2, 197 / 2), 190 / 2)
drawo_in((397 - 190 / 2, 197 / 2), 180 / 2, background_color)
mark_when_player_click(0,1)
print(board)
player = 2
if pos[0] > 403 and pos[1] > 0 and pos[0] <= 600 and pos[1]<= 197 and board[0][2] == 0:
drawo_out((600 - 197 / 2, 197 / 2), 190 / 2)
drawo_in((600 - 197 / 2, 197 / 2), 180 / 2, background_color)
mark_when_player_click(0,2)
print(board)
player = 2
# #drawo in row 2
if pos[0] > 0 and pos[1] > 203 and pos[0] <= 197 and pos[1]<= 397 and board[1][0] == 0:
drawo_out((197 / 2, 397 - 190 / 2), 190 / 2)
drawo_in((197 / 2, 397 - 190 / 2), 180 / 2, background_color)
mark_when_player_click(1,0)
print(board)
player=2
if pos[0] > 203 and pos[1] > 203 and pos[0] <= 397 and pos[1]<= 397 and board[1][1] == 0:
drawo_out((397 - 190 / 2, 397 - 190 / 2), 190 / 2)
drawo_in((397 - 190 / 2, 397 - 190 / 2), 180 / 2, background_color)
mark_when_player_click(1,1)
print(board)
player = 2
if pos[0] > 403 and pos[1] > 203 and pos[0] <= 600 and pos[1]<= 397 and board[1][2] == 0:
drawo_out((600 - 197 / 2, 397 - 190 / 2), 190 / 2)
drawo_in((600 - 197 / 2, 397 - 190 / 2), 180 / 2, background_color)
mark_when_player_click(1,2)
print(board)
player = 2
# #drawo in row 3
if pos[0] > 0 and pos[1] > 403 and pos[0] <= 197 and pos[1]<= 600 and board[2][0] == 0:
drawo_out((197 / 2, 600 - 197 / 2), 190 / 2)
drawo_in((197 / 2, 600 - 197 / 2), 180 / 2, background_color)
mark_when_player_click(2,0)
print(board)
player=2
if pos[0] > 203 and pos[1] > 403 and pos[0] <= 397 and pos[1]<= 600 and board[2][1] == 0:
drawo_out((397 - 190 / 2, 600 - 197 / 2), 190 / 2)
drawo_in((397 - 190 / 2, 600 - 197 / 2), 180 / 2, background_color)
mark_when_player_click(2,1)
print(board)
player = 2
if pos[0] > 403 and pos[1] > 403 and pos[0] <= 600 and pos[1]<= 600 and board[2][2] == 0:
drawo_out((600 - 197 / 2, 600 - 197 / 2), 190 / 2)
drawo_in((600 - 197 / 2, 600 - 197 / 2), 180 / 2, background_color)
mark_when_player_click(2,2)
print(board)
player = 2
def drawx_after_click(pos):
global player
#drawo in row 1
if pos[0] > 0 and pos[1] > 0 and pos[0] <= 197 and pos[1]<= 197 and board[0][0] == 0:
drawx((10,10), (197-10,197-10))
drawx((10,197-10), (197 - 10, 10))
mark_when_player_click(0,0)
print(board)
player=1
if pos[0] > 203 and pos[1] > 0 and pos[0] <= 397 and pos[1]<= 197 and board[0][1] == 0:
drawx((203+10, 0+10), (397 - 10, 197 - 10))
drawx((203+10, 197 - 10), (397 - 10, 0+10))
mark_when_player_click(0,1)
print(board)
player = 1
if pos[0] > 403 and pos[1] > 0 and pos[0] <= 600 and pos[1]<= 197 and board[0][2] == 0:
drawx((403 + 10, 0 + 10), (600 - 10, 197 - 10))
drawx((403 + 10, 197 - 10), (600 - 10, 0 + 10))
mark_when_player_click(0,2)
print(board)
player = 1
# #drawo in row 2
if pos[0] > 0 and pos[1] > 203 and pos[0] <= 197 and pos[1]<= 397 and board[1][0] == 0:
drawx((0 + 10, 203 + 10), (197 - 10, 397 - 10))
drawx((0 + 10, 397 - 10), (197 - 10, 203 + 10))
mark_when_player_click(1,0)
print(board)
player=1
if pos[0] > 203 and pos[1] > 203 and pos[0] <= 397 and pos[1]<= 397 and board[1][1] == 0:
drawx((203 + 10, 203 + 10), (397 - 10, 397 - 10))
drawx((203 + 10, 397 - 10), (397 - 10, 203 + 10))
mark_when_player_click(1,1)
print(board)
player = 1
if pos[0] > 403 and pos[1] > 203 and pos[0] <= 600 and pos[1]<= 397 and board[1][2] == 0:
drawx((403 + 10, 203 + 10), (600 - 10, 397 - 10))
drawx((403 + 10, 397 - 10), (600 - 10, 203 + 10))
mark_when_player_click(1,2)
print(board)
player = 1
# #drawo in row 3
if pos[0] > 0 and pos[1] > 403 and pos[0] <= 197 and pos[1]<= 600 and board[2][0] == 0:
drawx((0 + 10, 403 + 10), (197 - 10, 600 - 10))
drawx((0 + 10, 600 - 10), (197 - 10, 403 + 10))
mark_when_player_click(2,0)
print(board)
player=1
if pos[0] > 203 and pos[1] > 403 and pos[0] <= 397 and pos[1]<= 600 and board[2][1] == 0:
drawx((203 + 10, 403 + 10), (397 - 10, 600 - 10))
drawx((203 + 10, 600 - 10), (397 - 10, 403 + 10))
mark_when_player_click(2,1)
print(board)
player = 1
if pos[0] > 403 and pos[1] > 403 and pos[0] <= 600 and pos[1]<= 600 and board[2][2] == 0:
drawx((403 + 10, 403 + 10), (600 - 10, 600 - 10))
drawx((403 + 10, 600 - 10), (600 - 10, 403 + 10))
mark_when_player_click(2,2)
print(board)
player = 1
# run_the_game
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.ext()
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
if player == 1:
drawo_after_click(pos)
#print(pos)
elif player == 2:
drawx_after_click(pos)
# check win of row
if board[0][0] == board[0][1] and board[0][1] == board [0][2] and board[0][1] != 0:
pygame.draw.line(screen,(255,255,255),(0, 197 / 2),(600, 197 / 2),10)
if board[1][0] == board[1][1] and board[1][1] == board [1][2] and board[1][1] != 0:
pygame.draw.line(screen,(255,255,255),(0, 397- 197 / 2),(600, 397-197 / 2),10)
if board[2][0] == board[2][1] and board[2][1] == board [2][2] and board[2][1] != 0:
pygame.draw.line(screen,(255,255,255),(0, 600-197 / 2),(600, 600-197 / 2),10)
# check win of col
if board[0][0] == board[1][0] and board[1][0] == board [2][0] and board[1][0] != 0:
pygame.draw.line(screen,(255,255,255),(197/2, 0),(197 / 2, 600),10)
if board[0][1] == board[1][1] and board[1][1] == board [2][1] and board[1][1] != 0:
pygame.draw.line(screen,(255,255,255),(397- 197 / 2, 0),(397-197 / 2,600),10)
if board[0][2] == board[1][2] and board[1][2] == board [2][2] and board[1][2] != 0:
pygame.draw.line(screen,(255,255,255),(600-197 / 2, 0),(600-197 / 2,600),10)
# check win of x
if board[0][0] == board[1][1] and board[1][1] == board [2][2] and board[1][1] != 0:
pygame.draw.line(screen,(255,255,255),(0, 0),(600, 600),10)
if board[0][2] == board[1][1] and board[1][1] == board [2][0] and board[1][1] != 0:
pygame.draw.line(screen,(255,255,255),(0, 600),(600, 0),10)
pygame.display.update()