-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe.js
30 lines (29 loc) · 1.1 KB
/
tic-tac-toe.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
var board = new Board();
var boardWrapper = document.getElementById('board');
var currentPlayer = document.getElementById('currentPlayer');
currentPlayer.innerText = board.whosChance;
for (let row=0; row < 3; row++) {
for (let col=0; col < 3; col++) {
let newOption = document.createElement("div");
newOption.dataset.row = row;
newOption.dataset.col = col;
newOption.classList.add("tile");
boardWrapper.appendChild(newOption);
newOption.addEventListener("click", (el)=>{
let data = board.play(el.target.dataset.row,el.target.dataset.col);
if(data.status && data.position){
el.target.innerText = data.position;
newOption.classList.add(data.position == "X" ? 'player1Selection' : 'player2Selection');
}
if(data.status != true){
if(data.status == 200){
document.getElementById('status').innerText = "Winner:";
}else if(data.status == 204){
document.getElementById('result').innerHTML = "<i> Its Tie </i>"
}
Toastify({text: data.msg, className: data.status == false ? "toast_error" : "toast_win"}).showToast();
}
currentPlayer.innerText = board.whosChance;
});
}
}