Advertisement
slipers

Tic-Tac-Toe terminal game

Jul 6th, 2021
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. # CC:Tweaked and Python 3
  2. # Tic-Tac-Toe terminal game
  3. # Version 1.0 06.07.2021 10:27:00
  4. # (c) 2021, MCAP-2020 (mcap_nikita & mcap_slipers)
  5.  
  6. from cc import os
  7. from cc import commands
  8. from cc import term, peripheral
  9.  
  10. out = term;  # out = peripheral.wrap("top")
  11. out.clear(); out.setCursorPos(1, 1); size = out.getSize()
  12. x_min = 1; y_min = 1; x_max = size[0]; y_max = size[1]
  13. tic = chr(120); tac = chr(111); toe = chr(7); scr = []
  14.  
  15. def say(msg):
  16.     commands.exec('say ' + str(msg))
  17.  
  18. def check(y, x):
  19.      global scr
  20.      x1 = max(x_min, x - 4); x2 = min(x_max, x + 4) + 1
  21.      y1 = max(y_min, y - 4); y2 = min(y_max, y + 4) + 1
  22.      res = '['
  23.      for dx in range(x1, x2):
  24.           res = res + ('-' if scr[y][dx] == '' else scr[y][dx])
  25.      res = res + '<>'
  26.      for dy in range(y1, y2):
  27.           res = res + ('-' if scr[dy][x] == '' else scr[dy][x])
  28.      res = res + '<>'
  29.  
  30.      dxi = 0
  31.      dx1 = x - min((x - x1), (y - y1)); dy1 = y - min((x - x1), (y - y1))
  32.      dx2 = x + min((x2 - x), (y2 - y)); dy2 = y + min((x2 - x), (y2 - y))
  33.      for dx in range(dx1, dx2):
  34.           dxi = dxi + 1; dyi = 0
  35.           for dy in range(dy1, dy2):    
  36.                dyi = dyi + 1
  37.                if (dxi == dyi):
  38.                     res = res + ('-' if scr[dy][dx] == '' else scr[dy][dx])
  39.      res = res + '<>'
  40.  
  41.      dxi = 0
  42.      dx1 = x + min((x2 - x), (y - y1)); dy1 = y - min((x2 - x), (y - y1))
  43.      dx2 = x - min((x - x1), (y2 - y)) - 1; dy2 = y + min((x - x1), (y2 - y)) + 1
  44.      for dx in range(dx1, dx2, -1):
  45.           dxi = dxi + 1; dyi = 0
  46.           for dy in range(dy1, dy2):    
  47.                dyi = dyi + 1
  48.                if (dxi == dyi):
  49.                     res = res + ('-' if scr[dy][dx] == '' else scr[dy][dx])
  50.      res = res + ']'
  51.      
  52.      # say(res)
  53.  
  54.      tics = res.find(tic * 5); tacs = res.find(tac * 5)
  55.      if (tics != -1):
  56.           return 'TIC'
  57.      elif (tacs != -1):
  58.           return 'TAC'
  59.      else:
  60.           return None
  61.  
  62.  
  63. def redrawing(x, y):
  64.      global scr
  65.      out.setCursorPos(x - 1, y)
  66.      if scr[y][x - 1] == '':
  67.           out.write(' '); out.write('')
  68.      else:
  69.           out.write(scr[y][x - 1])
  70.  
  71. for i in range(y_max + 2):
  72.     save = []
  73.     for j in range(x_max + 2):
  74.         save.append('')
  75.     scr.append(save)
  76.  
  77. out.setCursorPos(1, 1); out.write(toe)
  78. for e in os.captureEvent('key'):
  79.      key = e[0]
  80.      cur_pos = out.getCursorPos(); x = cur_pos[0]; y = cur_pos[1]
  81.      if key == 16:
  82.         out.clear(); out.setCursorPos(1, 1); break
  83.      else:
  84.           win = None
  85.           if (key == 205) and (x <= x_max):
  86.                redrawing(x, y); out.setCursorPos(x, y); out.write(toe)
  87.           elif (key == 203) and ((x - 2) >= x_min):
  88.                redrawing(x, y); out.setCursorPos(x - 2, y); out.write(toe)
  89.           elif (key == 208) and ((y + 1) <= y_max):
  90.                redrawing(x, y); out.setCursorPos(x - 1, y + 1); out.write(toe)
  91.           elif (key == 200) and ((y - 1) >= y_min):
  92.                redrawing(x, y); out.setCursorPos(x - 1, y - 1); out.write(toe)
  93.           elif key == 29:
  94.                out.setCursorPos(x - 1, y); out.write(tic)
  95.                scr[y][x - 1] = tic
  96.                win = check(y, x - 1)
  97.           elif key == 56:
  98.                out.setCursorPos(x - 1, y); out.write(tac)
  99.                scr[y][x - 1] = tac
  100.                win = check(y, x - 1)
  101.      if win != None:
  102.           out.setCursorPos(1, 1); print('WIN ' + win)
  103.           break
  104.  
  105. # 29 - tic, 56 - tac
  106. # 200 - up, 208 - down, 203 - left, 205 - right
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement