Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #dictionary to hold the board state
- theBoard = {"top-l":" ", "top-m":" ", "top-r":" ",
- "mid-l":" ", "mid-m":" ", "mid-r":" ",
- "bot-l":" ", "bot-m":" ", "bot-r":" "}
- #function to print the current board state
- def printBoard(board):
- print(board["top-l"] + "|" + board["top-m"] + "|" + board["top-r"])
- print("-+-+-")
- print(board["mid-l"] + "|" + board["mid-m"] + "|" + board["mid-r"])
- print("-+-+-")
- print(board["bot-l"] + "|" + board["bot-m"] + "|" + board["bot-r"])
- #function to check if the board is full
- #returns false if there is an empty space, returns true if the board is full
- def stalemate(board):
- check = True
- for i in board.values():
- if i == " ":
- check = False
- break
- return check
- #function to determine if a player has won
- def checkWinner(board, player):
- if board["top-l"]==board["top-m"] and board["top-m"]==board["top-r"]:
- if board["top-m"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif board["mid-l"]==board["mid-m"] and board["mid-m"]==board["mid-r"]:
- if board["mid-m"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif board["bot-l"]==board["bot-m"] and board["bot-m"]==board["bot-r"]:
- if board["bot-m"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif board["top-l"]==board["mid-l"] and board["mid-l"]==board["bot-l"]:
- if board["mid-l"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif board["top-m"]==board["mid-m"] and board["mid-m"]==board["bot-m"]:
- if board["mid-m"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif board["top-r"]==board["mid-r"] and board["mid-r"]==board["bot-r"]:
- if board["mid-r"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif board["top-l"]==board["mid-m"] and board["mid-m"]==board["bot-r"]:
- if board["mid-m"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif board["top-r"]==board["mid-m"] and board["mid-m"]==board["bot-l"]:
- if board["mid-m"] != " ":
- print(player + " has won the game!")
- return True
- else:
- return False
- elif stalemate(theBoard):
- print("The board is full, and the game is a draw!")
- return True
- else:
- return False
- #function to clear the board
- def clearBoard(board):
- board["top-l"] = " "
- board["top-m"] = " "
- board["top-r"] = " "
- board["mid-l"] = " "
- board["mid-m"] = " "
- board["mid-r"] = " "
- board["bot-l"] = " "
- board["bot-m"] = " "
- board["bot-r"] = " "
- #function to play the game one time to completion
- def theGame():
- turn = "X"
- winner = False
- print("Let's play tic-tac-toe!")
- print("The rows are: top, mid, and bot; the columns are l, m, and r.")
- print("Your selection should be formatted as 'top-m', etc.")
- printBoard(theBoard)
- while winner == False:
- print("Turn for " + turn + ". Move on which space?")
- move = input()
- #check to see if the selection is a valid spot
- while move.lower() not in theBoard:
- print("That is an invalid board space!")
- print("The rows are: top, mid, and bot; the columns are l, m, and r.")
- print("Your selection should be formatted as 'top-m', etc.")
- move = input()
- #check to see if the board spot is already occupied
- while theBoard[move.lower()] != " ":
- print("That board spot is already occupied! Please choose another place to move.")
- move = input()
- theBoard[move.lower()] = turn
- winner = checkWinner(theBoard, turn)
- if turn == "X":
- turn = "O"
- else:
- turn = "X"
- printBoard(theBoard)
- cont = True
- while cont == True:
- theGame()
- print("If you would like to play again, press 1. Otherwise, press any other key.")
- select = input()
- clearBoard(theBoard)
- if select == 1:
- cont == True
- else:
- cont == False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement