Skip to content

Instantly share code, notes, and snippets.

@vinicius5581
Last active February 1, 2021 07:25
Show Gist options
  • Save vinicius5581/ad45684ea74a839b52c48c1a5f4905bf to your computer and use it in GitHub Desktop.
Save vinicius5581/ad45684ea74a839b52c48c1a5f4905bf to your computer and use it in GitHub Desktop.
Tic Tac Toe - Using Class - v 0.0.1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic tac toe</title>
<script src="./script.js" type="module"></script>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<div id="tic-tac-toe"></div>
</body>
</html>
import TicTacToe from './tic-tac-toe.js';
const game1 = new TicTacToe('tic-tac-toe');
#tic-tac-toe {
/* background: rgb(245, 214, 219); */
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#wrapper {
/* background: rgb(238, 128, 128); */
height: 50vh;
width: 50vh;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: center;
}
.cell {
/* background: rgb(102, 102, 241); */
height: 33%;
width: 33%;
border: 0px black solid;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
}
.cell:nth-child(1) {
border-width: 0 1px 1px 0;
}
.cell:nth-child(2) {
border-width: 0 1px 1px 0;
}
.cell:nth-child(3) {
border-width: 0 0 1px 0;
}
.cell:nth-child(4) {
border-width: 0 1px 1px 0;
}
.cell:nth-child(5) {
border-width: 0 1px 1px 0;
}
.cell:nth-child(6) {
border-width: 0 0 1px 0;
}
.cell:nth-child(7) {
border-width: 0 1px 0 0;
}
.cell:nth-child(8) {
border-width: 0 1px 0px 0;
}
.cell:nth-child(9) {
border-width: 0 0 0 0;
}
.cell span {
font-size: 40px;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
export default class TicTacToe{
constructor(el){
this.el = document.getElementById(el);
this.setupNewGame();
this.setupEventHandlers();
}
setupNewGame() {
this.gameState = [null, null, null, null, null, null, null, null, null];
this.currentPlayer = "X";
this.hasWinner = false;
this.render();
}
setupEventHandlers() {
this.el.addEventListener('click', e => {
if (!this.hasWinner) {
// Update game state
this.updateGameState(e.target.id);
// check for win
const isCurrentPlayerWinner = this.checkForWin();
if (isCurrentPlayerWinner) {
this.hasWinner = true;
} else {
// switch current player
this.currentPlayer = this.currentPlayer === 'X' ? 'O' : 'X';
}
// render board
this.render();
} else {
this.setupNewGame();
}
});
}
updateGameState(cellId) {
const cellIdx = cellId.substring(5) - 1;
this.gameState[cellIdx] = this.currentPlayer;
}
checkForWin() {
// check rows
for (let rowFirstCellIndex = 0; rowFirstCellIndex <= 6; rowFirstCellIndex += 3) {
let rowArray = [];
for (let cellIndex = rowFirstCellIndex; cellIndex <= rowFirstCellIndex + 2; cellIndex++) {
rowArray.push(this.gameState[cellIndex])
}
if (rowArray.every( v => v === rowArray[0] && !!rowArray[0])) {
return true
};
}
// check col
for (let colFirstCellIndex = 0; colFirstCellIndex <= 2; colFirstCellIndex++) {
let colArray = [];
for (let cellIndex = colFirstCellIndex; cellIndex <= colFirstCellIndex + 6; cellIndex += 3) {
colArray.push(this.gameState[cellIndex])
}
if (colArray.every( v => v === colArray[0] && !!colArray[0])) {
return true
};
}
// check diagonal top left to bottom right
const diag1 = [this.gameState[0],this.gameState[4],this.gameState[8]];
if (diag1.every( v => v === diag1[0] && !!diag1[0])) {
return true
};
// check diagonal top right to bottom left
const diag2 = [this.gameState[2],this.gameState[4],this.gameState[6]];
if (diag2.every( v => v === diag2[0] && !!diag2[0])) {
return true
};
return false;
}
render() {
this.el.innerHTML = '';
// create wrapper div
const wrapper = document.createElement('div');
wrapper.id = 'wrapper';
for(let i = 0; i < 9; i++) {
// create cell div
const cell = document.createElement('div');
cell.id = `cell-${i + 1}`;
cell.classList.add('cell');
// create span
const span = document.createElement('span');
span.innerText = this.gameState[i]
// append span into cell
cell.appendChild(span);
// append cell into wrapper
wrapper.append(cell);
}
const header = document.createElement('h1');
header.innerText = this.hasWinner ? `${this.currentPlayer} is the winner!` : `${this.currentPlayer} turn`
this.el.append(header);
this.el.append(wrapper);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment