-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
148 lines (121 loc) · 3.89 KB
/
script.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const gameBoard = (()=>{
const board=["","","","","","","","",""]
let round=0;
function getItem(index)
{
return board[index]
}
function setItem(index,val)
{
board[index]=val
}
function setRound(val)
{
round=val
}
function getRound()
{
return round
}
function reset()
{
board.forEach((item,index)=>{
board[index]="";
})
displayController.displayGrid();
displayController.result.innerHTML = "";
displayController.gridBoard.style.pointerEvents = "auto";
}
return {getItem,setItem,setRound,getRound,reset}
})()
const Player = (sign)=>{
return {sign}
}
const displayController = (()=>{
const grid = document.querySelectorAll(".grid-item")
const btn = document.querySelector(".btn")
const result = document.querySelector(".result")
const gridBoard = document.querySelector(".grid");
const displayGrid = ()=>{
grid.forEach((item,index)=>{
item.innerHTML = gameBoard.getItem(index);
})
}
btn.addEventListener("click",()=>{
gameBoard.reset();
})
grid.forEach((item,index)=>{
item.addEventListener("click",()=>{
let boardValue = gameBoard.getItem(index)
let val = gameController.getSign()
if(boardValue=="")
gameController.changeSign();
else
val=boardValue;
gameBoard.setItem(index,val)
item.innerHTML = val;
if(gameController.isWinning(val))
{
result.innerHTML = `${val} is Winner`
gridBoard.style.pointerEvents = "none";
}
else if(gameController.isFull())
result.innerHTML = `Draw`
displayGrid();
})
})
return {displayGrid,gridBoard,result};
})()
const gameController = (()=>{
const Player1 = Player("O");
const Player2 = Player("X");
const getSign = ()=>{
let round=gameBoard.getRound();
return (round==0)?Player1.sign:Player2.sign
}
const changeSign = ()=>{
let round=gameBoard.getRound();
gameBoard.setRound(1-round);
}
const isWinning = (symbol)=>{
const one=gameBoard.getItem(0)
const two=gameBoard.getItem(1)
const third=gameBoard.getItem(2)
const four=gameBoard.getItem(3)
const five=gameBoard.getItem(4)
const six=gameBoard.getItem(5)
const seven=gameBoard.getItem(6)
const eight=gameBoard.getItem(7)
const nine=gameBoard.getItem(8)
if(one==symbol && two==symbol && third==symbol)
return true;
if(four==symbol && five==symbol && six==symbol)
return true;
if(seven==symbol && eight==symbol && nine==symbol)
return true;
if(one==symbol && five==symbol && nine==symbol)
return true;
if(two==symbol && five==symbol && eight==symbol)
return true;
if(third==symbol && five==symbol && seven==symbol)
return true;
if(one==symbol && four==symbol && seven==symbol)
return true;
if(third==symbol && five==symbol && nine==symbol)
return true
if(third==symbol && six==symbol && nine==symbol)
return true
if(one==symbol && four==symbol && seven==symbol)
return true
return false
}
const isFull = ()=>{
for(let i=0;i<10;i++)
{
if(gameBoard.getItem(i)=="")
return false
}
return true
}
return {getSign,isFull,isWinning,changeSign}
})()