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

Function askForPlayerMove

src/gamesbyexample/fourinarow.py:90–116  ·  view source on GitHub ↗

Let a player select a column on the board to drop a tile into. Returns a tuple of the (column, row) that the tile falls into.

(playerTile, board)

Source from the content-addressed store, hash-verified

88
89
90def askForPlayerMove(playerTile, board):
91 """Let a player select a column on the board to drop a tile into.
92
93 Returns a tuple of the (column, row) that the tile falls into."""
94 while True: # Keep asking player until they enter a valid move.
95 print('Player {}, enter a column or QUIT:'.format(playerTile))
96 response = input('> ').upper().strip()
97
98 if response == 'QUIT':
99 print('Thanks for playing!')
100 sys.exit()
101
102 if response not in COLUMN_LABELS:
103 print('Enter a number from 1 to {}.'.format(BOARD_WIDTH))
104 continue # Ask player again for their move.
105
106 columnIndex = int(response) - 1 # -1 for 0-based the index.
107
108 # If the column is full, ask for a move again:
109 if board[(columnIndex, 0)] != EMPTY_SPACE:
110 print('That column is full, select another one.')
111 continue # Ask player again for their move.
112
113 # Starting from the bottom, find the first empty space.
114 for rowIndex in range(BOARD_HEIGHT - 1, -1, -1):
115 if board[(columnIndex, rowIndex)] == EMPTY_SPACE:
116 return (columnIndex, rowIndex)
117
118
119def isFull(board):

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected