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

Function askForPlayerMove

src/gamesbyexample/pegsolitaire.py:121–163  ·  view source on GitHub ↗

Let the player enter their move.

(board)

Source from the content-addressed store, hash-verified

119
120
121def askForPlayerMove(board):
122 """Let the player enter their move."""
123 while True:
124 # Ask the player to select a peg to move:
125 moveablePegs = getMoveablePegs(board)
126
127 if len(moveablePegs) == 0:
128 # No pegs left to move, which means game over.
129 print('You have run out of pegs to move! Game over.')
130 print('Your score is', getScore(board), 'out of 31.')
131 sys.exit()
132
133 # Let the player select which peg they want to move:
134 print('Enter peg to move: ' + ' '.join(moveablePegs) + ' QUIT')
135 space = input('> ').upper()
136
137 if space == 'QUIT':
138 print('Your score is', getScore(board), 'out of 31.')
139 print('Thanks for playing!')
140 sys.exit()
141
142 if space in moveablePegs:
143 break
144
145 # Get the possible directions that the selected peg can jump:
146 possibleDirections = []
147 for direction in [NORTH, SOUTH, EAST, WEST]:
148 if canMoveInDirection(board, space, direction):
149 possibleDirections.append(direction)
150
151 if len(possibleDirections) == 1:
152 # There is only one possible direction to jump, so select it:
153 jumpDirection = possibleDirections[0]
154 else:
155 while True:
156 # Ask the player which direction to jump:
157 print('Enter direction: ' + ' '.join(possibleDirections))
158 jumpDirection = input('> ').upper()
159
160 if jumpDirection in possibleDirections:
161 break
162
163 return (space, jumpDirection)
164
165
166def makeMove(board, space, direction):

Callers 1

mainFunction · 0.70

Calls 3

getMoveablePegsFunction · 0.85
canMoveInDirectionFunction · 0.85
getScoreFunction · 0.70

Tested by

no test coverage detected