Exemplo n.º 1
0
 def test_score_player(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init (control x))
     (<= (goal ?player 100) (true (control ?player)))
     '''
     fsm.store(data=data)
     self.assertEqual(100, fsm.score('x'))
Exemplo n.º 2
0
 def test_legal_no_such_player(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init 1)
     (<= (legal x ?x) (true ?x))
     '''
     fsm.store(data=data)
     self.assertFalse(fsm.legal(player='o', move='1'))
Exemplo n.º 3
0
 def test_legal_player_and_move_failure(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init 1)
     (<= (legal x ?x) (true ?x))
     '''
     fsm.store(data=data)
     self.assertFalse(fsm.legal(player='x', move='2'))
Exemplo n.º 4
0
 def test_is_terminal_failure(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init (control x))
     (init (cell o))
     (<= terminal (true (control ?x)) (true (cell ?x)))
     '''
     fsm.store(data=data)
     self.assertFalse(fsm.is_terminal())
Exemplo n.º 5
0
 def test_store_no_roles_error(self):
     fsm = StateMachine()
     data = '''
     (init (cell a))
     (init (cell b))
     (init (cell c))
     (init (cell d))
     '''
     with self.assertRaises(GameError):
         fsm.store(data=data)
Exemplo n.º 6
0
 def test_move_nonexistent_player_error(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init (cell 1 1 b))
     (<= (legal x ?x) (true ?x))
     '''
     fsm.store(data=data)
     with self.assertRaises(GameError):
         fsm.move('o', '(cell 1 1 b)')
Exemplo n.º 7
0
 def test_store_roles(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (role o)
     (init (cell a))
     (init (cell b))
     (init (cell c))
     (init (cell d))
     '''
     fsm.store(data=data)
     self.assertEqual(set(['x', 'o']), fsm.players)
Exemplo n.º 8
0
 def test_store_true_error(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (role o)
     (true (cell a))
     (true (cell b))
     (true (cell c))
     (true (cell d))
     '''
     with self.assertRaises(GameError):
         fsm.store(data=data)
Exemplo n.º 9
0
 def test_score_all_players(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (role o)
     (init (control x))
     (enemies x o) (enemies o x)
     (<= (goal ?player 100) (true (control ?player)))
     (<= (goal ?player 0) (true (control ?other)) (enemies ?player ?other))
     '''
     fsm.store(data=data)
     self.assertEqual({'x': 100, 'o': 0}, fsm.score())
Exemplo n.º 10
0
 def test_legal_all_moves(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (role o)
     (init 1) (init 2) (init 3) (init 4)
     (even 2) (even 4)
     (odd 1) (odd 3)
     (<= (legal x ?x) (true ?x) (even ?x))
     (<= (legal o ?x) (true ?x) (odd ?x))
     '''
     fsm.store(data=data)
     self.assertEqual({'x': ['2', '4'], 'o': ['1', '3']}, fsm.legal())
Exemplo n.º 11
0
 def test_legal_player_moves(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (role o)
     (init 1) (init 2) (init 3) (init 4)
     (even 2) (even 4)
     (odd 1) (odd 3)
     (<= (legal x ?x) (true ?x) (even ?x))
     (<= (legal o ?x) (true ?x) (odd ?x))
     '''
     fsm.store(data=data)
     self.assertEqual(['2', '4'], fsm.legal(player='x'))
Exemplo n.º 12
0
 def test_store_init(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (role o)
     (init (cell a))
     (init (cell b))
     (init (cell c))
     (init (cell d))
     '''
     fsm.store(data=data)
     query = ASTNode.new('true')
     query.children = [ASTNode.new('?state')]
     results = [{k: str(d[k]) for k in d} for d in fsm.db.query(query)]
     for i in ('a', 'b', 'c', 'd'):
         self.assertIn({'?state': '(cell %s)' % i}, results)
Exemplo n.º 13
0
 def test_move_player_twice_error(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init (cell 1 1 b))
     (<= (legal x ?x) (true ?x))
     '''
     fsm.store(data=data)
     fsm.move('x', '(cell 1 1 b)')
     with self.assertRaises(GameError):
         fsm.move('x', '(cell 2 2 b)')
Exemplo n.º 14
0
 def test_score_no_such_player_error(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init (control x))
     (<= (goal ?player 100) (true (control ?player)))
     '''
     fsm.store(data=data)
     with self.assertRaises(GameError):
         fsm.score('o')
Exemplo n.º 15
0
 def test_move_illegal_error(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init 1)
     (<= (legal x ?x) (true ?x))
     '''
     fsm.store(data=data)
     with self.assertRaises(GameError):
         fsm.move('x', '2')
Exemplo n.º 16
0
    def test_next(self):
        fsm = StateMachine()
        data = '''
        (role x)
        (role o)
        (init (control x))
        (init (cell 0 0 b))
        (init (cell 0 1 b))
        (init (cell 1 0 b))
        (init (cell 1 1 b))

        (<= (legal ?player (mark ?x ?y))
            (true (cell ?x ?y b))
            (true (control ?player)))
        (<= (legal x noop) (true (control o)))
        (<= (legal o noop) (true (control x)))

        ; swap control on every turn
        (<= (next (control x)) (true (control o)))
        (<= (next (control o)) (true (control x)))

        (<= (next (cell ?x ?y ?player))
            (does ?player (mark ?x ?y))
            (true (cell ?x ?y b)))

        (<= (next (cell ?x ?y b))
            (does ?player (mark ?m ?n))
            (true (cell ?x ?y b))
            (or (distinct ?m ?x) (distinct ?n ?y)))
        '''
        fsm.store(data=data)
        fsm.move('x', '(mark 0 0)')
        fsm.move('o', 'noop')
        next = fsm.next()
        fsm.db.facts = {}
        states = [str(x[0]) for x in next.db.facts[('true', 1)]]
        self.assertIn('(cell 0 0 x)', states)
        self.assertIn('(control o)', states)
        self.assertNotIn(('does', 2), next.db.facts)
Exemplo n.º 17
0
 def test_move_player(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init 1)
     (<= (legal x ?x) (true ?x))
     '''
     fsm.store(data=data)
     fsm.move('x', '1')
     query = ASTNode.new('does')
     query.children = [ASTNode.new(x) for x in ('x', '1')]
     self.assertTrue(fsm.db.query(query))
Exemplo n.º 18
0
 def test_next_no_moves_error(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (role o)
     (init (cell a))
     (init (cell b))
     (init (cell c))
     (init (cell d))
     '''
     fsm.store(data=data)
     with self.assertRaises(GameError):
         fsm.next()
Exemplo n.º 19
0
 def test_move_player_complex(self):
     fsm = StateMachine()
     data = '''
     (role x)
     (init (cell 1 1 b))
     (<= (legal x ?x) (true ?x))
     '''
     fsm.store(data=data)
     fsm.move('x', '(cell 1 1 b)')
     cell = ASTNode.new('cell')
     cell.children = [ASTNode.new(x) for x in ('1', '1', 'b')]
     query = ASTNode.new('does')
     query.children = [ASTNode.new('x'), cell]
     self.assertTrue(fsm.db.query(query))
Exemplo n.º 20
0
    def test_hash(self):
        one = StateMachine()
        data = '''
        (role x)
        (role o)
        (init (cell a))
        (init (cell b))
        (init (cell c))
        (init (control x))
        '''
        one.store(data=data)

        two = StateMachine()
        data = '''
        (role x)
        (role o)
        (init (cell b))
        (init (cell a))
        (init (control x))
        (init (cell c))
        '''
        two.store(data=data)
        self.assertEqual(hash(one), hash(two))

        three = StateMachine()
        data = '''
        (role x)
        (role o)
        (init (cell b))
        (init (cell a))
        (init (control o))
        (init (cell c))
        '''
        three.store(data=data)
        self.assertNotEqual(hash(two), hash(three))