Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (564)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  If Statement Efficiency Help  (Read 624 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Member





« Posted 2013-05-24 22:02:44 »

ok, so I have an if statement that is huge :
1  
2  
3  
4  
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.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 132
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-05-24 22:04:57 »

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.
Offline _Scyth_

Junior Member





« Reply #2 - Posted 2013-05-24 22:07:26 »

That's okay then Smiley 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2013-05-24 22:10:15 »

That's okay then Smiley 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)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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
  }
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Damocles
« Reply #4 - Posted 2013-05-24 22:12:04 »

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

1  
2  
3  
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.

Offline _Scyth_

Junior Member





« Reply #5 - Posted 2013-05-24 22:15:17 »

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?
Offline Damocles
« Reply #6 - Posted 2013-05-24 22:17:19 »

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

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2013-05-24 22:20:12 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline _Scyth_

Junior Member





« Reply #8 - Posted 2013-05-24 22:23:49 »

Thanks riven, that'll help so much when i come to check for more than one of the same slot at the same time Cheesy
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2013-05-24 22:29:15 »

I used this approach in a Poker AI (for Java4K)
http://www.java-gaming.org/topics/poker4k/11708/view.html

Checking for something like Full House becomes trivial:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
// 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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Scyth_

Junior Member





« Reply #10 - Posted 2013-05-24 22:33:29 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (21 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (34 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (108 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!