-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (119 loc) · 3.83 KB
/
script.js
File metadata and controls
140 lines (119 loc) · 3.83 KB
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
let isFirstLoad = true;
// gamboard -- IIFE
const Gameboard = (() => {
let board = ["", "", "", "", "", "", "", "", ""]; //kept private
const getBoard = () => board;
const updateBoard = (index, marker) => {
if (board[index] === "") {
board[index] = marker;
}
};
const resetBoard = () => {
board.fill("");
};
return { getBoard, updateBoard, resetBoard };
})();
// This is Factory Function
const Player = (name, marker) => {
return { name, marker };
};
// Game Module (IIFE)
const Game = (() => {
let player1, player2;
let currentPlayer;
let isGameOver = false;
const startGame = (player1Name, player2Name) => {
isFirstLoad = false;
player1 = Player(player1Name, "X");
player2 = Player(player2Name, "O");
currentPlayer = player1;
isGameOver = false;
Gameboard.resetBoard();
DisplayController.render();
DisplayController.switchColoring(currentPlayer);
DisplayController.setMessage(`${currentPlayer.name}'s turn`);
};
const switchPlayer = () => {
currentPlayer = currentPlayer === player1 ? player2 : player1;
DisplayController.switchColoring(currentPlayer);
};
const checkWin = () => {
const board = Gameboard.getBoard();
const winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let condition of winConditions) {
const [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
isGameOver = true;
return `${currentPlayer.name} wins!`;
}
}
if (!board.includes("")) {
isGameOver = true;
return "It's a tie!";
}
return null;
};
const makeMove = (index) => {
if (!isGameOver && Gameboard.getBoard()[index] === "") {
Gameboard.updateBoard(index, currentPlayer.marker);
const result = checkWin();
if (result) {
DisplayController.setMessage(result);
} else {
switchPlayer();
DisplayController.setMessage(`${currentPlayer.name}'s turn`);
}
DisplayController.render();
}
};
return { startGame, makeMove, getCurrentPlayer: () => currentPlayer };
})();
// Display Controller Module (IIFE)
const DisplayController = (() => {
const gameboardWrapper = document.getElementById("gameboard-wrapper");
const messageElement = document.getElementById("message");
const restartButton = document.getElementById("restart-button");
const switchColoring = (currentPlayer) => {
const htmlbody = document.querySelector('body');
htmlbody.classList.remove("player1", "player2");
const className = currentPlayer.marker === "X" ? "player1" : "player2";
htmlbody.classList.add(className);
};
const render = () => {
gameboardWrapper.innerHTML = "";
const gameboardElement = document.createElement("div");
gameboardElement.classList.add("gameboard");
gameboardWrapper.appendChild(gameboardElement);
const board = Gameboard.getBoard();
board.forEach((cell, index) => {
const cellElement = document.createElement("div");
cellElement.classList.add("cell");
cellElement.textContent = cell;
cellElement.addEventListener("click", () => Game.makeMove(index));
gameboardElement.appendChild(cellElement);
});
if (!isFirstLoad) {
restartButton.innerText = "Restart Button";
}
};
const setMessage = (message) => {
messageElement.textContent = message;
};
restartButton.addEventListener("click", () => {
const player1Name = prompt("Enter name for Player 1 (X):", "Player 1");
const player2Name = prompt("Enter name for Player 2 (O):", "Player 2");
Game.startGame(player1Name, player2Name);
});
return { render, setMessage, switchColoring };
})();
// Start the game initially with default names
// Game.startGame("Player 1", "Player 2");