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 7909409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:23:52+00:00 2026-06-03T12:23:52+00:00

I’m making a tic-tac-toe game. How would I change the following code so that

  • 0

I’m making a tic-tac-toe game.

How would I change the following code so that the winner could be declared before complete is true, (every square filled) and the winner is the first to achieve 3 matching values, not the last to achieve it?

function check_squares () {
    var values = new Array(); 
    var complete = true;

    $('.square').each(function () {     
        values.push( $(this).html() );  
        //if ($(this).html() == '') complete = false;
    });

    if (complete == true) { 
        var winner = false;

        if ( values[0] == values[1] && values[1] == values[2] ) winner = values[0];
        if ( values[3] == values[4] && values[4] == values[5] ) winner = values[3];
        if ( values[6] == values[7] && values[7] == values[8] ) winner = values[6];
        if ( values[0] == values[3] && values[3] == values[6] ) winner = values[0];
        if ( values[1] == values[4] && values[4] == values[7] ) winner = values[1];
        if ( values[2] == values[5] && values[5] == values[8] ) winner = values[2];
        if ( values[0] == values[4] && values[4] == values[8] ) winner = values[0];
        if ( values[2] == values[4] && values[4] == values[6] ) winner = values[2];

        if (winner) {           
            $('#output').html('Winner: ' + winner);
        } else {        
            $('#output').html('No Winner');
        }
    } else {    
        if (player_x == true) {     
            $('#output').html('x turn to move');
        } else {        
            $('#output').html('o turn to move');
        }       
    }
}
  • 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-03T12:23:54+00:00Added an answer on June 3, 2026 at 12:23 pm

    Your function is almost correct. Mainly, you just need to call it each time that a player changes a cell’s value.

    I have tweaked your function a little bit to allow it to be called multiple times. I have added comments to the sections that I changed to make them easier to find.

    // Move these two variables into the global scope so that they are not reset
    // by each call to check_squares and so that our click function can access them.
    var complete = false;
    var winner = null;
    
    function check_squares () {
        var values = new Array(); 
    
        $('.square').each(function () {     
            values.push( $(this).html() );  
            //if ($(this).html() == '') complete = false;
        });
    
        // Debug statement to see the order of the values.
        console.log(values);
    
        if (!complete) {
            if ( values[0] == values[1] && values[1] == values[2] ) winner = values[0];
            if ( values[3] == values[4] && values[4] == values[5] ) winner = values[3];
            if ( values[6] == values[7] && values[7] == values[8] ) winner = values[6];
            if ( values[0] == values[3] && values[3] == values[6] ) winner = values[0];
            if ( values[1] == values[4] && values[4] == values[7] ) winner = values[1];
            if ( values[2] == values[5] && values[5] == values[8] ) winner = values[2];
            if ( values[0] == values[4] && values[4] == values[8] ) winner = values[0];
            if ( values[2] == values[4] && values[4] == values[6] ) winner = values[2];
    
            if (winner) {           
                $('#output').html('Winner: ' + winner);
                // Indicate that there was a winner so no more input is allowed.
                complete = true;
            } else {        
                $('#output').html('No Winner');
            }
        } else {    
            if (player_x == true) {     
                $('#output').html('x turn to move');
            } else {        
                $('#output').html('o turn to move');
            }       
        }
    }
    
    // Add a click handler to each square that checks for win conditions after each
    // X or 0 is added.
    $(".square").click(function() {
      if (!complete)
      {
        // This obviously needs to be changed to determine which player is going.
        $(this).html("X");
        check_squares();
      }
    });
    

    I have created a simple JSFiddle to see a working example.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.