repl.it
@mat1/

TicTacToe: Turkey Edition

Python

No description

fork
loading
Files
  • main.py
  • turkey.txt
main.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import sys
import termios
import os
import string
import random
from threading import Thread
import time


def escape(code):
	return '\033['+code
width, height = os.get_terminal_size()
stream = sys.stdin
mode = termios.tcgetattr(stream)
mode[3] = mode[3] &~ (termios.ECHO | termios.ICANON | termios.IEXTEN | termios.ISIG)
termios.tcsetattr(sys.stdin,1,mode)
print(escape('?1003h'),end='') # click detection
print(escape('?1006h'),end='') # make it better
print(escape('?25l'),end='') # hide cursor
print(escape('?7h'),end='') # don't  line wrap
print(escape('2J'),end=escape('H'),flush='') # clear screen
def parseMouseCode():
	mouseid = ''
	while True:
		text = stream.read(1)
		if text == ';':break
		mouseid += text
	x = ''
	while True:
		text = stream.read(1)
		if text == ';':break
		x += text
	y = ''
	while True:
		text = stream.read(1)
		if text in 'Mm':break
		y += text
	x, y, mouseid = int(x)-1, int(y)-1, int(mouseid)
	mousenames = {0:'Click',33:'Mouse up',32:'Mouse dragged',2:'Mouse 2 down',34:'Mouse 2 dragged',64:'Scroll up',65:'Scroll down',1:'Middle mouse down',33:'Middle mouse scroll',4:'Forward button down',36:'Forward button still down',35:'Back button down'}
	if mouseid in mousenames:
		return mousenames[mouseid],x,y,'Down' if text=='M' else 'Up'
	else:
		return mouseid,x,y,'Down' if text=='M' else 'Up'

def parseMagicKeys(esccode):
	oof = esccode[-1]
	esccode = esccode[:-1]
	data = esccode.split(';')
	if oof == '~':
		if len(data) == 1:
			if data[0] == '3':
				return 'DELETE'
			elif data[0] == '6':
				return 'PAGEDOWN'
			elif data[0] == '5':
				return 'PAGEUP'
			elif data[0] == '15':
				return 'F5'
			elif data[0] == '17':
				return 'F6'
			elif data[0] == '18':
				return 'F7'
			elif data[0] == '19':
				return 'F8'
			elif data[0] == '20':
				return 'F9'
			elif data[0] == '21':
				return 'F10'
			elif data[0] == '24':
				return 'F12'
	elif oof == 'A':
		if len(data) == 2:
			if data[1] == '2':
				return 'SHIFT+UP'
			elif data[1] == '9':
				return 'CMD+UP'
	elif oof == 'B':
		if len(data) == 2:
			if data[1] == '2':
				return 'SHIFT+DOWN'
			elif data[1] == '9':
				return 'CMD+DOWN'
	elif oof == 'C':
		if len(data) == 2:
			if data[1] == '2':
				return 'SHIFT+RIGHT'
			elif data[1] == '9':
				return 'CMD+RIGHT'
	elif oof == 'D':
		if len(data) == 2:
			if data[1] == '2':
				return 'SHIFT+LEFT'
			elif data[1] == '9':
				return 'CMD+LEFT'
	return str(esccode)+' '+str(data)+' '+str(oof)

def parseEscapeCode():
	esccode=''
	while True:
		text = stream.read(1)
		esccode += text
		if text in string.ascii_letters+'<~':
			if esccode == '[D':#left arrow
				return 'LEFT'
			elif esccode == '[C':#right arrow
				return 'RIGHT'
			elif esccode == '[A':#up arrow
				return 'UP'
			elif esccode == '[B':#down arrow
				return 'DOWN'
			elif esccode == '[O':#unfocus
				return 'UNFOCUS'
			elif esccode == '[I':#focus
				return 'FOCUS'
			elif esccode == '[H':#home
				return 'HOME'
			elif esccode == '[F':#end
				return 'END'
			elif esccode == things[1]:#f 1-4
				f = stream.read(1)
				return f'F{ord(f)-79}'
			elif esccode == '[<':#better mouse
				mousedata = parseMouseCode()
				return mousedata
			elif esccode[-1] in '~ABCDF':#idek what these do
				return parseMagicKeys(esccode[1:])
			else:
				raise NameError(f'Unknown escape code: {esccode}')
			esccode = ''
			break
def getrgb(r,g,b):
	return escape(f'38;2;{r};{g};{b}m')
def getKey():
	c = stream.read(1)
	if ord(c)==27:#escape
		key = parseEscapeCode()
		return key
	elif ord(c)==127:
		return 'BACKSPACE'
	elif ord(c)==10:
		return 'ENTER'
	else:
		return c
def gethex(hexdata):
	return getrgb(*tuple(int(hexdata[i:i+2], 16) for i in (0, 2 ,4)))

if random.randint(0,10)==1:
	things = ['🦃','🤠']
else:
	things = ['X','O']

output=''
def print(string):
	global output
	output+=str(string)

colors = [
	escape('33m'), # orange
	escape('93m'), # yellow
	escape('31m')
]
bgcolors = 	[
	escape('43m'), # orange
	escape('103m'), # yellow
	escape('41m') # red
]

print('TicTacToe: Turkey Edition\n\n')
boardsize = 2
print(escape('94m'))
if boardsize == 1:
	print('     |     |     \n')
	print('----- ----- -----\n')
	print('     |     |     \n')
	print('----- ----- -----\n')
	print('     |     |    \n')
else:
	print('       |       |       \n')
	print('       |       |       \n')
	print('       |       |       \n')
	print('------- ------- -------\n')
	print('       |       |      \n')
	print('       |       |       \n')
	print('       |       |       \n')
	print('------- ------- -------\n')
	print('       |       |      \n')
	print('       |       |       \n')
	print('       |       |       \n')
print(escape('0m'))

current = things[0]


def changeTurkey():
	i=0
	while True:
		i+=1
		turkeyoutput = escape('s')
		if boardsize == 1:
			turkeyoutput+=escape('12;H')
		else:
			turkeyoutput+=escape('20;H')
		a=i
		l2=''
		for l in open('turkey.txt').read():
			a+=1
			if l == ' ':
				turkeyoutput+=random.choice(colors)
			turkeyoutput+=l
			l2=l
		current = things[0]
		turkeyoutput+=escape('u')
		__builtins__.print(turkeyoutput,end='',flush=True)
		time.sleep(0.5)
Thread(target=changeTurkey).start()


if boardsize == 1:
	bottompos = (9,9)
else:
	bottompos = (15,12)
print(escape(f'{bottompos[0]};{bottompos[1]}H'))

print(colors[0]+current)

board = ['']*9

def getaimove():
	h1 = board[0]+board[1]+board[2]
	h2 = board[3]+board[4]+board[5]
	h3 = board[6]+board[7]+board[8]

	v1 = board[0]+board[3]+board[6]
	v2 = board[1]+board[4]+board[7]
	v3 = board[2]+board[5]+board[8]

	d1 = board[0]+board[4]+board[8]
	d2 = board[2]+board[4]+board[6]

	for c in [things[0],things[1]]:
		if h1 == c*2:
			for i in range(3):
				if board[i]=='':
					return i
		if h2 == c*2:
			for i in range(3):
				if board[i+3]=='':
					return i+3
		if h3 == c*2:
			for i in range(3):
				if board[i+6]=='':
					return i+6

		if v1 == c*2:
			for i in range(3):
				if board[i*3]=='':
					return i*3
		if v2 == c*2:
			for i in range(3):
				if board[i*3+1]=='':
					return i*3+1
		if v3 == c*2:
			for i in range(3):
				if board[i*3+2]=='':
					return i*3+2

		if d1 == c*2:
			b=(0,4,8)
			if board[b[0]]+board[b[1]] == c*2:
				return b[2]
			elif board[b[0]]+board[b[2]] == c*2:
				return b[1]
			elif board[4]+board[b[2]] == c*2:
				return b[0]
		if d2 == c*2:
			b=(2,4,6)
			if board[b[0]]+board[b[1]] == c*2:
				return b[2]
			elif board[b[0]]+board[b[2]] == c*2:
				return b[1]
			elif board[4]+board[b[2]] == c*2:
				return b[0]
	while True:
		randpos = random.randint(0,8)
		if board[randpos]=='':
			return randpos
		if len(''.join(board))==9:
			return 0

def checkWin():
	h1 = board[0]+board[1]+board[2]
	h2 = board[3]+board[4]+board[5]
	h3 = board[6]+board[7]+board[8]

	v1 = board[0]+board[3]+board[6]
	v2 = board[1]+board[4]+board[7]
	v3 = board[2]+board[5]+board[8]

	d1 = board[0]+board[4]+board[8]
	d2 = board[2]+board[4]+board[6]

	for c in [things[0],things[1]]:
		if h1 == c*3:
			return c
		elif h2 == c*3:
			return c
		elif h3 == c*3:
			return c

		elif v1 == c*3:
			return c
		elif v2 == c*3:
			return c
		elif v3 == c*3:
			return c

		elif d1 == c*3:
			return c
		elif d2 == c*3:
			return c


print(escape(f'{bottompos[0]};{bottompos[1]}H'))

ai = True
if '🦃' in things:
	aiturn = '🦃'
else:
	aiturn = random.choice(things)

while True:
	__builtins__.print(output,end='',flush=True)
	output = ''
	if (not ai) or aiturn != current:
		c = getKey()
		if type(c) == tuple:
			if c[0] == 'Click' and c[3] == 'Down':
				if boardsize == 1:
					xpos = int((c[1])/6)
					ypos = int((c[2]-1)/2)
				else:
					xpos = int((c[1]-2)/8)
					ypos = int((c[2]-2)/4)
			else:
				continue
		else:
			continue
	else:
		time.sleep(random.uniform(0.1,0.5))
		pos=getaimove()
		xpos=pos%3
		ypos=int(pos/3)

	if boardsize == 1:
		xclick = xpos*6+3
		yclick = ypos*2+3
	else:
		xclick = xpos*8+4
		yclick = ypos*4+4

	if xpos > 2:continue
	elif ypos > 2:continue
	boardpos = xpos+ypos*3
	if board[boardpos] == '':
		board[boardpos]=current
		print(escape(f'{yclick};{xclick}H')+(colors[0] if current == things[0] else colors[1])+current)
		#print(yclick)

		current = things[0] if current == things[1] else things[1]
		print(escape(f'{bottompos[0]};{bottompos[1]}H'))
		print((colors[0] if current == things[0] else colors[1])+current)
		winner = checkWin()
		if winner == things[0]:
			break
		elif winner == things[1]:
			break
		else:
			if len(''.join(board))==9:
				winner = None
				break
print = __builtins__.print
print(output)
if winner == None:
	print(escape('0m')+escape('1m')+escape(f'{bottompos[0]};{bottompos[1]-3}H')
+'ITS A TIE')
else:
	print((colors[0] if winner == things[0] else colors[1])+escape('1m')+escape(f'{bottompos[0]};{bottompos[1]-3}H')+winner+' WINS')
?