Advertisement
AlfonsoPEREZ

Tic Tac Toe

Apr 27th, 2020
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.17 KB | None | 0 0
  1. import sys, time, random
  2.  
  3. board = {"top-L": " ", "top-M": " ", "top-R": " ",
  4.          "mid-L": " ", "mid-M": " ", "mid-R": " ",
  5.          "low-L": " ", "low-M": " ", "low-R": " "}
  6.  
  7. gameEndings = {'1': ['mid-M', 'mid-L'], '2': ['mid-M', 'mid-R'], '3': ['mid-R', 'mid-L'], '4': ['mid-M', 'top-M'],
  8.                '5': ['mid-M', 'low-M'], '6': ['low-M', 'top-M'], '7': ['mid-M', 'top-R'], '8': ['mid-M', 'top-L'],
  9.                '9': ['mid-M', 'low-R'], '10': ['mid-M', 'low-L'], '11': ['low-R', 'top-L'], '12': ['low-L', 'top-R'],
  10.                '13': ['top-M', 'top-L'], '14': ['top-M', 'top-R'], '15': ['top-R', 'top-L'], '16': ['mid-R', 'top-R'],
  11.                '17': ['mid-R', 'low-R'], '18': ['low-R', 'top-R'], '19': ['low-M', 'low-R'], '20': ['low-M', 'low-L'],
  12.                '21': ['low-L', 'low-R'], '22': ['mid-L', 'top-L'], '23': ['mid-L', 'low-L'], '24': ['low-L', 'top-L']}
  13. toWin = {'1': 'mid-R', '2': 'mid-L', '3': 'mid-M', '4': 'low-M', '5': 'top-M', '6': 'mid-M', '7': 'low-L', '8': 'low-R',
  14.          '9': 'top-L', '10': 'top-R', '11': 'mid-M', '12': 'mid-M', '13': 'top-R', '14': 'top-L', '15': 'top-M',
  15.          '16': 'low-R', '17': 'top-R', '18': 'mid-R', '19': 'low-L', '20': 'low-R', '21': 'low-M', '22': 'low-L',
  16.          '23': 'top-L', '24': 'mid-L'}
  17. boardPlaces = ["top-L", "top-M", "top-R", "mid-L", "mid-M", "mid-R", "low-L", "low-M", "low-R"]
  18. player = random.choice(["X", "O"])
  19.  
  20. while True:
  21.     i = input("1) Single \n2) Multiplayer \nType The Number Of Desired Operation:  ")
  22.     if i == "1":
  23.         multiplayer = False
  24.         break
  25.     elif i == "2":
  26.         multiplayer = True
  27.         break
  28.     else:
  29.         print("That's not what I asked for!")
  30.         print()
  31.  
  32. print()
  33.  
  34.  
  35. def printBoard(board_dictionary):
  36.     print(board_dictionary["top-L"] + "|" + board_dictionary["top-M"] + "|" + board_dictionary["top-R"])
  37.     print("- - -")
  38.     print(board_dictionary["mid-L"] + "|" + board_dictionary["mid-M"] + "|" + board_dictionary["mid-R"])
  39.     print("- - -")
  40.     print(board_dictionary["low-L"] + "|" + board_dictionary["low-M"] + "|" + board_dictionary["low-R"])
  41.  
  42.  
  43. def checkWin():
  44.     if (board["top-L"] == "X" and board["top-M"] == "X" and board["top-R"] == "X") \
  45.             or (board["mid-L"] == "X" and board["mid-M"] == "X" and board["mid-R"] == "X") \
  46.             or (board["low-L"] == "X" and board["low-M"] == "X" and board["low-R"] == "X") \
  47.             or (board["top-L"] == "X" and board["mid-M"] == "X" and board["low-R"] == "X") \
  48.             or (board["top-L"] == "X" and board["mid-L"] == "X" and board["low-L"] == "X") \
  49.             or (board["top-M"] == "X" and board["mid-M"] == "X" and board["low-M"] == "X") \
  50.             or (board["top-R"] == "X" and board["mid-R"] == "X" and board["low-R"] == "X") \
  51.             or (board["top-R"] == "X" and board["mid-M"] == "X" and board["low-L"] == "X"):
  52.         print("\nX Wins!")
  53.         time.sleep(5)
  54.         sys.exit()
  55.     elif (board["top-L"] == "O" and board["top-M"] == "O" and board["top-R"] == "O") \
  56.             or (board["mid-L"] == "O" and board["mid-M"] == "O" and board["mid-R"] == "O") \
  57.             or (board["low-L"] == "O" and board["low-M"] == "O" and board["low-R"] == "O") \
  58.             or (board["top-L"] == "O" and board["mid-M"] == "O" and board["low-R"] == "O") \
  59.             or (board["top-L"] == "O" and board["mid-L"] == "O" and board["low-L"] == "O") \
  60.             or (board["top-M"] == "O" and board["mid-M"] == "O" and board["low-M"] == "O") \
  61.             or (board["top-R"] == "O" and board["mid-R"] == "O" and board["low-R"] == "O") \
  62.             or (board["top-R"] == "O" and board["mid-M"] == "O" and board["low-L"] == "O"):
  63.         print("\nO Wins!")
  64.         time.sleep(5)
  65.         sys.exit()
  66.     elif board["top-L"] != " " and board["top-M"] != " " and board["top-R"] != " " \
  67.             and board["mid-L"] != " " and board["mid-M"] != " " and board["mid-R"] != " " \
  68.             and board["low-L"] != " " and board["low-M"] != " " and board["low-R"] != " " \
  69.             and board["top-L"] != " " and board["mid-M"] != " " and board["low-R"] != " " \
  70.             and board["top-L"] != " " and board["mid-L"] != " " and board["low-L"] != " " \
  71.             and board["top-M"] != " " and board["mid-M"] != " " and board["low-M"] != " " \
  72.             and board["top-R"] != " " and board["mid-R"] != " " and board["low-R"] != " " \
  73.             and board["top-R"] != " " and board["mid-M"] != " " and board["low-L"] != " ":
  74.         print("\nIts a Draw!")
  75.         time.sleep(5)
  76.         sys.exit()
  77.  
  78.  
  79. def x_turn():
  80.     print("X's Turn")
  81.     if player == "X" or multiplayer:
  82.         print("top-L, top-M, top-R, mid-L, mid-M, mid-R, low-L, low-M or low-R?")
  83.         print("Make Sure You Pick an Empty Spot!")
  84.         x_move = input()
  85.         if x_move in boardPlaces:
  86.             if board[x_move] == " ":
  87.                 board[x_move] = "X"
  88.             else:
  89.                 print("That Spot isn't empty!")
  90.                 x_turn()
  91.  
  92.         else:
  93.             print("That's Not What I Told You To Enter!")
  94.             x_turn()
  95.  
  96.     else:
  97.         if board["mid-M"] == " ":
  98.             board["mid-M"] = "X"
  99.         else:
  100.             for f in gameEndings:
  101.                 gameEnding = gameEndings[f]
  102.                 if board[gameEnding[0]] == "X" and board[gameEnding[1]] == "X":
  103.                     if board[toWin[f]] == " ":
  104.                         board[toWin[f]] = "X"
  105.                         print()
  106.                         printBoard(board)
  107.                         checkWin()
  108.                         o_turn()
  109.                         return
  110.  
  111.             for f2 in gameEndings:
  112.                 gameEnding = gameEndings[f2]
  113.                 if board[gameEnding[0]] == "O" and board[gameEnding[1]] == "O":
  114.                     if board[toWin[f2]] == " ":
  115.                         board[toWin[f2]] = "X"
  116.                         print()
  117.                         printBoard(board)
  118.                         checkWin()
  119.                         o_turn()
  120.                         return
  121.  
  122.             while True:
  123.                 randoPlace = random.choice(boardPlaces)
  124.                 if board[randoPlace] == " ":
  125.                     board[randoPlace] = "X"
  126.                     break
  127.  
  128.     print()
  129.     printBoard(board)
  130.     checkWin()
  131.     o_turn()
  132.  
  133.  
  134. def o_turn():
  135.     print("O's Turn")
  136.     if player == "O" or multiplayer:
  137.         print("top-L, top-M, top-R, mid-L, mid-M, mid-R, low-L, low-M or low-R?")
  138.         print("Make Sure You Pick an Empty Spot!")
  139.         o_move = input()
  140.         if o_move in boardPlaces:
  141.             if board[o_move] == " ":
  142.                 board[o_move] = "O"
  143.             else:
  144.                 print("That Spot isn't empty!")
  145.                 o_turn()
  146.  
  147.         else:
  148.             print("That's Not What I Told You To Enter!")
  149.             o_turn()
  150.  
  151.     else:
  152.         if board["mid-M"] == " ":
  153.             board["mid-M"] = "O"
  154.         else:
  155.             for f in gameEndings:
  156.                 gameEnding = gameEndings[f]
  157.                 if board[gameEnding[0]] == "O" and board[gameEnding[1]] == "O":
  158.                     if board[toWin[f]] == " ":
  159.                         board[toWin[f]] = "O"
  160.                         print()
  161.                         printBoard(board)
  162.                         checkWin()
  163.                         x_turn()
  164.                         return
  165.  
  166.             for f2 in gameEndings:
  167.                 gameEnding = gameEndings[f2]
  168.                 if board[gameEnding[0]] == "X" and board[gameEnding[1]] == "X":
  169.                     if board[toWin[f2]] == " ":
  170.                         board[toWin[f2]] = "O"
  171.                         print()
  172.                         printBoard(board)
  173.                         checkWin()
  174.                         x_turn()
  175.                         return
  176.  
  177.             while True:
  178.                 randoPlace = random.choice(boardPlaces)
  179.                 if board[randoPlace] == " ":
  180.                     board[randoPlace] = "O"
  181.                     break
  182.  
  183.     print()
  184.     printBoard(board)
  185.     checkWin()
  186.     x_turn()
  187.  
  188.  
  189. if not multiplayer:
  190.     print("You are %r" % player)
  191.  
  192. movesFirst = random.choice(["X", "O"])
  193. print("%r Moves First!" % movesFirst)
  194. printBoard(board)
  195. globals()["%s_turn" % movesFirst.lower()]()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement