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

Function main

src/gamesbyexample/tictactoeoop.py:12–40  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers 1

tictactoeoop.pyFile · 0.70

Calls 6

getBoardStrMethod · 0.95
isValidSpaceMethod · 0.95
updateBoardMethod · 0.95
isWinnerMethod · 0.95
isBoardFullMethod · 0.95
TTTBoardClass · 0.85

Tested by

no test coverage detected