Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:21:55+00:00 2026-06-15T00:21:55+00:00

Ok, consider this: I have a big array containing arrays , -1 , a

  • 0

Ok, consider this:

I have a big array containing arrays, -1, a and b.

The -1 means the field is empty:

var board = [
    [-1,-1, a],
    [-1,-1, b],
    [ b,-1, a]
]

Now i want to check smaller arrays agains this:

var solutions = [
    [
        [1, 1, 1]
    ],
    [
        [1],
        [1],
        [1]
    ],
    [
        [1],
        [0,1],
        [0,0,1]
    ],
    [
        [0,0,1],
        [0,1],
        [1]
    ]
]

To see if one existing value from board match the pattern in solutions.


Does a match any of pattern?
Does b match any of the pattern?


Can any of you see a better way than making a crazy nested loop:

var q,w,e,r,t,y;

q=w=e=r=t=y=0;

for( ; q < 3; q++ ) {
    for( ; w < 3; w++ ) {
        for( ; e < SOLUTIONS.length; e++ ) {
            .... and so on...
        }
    }
}

In this example I have used tic-tac-toe.

But i could be anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:21:57+00:00Added an answer on June 15, 2026 at 12:21 am

    Very interesting question. +1 🙂 Here is my take on this.

    Check my fiddle http://jsfiddle.net/BuddhiP/J9bLC/ for full solution. I’ll try to explain the main points in here.

    I start with a board like this. I’ve used 0 instead of -1 because its easier.

        var a = 'a', b = 'b';
        var board = [
           [a, 0, a],
           [b, b, b],
           [a, 0, a]
           ];
    

    My Strategy is simple.

    1. Check if any of the rows has the same player (a or b), if so we have a winner.
    2. Else, Check if any of the columns has the same player
    3. Else, Check if diagonals has a player

    Those are the three winning cases.

    First I created a function which can take set of rows (Ex: [a,0,b]), and check if entire row contains the same value, and if that value is not zero (or -1 in your case).

    checkForWinner = function () {
        lines = Array.prototype.slice.call(arguments);
        // Find compact all rows to unique values.
        var x = _.map(lines, function (l) {
            return _.uniq(l);
        });
        // Find the rows where all threee fields contained the same value.
        var y = _.filter(x, function (cl) {
            return (cl.length == 1 && cl[0] !== 0);
        });
        var w = (y.length > 0) ? y[0] : null;
        return w;
    };
    

    Here I take unique values in a row, and if I can find only one unique value which is not ZERO, the he is the winner.

    If there is no winner in the rows, I then check for columns. In order to reuse my code, I use _.zip() method to transform columns into rows and then use the same function above to check if we have a winner.

    var board2 = _.zip.apply(this, board);
    winner = checkForWinner.apply(this, board2);
    

    If I still don’t find a winner, time to check the diagonals. I’ve written this function to extract two diagonals from the board as two rows, and use the same checkForWinner function to see if diagonals are dominated by any of the players.

    extractDiagonals = function (b) {
        var d1 = _.map(b, function (line, index) {
            return line[index];
        });
        var d2 = _.map(b, function (line, index) {
            return line[line.length - index - 1];
        });
        return [d1, d2];
    };
    

    Finally this is where I actually check the board for a winner:

    // Check rows
    winner = checkForWinner.apply(this, board);
    if (!winner) {
        var board2 = _.zip.apply(this, board);
    
        // Check columns, now in rows
        winner = checkForWinner.apply(this, board2);
        if (!winner) {
            var diags = extractDiagonals(board);
            // Check for the diagonals now in two rows.
            winner = checkForWinner.apply(this, diags);
        }
    }
    

    If any of you wonder why I use apply() method instead of directly calling the function, reason is apply() allows you to pass an array elements as a list of arguments to a function.

    I believe this should work for 4×4 or higher matrics as well, although I did not test them.

    I had very little time to test the solution, so please let me know if you find any errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the question sounds silly, but consider this: I have an array of
I'm having trouble aggregating multiple arrays into one big array, I think this should
This may seem a very silly question. Consider this: I have a simple Boolean
Please Consider this scenario: We have a base class called clsMain : class clsMain
Consider this scenario. I have my own website, that I use as my identifier,
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Consider this page @ http://www.bloodbone.ws/screwed.html I need to be able to have the a.grow
Consider I have an anchor which looks like this <div class=res> <a href=~/Resumes/Resumes1271354404687.docx> ~/Resumes/Resumes1271354404687.docx
Before Consider to have a class and a global function: This is, for example,
Consider a grocery store scenario (I'm making this up) where you have FACT records

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.