Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tic Tac Toe Game using HTML,CSS, and JS #6776

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions code/tic-tac-toe/app.js
@@ -0,0 +1,93 @@
alert('Made with ❤ by Abhishek Anand')
var N_SIZE = 3,
EMPTY = ' ',
boxes = [],
turn = 'X',
score,
moves;

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();
}

function startNewGame() {
score = {
'X': 0,
'O': 0
};
moves = 0;
turn = 'X';
boxes.forEach(function (square) {
square.innerHTML = EMPTY;
});
}

function win(clicked) {
var memberOf = clicked.className.split(/\s+/);
for (var i = 0; i < memberOf.length; i++) {
var testClass = '.' + memberOf[i];
var items = contains('#tictactoe ' + testClass, turn);
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);
});
}

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();
56 changes: 56 additions & 0 deletions code/tic-tac-toe/css/tic-tac-toe.css
@@ -0,0 +1,56 @@
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}

.container {
display: flex;
background: #fff;
align-content: center;
justify-content: center;
flex-direction: column;
min-width: 700px;
min-height: 500px;
border-radius: 5%;
margin: auto;
}

table {
border-collapse: collapse;
margin: auto;
}

td {
background-color: #666;
border: 5px solid white;
font-size: 80px;
color: #fff;
border-radius: 10%;
}

#player {
padding: 1rem;
}

body {
display: flex;
background: #eee url('https://www.wallpapertip.com/wmimgs/82-825008_high-resolution-background-image-for-website.jpg') no-repeat
center center/cover;
height: 100vh;
width: 100%;
overflow: hidden;
}
@media(max-width: 880px){
.container{
display: flex;
background: #fff;
align-content: center;
justify-content: center;
flex-direction: column;
min-width: 350px;
min-height: 250px;
border-radius: 5%;
margin: auto;
}
}
17 changes: 17 additions & 0 deletions code/tic-tac-toe/index.html
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Tic Tac Toe</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="container">
<div id="tictactoe"></div>
<div id="player" align="center">
<span id="turn" align="">Player X</span>
</div>
</div>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
93 changes: 93 additions & 0 deletions code/tic-tac-toe/js/tic-tac-toe.js
@@ -0,0 +1,93 @@
alert('Made with ❤ by Abhishek Anand')
var N_SIZE = 3,
EMPTY = '&nbsp;',
boxes = [],
turn = 'X',
score,
moves;

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();
}

function startNewGame() {
score = {
'X': 0,
'O': 0
};
moves = 0;
turn = 'X';
boxes.forEach(function (square) {
square.innerHTML = EMPTY;
});
}

function win(clicked) {
var memberOf = clicked.className.split(/\s+/);
for (var i = 0; i < memberOf.length; i++) {
var testClass = '.' + memberOf[i];
var items = contains('#tictactoe ' + testClass, turn);
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);
});
}

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();
56 changes: 56 additions & 0 deletions code/tic-tac-toe/style.css
@@ -0,0 +1,56 @@
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}

.container {
display: flex;
background: #fff;
align-content: center;
justify-content: center;
flex-direction: column;
min-width: 700px;
min-height: 500px;
border-radius: 5%;
margin: auto;
}

table {
border-collapse: collapse;
margin: auto;
}

td {
background-color: #666;
border: 5px solid white;
font-size: 80px;
color: #fff;
border-radius: 10%;
}

#player {
padding: 1rem;
}

body {
display: flex;
background: #eee url('https://www.wallpapertip.com/wmimgs/82-825008_high-resolution-background-image-for-website.jpg') no-repeat
center center/cover;
height: 100vh;
width: 100%;
overflow: hidden;
}
@media(max-width: 880px){
.container{
display: flex;
background: #fff;
align-content: center;
justify-content: center;
flex-direction: column;
min-width: 350px;
min-height: 250px;
border-radius: 5%;
margin: auto;
}
}