Skip to content
Snippets Groups Projects
script.js 13.8 KiB
Newer Older
Kunal Shah's avatar
Kunal Shah committed
var setColor = "#79A6D8"
var fullBoard = [[], [], []];
var winningSet = null;
fullBoard[0][0] = null;
fullBoard[0][1] = null;
fullBoard[0][2] = null;
fullBoard[1][0] = null;
fullBoard[1][1] = null;
fullBoard[1][2] = null;
fullBoard[2][0] = null;
fullBoard[2][1] = null;
fullBoard[2][2] = null;

var win = null;
// game setup. called at startup.
// determines who will start
Kunal Shah's avatar
Kunal Shah committed
function startGame() {
	document.turn = "X";
	if (Math.random() < 0.5) {
		document.turn = "O";
	}
	setMessage(document.turn + " gets to start.");

	var squares = document.getElementsByClassName("Square");
	for (var s = 0; s < squares.length; s++){
            // console.log(squares[s]);
            squares[s].addEventListener('click',nextMove,false);
        }
Kunal Shah's avatar
Kunal Shah committed
    }
// will set status message
Kunal Shah's avatar
Kunal Shah committed
function setMessage(msg) {
	document.getElementById("message").innerText = msg;
}
// switches player and updates message
Kunal Shah's avatar
Kunal Shah committed
function nextMove() {
	square = this;
Pareek Ravi's avatar
Pareek Ravi committed
        // console.log(square.id);
Kunal Shah's avatar
Kunal Shah committed

        if (win == null) {
            // if tile is empty
            if (square.innerText == "") {

                // Print move to board
                square.innerText = document.turn;

                // log player move.
                // console.log("player: " + document.turn + " Played at: " + square.parentNode.parentNode.parentNode.parentNode.id + " || " + square.id);

                // display as last move
Kunal Shah's avatar
Kunal Shah committed
                // document.getElementById("sirep").innerText = "last move: " + square.parentNode.parentNode.parentNode.parentNode.id + " || " + square.id;
Kunal Shah's avatar
Kunal Shah committed


                // Check if win then change turn
                checkCompletedBoard(square);

                changeColour(square);
                switchTurn();
            }
        }
        else {
Kunal Shah's avatar
Kunal Shah committed
        	console.log('Game over');
Kunal Shah's avatar
Kunal Shah committed
        }
    }
// alternates player turn
Kunal Shah's avatar
Kunal Shah committed
function switchTurn() {
	if (document.turn == "X") {
		document.turn = "O";
	} else {
		document.turn = "X";
	}
	setMessage("It's " + document.turn + "'s turn!");
Kunal Shah's avatar
Kunal Shah committed
}
Kunal Shah's avatar
Kunal Shah committed
function changeColour(square) {
Kunal Shah's avatar
Kunal Shah committed
	var boardID;
	var tileID = square.id;
	tileID = tileID.substring(2);
Kunal Shah's avatar
Kunal Shah committed

        // set Next move's playable region boardID

        switch (tileID) {
Kunal Shah's avatar
Kunal Shah committed
        	case "s1":
        	boardID = 'B00';
        	break;
        	case "s2":
        	boardID = 'B01';
        	break;
        	case "s3":
        	boardID = 'B02';
        	break;
        	case "s4":
        	boardID = 'B10';
        	break;
        	case "s5":
        	boardID = 'B11';
        	break;
        	case "s6":
        	boardID = 'B12';
        	break;
        	case "s7":
        	boardID = 'B20';
        	break;
        	case "s8":
        	boardID = 'B21';
        	break;
        	case "s9":
        	boardID = 'B22';
        	break;
Kunal Shah's avatar
Kunal Shah committed
        }
        if (win == null) {
Kunal Shah's avatar
Kunal Shah committed
        	for (var i = 0; i < 3; i++) {
        		for (var j = 0; j < 3; j++) {
        			if (fullBoard[boardID.charAt(1)][boardID.charAt(2)] != null) {
        				if (fullBoard[i][j] == null) {
Kunal Shah's avatar
Kunal Shah committed
                            // set all backgound colour
                            document.getElementById("B" + i + j).style.backgroundColor = setColor;
                            // set whole board clickable
                            document.getElementById("B" + i + j).style.pointerEvents = 'auto';
                        }
                        else {
                            // set all backgound colour
                            document.getElementById("B" + i + j).style.backgroundColor = 'none';
                            // set whole board clickable
                            document.getElementById("B" + i + j).style.pointerEvents = 'none';
                        }
                    }
                    else {
Kunal Shah's avatar
Kunal Shah committed
                    	if (fullBoard[i][j] == null) {
Kunal Shah's avatar
Kunal Shah committed
                            // set all backgound colour
                            document.getElementById("B" + i + j).style.backgroundColor = 'white';
                            // set whole board clickable
                            document.getElementById("B" + i + j).style.pointerEvents = 'none';
                        }
                        // set playable region backgound colour to indicate active area.
                        document.getElementById(boardID).style.backgroundColor = setColor;
                        // set region clickable
                        document.getElementById(boardID).style.pointerEvents = 'auto'
                    }
                }
            }
        }
        else {
Kunal Shah's avatar
Kunal Shah committed
        	for (var i = 0; i < 3; i++) {
        		for (var j = 0; j < 3; j++) {
        			if (fullBoard[i][j] == null) {
        				document.getElementById("B" + i + j).style.backgroundColor = 'white';
        			}
        			document.getElementById("B" + i + j).style.pointerEvents = 'none';
        		}
        	}
Kunal Shah's avatar
Kunal Shah committed
        }
    }

    function checkCompletedBoard(square) {
Kunal Shah's avatar
Kunal Shah committed
    	var color = null;
    	if (square.innerText == 'O') {
    		color = 'red'
    	}
    	else {
    		color = 'pink';
    	}
Kunal Shah's avatar
Kunal Shah committed

        //id of the inner board
        var boardID = square.parentNode.parentNode.parentNode.parentNode.id;
        //the inner board element
        var boardTable = document.getElementById(boardID).children[0].children[0].children;
        //getting the inner board as a 2d array
        var innerBoard = getBoard(boardTable);
        //identifying the col and row of the innerBoard in terms of the fullBoard
        var row = boardID.charAt(1);
        var col = boardID.charAt(2);

        // row 1
        if (innerBoard[0][0] == square.innerText && innerBoard [0][1] == square.innerText && innerBoard[0][2] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        //row 2
        else if (innerBoard[1][0] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[1][2] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        //row 3
        else if (innerBoard[2][0] == square.innerText && innerBoard [2][1] == square.innerText && innerBoard[2][2] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        //col 1
        else if (innerBoard[0][0] == square.innerText && innerBoard [1][0] == square.innerText && innerBoard[2][0] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        //col 2
        else if (innerBoard[0][1] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[2][1] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        //col 3
        else if (innerBoard[0][2] == square.innerText && innerBoard [1][2] == square.innerText && innerBoard[2][2] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        // diagonal
        else if (innerBoard[0][0] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[2][2] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        //diagonal
        else if (innerBoard[0][2] == square.innerText && innerBoard [1][1] == square.innerText && innerBoard[2][0] == square.innerText) {
            //changing the color of the inner board to show a win
            document.getElementById(boardID).style.backgroundColor = color;

            //changing the label of the inner board to show a win
            document.getElementById(boardID).innerHTML = document.turn;

            //indicating on the full board that the inner board is won
            fullBoard[row][col] = square.innerText;
            // console.log(fullBoard)
        }
        else {
Kunal Shah's avatar
Kunal Shah committed
        	var flag = true;

        	for (var i = 0; i < 3; i++) {
        		for (var j = 0; j < 3; j++) {
        			if (innerBoard[i][j] == "") {
        				flag = false;
        				break;
        			}
        		}
        	}

        	if (flag) {
        		fullBoard[row][col] = "-";
Kunal Shah's avatar
Kunal Shah committed
                // console.log(fullBoard)
                document.getElementById(boardID).innerHTML = "-";
            }
        }

        win = checkWin();
    }

    function checkWin() {
Kunal Shah's avatar
Kunal Shah committed
    	var winner;
    	var winLine;
Kunal Shah's avatar
Kunal Shah committed
        // row 1
        if (fullBoard[0][0] == fullBoard[0][1] && fullBoard[0][0] == fullBoard[0][2] && fullBoard[0][0] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[0][0];
        	window.alert(fullBoard[0][0] + " wins the game!!!");
        	winningSet = ['00', '01', '02'];
Kunal Shah's avatar
Kunal Shah committed
        }
        //row 2
        else if (fullBoard[1][0] == fullBoard[1][1] && fullBoard[1][0] == fullBoard[1][2] && fullBoard[1][0] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[1][0];
        	window.alert(fullBoard[1][0] + " wins the game!!!");
        	winningSet = ['10', '11', '12'];
Kunal Shah's avatar
Kunal Shah committed
        }
        //row 3
        else if (fullBoard[2][0] == fullBoard[2][1] && fullBoard[2][0] == fullBoard[2][2] && fullBoard[2][0] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[2][0];
        	window.alert(fullBoard[2][0] + " wins the game!!!");
        	winningSet = ['20', '21', '22'];
Kunal Shah's avatar
Kunal Shah committed
        }
        //col 1
        else if (fullBoard[0][0] == fullBoard[1][0] && fullBoard[0][0] == fullBoard[2][0] && fullBoard[0][0] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[0][0];
        	window.alert(fullBoard[0][0] + " wins the game!!!");
        	winningSet = ['00', '10', '20'];
Kunal Shah's avatar
Kunal Shah committed
        }
        //col 2
        else if (fullBoard[0][1] == fullBoard[1][1] && fullBoard[0][1] == fullBoard[2][1] && fullBoard[0][1] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[0][1];
        	window.alert(fullBoard[0][1] + " wins the game!!!");
        	winningSet = ['01', '11', '21'];
Kunal Shah's avatar
Kunal Shah committed
        }
        //col 3
        else if (fullBoard[0][2] == fullBoard[1][2] && fullBoard[0][2] == fullBoard[2][2] && fullBoard[0][2] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[0][2];
        	window.alert(fullBoard[0][2] + " wins the game!!!");
        	winningSet = ['02', '12', '22'];
Kunal Shah's avatar
Kunal Shah committed
        }
        // diagonal
        else if (fullBoard[0][0] == fullBoard[1][1] && fullBoard[0][0] == fullBoard[2][2] && fullBoard[0][0] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[0][0];
        	window.alert(fullBoard[0][0] + " wins the game!!!");
        	winningSet = ['00', '11', '22'];
Kunal Shah's avatar
Kunal Shah committed
        }
        //diagonal
        else if (fullBoard[0][2] == fullBoard[1][1] && fullBoard[0][2] == fullBoard[2][0] && fullBoard[0][2] != null) {
Kunal Shah's avatar
Kunal Shah committed
        	winner = fullBoard[0][2];
        	window.alert(fullBoard[0][2] + " wins the game!!!");
        	winningSet = ['02', '11', '20'];
Kunal Shah's avatar
Kunal Shah committed
        }
        if (winningSet != null) {
Kunal Shah's avatar
Kunal Shah committed
        	for (var i = 0; i < winningSet.length; i++) {
Kunal Shah's avatar
Kunal Shah committed
                // set all backgound colour
                document.getElementById("B" + winningSet[i].charAt(0) + winningSet[i].charAt(1)).style.backgroundColor = 'green';
                // set whole board clickable
                document.getElementById("B" + winningSet[i].charAt(0) + winningSet[i].charAt(1)).style.pointerEvents = 'none';
            }
        }
        return winner;
    }

    function getBoard(boardTable) {
Kunal Shah's avatar
Kunal Shah committed
    	var innerBoard = [[], [], []];
    	for (var i = 0; i < boardTable.length; i++) {
Kunal Shah's avatar
Kunal Shah committed
            //get the row element
            var row = boardTable[i].children;
            for (var j = 0; j < row.length; j++) {
                //get the inner text of every element and put it in a 2d array
                innerBoard[i][j] = row[j].innerText;
            }
        }
        return innerBoard;
    }