()
| 8 | |
| 9 | |
| 10 | def main(): |
| 11 | print('Welcome to Tic-Tac-Toe!') |
| 12 | gameBoard = getBlankBoard() # Create a TTT board dictionary. |
| 13 | currentPlayer, nextPlayer = X, O # X goes first, O goes next. |
| 14 | |
| 15 | while True: # Main game loop. |
| 16 | # Display the board on the screen: |
| 17 | print(getBoardStr(gameBoard)) |
| 18 | |
| 19 | # Keep asking the player until they enter a number 1-9: |
| 20 | move = None |
| 21 | while not isValidSpace(gameBoard, move): |
| 22 | print('What is {}\'s move? (1-9)'.format(currentPlayer)) |
| 23 | move = input('> ') |
| 24 | updateBoard(gameBoard, move, currentPlayer) # Make the move. |
| 25 | |
| 26 | # Check if the game is over: |
| 27 | if isWinner(gameBoard, currentPlayer): # Check for a winner. |
| 28 | print(getBoardStr(gameBoard)) |
| 29 | print(currentPlayer + ' has won the game!') |
| 30 | break |
| 31 | elif isBoardFull(gameBoard): # Check for a tie. |
| 32 | print(getBoardStr(gameBoard)) |
| 33 | print('The game is a tie!') |
| 34 | break |
| 35 | # Switch turns to the next player: |
| 36 | currentPlayer, nextPlayer = nextPlayer, currentPlayer |
| 37 | print('Thanks for playing!') |
| 38 | |
| 39 | |
| 40 | def getBlankBoard(): |
no test coverage detected