Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import sys
- state = None
- user = 'x'
- def change_user():
- global user
- if user == 'x':
- user = 'o'
- else:
- user = 'x'
- def check_win():
- global state
- win_indices = [
- (0, 1, 2),
- (3, 4, 5),
- (6, 7, 8),
- (0, 3, 6),
- (1, 4, 7),
- (2, 5, 8),
- (0, 4, 8),
- (2, 4, 6)
- ]
- for idx in win_indices:
- els = [state[i] for i in idx]
- if all([el == 'x' for el in els]):
- return 'x'
- elif all([el == 'o' for el in els]):
- return 'o'
- return False
- def reset():
- global state
- state = [
- None, None, None,
- None, None, None,
- None, None, None,
- ]
- def draw_field():
- for i in range(3):
- for j in range(3):
- idx = j + i * 3
- el = state[idx]
- if el is None:
- print("_" , end='')
- else:
- print(el, end='')
- print(" ", end='')
- print()
- reset()
- if sys.version_info < (3, 0):
- input = raw_input
- for _ in range(9):
- draw_field()
- inp = input()
- i, j = [int(x) for x in inp.strip().split(' ')]
- i -= 1
- j -= 1
- state[j * 3 + i] = user
- res = check_win()
- if res:
- draw_field()
- print("{} won!".format(res))
- exit(0)
- change_user()
- print("DRAW")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement