2

I need to know if its better for me to make Decision Table or if-else statement. if decision table is better for the rules below please tell me what are the basic codes I need to make it. I have tried making it using if-else statement but the output is not what i want. This question is for baccarat game, and here are the rules:

  • First, if either player has a hand of 8 or 9, called a “natural”, the game ends and no third cards are ever drawn
  • Second, if there was no natural, the player receives a third card (“draws” or “hits”), or not (“stands pat”), according to the following rule: -The player must draw a card with a hand value of 5 or less, and stand pat with 6 or more. -Third, if there was no natural, the dealer draws or stands pat according to the following rules -If the player did not draw a card, then the dealer follows the same rule as the player: The dealer must draw a card with a hand of 5 or less, and stand pat with 6 or more.

  • -If the player did draw a third card, the decision is based on the value of that card alone (call it C3), and the value of the dealer’s hand. The dealer must draw a card with a hand value less than or equal to some limit L, where L is calculated as follows:

  • If C3 is 8 or more, let Y = C3-10, else let Y = C3. So Y will be from -2 to 7.

  • o To get L, divide Y by 2, truncate the result, and add 3. L will be from 2 to 6. o The dealer must draw with a hand value of L or
    less.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bumblebee
  • 23
  • 6
  • Pick the one that is the most readable. – tom Oct 09 '13 at 09:41
  • 2
    to me i think table wud be better. but idk how to form a table. at least i dont think my prof has taught us yet. but i want to learn to make it. no harm learning new knowledge ahead of class – bumblebee Oct 09 '13 at 09:46

3 Answers3

2

In principle, you should use a decision table / switch etc. (if you are worried about efficient processing through the cases) once your if-else-if clauses go up to 5 or more (just a value big enough to impact average access times) instances.

Update: 5 is not written any where. I used it to illustrate a concept though i remember having seen some compiler doing it when switch cases were 8 or more, but that was a long time ago

The rationale is that going through each of the if clause manually would cause linear over head where as for decision table, the access would be constant time.

In practice, your code is already optimized by every decent compiler into a decision table / hash table as soon as number of cases become significant so this is irrelevant.

Following would help. Note that choice of switch or decision table or if else also depends upon how your test clause is structured. Switch works on integer values only. If you cannot relate your test condition to be accessed randomly some how, you might not have another choice but to use if-else only.

Community
  • 1
  • 1
fkl
  • 5,412
  • 4
  • 28
  • 68
  • hey thanks. so does it mean it is better for me to use if-else statement or it depends on my overall code? sorry im new to java and im trying to get a hang of it.. thanks alot – bumblebee Oct 09 '13 at 09:42
  • No problem. If you can, do use Switch or decision table. But it doesn't matter in until there are five or more sequential checks in case of if-else-if. It is still better to write code in a way that you are up against least surprises caused by compiler optimization – fkl Oct 09 '13 at 09:45
  • Where does this number 5 come from? – Brian Agnew Oct 09 '13 at 09:48
  • what do u mean by sequential check. sorry dont really understand. and if i want to make a table. what should i do... – bumblebee Oct 09 '13 at 09:49
  • By sequential check, i meant it has to go through each clause of if-else, until reaching the right clause for every condition. @BrianAgnew , no significance of 5 just trying to give concept of 'at least enough clauses' – fkl Oct 09 '13 at 10:00
  • If you want to make a table, try using a hash function where each value based index computation satisfies each of your if clause. – fkl Oct 09 '13 at 10:02
1

Choose the most readable and maintainable solution. Don't worry about optimisation until you can actually demonstrate a performance issue.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • to me it sound like table is better and readable because there are alot of rules for baccarat. and me writing if-else statement in the end is only confusing myself. can u teach me how to make a table.. thank you! would appreaciate it alot – bumblebee Oct 09 '13 at 09:51
1

From your comments, you have mentioned using decision table is better because there are lots of rules.

It is right in certain extend but not really true.

From what you described, there are lot of rules for different purpose. No matter you are writing decision tables or hand craft if-else, you still need to properly organize the rules base on their usage and context, and separate the "business flow" to make use of those rules.

If you failed to do so, using decision table is even harder to code and harder to read.

When you are considering use of decision table, which implies you are going for a rule engine, readability is probably not the most important factor. Main reason for using rule engine is to facilitate change of rule in the future. If you don't foresee such need, I would strongly suggest "hand-crafting" the logic, with a properly structured code.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131