-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (75 loc) · 1.81 KB
/
app.js
File metadata and controls
87 lines (75 loc) · 1.81 KB
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
function startGame(){
for(var i = 1;i <= 9; i++){
clearBox(i);
}
document.write = "X";
document.winner = null;
setMessage(document.write + " get's to start.")
}
function nextMove(perpiece){
if(document.winner != null){
setMessage(document.write + " already won.");
}
else if(perpiece.innerText == ''){
perpiece.innerText=document.write;
switchTurn();
}else{
setMessage("Pick another square.")
}
}
// To change message section
function setMessage(msg){
document.getElementById("message").innerText = msg;
}
// To check X or O
function switchTurn(){
if(checkForWinner(document.write)){
setMessage("Congrats " + document.write + " , you won!")
document.winner = document.write;
}else if(CheckforTie())
{
setMessage("Tie!! Play again...!!!");
}
else if(document.write == "X"){
document.write = "O";
setMessage("It's " + document.write + "'s turn now !" );
}
else{
document.write = "X";
setMessage("It's " + document.write + "'s turn now !" );
}
}
function checkForWinner(move){
var result = false;
if(checkRow(1, 2, 3, move) ||
checkRow(4, 5, 6, move) ||
checkRow(7, 8, 9, move) ||
checkRow(1, 4, 7, move) ||
checkRow(2, 5, 8, move) ||
checkRow(3, 6, 9, move) ||
checkRow(1, 5, 9, move) ||
checkRow(3, 5, 7, move)){
result = true;
}
return result;
}
function checkRow(a, b, c, move){
var result = false;
if (getBox(a) == move && getBox(b) == move && getBox(c) == move){
result = true;
}
return result;
}
function CheckforTie(){
for(var i=1;i<10;i++){
if(getBox(i)=="")
return false;
}
return true;
}
function getBox(number){
return document.getElementById("s" + number).innerText;
}
function clearBox(number){
document.getElementById("s" + number).innerText = "";
}