-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
89 lines (76 loc) · 2.42 KB
/
script2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
console.clear();
var player1sTurn = true;
var isGameOver = false;
var turns = 0;
function ready(){
console.log('ready');
console.log($('.square').length + " elements found");
$('#button1').on('click',resetGame);
$('.square').on('click',playTurn);
function playTurn(){
//is the game over then no more turns allowed
console.log('i= ' + $(this).attr('id'));
if ( isGameOver ) {return};
console.log("playTurn at " + $(this).attr('id'));
if (($(this).attr('class') === "square playerX") || ($(this).attr('class') === "square playerO")) {
console.log("Square already played");
alert("Square already played! Try another square");
return;
}
if ( player1sTurn=== true ) {
$(this).addClass("playerX");
$(this).html("X");
player1sTurn = false;
turns ++;
$("#turn").html("It is Player O's Turn");
}
else {
$(this).addClass("playerO");
$(this).html("O");
player1sTurn = true;
turns ++;
$("#turn").html("It is Player X's Turn");
}
checkGameOver();
}
function checkGameOver(){
console.log("checking if gameover");
var winner = 0;
//check each possible line if no winner yet
winner = checkLine(0,1,2);
//check other lines e.g
if ( winner === 0 ) winner = checkLine(3,4,5);
if ( winner === 0 ) winner = checkLine(6,7,8);
if ( winner === 0 ) winner = checkLine(0,3,6);
if ( winner === 0 ) winner = checkLine(1,4,7);
if ( winner === 0 ) winner = checkLine(2,5,8);
if ( winner === 0 ) winner = checkLine(0,4,8);
if ( winner === 0 ) winner = checkLine(6,4,2);
if ( winner !== 0 ){
isGameOver = true;
alert("Winner is player " + winner );
resetGame();
}
if (turns>8) {
alert("It's a tie!");
resetGame();
}
}
function checkLine(a,b,c){
if ( $('.square')[a].className ==="square playerX" && ( $('.square')[a].className === $('.square')[b].className ) && ( $('.square')[a].className === $('.square')[c].className ) ){
return "X";}
else if ($('.square')[a].className ==="square playerO" && ( $('.square')[a].className === $('.square')[b].className ) && ( $('.square')[a].className === $('.square')[c].className )){
return "O";}
else {return 0;}
}
function resetGame(){
$('.square').removeClass("playerX");
$('.square').removeClass("playerO");
$('.square').html("");
player1sTurn = true;
isGameOver = false;
turns = 0;
console.log("board cleared");
}
}
window.addEventListener('load', ready, false);