-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
153 lines (124 loc) · 4.27 KB
/
script.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
/* eslint-disable no-plusplus */
// GAME BOARD
const gameBoard = (() => {
const currentBoard = [
['', '', ''],
['', '', ''],
['', '', '']
];
const getCurrentBoard = () => currentBoard;
const checkPlayerMove = (row, col, marker) => {
if (currentBoard[row][col] === '') {
currentBoard[row][col] = marker;
return true;
}
return false;
};
return { getCurrentBoard, checkPlayerMove };
})();
// PLAYER
const createPlayer = (name, marker) => ({ name, marker });
const player1 = createPlayer('Player O', 'O');
const player2 = createPlayer('Player X', 'X');
// GAME CONTROLLER
const gameController = (() => {
let currentPlayer = player1;
const switchPlayer = () => {
currentPlayer = currentPlayer === player1 ? player2 : player1;
};
const checkForWin = (board, marker) => {
const winningCombinations = [
// Rows
[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
// Columns
[board[0][0], board[1][0], board[2][0]],
[board[0][1], board[1][1], board[2][1]],
[board[0][2], board[1][2], board[2][2]],
// Diagonals
[board[0][0], board[1][1], board[2][2]],
[board[0][2], board[1][1], board[2][0]],
];
return winningCombinations.some(combination =>
combination.every(square => square === marker)
);
};
const checkForDraw = board => board.every(row => row.every(square => square !== ''));
return { switchPlayer, checkForWin, checkForDraw };
})();
// DISPLAY
const screenController = (() => {
const boardSquare = document.querySelectorAll('.board--square');
const playerInfo = document.querySelectorAll('.player__info');
let currentPlayer = player1;
const resetButton = document.querySelector('.reset__button');
const gameStatus = document.querySelector('.game__status');
let isGameOver = false;
const getRowCol = (index) => {
const row = Math.floor(index / 3);
const col = index % 3;
return [row, col];
};
const showWinMessage = (playerName) => {
gameStatus.textContent = `${playerName} wins!`;
isGameOver = true;
};
const showDrawMessage = () => {
gameStatus.textContent = 'It\'s a draw!';
isGameOver = true;
};
const updateScreen = () => {
playerInfo.forEach(info => {
info.classList.remove('underline');
});
const currentPlayerInfo = currentPlayer === player1 ? playerInfo[0] : playerInfo[1];
currentPlayerInfo.classList.add('underline');
boardSquare.forEach(square => {
const squareElement = square;
squareElement.textContent = '';
});
boardSquare.forEach((squareElement, index) => {
const button = document.createElement('button');
button.classList.add('board--button');
button.setAttribute('data-index', index);
squareElement.appendChild(button);
const [row, col] = getRowCol(index);
const marker = gameBoard.getCurrentBoard()[row][col];
button.textContent = marker;
button.addEventListener('click', () => {
if (!isGameOver) {
const [clickRow, clickCol] = getRowCol(index);
if (gameBoard.checkPlayerMove(clickRow, clickCol, currentPlayer.marker)) {
button.textContent = currentPlayer.marker;
if (gameController.checkForWin(gameBoard.getCurrentBoard(), currentPlayer.marker)) {
showWinMessage(currentPlayer.name);
} else if (gameController.checkForDraw(gameBoard.getCurrentBoard())) {
showDrawMessage();
} else {
currentPlayer = currentPlayer === player1 ? player2 : player1;
updateScreen();
}
}
}
});
});
const resetBoard = () => {
const board = gameBoard.getCurrentBoard();
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
board[i][j] = '';
}
}
currentPlayer = player1;
isGameOver = false;
updateScreen();
gameStatus.textContent = '';
};
resetButton.addEventListener('click', () => {
resetBoard();
});
};
return { updateScreen };
})();
screenController.updateScreen();