I am making a little Tic Tac Toe game and I want to alert the user when they try to click in a cell which already has a value in it. Here is what I have so far.
var turn = "O";
//player 1 = turn = O
//player2 = turn = X
var cell = $('#cell');
function go(cellID) {
//alert(turn);
var boxId = "#" + cellID;
if (cell = null) {
if (turn == 'O') {
$(boxId).html("O");
turn = 'X';
} else {
if (turn == 'X') {
$(boxId).html("X");
turn = 'O';
}
}
} else {
alert('Pick another box')
}
//alert(cellID);
$('h2:last').html('Turn: ' + turn);
return turn;
}
.cell {
height: 20px;
width: 10px;
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gameTable">
<table style="width: 100%;">
<tr>
<td onclick="go('C0')" id="C0" class="cell"></td>
<td onclick="go('C1')" id="C1" class="cell"></td>
<td onclick="go('C2')" id="C2" class="cell"></td>
</tr>
<tr>
<td onclick="go('C3')" id="C3" class="cell"></td>
<td onclick="go('C4')" id="C4" class="cell"></td>
<td onclick="go('C5')" id="C5" class="cell"></td>
</tr>
<tr>
<td onclick="go('C6')" id="C6" class="cell"></td>
<td onclick="go('C7')" id="C7" class="cell"></td>
<td onclick="go('C8')" id="C8" class="cell"></td>
</tr>
</table>
</div>