Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. from __future__ import print_function
  2. import sys
  3.  
  4. state = None
  5. user = 'x'
  6.  
  7. def change_user():
  8.     global user
  9.     if user == 'x':
  10.         user = 'o'
  11.     else:
  12.         user = 'x'
  13.  
  14. def check_win():
  15.     global state
  16.     win_indices = [
  17.         (0, 1, 2),
  18.         (3, 4, 5),
  19.         (6, 7, 8),
  20.         (0, 3, 6),
  21.         (1, 4, 7),
  22.         (2, 5, 8),
  23.         (0, 4, 8),
  24.         (2, 4, 6)
  25.     ]
  26.    
  27.     for idx in win_indices:
  28.         els = [state[i] for i in idx]
  29.         if all([el == 'x' for el in els]):
  30.             return 'x'
  31.         elif all([el == 'o' for el in els]):
  32.             return 'o'
  33.    
  34.     return False
  35.  
  36. def reset():
  37.     global state
  38.     state = [
  39.         None, None, None,
  40.         None, None, None,
  41.         None, None, None,
  42.     ]
  43.  
  44. def draw_field():
  45.     for i in range(3):
  46.         for j in range(3):
  47.             idx = j + i * 3
  48.             el = state[idx]
  49.             if el is None:
  50.                 print("_" , end='')
  51.             else:
  52.                 print(el, end='')
  53.             print(" ", end='')
  54.         print()
  55.  
  56. reset()
  57.  
  58. if sys.version_info < (3, 0):
  59.     input = raw_input
  60.  
  61. for _ in range(9):
  62.     draw_field()
  63.     inp = input()
  64.     i, j = [int(x) for x in inp.strip().split(' ')]
  65.     i -= 1
  66.     j -= 1
  67.     state[j * 3 + i] = user
  68.     res = check_win()
  69.     if res:
  70.         draw_field()
  71.         print("{} won!".format(res))
  72.         exit(0)
  73.        
  74.     change_user()
  75.  
  76. print("DRAW")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement