예제 #1
0
    def test_walking_entities(self):
        """Walking entities"""
        for entity in ENTITIES:
            grid = sg("...\n.{}.\n...".format(entity))

            # Make target grid states
            target_format = lambda x: sg(x.format(entity))
            targets = map(
                target_format,
                (
                    ".{}.\n...\n...",
                    "...\n...\n.{}.",
                    "...\n{}..\n...",
                    "...\n..{}\n...",
                ),
            )

            # 'Entity' is you rule
            behaviours = {entity.lower(): mb(you=True)}

            for step, target in zip(STEPS, targets):
                with self.subTest(grid):
                    grid2, youwin = timestep(grid, behaviours, step)
                    self.assertEqual(grid2, target)
                    self.assertIsNone(youwin)
예제 #2
0
 def test_identity_crisis(self):
     """Baba is not you"""
     for entity in ENTITIES:
         grid = sg("{}.".format(entity))
         behaviour = {entity.lower(): mb(you=False)}
         with self.subTest(entity):
             grid2, youwin = timestep(grid, behaviour, ">")
             self.assertEqual(grid2, grid)
             self.assertIsNone(youwin)
예제 #3
0
 def test_win_push_wall(self):
     """Walk into a win which is also push, but against the wall"""
     grids = map(sg, ("BF", "BFR"))
     behaviour = {**biy, **rip, "f": mb(win=True, push=True)}
     for grid in grids:
         with self.assertRaises(YouWin):
             _, youwin = timestep(grid, behaviour, ">")
             if youwin:
                 raise youwin
예제 #4
0
 def test_pushing(self):
     """Test pushing all the pieces of text"""
     behaviour = {"t": mb(push=True)}
     for text in chain(NOUNS, PROPERTIES, "i"):
         format_fun = lambda x: sp(x.format(text))
         piles = map(format_fun, ("{0}.", "{0}{0}.", "{0}.{0}"))
         targets = map(format_fun, (".{0}", ".{0}{0}", ".{0}{0}"))
         for pile, target in zip(piles, targets):
             with self.subTest(text=text):
                 self.assertEqual(attempt_to_move(pile, behaviour), target)
예제 #5
0
 def test_win(self):
     """Walk into a win"""
     grids = map(sg, ("BF", "BF."))
     behaviour = {**biy, "f": mb(win=True)}
     for grid in grids:
         with self.subTest(grid):
             with self.assertRaises(YouWin):
                 _, youwin = timestep(grid, behaviour, ">")
                 if youwin:
                     raise youwin
예제 #6
0
 def test_rock_doesnt_win(self):
     """Push a rock into a win"""
     grids = map(sg, ("BRF", "BRF."))
     targets = map(sg, ("BRF", ".BRF"))
     behaviour = {**biy, **rip, "f": mb(win=True, push=True)}
     for grid, target in zip(grids, targets):
         with self.subTest(grid):
             try:
                 grid2, youwin = timestep(grid, behaviour, ">")
                 if youwin:
                     raise youwin
                 self.assertEqual(grid2, target)
             except YouWin:
                 self.fail("'timestep' raised YouWin unexpectedly!")
예제 #7
0
 def test_dont_win_but_push(self):
     """Walk into a win which is also push"""
     grids = map(sg, ("BF.", "BFR."))
     targets = map(sg, (".BF", ".BFR"))
     behaviour = {**biy, **rip, "f": mb(win=True, push=True)}
     for grid, target in zip(grids, targets):
         with self.subTest(grid):
             try:
                 grid2, youwin = timestep(grid, behaviour, ">")
                 if youwin:
                     raise youwin
                 self.assertEqual(grid2, target)
             except YouWin:
                 self.fail("'timestep' raised YouWin unexpectedly!")
예제 #8
0
import unittest
from itertools import chain

# Add '.' to path so running this file by itself also works
import os, sys

sys.path.append(os.path.realpath("."))
from baba.play import attempt_to_move, UnableToMove
from baba.utils import make_behaviour as mb
from baba.utils import ENTITIES, PROPERTIES, NOUNS, TEXT

# 'Rock is push' and, implicitly, 'Wall is stop'
behaviours = {"r": mb(push=True), "w": mb()}

# String to pile
sp = lambda string: tuple(j for j in string)


class RockAndWall(unittest.TestCase):
    def test_simple_null(self):
        """Simple null cases"""
        piles = map(sp, (".", ".R", ".RR", ".R.", ".R.R", ".W", ".WW", ".RW"))
        for pile in piles:
            with self.subTest(pile):
                self.assertEqual(attempt_to_move(pile, behaviours), pile)

    def test_simple_moves(self):
        """Simple successful moves"""
        piles = map(sp, ("R.", "R..", "R.R", "R.W", "RR.W", "R.R..W"))
        targets = map(sp, (".R", ".R.", ".RR", ".RW", ".RRW", ".RR..W"))
        for pile, target in zip(piles, targets):
예제 #9
0
import unittest

# Add '.' to path so running this file by itself also works
import os, sys

sys.path.append(os.path.realpath("."))
from baba.play import timestep, STEPS, YouWin

from baba.utils import string_to_grid as sg
from baba.utils import make_behaviour as mb
from baba.utils import PROPERTIES, ENTITIES

# 'Baba is you' and 'Flag is win' rules
biy = {"b": mb(you=True)}
rip = {"r": mb(push=True)}


class Walking(unittest.TestCase):
    def test_walking_baba(self):
        """Simple walking baba"""
        grids = map(sg, (".\nB", "B\n.", ".B", "B."))
        targets = map(sg, ("B\n.", ".\nB", "B.", ".B"))
        for grid, step, target in zip(grids, STEPS, targets):
            with self.subTest(grid):
                grid2, youwin = timestep(grid, biy, step)
                self.assertEqual(grid2, target)
                self.assertIsNone(youwin)

    def test_walking_entities(self):
        """Walking entities"""
        for entity in ENTITIES:
예제 #10
0
from copy import deepcopy

# Add '.' to path so running this file by itself also works
import os, sys

sys.path.append(os.path.realpath("."))

from baba.rules import ruleparser
from baba.utils import make_behaviour as mb
from baba.utils import NOUNS

# from baba.utils import string_to_grid as sg

# Default behaviours
db = {noun: (mb()) for noun in NOUNS}
db["t"] = mb(push=True)


class ParseRules(unittest.TestCase):
    def test_grid_rules(self):
        """Just behaviours"""
        rules = [("b", "y"), ("f", "n"), ("r", "p")]

        target_behaviours = deepcopy(db)
        target_behaviours["b"]["y"] = True
        target_behaviours["f"]["n"] = True
        target_behaviours["r"]["p"] = True

        behaviours, swaps = ruleparser(rules)
        for key in behaviours: