JavaScript Tic Tac Toe - Finding All Possible Combinations of Numbers within an Array

hahaha

New Member
I am currently writing a basic Tic Tac Toe game (multiplayer, no AI) using web (HTML, CSS, Javascript). The game logic obviously is all within the Javascript. I have a question. The following is my code.\[code\] window.onload = function(){ console.log("Page has loaded");}var ctx;var turn = 0;var winningCombo = [[1,4,7], [2,5,8], [3,6,9], [1,2,3], [4,5,6], [7,8,9], [1,5,9], [3,5,7]];var playedComboX = [];var playedComboO = [];var filledSquares = [0];var filled = false;console.log(winningCombo.length);var checkWinnerX = function(){ for (var i = 0; i < winningCombo.length; i++) { console.log('Its in the X check winner loop'); if ((winningCombo[0] == playedComboX[0]) && (winningCombo[1] == playedComboX[1]) && (winningCombo[2] == playedComboX[2])) { alert("Congrats, you have won!"); return true; } return false; }}var checkWinnerO = function(){ for (var i = 0; i < winningCombo.length; i++) { console.log('Its in the 0 check winner loop'); if (winningCombo[0] == playedComboO[0] && winningCombo[1] == playedComboO[1] && winningCombo[2] == playedComboO[2]) { console.log('It has passed the if statement for X'); alert("Congrats, you have won!"); return true; } return false; }}var checkWinner = function(){}var draw = function(squareNumber){ console.log('draw has been called'); var squareID = "square" + squareNumber; var squareClicked = document.getElementById(squareID); ctx = squareClicked.getContext('2d'); for (var i = 0; i < filledSquares.length; i++) { if (filledSquares == squareNumber) { filled = true; } else { filled = false; } } if (filled == true) { alert("Invalid Move! Square is already occupied"); } else { filledSquares.push(squareNumber); ctx.beginPath(); if (turn % 2 == 0) { //Drawing a 'X' ctx.moveTo(20,20); ctx.lineTo(135,135); ctx.moveTo(135,20); ctx.lineTo(20,135); ctx.lineWidth = 6; ctx.stroke(); turn++; playedComboX.push(squareNumber); checkWinnerX(); } else { //Drawing a Circle ctx.arc(75,75,65, 2*Math.PI, false); ctx.lineWidth = 6; ctx.stroke(); turn++; playedComboO.push(squareNumber); checkWinnerO(); } }}\[/code\]Right now, the checkWinner functions only check for exact matches with the array elements within the winningCombo array (ex: if the combination is 3,1,2 instead of 1,2,3 then it won't register). Is there anyway I can check all possible combinations of the 3 numbers in each element? Hope my explanation makes sense, Thanks.PS: Forgive me if any of the code isn't as well-written as it could me, it's my first time attempting to write a game. But be as critical as you wish!
 
Back
Top