Advertisement
575

tic-tac-toe

575
Oct 29th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import sys
  2. import random
  3.  
  4.  
  5. def print_field():
  6.     for line in field:
  7.         print(' '.join(line))
  8.  
  9.  
  10. def move_player():
  11.     return map(int, input().split())
  12.  
  13.  
  14. def move_comp():
  15.     return random.randint(0, 2), random.randint(0, 2)
  16.  
  17.  
  18. #def move_comp_smart(n):
  19.     #if n == 1:
  20.         #return 1, 1
  21.     #elif n == 2
  22.        
  23.  
  24.  
  25. def move_X(n):
  26.     print('>> X turn <<')
  27.     x, y = move_comp()
  28.     while field[x][y] != '.':
  29.         print('>> Error turn. Try again <<')
  30.         x, y = move_comp()        
  31.     field[x][y] = 'X'
  32.     print_field()
  33.     if this_is_win():
  34.         print('>>> "X" VICTORY! <<<')
  35.         sys.exit()
  36.    
  37.    
  38. def move_O(n):
  39.     print('>> O turn <<')
  40.     x, y = move_player()
  41.     while field[x][y] != '.':
  42.         print('>> Error turn. Try again <<')
  43.         x, y = move_player()    
  44.     field[x][y] = 'O'
  45.     print_field()
  46.     if this_is_win():
  47.         print('>>> "O" VICTORY! <<<')
  48.         sys.exit()    
  49.    
  50.    
  51. def this_is_win():
  52.     return (field[0][0] == field[0][1] == field[0][2] != '.' or
  53.             field[1][0] == field[1][1] == field[1][2] != '.' or
  54.             field[2][0] == field[2][1] == field[2][2] != '.' or
  55.             field[0][0] == field[1][0] == field[2][0] != '.' or
  56.             field[0][1] == field[1][1] == field[2][1] != '.' or
  57.             field[0][2] == field[1][2] == field[2][2] != '.' or
  58.             field[0][0] == field[1][1] == field[2][2] != '.' or
  59.             field[0][2] == field[1][1] == field[2][0] != '.')
  60.            
  61.    
  62.  
  63. field = [['.'] * 3 for i in range (3)]
  64.  
  65. print_field()
  66. move_X(1)
  67. move_O(1)
  68. move_X(2)
  69. move_O(2)
  70. move_X(3)
  71. move_O(3)
  72. move_X(4)
  73. move_O(4)
  74. move_X(5)
  75. print(">> It's a draw :c <<")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement