获取与给定值相关的特定数组编号的值

时间:2015-01-18 13:35:30

标签: c# arrays

我正在尝试确定邻居在轮盘赌轮上给定数字的赌注。 目前我将口袋号码传递给一个功能,下面是我的代码。 “pocket_number()[0]”是我想要确定邻居的数字的值。

int[] neightbourbets = neighbourbets(pocket_number()[0]);

neighbourbets()如下所示(这会输出一个元素数组,所以我的程序中的其他地方可以从同一个数组结构中提取它们)。目前,我有一种粗略的方法来确定邻居的赌注并获得函数来说明哪一个数字是它的两侧的6个数字。 有一个更好的方法吗?我发现其中一个问题(我已经克服了以下内容)是因为我想知道“0”的邻居,例如这意味着代码需要从数组的末尾获取数字。

public int[] neighbourbets(int x)
{
    int[] pocket_array = new[] {0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26};
    int predictednum = Array.IndexOf(pocket_array,x); //element number of pocket_array for chosen number

    int[] neighbourbets = new[] {0,0,0,0,0,0,0,0,0,0,0,0,0};

    neighbourbets[0] = predictednum;
    neighbourbets[1] = predictednum+1;
    neighbourbets[2] = predictednum+2;
    neighbourbets[3] = predictednum+3;
    neighbourbets[4] = predictednum+4;
    neighbourbets[5] = predictednum+5;
    neighbourbets[6] = predictednum+6;
    neighbourbets[7] = predictednum-1;
    neighbourbets[8] = predictednum-2;
    neighbourbets[9] = predictednum-3;
    neighbourbets[10] = predictednum-4;
    neighbourbets[11] = predictednum-5;
    neighbourbets[12] = predictednum-6;

    for (int i = 0; i < neighbourbets.Length; i++)
    {
        //clockwise neighours
        if (neighbourbets[i] == -1) {
                neighbourbets[i] = 36;
        }
        if (neighbourbets[i] == -2) {
                neighbourbets[i] = 35;
        }
        if (neighbourbets[i] == -3) {
                neighbourbets[i] = 34;
        }
        if (neighbourbets[i] == -4) {
                neighbourbets[i] = 33;
        }
        if (neighbourbets[i] == -5) {
                neighbourbets[i] = 32;
        }
        if (neighbourbets[i] == -6) {
                neighbourbets[i] = 31;
        }

        //anticlockwise neighbours  
        if (neighbourbets[i] == 37) {
                neighbourbets[i] = 0;
        }
        if (neighbourbets[i] == 38) {
                neighbourbets[i] = 1;
        }
        if (neighbourbets[i] == 39) {
                neighbourbets[i] = 2;
        }
        if (neighbourbets[i] == 40) {
                neighbourbets[i] = 3;
        }
        if (neighbourbets[i] == 41) {
                neighbourbets[i] = 4;
        }
        if (neighbourbets[i] == 42) {
                neighbourbets[i] = 5;
        }
    }  
    return neighbourbets;
}

任何帮助或指导表示赞赏! :)

2 个答案:

答案 0 :(得分:1)

编写一个小帮助函数来包装索引:

private int GetPocketIndex( int start, int offset, int count )
{
    int pos = ( start + offset ) % count;
    if( pos >= 0 )
        return pos;
    else
        return count + pos; // pos is negative so we use +
}

当模数超过最大值时,模数将有助于它环绕,而if将以最小值完成。尽管如此,这可能会更容易实现,但目前我不能这样做。

然后,如果您需要特定的订单,可能是这样的:

int[] offsets = new int[] { 0,
                            1,  2,  3,  4,  5,  6,
                           -1, -2, -3, -4, -5, -6 };    
int[] neighbourbets = new int[offsets.Length];

for( int i = 0; i < offsets.Length; i++ )
    neighbourbets[i] = GetPocketIndex( predictednum, offsets[i], pocket_array.Length );

或者,如果有任何订单:

int count = 6;
int[] neighbourbets = new int[count * 2 + 1];
for( int i = 0; i < neighbourbets.Length; i++ )
    neightbourbets[i] = GetPocketIndex( predictednum, i - count, pocket_array.Length );

答案 1 :(得分:0)

以下内容将为您提供结果数组中间的x以及左侧和右侧的邻居的结果:

public static int[] neighbourbets2(int x, int neighborCount)
{
    int[] pocket_array = new[] { 0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26 };
    int predictednum = Array.IndexOf(pocket_array, x);

    // Initialize the result array. Its size is double the neighbour count + 1 for x
    int[] result = new int[neighborCount * 2 + 1];

    // Calc the start index. We begin at the most left item.
    int startAt = predictednum - neighborCount;

    // i - position in the result array
    // j - position in the pocket_array
    for (int i = 0, j = startAt; i < result.Length; i++, j++)
    {
        // Adjust j if it's less then 0 to wrap around the array.
        result[i] = pocket_array[j < 0 ? j + pocket_array.Length : j];

        // If we are at the end then start from the beginning.
        if (j == pocket_array.Length)
        {        
            j = 0;
        }
    }
    return result;
}