I just finished a little game of Tic-Tac-Toe for a class I'm in, and I think it looks alright, but I'm pretty fresh, so I would like to know how I might be able to improve it, given the situation I'm in. I'd like to host it on my website and I'd like it to be safe and expandable.
Please let me know where I'm off (or gone wrong) in terms of best practices and if you feel like being nice, what looks OK. I know I've got three global variables, but the program is small, so I let them stay there. If you can think of a way to easily eliminate those variables without drastically changing the rest of the code, I'd be interested. Again, I'd like this to become something else (a more complicated game of Tic-Tac-Toe) so if you can think of ways of making the code more usable at a future time, I'd also be interested.
JS:
<script type="text/javascript">
// Global variables
var firstGame = true;
var turnNum;
var squareTaken = new Array();
//functions
function newGame(firstGame){ //starts a new game by
turnNum = 0; //setting the turn number to 0
for (var i=0; i<9; i++){ //populating squareTaken array
squareTaken[i] = ""; //with "", as in no X or O
}
if (firstGame !== true){ //if this is not the first game
var canvasID;
var c;
var cxt;
for (var i= 0; i<9; i++){ //loop through the canvas contexts and erase their current drawings.
canvasID = "canvas" + i;
c = document.getElementById(canvasID);
cxt = c.getContext("2d");
cxt.clearRect(0,0,50,50); //Clear the rectangle starting at 0,0 and going to the width and height of each.
}
}
}
function canvasClicked(canvasNum){ //handles canvas clicks
var canvasID;
var c;
var cxt;
canvasID = "canvas" + canvasNum;
c = document.getElementById(canvasID);
cxt = c.getContext("2d");
if (squareTaken[canvasNum] == ""){
if (turnNum % 2 == 0){
cxt.beginPath();
cxt.moveTo(10,10);
cxt.lineTo(40,40);
cxt.moveTo(10,40);
cxt.lineTo(40,10);
cxt.stroke();
cxt.closePath();
squareTaken[canvasNum] = "X";
turnNum++;
}
else {
cxt.beginPath();
cxt.arc(25,25,15,0,2*Math.PI)
cxt.stroke();
cxt.closePath();
squareTaken[canvasNum] = "O";
turnNum++;
}
}
if (turnNum > 4){
checkWin(squareTaken, canvasNum);
}
}
function checkWin(squareTaken, canvasNum){ //returns true if game over
if ( //If any of the following combinations are true, someone has won. last && checks for non-occupied squares.
(squareTaken[0]===squareTaken[1] && squareTaken[1]===squareTaken[2] && squareTaken[0] != "") ||
(squareTaken[3]===squareTaken[4] && squareTaken[4]===squareTaken[5] && squareTaken[3] != "") ||
(squareTaken[6]===squareTaken[7] && squareTaken[7]===squareTaken[8] && squareTaken[6] != "") ||
(squareTaken[0]===squareTaken[3] && squareTaken[3]===squareTaken[6] && squareTaken[0] != "") ||
(squareTaken[1]===squareTaken[4] && squareTaken[4]===squareTaken[7] && squareTaken[1] != "") ||
(squareTaken[2]===squareTaken[5] && squareTaken[5]===squareTaken[8] && squareTaken[2] != "") ||
(squareTaken[0]===squareTaken[4] && squareTaken[4]===squareTaken[8] && squareTaken[0] != "") ||
(squareTaken[2]===squareTaken[4] && squareTaken[4]===squareTaken[6] && squareTaken[2] != "")
)
{
//Declare winner
alert(squareTaken[canvasNum] + " wins!\nStarting a new game!");
if (firstGame === true){
firstGame = false;
}
newGame(firstGame);
}
else if ( //Check for tie
squareTaken[0]!="" && squareTaken[1]!="" && squareTaken[2]!="" &&
squareTaken[3]!="" && squareTaken[4]!="" && squareTaken[5]!="" &&
squareTaken[6]!="" && squareTaken[7]!="" && squareTaken[8]!=""
)
{
//Declare tie
alert("It's a tie! \n Starting new game!");
if (firstGame === true){
firstGame = false;
}
newGame(firstGame);
}
}
//Start the game
newGame(firstGame);
</script>
Related HTML:
<canvas id="canvas0" width="50" height="50" onClick="canvasClicked(0)"></canvas>
<canvas id="canvas1" width="50" height="50" onClick="canvasClicked(1)"></canvas>
<canvas id="canvas2" width="50" height="50" onClick="canvasClicked(2)"></canvas>
<br />
<canvas id="canvas3" width="50" height="50" onClick="canvasClicked(3)"></canvas>
<canvas id="canvas4" width="50" height="50" onClick="canvasClicked(4)"></canvas>
<canvas id="canvas5" width="50" height="50" onClick="canvasClicked(5)"></canvas>
<br />
<canvas id="canvas6" width="50" height="50" onClick="canvasClicked(6)"></canvas>
<canvas id="canvas7" width="50" height="50" onClick="canvasClicked(7)"></canvas>
<canvas id="canvas8" width="50" height="50" onClick="canvasClicked(8)"></canvas>