- Home /
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!
Answer by whydoidoit · Jul 15, 2012 at 11:22 PM
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;}))
{
}
}
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.
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.
Ah sorry about that I have modified the answer (I forgot damn JS needs casts on in built arrays).
Could you mark the questions as answered for me? Get's it off the unsolved list :)
Your answer
Follow this Question
Related Questions
i need a player list help please 0 Answers
Looping through array to find a specific class? 0 Answers
if a string has certain letter(s) or number(s) 2 Answers
Check array slot against corresponding slot in another array? 1 Answer
Get Material of Normal? 0 Answers