Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CC:Tweaked and Python 3
- # Tic-Tac-Toe terminal game
- # Version 1.0 06.07.2021 10:27:00
- # (c) 2021, MCAP-2020 (mcap_nikita & mcap_slipers)
- from cc import os
- from cc import commands
- from cc import term, peripheral
- out = term; # out = peripheral.wrap("top")
- out.clear(); out.setCursorPos(1, 1); size = out.getSize()
- x_min = 1; y_min = 1; x_max = size[0]; y_max = size[1]
- tic = chr(120); tac = chr(111); toe = chr(7); scr = []
- def say(msg):
- commands.exec('say ' + str(msg))
- def check(y, x):
- global scr
- x1 = max(x_min, x - 4); x2 = min(x_max, x + 4) + 1
- y1 = max(y_min, y - 4); y2 = min(y_max, y + 4) + 1
- res = '['
- for dx in range(x1, x2):
- res = res + ('-' if scr[y][dx] == '' else scr[y][dx])
- res = res + '<>'
- for dy in range(y1, y2):
- res = res + ('-' if scr[dy][x] == '' else scr[dy][x])
- res = res + '<>'
- dxi = 0
- dx1 = x - min((x - x1), (y - y1)); dy1 = y - min((x - x1), (y - y1))
- dx2 = x + min((x2 - x), (y2 - y)); dy2 = y + min((x2 - x), (y2 - y))
- for dx in range(dx1, dx2):
- dxi = dxi + 1; dyi = 0
- for dy in range(dy1, dy2):
- dyi = dyi + 1
- if (dxi == dyi):
- res = res + ('-' if scr[dy][dx] == '' else scr[dy][dx])
- res = res + '<>'
- dxi = 0
- dx1 = x + min((x2 - x), (y - y1)); dy1 = y - min((x2 - x), (y - y1))
- dx2 = x - min((x - x1), (y2 - y)) - 1; dy2 = y + min((x - x1), (y2 - y)) + 1
- for dx in range(dx1, dx2, -1):
- dxi = dxi + 1; dyi = 0
- for dy in range(dy1, dy2):
- dyi = dyi + 1
- if (dxi == dyi):
- res = res + ('-' if scr[dy][dx] == '' else scr[dy][dx])
- res = res + ']'
- # say(res)
- tics = res.find(tic * 5); tacs = res.find(tac * 5)
- if (tics != -1):
- return 'TIC'
- elif (tacs != -1):
- return 'TAC'
- else:
- return None
- def redrawing(x, y):
- global scr
- out.setCursorPos(x - 1, y)
- if scr[y][x - 1] == '':
- out.write(' '); out.write('')
- else:
- out.write(scr[y][x - 1])
- for i in range(y_max + 2):
- save = []
- for j in range(x_max + 2):
- save.append('')
- scr.append(save)
- out.setCursorPos(1, 1); out.write(toe)
- for e in os.captureEvent('key'):
- key = e[0]
- cur_pos = out.getCursorPos(); x = cur_pos[0]; y = cur_pos[1]
- if key == 16:
- out.clear(); out.setCursorPos(1, 1); break
- else:
- win = None
- if (key == 205) and (x <= x_max):
- redrawing(x, y); out.setCursorPos(x, y); out.write(toe)
- elif (key == 203) and ((x - 2) >= x_min):
- redrawing(x, y); out.setCursorPos(x - 2, y); out.write(toe)
- elif (key == 208) and ((y + 1) <= y_max):
- redrawing(x, y); out.setCursorPos(x - 1, y + 1); out.write(toe)
- elif (key == 200) and ((y - 1) >= y_min):
- redrawing(x, y); out.setCursorPos(x - 1, y - 1); out.write(toe)
- elif key == 29:
- out.setCursorPos(x - 1, y); out.write(tic)
- scr[y][x - 1] = tic
- win = check(y, x - 1)
- elif key == 56:
- out.setCursorPos(x - 1, y); out.write(tac)
- scr[y][x - 1] = tac
- win = check(y, x - 1)
- if win != None:
- out.setCursorPos(1, 1); print('WIN ' + win)
- break
- # 29 - tic, 56 - tac
- # 200 - up, 208 - down, 203 - left, 205 - right
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement