JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<table>
<tr>
<td id='c1'></td>
<td id='c2'></td>
<td id='c3'></td>
</tr>
<tr>
<td id='c4'></td>
<td id='c5'></td>
<td id='c6'></td>
</tr>
<tr>
<td id='c7'></td>
<td id='c8'></td>
<td id='c9'></td>
</tr>
</table>
</body>
CSS
table, td {
border-collapse: collapse;
border: 1px solid black;
height: 35px;
width: 35px;
text-align: center;
background-color: grey;
color: black;
overflow: hidden;
table-layout: fixed;
white-space: nowrap;
}
JavaScript
//Creates the variables needed to be manipulated later
$(document).ready(function () {
var X = 'X';
var O = 'O';
var currentPlayer;
var turnCount = 0;
var xMoves = [];
var oMoves = [];
var cellTracker;
var winConditions = [[c1, c2, c3], [c4, c5, c6], [c7, c8, c9], [c1,c4, c7], [c2, c5, c8], [c3, c6, c9], [c1, c5, c9], [c3, c5, c7]];
/*Set's the current player to X if turnCount is odd
And to O if turnCount is even*/
var setCurrentPlayer = function () {
if (turnCount % 2 === 0) {
currentPlayer = O;
} else {
currentPlayer = X;
}
};
//Pushes cellTracker's value to the curent player's move variable
var storeMoves = function () {
if (currentPlayer === X) {
xMoves.push(cellTracker);
} else if (currentPlayer === O) {
oMoves.push(cellTracker);
}
};
//Compares players moves with the winConditions to determine a winner
var determineWin = function (pMoves) {
var winAlert;
for (var i = 0; i < winConditions.length; i++) {
if (winConditions[i].length > pMoves.length) {
continue;
}
for (var j = 0; j < winConditions[i].length; j++) {
winAlert = false;
for (var k = 0; k < pMoves.length; k++) {
if (pMoves[k] === winConditions[i][j]) {
winAlert = true;
break;
}
}
if (!winAlert) break;
}
if (winAlert) {
alert(currentPlayer + "wins!");
break;
}
}
};
/*Place's an X or an O when a cell is clicked depending
on the current player, and updates the turnCount*/
$('td').one('click', function () {
turnCount += 1;
setCurrentPlayer();
$(this).text(currentPlayer);
cellTracker = $(this).attr('id');
storeMoves();
...