|
1 | 1 | import View from "./view.js";
|
2 | 2 | import Store from "./store.js";
|
3 | 3 |
|
4 |
| -const App = { |
5 |
| - $: { |
6 |
| - menu: document.querySelector('[data-id="menu"]'), |
7 |
| - menuBtn: document.querySelector('[data-id="menu-btn"]'), |
8 |
| - menuItems: document.querySelector('[data-id="menu-items"]'), |
9 |
| - resetBtn: document.querySelector('[data-id="reset-btn"]'), |
10 |
| - newRoundBtn: document.querySelector('[data-id="new-round-btn"]'), |
11 |
| - squares: document.querySelectorAll('[data-id="squares"]'), |
12 |
| - modal: document.querySelector('[data-id="modal"]'), |
13 |
| - modalText: document.querySelector('[data-id="modal-text"]'), |
14 |
| - modalBtn: document.querySelector('[data-id="modal-btn"]'), |
15 |
| - turn: document.querySelector('[data-id="turn"]'), |
16 |
| - }, |
17 |
| - |
18 |
| - state: { |
19 |
| - moves: [], |
20 |
| - }, |
21 |
| - |
22 |
| - getGameStatus(moves) { |
23 |
| - const p1Moves = moves |
24 |
| - .filter((move) => move.playerID === 1) |
25 |
| - .map((move) => move.squareID); |
26 |
| - const p2Moves = moves |
27 |
| - .filter((move) => move.playerID === 2) |
28 |
| - .map((move) => move.squareID); |
29 |
| - |
30 |
| - const winningPatterns = [ |
31 |
| - [1, 2, 3], |
32 |
| - [1, 5, 9], |
33 |
| - [1, 4, 7], |
34 |
| - [2, 5, 8], |
35 |
| - [3, 5, 7], |
36 |
| - [3, 6, 9], |
37 |
| - [4, 5, 6], |
38 |
| - [7, 8, 9], |
39 |
| - ]; |
40 |
| - |
41 |
| - let winner = null; |
42 |
| - |
43 |
| - winningPatterns.forEach((pattern) => { |
44 |
| - const p1Wins = pattern.every((val) => p1Moves.includes(val)); |
45 |
| - const p2Wins = pattern.every((val) => p2Moves.includes(val)); |
46 |
| - |
47 |
| - if (p1Wins) winner = 1; |
48 |
| - if (p2Wins) winner = 2; |
49 |
| - }); |
50 |
| - |
51 |
| - return { |
52 |
| - status: |
53 |
| - moves.length === 9 || winner !== null ? "complete" : "in-progress", |
54 |
| - winner, |
55 |
| - }; |
56 |
| - }, |
57 |
| - |
58 |
| - init() { |
59 |
| - App.registerEventListners(); |
60 |
| - }, |
61 |
| - |
62 |
| - registerEventListners() { |
63 |
| - // actions menu toggle |
64 |
| - App.$.menu.addEventListener("click", () => { |
65 |
| - App.$.menuItems.classList.toggle("hidden"); |
66 |
| - App.$.menuBtn.classList.toggle("border"); |
67 |
| - }); |
68 |
| - |
69 |
| - // TODO |
70 |
| - App.$.resetBtn.addEventListener("click", () => { |
71 |
| - console.log("reset-btn was clicked"); |
72 |
| - }); |
73 |
| - |
74 |
| - // TODO |
75 |
| - App.$.newRoundBtn.addEventListener("click", () => { |
76 |
| - console.log("new-round-btn was clicked"); |
77 |
| - }); |
78 |
| - |
79 |
| - App.$.modalBtn.addEventListener("click", () => { |
80 |
| - App.state.moves = []; |
81 |
| - App.$.squares.forEach((square) => square.replaceChildren()); |
82 |
| - App.$.modal.classList.add("hidden"); |
83 |
| - }); |
84 |
| - |
85 |
| - App.$.squares.forEach((square) => { |
86 |
| - square.addEventListener("click", () => { |
87 |
| - // Check if there's already a play, if so, return early |
88 |
| - const hasMove = (squareID) => { |
89 |
| - const existingMove = App.state.moves.find( |
90 |
| - (move) => move.squareID === squareID |
91 |
| - ); |
92 |
| - return existingMove !== undefined; |
93 |
| - }; |
94 |
| - |
95 |
| - if (hasMove(+square.id)) return; |
96 |
| - |
97 |
| - // Determine which player icon to add to the square |
98 |
| - const lastMove = App.state.moves.at(-1); |
99 |
| - const getOppositePlayer = (playerID) => (playerID === 1 ? 2 : 1); |
100 |
| - const currentPlayer = |
101 |
| - App.state.moves.length === 0 |
102 |
| - ? 1 |
103 |
| - : getOppositePlayer(lastMove.playerID); |
104 |
| - const nextPlayer = getOppositePlayer(currentPlayer); |
105 |
| - |
106 |
| - const squareIcon = document.createElement("i"); |
107 |
| - const turnIcon = document.createElement("i"); |
108 |
| - const turnLabel = document.createElement("p"); |
109 |
| - turnLabel.textContent = `Player ${nextPlayer}, you're up!`; |
110 |
| - |
111 |
| - if (currentPlayer === 1) { |
112 |
| - squareIcon.classList.add("fa-solid", "fa-x", "turquoise"); |
113 |
| - turnIcon.classList.add("fa-solid", "fa-o"); |
114 |
| - App.$.turn.classList.replace("turquoise", "yellow"); |
115 |
| - } else { |
116 |
| - squareIcon.classList.add("fa-solid", "fa-o", "yellow"); |
117 |
| - turnIcon.classList.add("fa-solid", "fa-x"); |
118 |
| - App.$.turn.classList.replace("yellow", "turquoise"); |
119 |
| - } |
120 |
| - |
121 |
| - App.$.turn.replaceChildren(turnIcon, turnLabel); |
122 |
| - |
123 |
| - App.state.moves.push({ |
124 |
| - squareID: +square.id, |
125 |
| - playerID: currentPlayer, |
126 |
| - }); |
127 |
| - |
128 |
| - square.appendChild(squareIcon); |
129 |
| - |
130 |
| - // Check for a winner or tie game |
131 |
| - const game = App.getGameStatus(App.state.moves); |
132 |
| - |
133 |
| - let msg = ""; |
134 |
| - if (game.status === "complete") { |
135 |
| - if (game.winner) { |
136 |
| - msg = `Player ${game.winner} wins!`; |
137 |
| - } else { |
138 |
| - msg = `Tie game!`; |
139 |
| - } |
140 |
| - App.$.modalText.textContent = msg; |
141 |
| - App.$.modal.classList.remove("hidden"); |
142 |
| - } |
143 |
| - }); |
144 |
| - }); |
145 |
| - }, |
146 |
| -}; |
147 |
| - |
148 |
| -// window.addEventListener("load", App.init); |
149 |
| - |
150 | 4 | const players = [
|
151 | 5 | { id: 1, name: "Player 1", iconClass: "fa-x", colorClass: "turquoise" },
|
152 | 6 | { id: 2, name: "Player 2", iconClass: "fa-o", colorClass: "yellow" },
|
@@ -203,7 +57,7 @@ function init() {
|
203 | 57 | }
|
204 | 58 |
|
205 | 59 | // Set the next player's turn indicator
|
206 |
| - view.setTurnIndicator(store.game.currentPlayer); // this is the updated current player |
| 60 | + view.setTurnIndicator(store.game.currentPlayer); |
207 | 61 | });
|
208 | 62 | }
|
209 | 63 |
|
|
0 commit comments