MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

src/gamesbyexample/tictactoe.py:10–37  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8
9
10def 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
40def getBlankBoard():

Callers 1

tictactoe.pyFile · 0.70

Calls 6

getBoardStrFunction · 0.85
isValidSpaceFunction · 0.85
updateBoardFunction · 0.85
getBlankBoardFunction · 0.70
isWinnerFunction · 0.70
isBoardFullFunction · 0.70

Tested by

no test coverage detected