Fixed a few bugs and added laser mirror room generator.

This commit is contained in:
Niels Serup
2012-08-08 18:37:09 +02:00
parent 20e06e572e
commit 7666891baa
6 changed files with 157 additions and 12 deletions

View File

@@ -30,9 +30,9 @@ direction-changing turns. Also has a pseudo-random playfield generator.
from __future__ import print_function
import math
import random
import itertools
from robotgame.logic.direction import *
import random
class Blocker(object):
pass
@@ -70,7 +70,8 @@ def reaches_goal(playfield, width, height, max_steps, start_pos, goal_pos):
return False
def generate_simple_playfield(width, height, nturns, nstones):
def generate_simple_playfield(width, height, nturns, nstones,
do_transpose=None, start_inside=True):
"""
Generate a completable playfield where:
* the starting position is in the upper left corner
@@ -78,7 +79,7 @@ def generate_simple_playfield(width, height, nturns, nstones):
* the playfield is completable in nturns or less
* the playfield has at most nstones stones
Return (playfield : {(x, y): Direction | Blocker},
Return (playfield : {(x, y): Direction | Blocker | None},
steps : int)
where (x, y) : (int, int)
@@ -95,11 +96,13 @@ def generate_simple_playfield(width, height, nturns, nstones):
nturns = min(2 * (width - 1), 2 * (height - 1) - 1)
min_width, min_height = _min_play_size(nturns)
do_transpose = random.choice((True, False))
if do_transpose is None:
do_transpose = random.choice((True, False))
if do_transpose:
width, height = height, width
turns, stones = [((0, 0), None)], []
turns = [((0, 0), None)]
stones = []
x, y = (0, 0)
not_allowed_y = []
offset_x = 0
@@ -110,6 +113,7 @@ def generate_simple_playfield(width, height, nturns, nstones):
turns.append(((x, height - 1), Right))
break
elif missing == 0:
turns[-1] = ((width - 1, turns[-1][0][1]), Down)
break
else:
allowed = set(range(0, height)) - set(not_allowed_y)
@@ -129,6 +133,8 @@ def generate_simple_playfield(width, height, nturns, nstones):
turns.append(((x1, y1), None))
x, y = x1, y1
turns.append(((width - 1, height - 1), None))
if not start_inside:
del turns[0]
if do_transpose:
turns[:] = [((y, x), {
@@ -181,7 +187,7 @@ def print_playfield(playfield, width, height, hide_directions):
for (x, y), val in playfield.items():
if isDirection(val) and hide_directions:
continue
text[y][x] = '%' if val == Blocker else repr(val).rsplit('.', 1)[1][0] \
text[y][x] = '%' if val is Blocker else repr(val).rsplit('.', 1)[1][0] \
if isDirection(val) else 'G'
print('\n'.join(''.join(line) for line in text))