@robloxyoutube73/

TicTacToe: Terminal Edition

Python

I totally just forked it. I hive all credit the the person to made it. I thought it was so cool!

fork
loading
Files
  • main.py
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
import sys
import termios
import os
import string

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 == 'O':#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)))


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

print('TicTacToe: Terminal Edition\n\n')
print('     |     |     \n')
print('----- ----- -----\n')
print('     |     |     \n')
print('----- ----- -----\n')
print('     |     |    \n')

current = 'X'
colors = [
	escape('31m'), #red
	escape('34m') #blue
]

print(escape('9;9H'))
print(colors[0]+current)

board = ['']*9

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 ['X','O']:
		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

while True:
	__builtins__.print(output,end='',flush=True)
	output = ''
	c = getKey()
	if type(c) == tuple:
		if c[0] == 'Click' and c[3] == 'Down':
			xpos = int((c[1])/6)
			ypos = int((c[2]-1)/2)
			if xpos > 2:continue
			elif ypos > 2:continue
			xclick = xpos*6+3
			yclick = ypos*2+3
			boardpos = xpos+ypos*3
			if board[boardpos] == '':
				board[boardpos]=current
				print(escape(f'{yclick};{xclick}H')+(colors[0] if current == 'X' else colors[1])+current)
				#print(yclick)

				current = 'X' if current == 'O' else 'O'
				print(escape('9;9H'))
				print((colors[0] if current == 'X' else colors[1])+current)
				winner = checkWin()
				if winner == 'X':
					break
				elif winner == 'O':
					break
				else:
					if len(''.join(board))==9:
						winner = None
						break
print = __builtins__.print
print(output)
if winner == None:
	print(escape('90m')+escape('9;9H')+'NO ONE WINS')
else:
	print((colors[0] if winner == 'X' else colors[1])+escape('9;9H')+winner+' WINS')