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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:09:01+00:00 2026-06-04T06:09:01+00:00

I am making a tic tac toe game. Someone already taught me how to

  • 0

I am making a tic tac toe game. Someone already taught me how to get the winner declared and stop further play.

There are two persistent problems. The first is that when the wins are straight across, the counters add 1 to the x wins and 2 to the o wins. The second issue is that I can’t get the game to call a draw. I have tried else if without results.

Here is the whole js and the game can be seen at: http://jvonhausen.com/final/p19/

jQuery(function() {

    $('#start').click(clear_squares);

    $('.square').click(function () {    
        square_stuff(this);
    });

});

var player_x = true; 
var complete = false; 
var winner = '';
var x_wins = 0;
var o_wins = 0;
var draw = 0;

function clear_squares () {
    localStorage.setItem('x_count', x_wins); 
    localStorage.setItem('o_count', o_wins); 
    localStorage.setItem('draw_count', draw); 

    $('.square').html(''); 

    player_x = true;
    $('#output').html('turn: x');

    complete = false; //allows for new game
}


function square_stuff (square) {
    if (!complete) {
        if ($(square).html() == '') {       
            if (player_x == true) {
                $(square).html('x');
                player_x = false;               
            } else {            
                $(square).html('o');
                player_x = true;                
            }
        }
    }   
    check_squares();
}

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

    $('.square').each(function () {     
        values.push( $(this).html());
        if ($(this).html() == '') complete = false;
        if ( !winner && values[0] == values[1] && values[1] == values[2] ) winner = values[0];
        if ( !winner && values[3] == values[4] && values[4] == values[5] ) winner = values[4];
        if ( !winner && values[6] == values[7] && values[7] == values[8] ) winner = values[7];
        if ( !winner && values[0] == values[3] && values[3] == values[6] ) winner = values[3];
        if ( !winner && values[1] == values[4] && values[4] == values[7] ) winner = values[1];
        if ( !winner && values[2] == values[5] && values[5] == values[8] ) winner = values[5];
        if ( !winner && values[0] == values[4] && values[4] == values[8] ) winner = values[8];
        if ( !winner && values[2] == values[4] && values[4] == values[6] ) winner = values[2];      
        if (winner) {           
            complete = true;
            $('#output').html('winner: ' + winner);
            if ($(this).html() == 'x') {
                localStorage.setItem('x_count', x_wins); 
                x_wins++;
                $('#wins_x').val(x_wins);
            } 
            if ($(this).html() == 'o') {
                localStorage.setItem('o_count', o_wins); 
                o_wins++;
                $('#wins_o').val(o_wins);
            }
            if (winner == '' && complete == true) { 
                $('#output').html('no winner');
                localStorage.setItem('draw_count', draw); 
                draw++;
                $('#draws').val(draw);
            }   
        }                   
        if (winner == '' && complete == false) {    
            if (player_x == true) {     
                $('#output').html('turn: x');
            } else {        
                $('#output').html('turn: o');
            }       
        }
    });

}
  • 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-04T06:09:02+00:00Added an answer on June 4, 2026 at 6:09 am

    You are not breaking out of you “each” loop once you detect a winner. I think this will work:

    $('.square').each(function () {  
        if (winner) {
           return;
        }   
        values.push( $(this).html());
        ....
    

    Alternatively, this is just cleaner:

    function check_squares () {
    complete = true;
    var values = []; // better way to declare an empty array
    var winner = '';
    
    // build the values array first
    $('.square').each(function () {     
        values.push( $(this).html());
    }
    
    // then just check for a winner once... 
    if ( !winner && values[0] == values[1] && values[1] == values[2] ) winner = values[0];
    ....
    

    And the reason why your code isn’t detecting a draw is because your check for draw is occuring inside the “if (winner)” block.

       if (winner) {           
            complete = true;
            ....
    
            // the clause will never get executed, because winner must have already been set to have made it this far
            // so winner == '' will aways be false
            if (winner == '') { 
                $('#output').html('no winner');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is for a tic tac toe game. I need help making a check
I am making a Tic Tac Toe game and i created a function that
Ok, so I'm in the process of making a Tic-Tac-Toe game to help me
i am making a noughts and crosses game (tic tac toe) and in my
For learning, I'm making a tic tac toe game that's intended to be played
I'm working on making a two player tic-tac-toe game, and am in the phase
I'm making a tic-tac-toe game. How would I change the following code so that
I am making a tic tac toe sort of game and I want the
I am just getting started with programming and am making a Tic-Tac-Toe program. In
I'm having a huge block trying to understand trees while making a Tic-Tac-Toe bot.

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.