If Statement Efficiency Help

ok, so I have an if statement that is huge :

if( slotNo[0] == 1 && slotNo[1] != 1 && slotNo[2] != 1 && slotNo[3] != 1 && slotNo[4] != 1 && slotNo[5] != 1)
		{
			winnings = 2;
		}

and I would like to know if there is a more efficient way of doing this if statement. I don’t even know how reliable that statement is.

My if statements are gonna get worse too, because I’m going to have a lot of or’s and ands to check.

So if anyone has any advice on how I could do this more reliably and more efficiently would be very appreciated.

If you are doing any unnecessary checking, then you can remove that and it’ll be more efficient. In practice, however, a few things in an if statement won’t change much - you should be thinking more about if this is the right way (fits into your game model) rather than if it’s efficient. If your program is being slow 99% it’s not this.

That’s okay then :slight_smile: I’m doing a slot machine game, and therefore, the single cherry can appear anywhere on the screen and it’ll give 5 credits, so i need to check if it appears in only one place by checking what slotNo[] it appears in and that it doesnt appear in any other one.

What you need is a frequency table (or histogram)


int[] histogram = new int[numSlotIcons];
for(int slotVal: slotNo) {
   histogram[slotVal]++;
}



if(histogram[1] == 1) {
   // only 1 slot had a '1'
}
if(histogram[3] == 1) {
   // only 1 slot had a '3'
}

for(int freq: histogram) {
   if(freq == numSlotIcons) {
       // all slots had the same value
   }
}

Ok, if you had a lot of slots, this could be more managable:


int b=slotNo[0];
for(int i=1;i<slotNo.lenght;i++) b*=slotNo[i];
if(b==1) winnings=2;

In the end, your current if statment is faster.
If that would matter in your context.

riven could you explain a little bit more about your code, i understand that its kind of like a look up table, but i dont understand the part where i enter the values into it and how to look into it.

Also, Damocles, what does your b stand for?

sorry, did not see that it was !=1
forget that code sample

Say you have 5 slots, with the values: 1,3,2,2,0

Now you create a frequency table, with 4 slots (for the values 0,1,2,3)

For every slot value, you increment the entry in the frequency table:
freqTable[1] += 1
freqTable[3] += 1
freqTable[2] += 1
freqTable[2] += 1
freqTable[0] += 1

Your frequency table now holds these values:
1,1,2,1 (1x0, 1x1, 2x2, 1x3)

If you want to know how often ‘1’ was found in the slots, you look it up in the frequency table:
int numberOfOnes = freqTable[1];
int numberOfTwos = freqTable[2];

If numberOfOnes equals 1, then you know that ‘1’ was only found in 1 slot, regardless of which slot it was.

Thanks riven, that’ll help so much when i come to check for more than one of the same slot at the same time :smiley:

I used this approach in a Poker AI (for Java4K)

Checking for something like Full House becomes trivial:


// Ace=12, King=11, Queen=10, Jack=9, 10=8, ..., 2=0
int minDigit = 100;
int maxDigit = 0;
int maxNumberSet = 0;
int maxSymbolSet = 0;
int[] freqCardNumbers = new int[13];
int[] freqCardSymbols = new int[4];
for(Card card: hand) {
   maxNumberSet = Math.max(maxNumberSet, freqCardNumbers[card.digit] += 1);
   maxSymbolSet = Math.max(maxSymbolSet, freqCardSymbols[card.symbol] += 1);
   minDigit = Math.min(minDigit, card.digit);
   maxDigit = Math.max(maxDigit, card.digit);
}



// build histogram of histogram (for pairs check)
int[] freqSetsOfNumbers = new int[4+1];
for(int freq: freqCardNumbers) {
   freqSetsOfNumbers[freq]++;
}

// build histogram of histogram (for flush check)
int[] freqSetsOfSymbols = new int[hand.length+1];
for(int freq: freqCardSymbols) {
   freqSetsOfSymbols[freq]++;
}

if(maxNumberSet == 1 && (maxDigit-minDigit) == 5 && maxSymbolSet == 5) {
   if(maxDigit == 12) {
      // royal flush
   }
   else {
      // straight flush
   }
}
else if(freqSetsOfNumbers[4] == 1) {
   // four of a kind
}
else if(freqSetsOfNumbers[2] == 1 && freqSetsOfNumbers[3] == 1) {
   // full house
}
else if(freqSetsOfSymbols[5] == 1) {
   // flush
}
else if(maxNumberSet == 1 && (maxDigit-minDigit) == 5) {
   // straight
}
else if(freqSetsOfNumbers[3] == 1) {
   // three of a kind
}
else if(freqSetsOfNumbers[2] == 2) {
   // two pairs
}
else if(freqSetsOfNumbers[2] == 1) {
   // one pair
}
else {
   // high card
}

Ofcourse the code in Poker4K was more complex, as it had to compare two hands of equal type.

Well my goal is to create a range of casino games, and my next game idea was a video poker game, so this approach is very helpful thank you, i thought that my coding was going to become complicated with a lot of if statements, containing a lot of different conditions.