Board
constructor, move_blank
, and __repr__
>>> b = Board('012345678') >>> b.tiles result: [['0', '1', '2'], ['3', '4', '5'], ['6', '7', '8']] >>> b.move_blank('left') result: False >>> b.move_blank('up') result: False >>> b.move_blank('right') result: True >>> b.tiles result: [['1', '0', '2'], ['3', '4', '5'], ['6', '7', '8']] >>> b.blank_r result: 0 >>> b.blank_c result: 1 >>> b.move_blank('right') result: True >>> b.move_blank('right') result: False >>> b.move_blank('down') result: True >>> b.move_blank('down') result: True >>> b.move_blank('down') result: False >>> b result: 1 2 5 3 4 8 6 7 _ >>> b.tiles result: [['1', '2', '5'], ['3', '4', '8'], ['6', '7', '0']] >>> b.blank_r result: 2 >>> b.blank_c result: 2
Board
__eq__
and copy()>>> b1 = Board('142358607') >>> b2 = Board('142358670') >>> b1 == b2 result: False >>> b3 = b2.copy() >>> b3.tiles result: [['1', '4', '2'], ['3', '5', '8'], ['6', '7', '0']] >>> b3.blank_r result: 2 >>> b3.blank_c result: 2 >>> b3 == b2 result: True >>> b2.tiles result: [['1', '4', '2'], ['3', '5', '8'], ['6', '7', '0']] >>> b2.blank_r result: 2 >>> b2.blank_c result: 2
State
constructor and is_goal
>>> s1 = State(Board('032415678'), None, 'init') >>> s1.num_moves result: 0 >>> s1.is_goal() result: False >>> s2 = State(Board('302415678'), s1, 'right') >>> s2.num_moves result: 1 >>> s2.predecessor result: 032415678-init-0 >>> s2.move result: 'right' >>> s3 = State(Board('312405678'), s2, 'down') >>> s3.num_moves result: 2 >>> s3.move result: 'down'
Searcher
should_add
, add_state
and add_states
>>> searcher = Searcher(-1) # no depth limit >>> s1 = State(Board('032415678'), None, 'init') >>> s2 = State(Board('302415678'), s1, 'right') >>> s3 = State(Board('312405678'), s2, 'down') >>> s4 = State(Board('032415678'), s2, 'left') >>> searcher.should_add(s1) result: True >>> searcher.should_add(s2) result: True >>> searcher.should_add(s3) result: True >>> searcher.should_add(s4) # would create a cycle! result: False >>> searcher.add_states([s1, s2, s3, s4]) >>> searcher.states # should only include s1, s2 and s3 result: [032415678-init-0, 302415678-right-1, 312405678-down-2] >>> searcher = Searcher(2) # depth limit of 2 >>> searcher.add_state(s2) >>> searcher.states result: [302415678-right-1] >>> s5 = State(Board('320415678'), s2, 'right') >>> s5.num_moves result: 2 >>> searcher.should_add(s5) result: True >>> s6 = State(Board('325410678'), s5, 'down') >>> s6.num_moves result: 3 >>> searcher.should_add(s6) result: False >>> searcher.add_states([s5, s6]) >>> searcher.states result: [302415678-right-1, 320415678-right-2]
Last updated on April 17, 2024.