Toggle navigation
Golibrary Code Playground - Everyone for education
Run
(current)
Fork
Settings
Share
0
306
Anonymous
HTML/CSS/Javascript
Python
Ruby
Clojure
PHP
Javascript (NodeJS)
Scala
Go
C/C++
Java
VB.Net
C#
Bash
Objective C
MySQL
Perl
Rust
HTML/CSS/Javascript
Search
Login
Login
Reset Password ?!
Login
Register
×
RESET PASSWORD
@
×
Allowed Extensions
Only this extensions are allowed: jpg,png,gif,jpeg
×
Error Message
×
Share this code
Url of this code
Send SMS with url of this code
Save As Github Gist
×
Github Gist
×
SETTINGS
HTML
CSS
Javascript
Title
Description
Meta Tags
Add meta tag example
Known ressources
--
Foundation
Bootstrap
CSS Preprocessor
CSS
LESS
SCSS
External CSS
Add another ressource
Known ressources
--
JQuery UI
Modernizr
Tree.js
JQuery
External JS
Add another ressource
CSS
→
table { border-collapse:collapse; } td { background-color: black; border: 3px solid white; font-size:80px; color:#ffffff; border-radius: 10px 10px 10px 10px; }
Full Screen
Javascript
→
/* * Tic Tac Toe * * A Tic Tac Toe game in HTML/JavaScript/CSS. * * */ var N_SIZE = 3, EMPTY = " ", boxes = [], turn = "X", score, moves; /* * Initializes the Tic Tac Toe board and starts the game. */ function init() { var board = document.createElement('table'); board.setAttribute("border", 1); board.setAttribute("cellspacing", 0); var identifier = 1; for (var i = 0; i < N_SIZE; i++) { var row = document.createElement('tr'); board.appendChild(row); for (var j = 0; j < N_SIZE; j++) { var cell = document.createElement('td'); cell.setAttribute('height', 120); cell.setAttribute('width', 120); cell.setAttribute('align', 'center'); cell.setAttribute('valign', 'center'); cell.classList.add('col' + j,'row' + i); if (i == j) { cell.classList.add('diagonal0'); } if (j == N_SIZE - i - 1) { cell.classList.add('diagonal1'); } cell.identifier = identifier; cell.addEventListener("click", set); row.appendChild(cell); boxes.push(cell); identifier += identifier; } } document.getElementById("tictactoe").appendChild(board); startNewGame(); } /* * New game */ function startNewGame() { score = { "X": 0, "O": 0 }; moves = 0; turn = "X"; boxes.forEach(function (square) { square.innerHTML = EMPTY; }); } /* * Check if a win or not */ function win(clicked) { // Get all cell classes var memberOf = clicked.className.split(/\s+/); for (var i = 0; i < memberOf.length; i++) { var testClass = '.' + memberOf[i]; var items = contains('#tictactoe ' + testClass, turn); // winning condition: turn == N_SIZE if (items.length == N_SIZE) { return true; } } return false; } function contains(selector, text) { var elements = document.querySelectorAll(selector); return [].filter.call(elements, function(element){ return RegExp(text).test(element.textContent); }); } /* * Sets clicked square and also updates the turn. */ function set() { if (this.innerHTML !== EMPTY) { return; } this.innerHTML = turn; moves += 1; score[turn] += this.identifier; if (win(this)) { alert('Winner: Player ' + turn); startNewGame(); } else if (moves === N_SIZE * N_SIZE) { alert("Draw"); startNewGame(); } else { turn = turn === "X" ? "O" : "X"; document.getElementById('turn').textContent = 'Player ' + turn; } } init();
HTML
→
<div id="tictactoe"></div> <div align="left"> <span id='turn'>Player X</span> </div>