diff --git a/index.html b/index.html new file mode 100644 index 0000000..d3904f6 --- /dev/null +++ b/index.html @@ -0,0 +1,45 @@ + + + + Tic Tac Toe + + + + + +
+
+ +
+ + +
+
+ +
+ + +
+
+ + +
+ +
+ +
+
+ +
+
+ +
+
+
+
+
+ + + + + \ No newline at end of file diff --git a/script.js b/script.js new file mode 100644 index 0000000..8deefc4 --- /dev/null +++ b/script.js @@ -0,0 +1,285 @@ + +//defining global variables +var state = "X" +var boardSize = 3 +var finalInd = boardSize-1 +var player1Name = null +var player1Symbol = "X" +var player2Name = null +var player2Symbol = "O" +var emptyBoard = [] +var board = [] +var player1Wins = 0 +var player2Wins = 0 + +document.querySelector(".ttt-board").classList.add("hide") + +//function to initialise empty matrix and DOM tile elements called when user inputs boardSize +var initEmptyGrid = function(){ + document.querySelector(".ttt-board").innerHTML = "" + board = [] + emptyBoard =[] + for(i=0;ivalue&&value==arr[0])){ + rows = true + } + } + //check columns + for(i=0;i=0){ + if(board[0][finalInd]!=board[xInd][yInd]){ + break + } + xInd += 1 + yInd -= 1 + } + if(xInd==boardSize){ + diagonals = true + } + } + return diagonals || rows || columns +} + +//updates the js matrix board +var updateBoard = function(element){ + var firstIndex = parseInt(element.id[0]) + var secondIndex = parseInt(element.id[1]) + board[firstIndex][secondIndex] = state +} + +//if there is a win, this is the output +function winOutput(){ + endTimer() + //say who wins + if(state=="X"){ + player1Wins++ + var winner = player1Name + var loser = player2Name + state = "O" + } else if (state=="O"){ + player2Wins++ + var winner = player2Name + var loser = player1Name + state = "X" + } + document.querySelector(".result").innerText = `Congratulations, ${winner}! You win.\n${loser}, better luck next time.` + + + //update winCount DOM + document.querySelector(".player1-wins").innerHTML = `${player1Name} wins: ${player1Wins}` + document.querySelector(".player2-wins").innerHTML = `${player2Name} wins: ${player2Wins}` + + //Reset Game + document.querySelector(".user-input").classList.remove("hide") + document.querySelector("#start").innerText = "Play again!" +} + + +//checks if all the squares are filled ie a draw +function checkDraw(){ + for(i=0;i