Change name Stone to more abstract Blocker.
This commit is contained in:
parent
e759052c60
commit
e79b9d20d6
|
@ -34,10 +34,7 @@ import itertools
|
||||||
from robotgame.logic.direction import *
|
from robotgame.logic.direction import *
|
||||||
import random
|
import random
|
||||||
|
|
||||||
class RollingStoneError(Exception):
|
class Blocker(object):
|
||||||
pass
|
|
||||||
|
|
||||||
class Stone(object):
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def step(playfield, width, height, old_pos, direc):
|
def step(playfield, width, height, old_pos, direc):
|
||||||
|
@ -47,7 +44,7 @@ def step(playfield, width, height, old_pos, direc):
|
||||||
"""
|
"""
|
||||||
pos = direc.next_pos(old_pos)
|
pos = direc.next_pos(old_pos)
|
||||||
x, y = pos
|
x, y = pos
|
||||||
if playfield.get(pos) is Stone or x < 0 or x >= width \
|
if playfield.get(pos) is Blocker or x < 0 or x >= width \
|
||||||
or y < 0 or y >= height:
|
or y < 0 or y >= height:
|
||||||
pos = old_pos
|
pos = old_pos
|
||||||
elif isDirection(playfield.get(pos)):
|
elif isDirection(playfield.get(pos)):
|
||||||
|
@ -81,7 +78,7 @@ def generate_simple_playfield(width, height, nturns, nstones):
|
||||||
* the playfield is completable in nturns or less
|
* the playfield is completable in nturns or less
|
||||||
* the playfield has at most nstones stones
|
* the playfield has at most nstones stones
|
||||||
|
|
||||||
Return (playfield : {(x, y): Direction | Stone},
|
Return (playfield : {(x, y): Direction | Blocker},
|
||||||
steps : int)
|
steps : int)
|
||||||
where (x, y) : (int, int)
|
where (x, y) : (int, int)
|
||||||
|
|
||||||
|
@ -151,7 +148,7 @@ def generate_simple_playfield(width, height, nturns, nstones):
|
||||||
break
|
break
|
||||||
pos = random.choice(list(emptys))
|
pos = random.choice(list(emptys))
|
||||||
emptys.remove(pos)
|
emptys.remove(pos)
|
||||||
playfield[pos] = Stone
|
playfield[pos] = Blocker
|
||||||
return playfield, len(used_fields) - 1
|
return playfield, len(used_fields) - 1
|
||||||
|
|
||||||
def generate_simple_unsolved_solvable_playfield(width, height, nturns, nstones):
|
def generate_simple_unsolved_solvable_playfield(width, height, nturns, nstones):
|
||||||
|
@ -162,7 +159,7 @@ def generate_simple_unsolved_solvable_playfield(width, height, nturns, nstones):
|
||||||
playfield = generate_simple_playfield(width, height, nturns, stones)
|
playfield = generate_simple_playfield(width, height, nturns, stones)
|
||||||
new_playfield, directions = {}, []
|
new_playfield, directions = {}, []
|
||||||
for pos, val in playfield.items():
|
for pos, val in playfield.items():
|
||||||
if val is Stone:
|
if val is Blocker:
|
||||||
new_playfield[pos] = val
|
new_playfield[pos] = val
|
||||||
else:
|
else:
|
||||||
directions.append(val)
|
directions.append(val)
|
||||||
|
@ -184,7 +181,7 @@ def print_playfield(playfield, width, height, hide_directions):
|
||||||
for (x, y), val in playfield.items():
|
for (x, y), val in playfield.items():
|
||||||
if isDirection(val) and hide_directions:
|
if isDirection(val) and hide_directions:
|
||||||
continue
|
continue
|
||||||
text[y][x] = '%' if val == Stone else repr(val).rsplit('.', 1)[1][0] \
|
text[y][x] = '%' if val == Blocker else repr(val).rsplit('.', 1)[1][0] \
|
||||||
if isDirection(val) else 'G'
|
if isDirection(val) else 'G'
|
||||||
print('\n'.join(''.join(line) for line in text))
|
print('\n'.join(''.join(line) for line in text))
|
||||||
|
|
||||||
|
|
|
@ -10,15 +10,15 @@ class RollingStoneTest(unittest.TestCase):
|
||||||
playfield_example_succeed = {
|
playfield_example_succeed = {
|
||||||
(0, 0): Down,
|
(0, 0): Down,
|
||||||
(0, 2): Right,
|
(0, 2): Right,
|
||||||
(1, 3): Stone,
|
(1, 3): Blocker,
|
||||||
(2, 1): Stone,
|
(2, 1): Blocker,
|
||||||
(2, 2): Down,
|
(2, 2): Down,
|
||||||
(2, 3): Right,
|
(2, 3): Right,
|
||||||
}
|
}
|
||||||
self.assertTrue(reaches_goal(playfield_example_succeed,
|
self.assertTrue(reaches_goal(playfield_example_succeed,
|
||||||
4, 4, 100, (0, 0), (3, 3)))
|
4, 4, 100, (0, 0), (3, 3)))
|
||||||
|
|
||||||
playfield_example_succeed[(1, 2)] = Stone
|
playfield_example_succeed[(1, 2)] = Blocker
|
||||||
self.assertFalse(reaches_goal(playfield_example_succeed,
|
self.assertFalse(reaches_goal(playfield_example_succeed,
|
||||||
4, 4, 100, (0, 0), (3, 3)))
|
4, 4, 100, (0, 0), (3, 3)))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue