0

In casino slot games you often have a Wild game piece. What would be a good way of including this mechanic into comparing with 2 other pieces? e.g. given 3 game pieces [Cherry][Cherry][Joker] would be a match.

The code I'm using right now seems really overweight, is there anything that can be done (think bitwise operators?) to make it easier to work with?

if ((box1.BoxRank == box2.BoxRank || 
      box1.BoxRank == BoxGameObject.Ranks.Joker ||
      box2.BoxRank == BoxGameObject.Ranks.Joker) &&
    (box1.BoxRank == box3.BoxRank || 
      box1.BoxRank == BoxGameObject.Ranks.Joker ||
      box3.BoxRank == BoxGameObject.Ranks.Joker) &&
    (box2.BoxRank == box3.BoxRank || 
      box2.BoxRank == BoxGameObject.Ranks.Joker ||
      box3.BoxRank == BoxGameObject.Ranks.Joker))
{
  // has 3 of a kind, or 1 joker and 2 of a kind, or 2 jokers and 1 other
  return true;
}
ferr
  • 1,137
  • 3
  • 12
  • 28

2 Answers2

2

This is easier if you think of the operation in terms of the set of the value of all of the boxes. Just remove all of the jokers from that set and then verify that all of the values are identical:

var allRanks = new[]{box1.BoxRank, box2.BoxRank, box3.BoxRank};

var threeOfAKind = allRanks.Where(rank => rank != BoxGameObject.Ranks.Joker)
    .Distinct()
    .Count() < 2;

First just remove all of the jokers. If, after doing that, there are two or more distinct values then we do not have a three of a kind.

Servy
  • 202,030
  • 26
  • 332
  • 449
1

Yes, represent the Joker as an int with all binary 1's, like 7, 15 or 31. Represent the cherry and others with an int with only singe binary 1 like 1,2,4,8 smaller than the Joker. Leave zero unused. Then, using bitwise AND your condition is equivalent to:

(box1.BoxRank & box2.BoxRank & box3.BoxRank) > 0

Note that 3 Jokers will satisfy the condition too.

P. B. M.
  • 262
  • 2
  • 6
  • Looks great, I'll test it out. I'm guessing it should be written as (box1.BoxRank & box2.BoxRank & box3.BoxRank) == box1.BoxRank – ferr Oct 09 '13 at 15:37
  • Yes, you are right, equality has higher precedance than bitwise AND. I edited the answer. – P. B. M. Oct 09 '13 at 15:47
  • Sorry, one more correction and a very important one: for non-Joker items only int's with a single binary one can be used. Then, it unmistakenly works! – P. B. M. Oct 09 '13 at 15:58
  • Yeah- I was testing and didn't realize that- works now! Thanks – ferr Oct 09 '13 at 16:00
  • Is there an issue if box1.BoxRank is the Joker since the equality condition is based on box1.BoxRank? In my testing that seems to be problematic. – ferr Oct 09 '13 at 19:31
  • I went with this: if(((box1.BoxRank & box2.BoxRank) & (box1.BoxRank & box3.BoxRank)) != RankTypes.None) ... where None is 0. It's not as simple, but it works and isn't so bad. – ferr Oct 09 '13 at 21:34
  • I changed the condition to check for "greater than zero". That covers the case when box1.BoxRank is the Joker. "not eqal zero" works as well. Sorry for many edits! – P. B. M. Oct 09 '13 at 22:57