-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (159 loc) · 5.05 KB
/
script.js
File metadata and controls
178 lines (159 loc) · 5.05 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
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
178
const colors = ['rouge', 'bleu', 'vert', 'orange'];
let currentPlayer = 1;
let player1Score = 0;
let player2Score = 0;
let player1Moves = 2;
let player2Moves = 2;
const maxMoves = 2;
let grid = [];
let currentBlock = null;
const gridElement = document.getElementById('grid');
const drawBlockButton = document.getElementById('drawBlockButton');
const endTurnButton = document.getElementById('endTurnButton');
const messageElement = document.getElementById('message');
const player1ScoreElement = document.getElementById('player1Score');
const player2ScoreElement = document.getElementById('player2Score');
const player1MovesElement = document.getElementById('player1Moves');
const player2MovesElement = document.getElementById('player2Moves');
function initializeGrid() {
for (let i = 0; i < 5; i++) {
grid[i] = [];
for (let j = 0; j < 5; j++) {
grid[i][j] = '';
}
}
renderGrid();
}
function renderGrid() {
gridElement.innerHTML = '';
for (let i = 0; i < 5; i++) {
const row = document.createElement('tr');
for (let j = 0; j < 5; j++) {
const cell = document.createElement('td');
cell.dataset.row = i;
cell.dataset.col = j;
if (grid[i][j]) {
cell.classList.add(grid[i][j]);
}
cell.addEventListener('click', handleCellClick);
row.appendChild(cell);
}
gridElement.appendChild(row);
}
}
function handleCellClick(event) {
const row = parseInt(event.target.dataset.row);
const col = parseInt(event.target.dataset.col);
if (currentBlock && !grid[row][col]) {
grid[row][col] = currentBlock;
currentBlock = null;
checkChains();
renderGrid();
drawBlockButton.disabled = playerMovesRemaining() === 0;
endTurnButton.disabled = false; // Reactivate the "End Turn" button
}
}
function drawBlock() {
if (playerMovesRemaining() <= 0) {
return;
}
const colorIndex = Math.floor(Math.random() * colors.length);
currentBlock = colors[colorIndex];
messageElement.textContent = `Joueur ${currentPlayer} a tiré un bloc ${currentBlock}`;
decrementMoves();
drawBlockButton.disabled = true;
endTurnButton.disabled = true; // Deactivate the "End Turn" button
}
function endTurn() {
currentPlayer = currentPlayer === 1 ? 2 : 1;
resetMoves();
messageElement.textContent = `C'est au tour du Joueur ${currentPlayer}`;
drawBlockButton.disabled = false;
endTurnButton.disabled = true;
renderGrid();
}
function playerMovesRemaining() {
return currentPlayer === 1 ? player1Moves : player2Moves;
}
function decrementMoves() {
if (currentPlayer === 1) {
player1Moves--;
player1MovesElement.textContent = player1Moves;
} else {
player2Moves--;
player2MovesElement.textContent = player2Moves;
}
}
function resetMoves() {
player1Moves = maxMoves;
player2Moves = maxMoves;
player1MovesElement.textContent = player1Moves;
player2MovesElement.textContent = player2Moves;
}
function checkChains() {
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
if (grid[i][j]) {
const color = grid[i][j];
checkHorizontalChain(i, j, color);
checkVerticalChain(i, j, color);
}
}
}
}
function checkHorizontalChain(row, col, color) {
let chainLength = 1;
for (let k = col + 1; k < 5 && grid[row][k] === color; k++) {
chainLength++;
}
if (chainLength >= 3) {
removeChain(row, col, 'horizontal');
}
}
function checkVerticalChain(row, col, color) {
let chainLength = 1;
for (let k = row + 1; k < 5 && grid[k][col] === color; k++) {
chainLength++;
}
if (chainLength >= 3) {
removeChain(row, col, 'vertical');
}
}
function removeChain(row, col, direction) {
let chainLength = 0;
const color = grid[row][col];
if (direction === 'horizontal') {
for (let j = col; j < 5 && grid[row][j] === color; j++) {
grid[row][j] = '';
chainLength++;
}
} else if (direction === 'vertical') {
for (let i = row; i < 5 && grid[i][col] === color; i++) {
grid[i][col] = '';
chainLength++;
}
}
if (currentPlayer === 1) {
player1Score += chainLength;
player1ScoreElement.textContent = player1Score;
} else {
player2Score += chainLength;
player2ScoreElement.textContent = player2Score;
}
messageElement.textContent = `Joueur ${currentPlayer} a retiré ${chainLength} blocs de couleur ${color}`;
incrementMoves();
}
function incrementMoves() {
if (currentPlayer === 1) {
player1Moves++;
player1MovesElement.textContent = player1Moves;
} else {
player2Moves++;
player2MovesElement.textContent = player2Moves;
}
drawBlockButton.disabled = false;
}
initializeGrid();
resetMoves();
drawBlockButton.addEventListener('click', drawBlock);
endTurnButton.addEventListener('click', endTurn);