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

Function main

src/gamesbyexample/fourinarow.py:20–51  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18
19
20def main():
21 print("""Four in a Row, by Al Sweigart al@inventwithpython.com
22
23Two players take turns dropping tiles into one of seven columns, trying
24to make four in a row horizontally, vertically, or diagonally.
25""")
26
27 # Set up a new game:
28 gameBoard = getNewBoard()
29 playerTurn = PLAYER_X
30
31 while True: # Run a player's turn.
32 # Display the board and get player's move:
33 displayBoard(gameBoard)
34 playerMove = askForPlayerMove(playerTurn, gameBoard)
35 gameBoard[playerMove] = playerTurn
36
37 # Check for a win or tie:
38 if isWinner(playerTurn, gameBoard):
39 displayBoard(gameBoard) # Display the board one last time.
40 print('Player ' + playerTurn + ' has won!')
41 sys.exit()
42 elif isFull(gameBoard):
43 displayBoard(gameBoard) # Display the board one last time.
44 print('There is a tie!')
45 sys.exit()
46
47 # Switch turns to other player:
48 if playerTurn == PLAYER_X:
49 playerTurn = PLAYER_O
50 elif playerTurn == PLAYER_O:
51 playerTurn = PLAYER_X
52
53
54def getNewBoard():

Callers 1

fourinarow.pyFile · 0.70

Calls 5

getNewBoardFunction · 0.70
displayBoardFunction · 0.70
askForPlayerMoveFunction · 0.70
isWinnerFunction · 0.70
isFullFunction · 0.70

Tested by

no test coverage detected