var output = function (messageElement) { var feedback = messageElement; function setText(message) { infoBox.innerText = message; } return { setMessage: setText }; }; function goodMove(box) { return box.innerText.length == 0; }; /* Experimental Version with algorhythm. function checkForWinner(boxes, playas, currentTurn){ function rowWinner(boxes){ var win = player[currentTurn]; ( (boxes[i] == win) && (boxes[i++]) && (boxes[i++]) ) return true; } function collumnWinner (boxes) { } */ function checkForWinner(boxes, playas, currentTurn) { if (boxes[0].innerText == playas[currentTurn] && boxes[1].innerText == playas[currentTurn] && boxes[2].innerText == playas[currentTurn]) return true; if (boxes[3].innerText == playas[currentTurn] && boxes[4].innerText == playas[currentTurn] && boxes[5].innerText == playas[currentTurn]) return true; if (boxes[6].innerText == playas[currentTurn] && boxes[7].innerText == playas[currentTurn] && boxes[8].innerText == playas[currentTurn]) return true; if (boxes[0].innerText == playas[currentTurn] && boxes[3].innerText == playas[currentTurn] && boxes[6].innerText == playas[currentTurn]) return true; if (boxes[1].innerText == playas[currentTurn] && boxes[4].innerText == playas[currentTurn] && boxes[7].innerText == playas[currentTurn]) return true; if (boxes[2].innerText == playas[currentTurn] && boxes[5].innerText == playas[currentTurn] && boxes[8].innerText == playas[currentTurn]) return true; if (boxes[0].innerText == playas[currentTurn] && boxes[4].innerText == playas[currentTurn] && boxes[8].innerText == playas[currentTurn]) return true; if (boxes[2].innerText == playas[currentTurn] && boxes[4].innerText == playas[currentTurn] && boxes[6].innerText == playas[currentTurn]) return true; return false; }; function isDraw(boxes) { for (var i = 0, len = boxes.length; i < len; i++) if (boxes[i].innerText.length == 0) return false; return true; } function setMark(box, mark) { box.innerText = mark; } function play() { var boxes = document.querySelectorAll("#emanon td"); var playas = ["X", "O"]; var currentTurn = 0; var gameOver = false; var feedback = new output(document.querySelector("#infoBox")); for (var i = 0, limit = boxes.length; i < limit; i++) { boxes[i].addEventListener("click", function () { if (gameOver) { return; } if (!goodMove(this)) { feedback.setMessage("No way Jose ^_^"); } else { setMark(this, playas[currentTurn]); gameOver = checkForWinner(boxes, playas, currentTurn); if (gameOver) { feedback.setMessage("Playa " + playas[currentTurn] + " is victorious"); return; } if (isDraw(boxes)) { feedback.setMessage("It's a draw pilgram.") return; } currentTurn = (currentTurn == "0") ? "1" : "0"; feedback.setMessage("Now it's " + playas[currentTurn] + "\'s turn.") } }); }; } play();
<h1>Tic Tac Toe</h1> <table id="emanon" border="1"> <tr> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> </tr> </table> <div id="infoBox">Ready to Play?</div> <pre> _ ___ _ _ | |___ __ _ _ _ ___| _ (_)___ _ _ _ _ ___ | || / -_) _` | ' \___| _/ / -_) '_| '_/ -_) \__/\___\__,_|_||_| |_| |_\___|_| |_| \___| </pre>
body { font-family: courier; text-align:center; } h1 { color:#999; } table, tr { border:none; } #emanon { margin: 50px auto; } td { border: 3px dotted #5CB3FF; width: 80px; height:80px; } td:hover { background: #9C0063; color:white; } td:active { background:#FF0080; } td { font-family:helvetica; font-weight:700; font-size:2em; } #infoBox { width:300px; height: 20px; padding:15px; margin:0 auto; background:black; color:green; }