Search code examples
javascripttic-tac-toe

Is there anyway to record X or O to a data array to check for winner?


I’m building a tic tac toe game. I would like to ask Is there anyway to record X or O (upon clicking on the board) to a data array to check for winner later on? Specifically, I would like to record data to the below variable Thanks.

var board = [[‘’,’’,’’],[‘’,’’,’’],[‘’,’’,’’]];

Solution

  • There certainly is. Currently your board seems to be set up like so:

    -------------
    |0,0|0,1|0,2|
    -------------
    |1,0|1,1|1,2|
    -------------
    |2,0|2,1|2,2|
    -------------
    

    You can acces and set these values in your array like so:

    board[0][0] //the top left cell
    

    If you want to set the value of a cell you do it like this:

    board[0][0] = "X" 
    

    You can access an entire row of your board like this:

    board[0]; 
    //this would be ["X","",""] right now
    

    I hope this helps you understand array access.

    (I also suggest removing the jQuery tag, it has no place here. This is raw javascript)