-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
177 lines (156 loc) · 4.89 KB
/
functions.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { SYMBOLS, ROWS_COLS_COUNT, MOVE_INTERVAL } from "./constants";
// Each cell
export const createButton = (row, column) => {
const button = document.createElement("button");
button.dataset.row = row;
button.dataset.column = column;
button.dataset.filled = "no";
return button;
};
// Generate 3x3 cells
export const createGameBoard = (rows, cols) => {
const board = document.createElement("div");
board.className = "board";
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const button = createButton(row, col);
board.appendChild(button);
}
}
return board;
};
// Fetch all cells
export const fetchButtonList = () => {
return Array.from(document.querySelectorAll(".board button"));
};
// Display various game messages
export const displayMessage = (message = "") => {
const infoHeading = document.createElement("h2");
infoHeading.innerText = message;
infoHeading.style.marginTop = "30px";
infoHeading.classList.add("message");
document
.querySelector(".board")
.insertAdjacentElement("afterend", infoHeading);
};
// Welcome game start
export const gameStartFn = (state, btnText) => {
const gameStart = document.createElement("button");
gameStart.innerText = btnText;
gameStart.style.marginTop = "30px";
gameStart.classList.add("playagain");
gameStart.addEventListener("click", () => resetGame(state));
document
.querySelector(".message")
.insertAdjacentElement("afterend", gameStart);
if (!state.autoplayEnabled) {
instantiateMouseTracker();
startFollowing(state);
}
};
// Remove message
export const removeMessage = () => {
document.querySelector(".message").remove();
document.querySelector(".playagain").remove();
};
// No clicks after win/draw/start state
export const freezeGame = () => {
const buttonList = fetchButtonList();
buttonList.forEach((btn) => btn.classList.add("no-click"));
};
// Reset all variables
export const unfreezeGame = () => {
const buttonList = fetchButtonList();
buttonList.forEach((btn) => {
btn.disabled = false;
btn.dataset.filled = "no";
btn.textContent = "";
btn.classList.remove("winner");
btn.classList.remove("pop");
btn.classList.remove("no-click");
});
};
// Check for left/right diagonal paths
export const setWinningDiagonal = (direction) => {
const buttonList = fetchButtonList();
if (direction == "left") {
buttonList.forEach((btn) => {
if (btn.dataset.row == btn.dataset.column) btn.classList.add("winner");
});
}
if (direction == "right") {
buttonList.forEach((btn) => {
if (
Number(btn.dataset.row) + Number(btn.dataset.column) ==
ROWS_COLS_COUNT - 1
)
btn.classList.add("winner");
});
}
};
// Highlight boxes of winning pattern
export const markWinningBoxes = (winner) => {
const buttonList = fetchButtonList();
if (winner.row !== null) {
buttonList.forEach((btn) => {
if (btn.dataset.row == winner.row) btn.classList.add("winner");
});
}
if (winner.column !== null) {
buttonList.forEach((btn) => {
if (btn.dataset.column == winner.column) btn.classList.add("winner");
});
}
if (winner.diagonal !== null) {
setWinningDiagonal(winner.diagonal);
}
};
// Reset entire game
export const resetGame = (state) => {
removeMessage();
unfreezeGame();
state.selectedBoxCount = 0;
state.currentSymbol = SYMBOLS.player1;
state.gameArr = Array(ROWS_COLS_COUNT)
.fill(undefined)
.map(() => Array(ROWS_COLS_COUNT).fill(undefined));
if (state.autoplayEnabled) state.timer = autoGameRunner();
};
const startFollowing = (state) => {
const mouseTrackWrapper = document.querySelector(".track-wrapper");
if (state.autoplayEnabled) {
mouseTrackWrapper.style.display = "none";
return;
}
mouseTrackWrapper.style.display = "inline-block";
document.querySelector("body").addEventListener("mousemove", (e) => {
const { clientX, clientY } = e;
mouseTrackWrapper.textContent = state.currentSymbol;
mouseTrackWrapper.style.position = "absolute";
mouseTrackWrapper.style.top = clientY - 60 + "px";
mouseTrackWrapper.style.left = clientX - 50 + "px";
});
};
const instantiateMouseTracker = () => {
const wrapper = document.createElement("div");
wrapper.classList.add("track-wrapper");
wrapper.style.position = "absolute";
wrapper.style.top = 0 + "px";
wrapper.style.left = 0 + "px";
document.querySelector("body").appendChild(wrapper);
};
// Switch play modes
export const switchPlayModes = (state) => {
state.autoplayEnabled = !state.autoplayEnabled;
startFollowing(state);
};
// Auto game play, set to run every MOVE_INTERVAL ms
const autoGameRunner = () => {
return setInterval(() => {
const buttonList = Array.from(
document.querySelectorAll("[data-filled='no']")
);
const randomIdx = Math.floor(Math.random() * buttonList.length);
buttonList[randomIdx].click();
}, MOVE_INTERVAL);
};