1

I am trying to determine what the neighbour bets would be for a given number on a roulette wheel. At the moment I pass a pocket number to a function and below is my code. "pocket_number()[0]" is the value of the number I want to determine the neighbours of.

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

neighbourbets() is as follows (this outputs an array of element numbers so elsewhere in my program I can extract them from the same array structure). At the moment I have a crude way of determining the the neighbour bets and getting the function to state which numbers are 6 numbers either side of it. Is there a better way to do this? I've found one of the problems (that I've overcome with the below) is if I want to know the neighbours for "0" for example which means the code needs to get the numbers from the end of the array.

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;
}

Any helps or guidence is appreciated! :)

2 Answers 2

1

Write a small helper function to wrap around the index:

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 +
}

The modulus there will help it wrap around when it goes above the maximum, and the if will do it for the minimum. This could probably be done easier though, but it eludes me at the moment.

Then, if you need that specific order, perhaps something like this:

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 );

Or, if any order will do:

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 );
Sign up to request clarification or add additional context in comments.

Comments

0

The following would give you the result with the x in the middle of the result array and the neighbours to the left and right of it:

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.