Skip to content

Commit

Permalink
fix: Fix possibility that two generals are generated in the same loca…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
strakam committed Sep 28, 2024
1 parent 3ee9a68 commit 46c9a82
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions generals/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ def generate_map(

# Place generals on random squares - generals_positions is a list of two tuples
if general_positions is None:
general_positions = [
(rng.integers(0, grid_width), rng.integers(0, grid_height)),
(rng.integers(0, grid_width), rng.integers(0, grid_height)),
]
general_positions = []
while len(general_positions) < 2:
position = tuple(rng.integers(0, grid_dims))
if position not in general_positions:
general_positions.append(position)

for i, idx in enumerate(general_positions):
map[idx[0], idx[1]] = chr(ord("A") + i)

# Convert to string
map = "\n".join(["".join(row) for row in map])
map = Mapper.stringify_map(map)

# Iterate until map is valid
if Mapper.validate_map(map):
return map
Expand Down Expand Up @@ -114,6 +115,8 @@ def dfs(map, visited, square):

map = Mapper.numpify_map(map)
generals = np.argwhere(np.isin(map, ["A", "B"]))
print(Mapper.stringify_map(map))
print()
start, end = generals[0], generals[1]
visited = np.zeros_like(map, dtype=bool)
dfs(map, visited, start)
Expand Down

0 comments on commit 46c9a82

Please sign in to comment.