Basically, I wrote a simple tic tac toe game. it uses a 2d array to represent the board (0 being empty, 1 being X and 2 being O)
I checked out solutions by other people but they used a whole different way to represent the board
here is the HTML
let board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
//the team symbols
let teamcross = "x";
let teamcircle = "o";
//for the tileclick function
let x = 0;
let y = 0;
var tile = "test";
//1 is Team X and 2 is Team O
var currentteam = 1;
//the tileclick function
function tileclick(tile, y, x) {
switch (currentteam) {
case 1:
document.getElementById(tile).innerHTML = "x";
board[y][x] = 1;
console.log(board)
currentteam = 2;
document.getElementById("turndisplay").innerHTML = "current team is: O";
break;
case 2:
document.getElementById(tile).innerHTML = "o"
board[y][x] = 2;
console.log(board)
currentteam = 1;
document.getElementById("turndisplay").innerHTML = "current team is: X";
break;
default:
window.alert("something is broken. Current Team is " + currentteam);
break;
}
}
<table id='gameboard'>
<tr id="toprow">
<td class="tile" onclick="tileclick('tl',0,0)" id="tl">*</td>
<td class="tile" onclick="tileclick('tc',0,1)" id="tc">*</td>
<td class="tile" onclick="tileclick('tr',0,2)" id="tr">*</td>
</tr>
<tr id="middlerow">
<td class="tile" onclick="tileclick('cl',1,0)" id="cl">*</td>
<td class="tile" onclick="tileclick('cc',1,1)" id="cc">*</td>
<td class="tile" onclick="tileclick('cr',1,2)" id="cr">*</td>
</tr>
<tr id="bottomrow">
<td class="tile" onclick="tileclick('bl',2,0)" id="bl">*</td>
<td class="tile" onclick="tileclick('bc',2,1)" id="bc">*</td>
<td class="tile" onclick="tileclick('br',2,2)" id="br">*</td>
</tr>
<h3 id="turndisplay">current team is: X</h3>