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

Function getNewBoard

src/gamesbyexample/hungryrobots.py:62–92  ·  view source on GitHub ↗

Returns a dictionary that represents the board. The keys are (x, y) tuples of integer indexes for board positions, the values are WALL, EMPTY_SPACE, or DEAD_ROBOT. The dictionary also has the key 'teleports' for the number of teleports the player has left. The living robots are store

()

Source from the content-addressed store, hash-verified

60
61
62def getNewBoard():
63 """Returns a dictionary that represents the board. The keys are
64 (x, y) tuples of integer indexes for board positions, the values are
65 WALL, EMPTY_SPACE, or DEAD_ROBOT. The dictionary also has the key
66 'teleports' for the number of teleports the player has left.
67 The living robots are stored separately from the board dictionary."""
68 board = {'teleports': NUM_TELEPORTS}
69
70 # Create an empty board:
71 for x in range(WIDTH):
72 for y in range(HEIGHT):
73 board[(x, y)] = EMPTY_SPACE
74
75 # Add walls on the edges of the board:
76 for x in range(WIDTH):
77 board[(x, 0)] = WALL # Make top wall.
78 board[(x, HEIGHT - 1)] = WALL # Make bottom wall.
79 for y in range(HEIGHT):
80 board[(0, y)] = WALL # Make left wall.
81 board[(WIDTH - 1, y)] = WALL # Make right wall.
82
83 # Add the random walls:
84 for i in range(NUM_WALLS):
85 x, y = getRandomEmptySpace(board, [])
86 board[(x, y)] = WALL
87
88 # Add the starting dead robots:
89 for i in range(NUM_DEAD_ROBOTS):
90 x, y = getRandomEmptySpace(board, [])
91 board[(x, y)] = DEAD_ROBOT
92 return board
93
94
95def getRandomEmptySpace(board, robots):

Callers 1

mainFunction · 0.70

Calls 1

getRandomEmptySpaceFunction · 0.85

Tested by

no test coverage detected