x


Check if an array contains certain number combos?

I am making a simple game for Android in JavaScript. In order for a player to win the game, an array holding their score numbers has to match one of eight winning number combinations.

My idea was to create a multidimensional array to hold the eight winning number combinations. Then I would have the game compare the players' score arrays with those combinations to see if anyone has won yet. This is what my multidimensional array looks like:

var winCom1 = [1,2,3];
var winCom2 = [4,5,6];
var winCom3 = [7,8,9];
var winCom4 = [1,4,7];
var winCom5 = [2,5,8];
var winCom6 = [3,6,9];
var winCom7 = [1,5,9];
var winCom8 = [7,5,3];

var winComArray = [winCom1, winCom2, winCom3, winCom4, winCom5, winCom6, winCom7, winCom8];

Possible issue: My players' score arrays could have between 3 and 5 elements, so they may not have the same length as the winning combo array I'm trying to compare them to.

Also, it shouldn't matter what order the score numbers are in. Whether the player's score array looks like "2,3,1", or "2,7,1,3", etc... I still want that player to win because their array contains "winCom1".

Is there a good way to go about this method, if it's even possible? And if anyone has any alternate (i.e. simpler) ideas on how to check for winning number combos, I'd love to hear them.

Thank you in advance!

more ▼

asked Jul 15 '12 at 11:18 PM

katie88 gravatar image

katie88
36 3 4 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
 import System.Linq;

 ...


function Start()
{
 var winCom1 = [1,2,3];
 var winCom2 = [4,5,6];
 var winCom3 = [7,8,9];
 var winCom4 = [1,4,7];
 var winCom5 = [2,5,8];
 var winCom6 = [3,6,9];
 var winCom7 = [1,5,9];
 var winCom8 = [7,5,3];

 var winComArray = [winCom1, winCom2, winCom3, winCom4, winCom5, winCom6, winCom7, winCom8];

 var playerScoreArray = [10,11,1,3,2];

 if(winComArray.Cast.<int[]>().Any(function(combo){return combo.Cast.<int>().Intersect(playerScoreArray.Cast.<int>()).Count()>=3;}))
 { 
 }
}
more ▼

answered Jul 15 '12 at 11:22 PM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 101

An explanation:

  • Linq is used to work with sets of data
  • The if says take each entry in the winComArray and intersect the elements with the playerScoreArray, then count it - if any one returns>=3 then the player won.
Jul 15 '12 at 11:27 PM whydoidoit

Thank you for your response! I tried that out, but the compiler gives 2 errors:

'Any' is not a member of 'Array' 'Intersect' is not a member of 'Object'.

I will look deeper into Linq, as I've never heard of it. And I hadn't thought of using Intersect. So I will have to look into that, as well.

Jul 16 '12 at 01:30 AM katie88

Ah sorry about that I have modified the answer (I forgot damn JS needs casts on in built arrays).

Jul 16 '12 at 01:41 AM whydoidoit

Ahh that works perfectly! Thank you so much!!

Jul 16 '12 at 02:19 AM katie88

Could you mark the questions as answered for me? Get's it off the unsolved list :)

Jul 16 '12 at 06:33 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x94
x20
x12

asked: Jul 15 '12 at 11:18 PM

Seen: 459 times

Last Updated: Jul 16 '12 at 10:48 AM