コード例 #1
0
ファイル: test_2048.py プロジェクト: porduna/e2048
def check_building_arrays_equals(value):
    arr_built     = e2048.build_array(value)
    value_built = e2048.build_value(arr_built)
    print hex(value_built), hex(value)
    assert value_built == value
コード例 #2
0
ファイル: test_2048.py プロジェクト: porduna/e2048
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))