Advertisement
Musical_Muze

Basic Tic Tac Toe Game

Nov 27th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. #dictionary to hold the board state
  2. theBoard = {"top-l":" ", "top-m":" ", "top-r":" ",
  3.             "mid-l":" ", "mid-m":" ", "mid-r":" ",
  4.             "bot-l":" ", "bot-m":" ", "bot-r":" "}
  5.  
  6. #function to print the current board state
  7. def printBoard(board):
  8.     print(board["top-l"] + "|" + board["top-m"] + "|" + board["top-r"])
  9.     print("-+-+-")
  10.     print(board["mid-l"] + "|" + board["mid-m"] + "|" + board["mid-r"])
  11.     print("-+-+-")
  12.     print(board["bot-l"] + "|" + board["bot-m"] + "|" + board["bot-r"])
  13.  
  14. #function to check if the board is full
  15. #returns false if there is an empty space, returns true if the board is full
  16. def stalemate(board):
  17.     check = True
  18.     for i in board.values():
  19.         if i == " ":
  20.             check = False
  21.             break
  22.     return check
  23.  
  24. #function to determine if a player has won
  25. def checkWinner(board, player):
  26.     if board["top-l"]==board["top-m"] and board["top-m"]==board["top-r"]:
  27.         if board["top-m"] != " ":
  28.             print(player + " has won the game!")
  29.             return True
  30.         else:
  31.             return False
  32.     elif board["mid-l"]==board["mid-m"] and board["mid-m"]==board["mid-r"]:
  33.         if board["mid-m"] != " ":
  34.             print(player + " has won the game!")
  35.             return True
  36.         else:
  37.             return False
  38.     elif board["bot-l"]==board["bot-m"] and board["bot-m"]==board["bot-r"]:
  39.         if board["bot-m"] != " ":
  40.             print(player + " has won the game!")
  41.             return True
  42.         else:
  43.             return False
  44.     elif board["top-l"]==board["mid-l"] and board["mid-l"]==board["bot-l"]:
  45.         if board["mid-l"] != " ":
  46.             print(player + " has won the game!")
  47.             return True
  48.         else:
  49.             return False
  50.     elif board["top-m"]==board["mid-m"] and board["mid-m"]==board["bot-m"]:
  51.         if board["mid-m"] != " ":
  52.             print(player + " has won the game!")
  53.             return True
  54.         else:
  55.             return False
  56.     elif board["top-r"]==board["mid-r"] and board["mid-r"]==board["bot-r"]:
  57.         if board["mid-r"] != " ":
  58.             print(player + " has won the game!")
  59.             return True
  60.         else:
  61.             return False
  62.     elif board["top-l"]==board["mid-m"] and board["mid-m"]==board["bot-r"]:
  63.         if board["mid-m"] != " ":
  64.             print(player + " has won the game!")
  65.             return True
  66.         else:
  67.             return False
  68.     elif board["top-r"]==board["mid-m"] and board["mid-m"]==board["bot-l"]:
  69.         if board["mid-m"] != " ":
  70.             print(player + " has won the game!")
  71.             return True
  72.         else:
  73.             return False
  74.     elif stalemate(theBoard):
  75.         print("The board is full, and the game is a draw!")
  76.         return True
  77.     else:
  78.         return False
  79.  
  80. #function to clear the board
  81. def clearBoard(board):
  82.     board["top-l"] = " "
  83.     board["top-m"] = " "
  84.     board["top-r"] = " "
  85.     board["mid-l"] = " "
  86.     board["mid-m"] = " "
  87.     board["mid-r"] = " "
  88.     board["bot-l"] = " "
  89.     board["bot-m"] = " "
  90.     board["bot-r"] = " "
  91.  
  92. #function to play the game one time to completion
  93. def theGame():
  94.     turn = "X"
  95.  
  96.     winner = False
  97.  
  98.     print("Let's play tic-tac-toe!")
  99.     print("The rows are: top, mid, and bot; the columns are l, m, and r.")
  100.     print("Your selection should be formatted as 'top-m', etc.")
  101.  
  102.     printBoard(theBoard)
  103.  
  104.     while winner == False:
  105.        
  106.         print("Turn for " + turn + ". Move on which space?")
  107.         move = input()
  108.        
  109.         #check to see if the selection is a valid spot
  110.         while move.lower() not in theBoard:
  111.             print("That is an invalid board space!")
  112.             print("The rows are: top, mid, and bot; the columns are l, m, and r.")
  113.             print("Your selection should be formatted as 'top-m', etc.")
  114.             move = input()
  115.            
  116.         #check to see if the board spot is already occupied
  117.         while theBoard[move.lower()] != " ":
  118.             print("That board spot is already occupied! Please choose another place to move.")
  119.             move = input()
  120.                  
  121.         theBoard[move.lower()] = turn
  122.         winner = checkWinner(theBoard, turn)
  123.                  
  124.         if turn == "X":
  125.             turn = "O"
  126.         else:
  127.             turn = "X"
  128.                  
  129.         printBoard(theBoard)
  130.  
  131. cont = True
  132.  
  133. while cont == True:
  134.     theGame()
  135.     print("If you would like to play again, press 1. Otherwise, press any other key.")
  136.     select = input()
  137.     clearBoard(theBoard)
  138.     if select == 1:
  139.         cont == True
  140.     else:
  141.         cont == False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement