Tic-Tac-Toe Help

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
Smail
Posts: 1
Joined: Wed Nov 30, 2016 5:44 am
Occupation: Student

Tic-Tac-Toe Help

Post by Smail »

Hello, while trying to run my code it doesn't work, and I really don't know what to do. Please, I need to do this before the 11 or I am screwed.

Code: Select all

document.addEventListener('DOMContentLoaded', function () {
        var board=Array(9);
        var wins=[
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6] ];
        var turn=0;
        var boardNum=0;
        var openSquare=[];
        var squareNum=1;
        Array.from(document.querySelectorAll('td a'), function (link, i) {
        link.addEventListener('click', gotClicked.bind(null,link,i),randomAi(null,link,i));
        });
            function gotClicked(link, i,e) {
            e.preventDefult();
            // From the `turn` value we can know who the current player is:  0 or 1
            // We use the modulo operator to get the remainder when dividing by 2
            var player=turn%2;
            // Use this 0 or 1 as index in the XO string and display it
            // When player is 0 this gives 'X', when 1 it gives 'O'
            link.parentNode.textContent='XO'[player];
            // Also put the 0 or 1 in our array
            board[i]=player;
            randomAi(link,i);
            // Now board has changed, check if there is a 3-in-a-row for this player
            if (isWinFor(player)) alert('Winning move!');
            // Player played, so now increment `turn`
            turn++;
          }
          
            function RandomAi(link,i) {
                if (player==1) {
                while (board[boardNum]!==board[8]) {
                    if(board[boardNum]!=1 || 0) {
                        openSquare[squareNum]=board[boardNum];
                        squareNum=squareNum+1;
                    }
                    boardNum=boardNum+1;
                }
                var randomPick=Math.random()*squareNum;
                var choosenSquare=openSquare[randomPick];
                board[choosenSquare]=1;
                link.parentNode.textContent='XO'[player];
                boardNum=0;
                squareNum=0;
            }
            }
            
            function isWinFor(player) {
            // Player is 0 or 1.
            // Iterate over all the 8 potential wins
            for (var win of wins) {
            // `win` is a triplet of indices, which when they contain the player's value
            // constitute a winning position. Let's count how many of those three
            // have the player's value (0 or 1)
            var count = 0;
            for (var index of win) {
            // Is the value on the board the value that corresponds to the player?
            if (board[index] !== player) break; // don't bother looking further.
            count++;
            }
                // If we have a count of 3 here, it is a winning position.
                if (count == 3) return true;
        }
    }
});
Locked

Return to “Grades 6-8: Math and Computer Science”