Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, time, random
- board = {"top-L": " ", "top-M": " ", "top-R": " ",
- "mid-L": " ", "mid-M": " ", "mid-R": " ",
- "low-L": " ", "low-M": " ", "low-R": " "}
- gameEndings = {'1': ['mid-M', 'mid-L'], '2': ['mid-M', 'mid-R'], '3': ['mid-R', 'mid-L'], '4': ['mid-M', 'top-M'],
- '5': ['mid-M', 'low-M'], '6': ['low-M', 'top-M'], '7': ['mid-M', 'top-R'], '8': ['mid-M', 'top-L'],
- '9': ['mid-M', 'low-R'], '10': ['mid-M', 'low-L'], '11': ['low-R', 'top-L'], '12': ['low-L', 'top-R'],
- '13': ['top-M', 'top-L'], '14': ['top-M', 'top-R'], '15': ['top-R', 'top-L'], '16': ['mid-R', 'top-R'],
- '17': ['mid-R', 'low-R'], '18': ['low-R', 'top-R'], '19': ['low-M', 'low-R'], '20': ['low-M', 'low-L'],
- '21': ['low-L', 'low-R'], '22': ['mid-L', 'top-L'], '23': ['mid-L', 'low-L'], '24': ['low-L', 'top-L']}
- 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',
- '9': 'top-L', '10': 'top-R', '11': 'mid-M', '12': 'mid-M', '13': 'top-R', '14': 'top-L', '15': 'top-M',
- '16': 'low-R', '17': 'top-R', '18': 'mid-R', '19': 'low-L', '20': 'low-R', '21': 'low-M', '22': 'low-L',
- '23': 'top-L', '24': 'mid-L'}
- boardPlaces = ["top-L", "top-M", "top-R", "mid-L", "mid-M", "mid-R", "low-L", "low-M", "low-R"]
- player = random.choice(["X", "O"])
- while True:
- i = input("1) Single \n2) Multiplayer \nType The Number Of Desired Operation: ")
- if i == "1":
- multiplayer = False
- break
- elif i == "2":
- multiplayer = True
- break
- else:
- print("That's not what I asked for!")
- print()
- print()
- def printBoard(board_dictionary):
- print(board_dictionary["top-L"] + "|" + board_dictionary["top-M"] + "|" + board_dictionary["top-R"])
- print("- - -")
- print(board_dictionary["mid-L"] + "|" + board_dictionary["mid-M"] + "|" + board_dictionary["mid-R"])
- print("- - -")
- print(board_dictionary["low-L"] + "|" + board_dictionary["low-M"] + "|" + board_dictionary["low-R"])
- def checkWin():
- if (board["top-L"] == "X" and board["top-M"] == "X" and board["top-R"] == "X") \
- or (board["mid-L"] == "X" and board["mid-M"] == "X" and board["mid-R"] == "X") \
- or (board["low-L"] == "X" and board["low-M"] == "X" and board["low-R"] == "X") \
- or (board["top-L"] == "X" and board["mid-M"] == "X" and board["low-R"] == "X") \
- or (board["top-L"] == "X" and board["mid-L"] == "X" and board["low-L"] == "X") \
- or (board["top-M"] == "X" and board["mid-M"] == "X" and board["low-M"] == "X") \
- or (board["top-R"] == "X" and board["mid-R"] == "X" and board["low-R"] == "X") \
- or (board["top-R"] == "X" and board["mid-M"] == "X" and board["low-L"] == "X"):
- print("\nX Wins!")
- time.sleep(5)
- sys.exit()
- elif (board["top-L"] == "O" and board["top-M"] == "O" and board["top-R"] == "O") \
- or (board["mid-L"] == "O" and board["mid-M"] == "O" and board["mid-R"] == "O") \
- or (board["low-L"] == "O" and board["low-M"] == "O" and board["low-R"] == "O") \
- or (board["top-L"] == "O" and board["mid-M"] == "O" and board["low-R"] == "O") \
- or (board["top-L"] == "O" and board["mid-L"] == "O" and board["low-L"] == "O") \
- or (board["top-M"] == "O" and board["mid-M"] == "O" and board["low-M"] == "O") \
- or (board["top-R"] == "O" and board["mid-R"] == "O" and board["low-R"] == "O") \
- or (board["top-R"] == "O" and board["mid-M"] == "O" and board["low-L"] == "O"):
- print("\nO Wins!")
- time.sleep(5)
- sys.exit()
- elif board["top-L"] != " " and board["top-M"] != " " and board["top-R"] != " " \
- and board["mid-L"] != " " and board["mid-M"] != " " and board["mid-R"] != " " \
- and board["low-L"] != " " and board["low-M"] != " " and board["low-R"] != " " \
- and board["top-L"] != " " and board["mid-M"] != " " and board["low-R"] != " " \
- and board["top-L"] != " " and board["mid-L"] != " " and board["low-L"] != " " \
- and board["top-M"] != " " and board["mid-M"] != " " and board["low-M"] != " " \
- and board["top-R"] != " " and board["mid-R"] != " " and board["low-R"] != " " \
- and board["top-R"] != " " and board["mid-M"] != " " and board["low-L"] != " ":
- print("\nIts a Draw!")
- time.sleep(5)
- sys.exit()
- def x_turn():
- print("X's Turn")
- if player == "X" or multiplayer:
- print("top-L, top-M, top-R, mid-L, mid-M, mid-R, low-L, low-M or low-R?")
- print("Make Sure You Pick an Empty Spot!")
- x_move = input()
- if x_move in boardPlaces:
- if board[x_move] == " ":
- board[x_move] = "X"
- else:
- print("That Spot isn't empty!")
- x_turn()
- else:
- print("That's Not What I Told You To Enter!")
- x_turn()
- else:
- if board["mid-M"] == " ":
- board["mid-M"] = "X"
- else:
- for f in gameEndings:
- gameEnding = gameEndings[f]
- if board[gameEnding[0]] == "X" and board[gameEnding[1]] == "X":
- if board[toWin[f]] == " ":
- board[toWin[f]] = "X"
- print()
- printBoard(board)
- checkWin()
- o_turn()
- return
- for f2 in gameEndings:
- gameEnding = gameEndings[f2]
- if board[gameEnding[0]] == "O" and board[gameEnding[1]] == "O":
- if board[toWin[f2]] == " ":
- board[toWin[f2]] = "X"
- print()
- printBoard(board)
- checkWin()
- o_turn()
- return
- while True:
- randoPlace = random.choice(boardPlaces)
- if board[randoPlace] == " ":
- board[randoPlace] = "X"
- break
- print()
- printBoard(board)
- checkWin()
- o_turn()
- def o_turn():
- print("O's Turn")
- if player == "O" or multiplayer:
- print("top-L, top-M, top-R, mid-L, mid-M, mid-R, low-L, low-M or low-R?")
- print("Make Sure You Pick an Empty Spot!")
- o_move = input()
- if o_move in boardPlaces:
- if board[o_move] == " ":
- board[o_move] = "O"
- else:
- print("That Spot isn't empty!")
- o_turn()
- else:
- print("That's Not What I Told You To Enter!")
- o_turn()
- else:
- if board["mid-M"] == " ":
- board["mid-M"] = "O"
- else:
- for f in gameEndings:
- gameEnding = gameEndings[f]
- if board[gameEnding[0]] == "O" and board[gameEnding[1]] == "O":
- if board[toWin[f]] == " ":
- board[toWin[f]] = "O"
- print()
- printBoard(board)
- checkWin()
- x_turn()
- return
- for f2 in gameEndings:
- gameEnding = gameEndings[f2]
- if board[gameEnding[0]] == "X" and board[gameEnding[1]] == "X":
- if board[toWin[f2]] == " ":
- board[toWin[f2]] = "O"
- print()
- printBoard(board)
- checkWin()
- x_turn()
- return
- while True:
- randoPlace = random.choice(boardPlaces)
- if board[randoPlace] == " ":
- board[randoPlace] = "O"
- break
- print()
- printBoard(board)
- checkWin()
- x_turn()
- if not multiplayer:
- print("You are %r" % player)
- movesFirst = random.choice(["X", "O"])
- print("%r Moves First!" % movesFirst)
- printBoard(board)
- globals()["%s_turn" % movesFirst.lower()]()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement