Advertisement
MarLind

Tic Tac Toe

May 29th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. __author__ = 'malia015'
  2.  
  3. Size = 3
  4. Grid = [["" for n in range(Size)] for n in range(Size)]
  5. Players = ["X", "O"]
  6. Turn = 0
  7. TurnSign = Players[Turn % len(Players)]
  8.  
  9. def checkdir(down, right):
  10.     for n in range(Size):
  11.         m = 0
  12.         while Grid[(n*down + m*right)][(n*right + m*down)] == TurnSign:
  13.             m += 1
  14.             if m >= Size:
  15.                 return True
  16.     return False
  17.  
  18. def check():
  19.     if checkdir(1, 0) or checkdir(0, 1) or \
  20.             {Grid[n][n] for n in range(Size)} == {TurnSign} or \
  21.             {Grid[-(n+1)][n] for n in range(Size)} == {TurnSign}:
  22.         return True
  23.  
  24.  
  25. def out():
  26.     for n in range(Size):
  27.         print("\n+" + "---+"*Size, end="\n|")
  28.         for m in range(Size):
  29.             print("{:^3}".format(Grid[n][m]), end="|")
  30.     print("\n+" + "---+"*Size)
  31.  
  32.  
  33. out()
  34. while Turn < Size**2:
  35.     Move = input("{}'s turn: ".format(TurnSign))
  36.     Move = [int(n)-1 for n in Move if n in {"1", "2", "3"}]
  37.     if len(Move) != 2:
  38.         print("Try again; 1, 2 or 3, two integers.")
  39.         out()
  40.         continue
  41.     Move.reverse()
  42.     Move[0] = (Move[0] - 1) * (-1) + 1
  43.     if Grid[Move[0]][Move[1]] != "":
  44.         print("Try again; that space is taken.")
  45.         out()
  46.         continue
  47.  
  48.     Grid[Move[0]][Move[1]] = TurnSign
  49.     out()
  50.     if check():
  51.         print(str(TurnSign) + " wins!")
  52.         break
  53.     Turn += 1
  54.     if Turn >= Size**2:
  55.         print("It's a draw!")
  56.     TurnSign = Players[Turn % len(Players)]
  57.  
  58. input(str(8*7*6*5*4*3*2))
  59. for n in range(400000):
  60.     check()
  61.  
  62. print("Fett!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement