Question

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;
}
Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top