()
| 18 | |
| 19 | |
| 20 | def main(): |
| 21 | print("""Four in a Row, by Al Sweigart al@inventwithpython.com |
| 22 | |
| 23 | Two players take turns dropping tiles into one of seven columns, trying |
| 24 | to 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 | |
| 54 | def getNewBoard(): |
no test coverage detected