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

Function getValidMoves

src/gamesbyexample/parkingvalet.py:125–166  ·  view source on GitHub ↗

Return a list of valid moves that can be made on the board.

(board)

Source from the content-addressed store, hash-verified

123
124
125def getValidMoves(board):
126 """Return a list of valid moves that can be made on the board."""
127 validMoves = []
128 for x in range(board['width']):
129 for y in range(board['height']):
130 if board[(x, y)] in (EMPTY_SPACE, WALL):
131 continue # Skip this empty or wall space.
132
133 xNotOnLeftEdge = x != 0
134 xNotOnRightEdge = x != board['width'] - 1
135 yNotOnTopEdge = y != 0
136 yNotOnBottomEdge = y != board['height'] - 1
137
138 # Check if the car at x, y can move down.
139 if (yNotOnTopEdge
140 and board[(x, y)] == board[(x, y - 1)]
141 and y + 1 < board['height']
142 and board[(x, y + 1)] == EMPTY_SPACE):
143 validMoves.append(board[(x, y)] + 'D')
144
145 # Check if the car at x, y can move up.
146 if (yNotOnBottomEdge
147 and board[(x, y)] == board[(x, y + 1)]
148 and y - 1 >= 0
149 and board[(x, y - 1)] == EMPTY_SPACE):
150 validMoves.append(board[(x, y)] + 'U')
151
152 # Check if the car at x, y can move right.
153 if (xNotOnLeftEdge
154 and board[(x, y)] == board[(x - 1, y)]
155 and x + 1 < board['width']
156 and board[(x + 1, y)] == EMPTY_SPACE):
157 validMoves.append(board[(x, y)] + 'R')
158
159 # Check if the car at x, y can move left.
160 if (xNotOnRightEdge
161 and board[(x, y)] == board[(x + 1, y)]
162 and x - 1 >= 0
163 and board[(x - 1, y)] == EMPTY_SPACE):
164 validMoves.append(board[(x, y)] + 'L')
165
166 return validMoves
167
168
169def makeMove(board, move):

Callers 2

makeMoveFunction · 0.70
askForPlayerMoveFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected