-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic.js
113 lines (97 loc) · 2.83 KB
/
tic.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
import { Events } from "./events.js";
let game = [];
let currPlayer = "";
let scoreX = 0;
let scoreO = 0;
const SIZE = 3;
const INST_YOUR_TURN = "It's your turn to play!";
const INST_OPP_TURN = "Waiting for opponent to play!";
var socket = io();
document.addEventListener("DOMContentLoaded", function () {
var isPlayerLocked = false;
const player = document.getElementById("playing_as");
socket.on(Events.START_GAME, (response) => {
if (response !== "O" || response != "X") {
isPlayerLocked = true;
}
initialize(isPlayerLocked);
if (response == "O") {
myTurn();
currPlayer = "O";
player.innerHTML = "You are playing as O";
} else if (response == "X") {
oppTurn();
currPlayer = "X";
player.innerHTML = "You are playing as X";
}
});
});
// Create buttons for tic tac toe
function initialize(isPlayerLocked) {
var grid = document.querySelector("#grid");
grid.replaceChildren();
game = [];
for (let i = 0; i < SIZE; i++) {
const row = [];
for (let j = 0; j < SIZE; j++) {
row.push(null);
grid.appendChild(createButton(i, j, isPlayerLocked));
}
game.push(row);
}
}
function createButton(row, col, isDisabled) {
const item = document.createElement("button");
item.setAttribute("type", "button");
item.onclick = function () {
onButtonClick(row, col);
};
item.setAttribute("class", "grid-item");
item.setAttribute("id", "b" + row + col);
if (isDisabled) {
item.setAttribute("disabled", true);
}
item.appendChild(document.createTextNode(""));
const newDiv = document.createElement("div");
newDiv.appendChild(item);
return newDiv;
}
function onButtonClick(row, col) {
const box = document.getElementById("b" + row + col);
if (box.textContent !== "") {
return;
}
box.textContent = currPlayer;
game[row][col] = currPlayer;
socket.emit(Events.PLAY, { player: currPlayer, row: row, col: col });
oppTurn();
}
socket.on(Events.OPP_PLAY, (args) => {
const box = document.getElementById("b" + args["row"] + args["col"]);
box.textContent = args["player"];
myTurn();
});
socket.on(Events.WIN, (player) => {
if (player == "O") {
scoreO += 1;
} else {
scoreX += 1;
}
document.getElementById("scores").innerText =
"Scores: PlayerX: " + scoreX + ", PlayerO: " + scoreO;
alert("Player " + player + " won!");
disableAllButtons();
});
function myTurn() {
document.getElementById("instruction").innerText = INST_YOUR_TURN;
const elements = document.querySelectorAll('[id^="b"]');
elements.forEach((elem) => elem.removeAttribute("disabled"));
}
function oppTurn() {
document.getElementById("instruction").innerText = INST_OPP_TURN;
disableAllButtons();
}
function disableAllButtons() {
const elements = document.querySelectorAll('[id^="b"]');
elements.forEach((elem) => elem.setAttribute("disabled", true));
}