-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_game.js
129 lines (109 loc) · 2.94 KB
/
tic_game.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Define necessary variables
var squares = $('td');
var start = $('#start');
var restart = $('#restart');
var game_on = false;
var won = "";
var player1 = '';
var player2 = '';
var turn = player1;
// Restart Game button
$('#restart').on('click', function () {
for (var i=0; i < squares.length;i++) {
squares[i].textContent = '';
}
$(this).attr("disabled", true);
start.attr("disabled", false);
start.text("Start the Game!");
game_on = false;
disable_table()
})
function disable_table() {
if (game_on === false) {
for (var i=0;i<squares.length;i++) {
squares.eq(i).attr("disabled", "disabled")
}
}
else {
for (var i=0;i<squares.length;i++) {
squares.eq(i).attr("disabled", false)
}
}
}
// Start the game!
$('#start').on('click', function () {
game_on = true
restart.attr("disabled", false);
$(this).attr("disabled", true);
$(this).text("Game Started!");
player1 = prompt("Please enter the name of player1");
player2 = prompt("Please enter the name of player2");
$('h3').html("It is " + player1 + " turn")
turn = player1
})
// Input the X or O values
squares.click(changeMarker)
function changeMarker() {
if (game_on === false) {
alert("Please click on Start the Game!")
return;
}
if (this.textContent === 'X' || this.textContent === 'O') {
alert("This field is already Occupied");
return;
}
if (this.textContent === '') {
if (turn === player1) {
this.textContent = 'X';
}
else {
this.textContent = 'O';
}
}
console.log(turn + " selected");
if (winner() === true) {
console.log(turn + " is the Winner");
alert(turn + " is the Winner");
$('h3').html(turn + " is the Winner. Please restart the game.");
game_on = false;
disable_table()
}
if (tableFull() === true && game_on === true) {
$('h3').html("Table is Full. Please restart the game.");
alert("Table is Full. Please restart your game");
game_on = false;
}
if (turn === player1) {
turn = player2
}
else {
turn = player1
}
$('h3').html("It is " + turn + " turn")
}
// Check for winning
function checkTable(p1, p2, p3) {
if (squares.eq(p1).text() === squares.eq(p2).text() &&
squares.eq(p2).text() === squares.eq(p3).text() &&
squares.eq(p1).text() != '') {
return true
}
return false
}
// Check if table is Full
function tableFull() {
for (var i=0; i< squares.length;i++) {
if (squares.eq(i).text() === '') return false;
}
return true;
}
function winner() {
return checkTable(0, 1, 2)
|| checkTable(3, 4, 5)
|| checkTable(6, 7, 8)
|| checkTable(0, 3, 6)
|| checkTable(1, 4, 7)
|| checkTable(2, 5, 8)
|| checkTable(0, 4, 8)
|| checkTable(6, 4, 2)
}