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

Function makeMove

src/gamesbyexample/twentyfortyeight.py:148–202  ·  view source on GitHub ↗

Carries out the move on the board. The move argument is either 'W', 'A', 'S', or 'D' and the function returns the resulting board data structure.

(board, move)

Source from the content-addressed store, hash-verified

146
147
148def makeMove(board, move):
149 """Carries out the move on the board.
150
151 The move argument is either 'W', 'A', 'S', or 'D' and the function
152 returns the resulting board data structure."""
153
154 # The board is split up into four columns, which are different
155 # depending on the direction of the move:
156 if move == 'W':
157 allColumnsSpaces = [[(0, 0), (0, 1), (0, 2), (0, 3)],
158 [(1, 0), (1, 1), (1, 2), (1, 3)],
159 [(2, 0), (2, 1), (2, 2), (2, 3)],
160 [(3, 0), (3, 1), (3, 2), (3, 3)]]
161 elif move == 'A':
162 allColumnsSpaces = [[(0, 0), (1, 0), (2, 0), (3, 0)],
163 [(0, 1), (1, 1), (2, 1), (3, 1)],
164 [(0, 2), (1, 2), (2, 2), (3, 2)],
165 [(0, 3), (1, 3), (2, 3), (3, 3)]]
166 elif move == 'S':
167 allColumnsSpaces = [[(0, 3), (0, 2), (0, 1), (0, 0)],
168 [(1, 3), (1, 2), (1, 1), (1, 0)],
169 [(2, 3), (2, 2), (2, 1), (2, 0)],
170 [(3, 3), (3, 2), (3, 1), (3, 0)]]
171 elif move == 'D':
172 allColumnsSpaces = [[(3, 0), (2, 0), (1, 0), (0, 0)],
173 [(3, 1), (2, 1), (1, 1), (0, 1)],
174 [(3, 2), (2, 2), (1, 2), (0, 2)],
175 [(3, 3), (2, 3), (1, 3), (0, 3)]]
176
177 # The board data structure after making the move:
178 boardAfterMove = {}
179 for columnSpaces in allColumnsSpaces: # Loop over all 4 columns.
180 # Get the tiles of this column (The first tile is the "bottom"
181 # of the column):
182 firstTileSpace = columnSpaces[0]
183 secondTileSpace = columnSpaces[1]
184 thirdTileSpace = columnSpaces[2]
185 fourthTileSpace = columnSpaces[3]
186
187 firstTile = board[firstTileSpace]
188 secondTile = board[secondTileSpace]
189 thirdTile = board[thirdTileSpace]
190 fourthTile = board[fourthTileSpace]
191
192 # Form the column and combine the tiles in it:
193 column = [firstTile, secondTile, thirdTile, fourthTile]
194 combinedTilesColumn = combineTilesInColumn(column)
195
196 # Set up the new board data structure with the combined tiles:
197 boardAfterMove[firstTileSpace] = combinedTilesColumn[0]
198 boardAfterMove[secondTileSpace] = combinedTilesColumn[1]
199 boardAfterMove[thirdTileSpace] = combinedTilesColumn[2]
200 boardAfterMove[fourthTileSpace] = combinedTilesColumn[3]
201
202 return boardAfterMove
203
204
205def askForPlayerMove():

Callers 1

mainFunction · 0.70

Calls 1

combineTilesInColumnFunction · 0.85

Tested by

no test coverage detected