def check_only_move(initial_data, expected, direction): arr = e2048._build_from_2048(np.array(initial_data)) board1 = e2048.Board.fromarray(arr) board2 = e2048.Board.fromarray(e2048._build_from_2048(np.array(initial_data))) moved = e2048._only_move(arr, direction) assert board1 == board2, "A new array is built" expected_board = e2048.Board.fromarray(expected) resulting_board = e2048.Board.fromarray(moved) assert expected_board == resulting_board
def test_only_move(): initial_data = [ [ 0, 0, 0, 0 ], [ 2, 2, 4, 8 ], [ 0, 0, 2, 2 ], [ 2, 2, 2, 2 ] ] expected_down = e2048._build_from_2048(np.array([ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 4, 8 ], [ 4, 4, 4, 4 ] ])) yield check_only_move, initial_data, expected_down, Directions.down expected_up = e2048._build_from_2048(np.array([ [ 4, 4, 4, 8 ], [ 0, 0, 4, 4 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ])) yield check_only_move, initial_data, expected_up, Directions.up expected_right = e2048._build_from_2048(np.array([ [ 0, 0, 0, 0 ], [ 0, 4, 4, 8 ], [ 0, 0, 0, 4 ], [ 0, 0, 4, 4 ] ])) yield check_only_move, initial_data, expected_right, Directions.right expected_left = e2048._build_from_2048(np.array([ [ 0, 0, 0, 0 ], [ 4, 4, 8, 0 ], [ 4, 0, 0, 0 ], [ 4, 4, 0, 0 ] ])) yield check_only_move, initial_data, expected_left, Directions.left
def test_potential_states(): # This can be moved up, down, left and right. # In each case, there will be one 0 (and therefore, # 2 potential new states) initial_data = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 2 ], [ 64, 32, 2, 2 ] ])) expected_up_2 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 4 ], [ 32, 64, 8, 2 ], [ 64, 32, 2, 2 ] ])) expected_up_4 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 4 ], [ 32, 64, 8, 2 ], [ 64, 32, 2, 4 ] ])) expected_down_2 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 2 ], [ 16, 8, 4, 16 ], [ 32, 64, 8, 2 ], [ 64, 32, 2, 4 ] ])) expected_down_4 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 4 ], [ 16, 8, 4, 16 ], [ 32, 64, 8, 2 ], [ 64, 32, 2, 4 ] ])) expected_right_2 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 2 ], [ 2, 64, 32, 4 ] ])) expected_right_4 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 2 ], [ 4, 64, 32, 4 ] ])) expected_left_2 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 2 ], [ 64, 32, 4, 2 ] ])) expected_left_4 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 2 ], [ 64, 32, 4, 4 ] ])) expected_potential_states = { Directions.left : { e2048.build_value(expected_left_2) : 0.9, e2048.build_value(expected_left_4) : 0.1, }, Directions.right : { e2048.build_value(expected_right_2) : 0.9, e2048.build_value(expected_right_4) : 0.1, }, Directions.up : { e2048.build_value(expected_up_2) : 0.9, e2048.build_value(expected_up_4) : 0.1, }, Directions.down : { e2048.build_value(expected_down_2) : 0.9, e2048.build_value(expected_down_4) : 0.1, }, } resulting_potential_states = e2048.potential_states(initial_data) assert pprint.pformat(resulting_potential_states) == pprint.pformat(expected_potential_states) # This can be moved up, down, left and right. # In each case, there will be one 0 (and therefore, # 2 potential new states) initial_data = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 16 ], [ 64, 32, 2, 2 ] ])) expected_left_2 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 16 ], [ 64, 32, 4, 2 ] ])) expected_left_4 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 16 ], [ 64, 32, 4, 4 ] ])) expected_right_2 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 16 ], [ 2, 64, 32, 4 ] ])) expected_right_4 = e2048._build_from_2048(np.array([ [ 2, 4, 8, 16 ], [ 16, 8, 4, 2 ], [ 32, 64, 8, 16 ], [ 4, 64, 32 , 4 ] ])) expected_potential_states = { Directions.left : { e2048.build_value(expected_left_2) : 0.9, e2048.build_value(expected_left_4) : 0.1, }, Directions.right : { e2048.build_value(expected_right_2) : 0.9, e2048.build_value(expected_right_4) : 0.1, }, } resulting_potential_states = e2048.potential_states(initial_data) assert str(pprint.pformat(resulting_potential_states)) == str(pprint.pformat(expected_potential_states))