To address your requirements, I've updated your HTML, CSS, and JavaScript code accordingly. Here's a summary of the changes made:
-
Responsive Design & Touch-Friendly Corners:
- CSS Adjustments:
- Utilized relative units (
vw, vh, and %) instead of fixed px units to ensure responsiveness across different screen sizes.
- Implemented CSS Grid's
fr units to make the game board scale with the viewport.
- Increased the size of corner elements and added padding to make them easier to tap on touchscreens.
- Added media queries to fine-tune the layout for smaller screens.
-
Flood Fill Logic Fix:
- JavaScript Adjustments:
- Modified the
performFloodFill function to return a boolean indicating whether the flood fill was successful (i.e., the area is enclosed).
- Updated the flood button handler to only call
endTurn() if the flood fill was successful, preventing the turn from passing on a failed flood fill.
-
Reworked Fog of War & Terrain Generation:
-
JavaScript Adjustments:
- Removed the
disableRandomWalls function and its invocation.
- Implemented a "Fog of War" system by overlaying fog patches on the game board.
- Created 9 fog patches (3x3 grid) that cover 3x3 cells each. These patches can be clicked to remove the fog and reveal the underlying area.
- Added logic to handle the first two fog clearings separately (terrain generation is skipped) and enable terrain generation for subsequent clearings.
- Created a
generateTerrainFeatures function that, for each cleared patch beyond the first two, iterates over the perimeter walls and converts them into black walls with a 20% probability.
-
CSS Adjustments:
- Styled the fog patches with semi-transparent overlays to indicate covered areas.
- Ensured fog patches are responsive and aligned correctly with the game grid.
Below is the complete updated code incorporating all the changes:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Interactive Game Board</title>
<style>
/* Root CSS Variables for Responsiveness */
:root {
--wall-size: 1vw; /* Responsive wall size */
--cell-size: 5vw; /* Responsive cell size */
--corner-size: 3vw; /* Increased size for touch */
--fog-color: rgba(0, 0, 0, 0.5); /* Semi-transparent fog */
--max-board-size: 90vmin; /* Max size for the board */
}
/* Game board container */
#game-container {
display: grid;
/* Define alternating wall and cell sizes using relative units */
grid-template-columns: var(--wall-size) repeat(9, var(--cell-size) var(--wall-size));
grid-template-rows: var(--wall-size) repeat(9, var(--cell-size) var(--wall-size));
gap: 0;
position: relative;
width: var(--max-board-size);
height: var(--max-board-size);
margin: 20px auto;
border: var(--wall-size) solid #333;
background-color: #fff;
box-sizing: border-box;
}
/* Cells */
.cell {
width: 100%;
height: 100%;
background-color: white;
border: none; /* Borders are handled by grid walls */
position: relative;
cursor: pointer;
overflow: hidden;
}
/* Marker container */
.marker-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-weight: bold;
font-size: 2vw;
pointer-events: none; /* Allows clicks to pass through to the cell */
}
/* Number container */
.number-container {
position: absolute;
top: 1vw;
left: 1vw;
font-size: 1.2vw;
pointer-events: none; /* Allows clicks to pass through to the cell */
}
/* Corners */
.corner {
width: 100%;
height: 100%;
background-color: #666;
cursor: pointer;
/* Increased size for touch devices */
padding: 0.5vw;
box-sizing: border-box;
}
/* Horizontal walls */
.h-wall {
width: 100%;
height: 100%;
background-color: transparent; /* Initially no wall */
cursor: pointer;
}
/* Vertical walls */
.v-wall {
width: 100%;
height: 100%;
background-color: transparent; /* Initially no wall */
cursor: pointer;
}
/* Hover effect */
.cell:hover,
.corner:hover,
.h-wall:hover,
.v-wall:hover {
background-color: #ffd700;
}
/* Controls */
#controls {
text-align: center;
margin: 10px;
}
#controls button {
padding: 1vw 2vw;
margin: 0.5vw;
font-size: 1.2vw;
cursor: pointer;
}
/* Status display */
#status-container {
text-align: center;
font-size: 1.5vw;
margin-bottom: 1vw;
}
.status-item {
margin: 0.5vw;
}
.wall-counter {
margin-top: 1vw;
font-size: 1.2vw;
color: #333;
}
/* Message display */
#message {
text-align: center;
font-size: 1.2vw;
margin-bottom: 1vw;
color: green;
}
/* Filled area */
.filled {
background-color: rgba(0, 0, 255, 0.2);
}
.filled-red {
background-color: rgba(255, 0, 0, 0.2);
}
/* Disabled walls (black walls) */
.disabled-wall {
background-color: black !important;
cursor: not-allowed !important;
}
/* Fog of War */
.fog {
position: absolute;
background-color: var(--fog-color);
z-index: 10;
cursor: pointer;
border: 2px solid #444;
box-sizing: border-box;
transition: opacity 0.3s;
}
/* Responsive adjustments */
@media (max-width: 600px) {
:root {
--wall-size: 2vw;
--cell-size: 8vw;
--corner-size: 4vw;
--fog-color: rgba(0, 0, 0, 0.6);
}
.marker-container {
font-size: 3vw;
}
.number-container {
font-size: 2vw;
}
#controls button {
padding: 1.5vw 3vw;
font-size: 2vw;
}
#status-container {
font-size: 2vw;
}
#message {
font-size: 2vw;
}
}
</style>
</head>
<body>
<div id="status-container">
<div class="status-item">Current Player: <span id="current-player" style="color: blue;">Blue (O)</span></div>
<div class="wall-counter">
Blue Walls Used: <span id="blue-walls-used">0</span> |
Red Walls Used: <span id="red-walls-used">0</span>
</div>
</div>
<div id="message"></div>
<div id="game-container"></div>
<div id="controls">
<button id="flood-button" disabled>Flood</button>
<button id="count-button" disabled>Count</button>
<button id="pass-button" disabled>Pass Turn</button>
<button id="reset-button">Reset Game</button>
</div>
<script>
const N = 9; // Grid size
const gameContainer = document.getElementById('game-container');
const statusDisplay = document.getElementById('current-player');
const messageDisplay = document.getElementById('message');
const floodButton = document.getElementById('flood-button');
const countButton = document.getElementById('count-button');
const passButton = document.getElementById('pass-button');
const resetButton = document.getElementById('reset-button');
const blueWallsUsedDisplay = document.getElementById('blue-walls-used');
const redWallsUsedDisplay = document.getElementById('red-walls-used');
let currentPlayer = 'blue'; // 'blue' or 'red'
let turnPhase = 1; // 1: Place marker, 2: Choose action
let actionMode = null; // 'flood' or 'count'
let wallMode = false; // Indicates if in wall drawing mode
let firstCorner = null; // Stores first corner selected for wall
let countNumber = {
blue: 1,
red: 1
};
let optionalWallDrawn = false; // Tracks optional wall during flood
// Initialize variables for Fog of War
const fogSize = 3; // Each fog patch covers 3x3 cells
const totalPatches = 9; // 3x3 grid of fog patches
let patchesCleared = 0; // Tracks number of cleared patches
const maxInitialClearings = 2; // First two clearings do not generate terrain
const players = {
blue: {
name: 'Blue',
color: 'blue',
marker: 'O',
markerPosition: null,
usedWalls: 0
},
red: {
name: 'Red',
color: 'red',
marker: 'X',
markerPosition: null,
usedWalls: 0
}
};
// Initialize game board elements
function initGameBoard() {
gameContainer.innerHTML = '';
// Create cells
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.row = i;
cell.dataset.col = j;
cell.style.gridRowStart = 2 + i * 2; // Cells are on even grid lines
cell.style.gridColumnStart = 2 + j * 2;
// Create marker and number containers
const markerContainer = document.createElement('div');
markerContainer.classList.add('marker-container');
markerContainer.id = `marker-${i}-${j}`;
const numberContainer = document.createElement('div');
numberContainer.classList.add('number-container');
numberContainer.id = `number-${i}-${j}`;
cell.appendChild(markerContainer);
cell.appendChild(numberContainer);
cell.addEventListener('click', onCellClick);
gameContainer.appendChild(cell);
}
}
// Create corners
for (let i = 0; i <= N; i++) {
for (let j = 0; j <= N; j++) {
const corner = document.createElement('div');
corner.classList.add('corner');
corner.dataset.row = i;
corner.dataset.col = j;
corner.style.gridRowStart = 1 + i * 2;
corner.style.gridColumnStart = 1 + j * 2;
corner.addEventListener('click', onCornerClick);
gameContainer.appendChild(corner);
}
}
// Create horizontal walls
for (let i = 0; i <= N; i++) {
for (let j = 0; j < N; j++) {
const hWall = document.createElement('div');
hWall.classList.add('h-wall');
hWall.dataset.row = i;
hWall.dataset.col = j;
hWall.style.gridRowStart = 1 + i * 2;
hWall.style.gridColumnStart = 2 + j * 2;
hWall.addEventListener('click', onWallClick);
gameContainer.appendChild(hWall);
}
}
// Create vertical walls
for (let i = 0; i < N; i++) {
for (let j = 0; j <= N; j++) {
const vWall = document.createElement('div');
vWall.classList.add('v-wall');
vWall.dataset.row = i;
vWall.dataset.col = j;
vWall.style.gridRowStart = 2 + i * 2;
vWall.style.gridColumnStart = 1 + j * 2;
vWall.addEventListener('click', onWallClick);
gameContainer.appendChild(vWall);
}
}
// Initialize Fog of War
initFogOfWar();
}
// Initialize Fog of War by creating nine fog patches
function initFogOfWar() {
for (let p = 0; p < totalPatches; p++) {
const patch = document.createElement('div');
patch.classList.add('fog');
// Calculate patch position based on patch index
const patchRow = Math.floor(p / 3);
const patchCol = p % 3;
// Each patch covers 3x3 cells, which in grid terms spans 7x7 grid lines (including walls)
const gridRowStart = 1 + patchRow * (fogSize * 2);
const gridColStart = 1 + patchCol * (fogSize * 2);
const gridRowEnd = gridRowStart + (fogSize * 2);
const gridColEnd = gridColStart + (fogSize * 2);
// Position the fog patch in the grid
patch.style.gridRowStart = gridRowStart;
patch.style.gridRowEnd = gridRowEnd;
patch.style.gridColumnStart = gridColStart;
patch.style.gridColumnEnd = gridColEnd;
// Add click handler to remove fog
patch.addEventListener('click', () => {
removeFogPatch(patch);
});
gameContainer.appendChild(patch);
}
}
// Remove fog patch and generate terrain if applicable
function removeFogPatch(patch) {
patch.style.opacity = '0';
setTimeout(() => {
patch.remove();
}, 300); // Match with CSS transition
patchesCleared++;
if (patchesCleared > maxInitialClearings) {
// Generate terrain features for this patch
generateTerrainFeatures(patch);
} else {
setMessage(`Fog cleared. No terrain generated for the first ${maxInitialClearings} clearings.`, 'blue');
}
}
// Generate terrain features for a cleared fog patch
function generateTerrainFeatures(patch) {
// Determine the patch's grid position
const gridRowStart = parseInt(patch.style.gridRowStart);
const gridColStart = parseInt(patch.style.gridColumnStart);
// Collect all perimeter walls (12 walls)
const perimeterWalls = [];
// Top and Bottom Horizontal Walls
for (let j = 0; j < fogSize; j++) {
// Top walls
const topWall = document.querySelector(`.h-wall[data-row="${(gridRowStart / 2)}"][data-col="${(gridColStart / 2) + j}"]`);
if (topWall) perimeterWalls.push(topWall);
// Bottom walls
const bottomWall = document.querySelector(`.h-wall[data-row="${(gridRowStart / 2) + fogSize}"][data-col="${(gridColStart / 2) + j}"]`);
if (bottomWall) perimeterWalls.push(bottomWall);
}
// Left and Right Vertical Walls
for (let i = 0; i < fogSize; i++) {
// Left walls
const leftWall = document.querySelector(`.v-wall[data-row="${(gridRowStart / 2) + i}"][data-col="${(gridColStart / 2)}"]`);
if (leftWall) perimeterWalls.push(leftWall);
// Right walls
const rightWall = document.querySelector(`.v-wall[data-row="${(gridRowStart / 2) + i}"][data-col="${(gridColStart / 2) + fogSize}"]`);
if (rightWall) perimeterWalls.push(rightWall);
}
// For each perimeter wall, with 20% probability, turn it into a black wall
perimeterWalls.forEach(wall => {
if (!isWallPresent(wall) && !wall.classList.contains('disabled-wall')) {
if (Math.random() < 0.2) { // 20% chance
wall.classList.add('disabled-wall');
wall.style.backgroundColor = 'black';
wall.classList.remove('h-wall', 'v-wall'); // Remove original class to prevent hover effects
}
}
});
setMessage('Terrain features generated for the cleared area.', 'green');
}
// Remove random wall disabling on setup as per requirements
// Removed the disableRandomWalls function and its invocation
// Reset Game
function resetGame() {
currentPlayer = 'blue';
turnPhase = 1;
actionMode = null;
wallMode = false;
firstCorner = null;
countNumber = {
blue: 1,
red: 1
};
optionalWallDrawn = false;
patchesCleared = 0;
players.blue.markerPosition = null;
players.red.markerPosition = null;
players.blue.usedWalls = 0;
players.red.usedWalls = 0;
updateStatus();
updateWallCounters();
setMessage('');
disableButtons();
initGameBoard();
}
// Update status display
function updateStatus() {
statusDisplay.textContent = `${players[currentPlayer].name} (${players[currentPlayer].marker})`;
statusDisplay.style.color = players[currentPlayer].color;
}
// Update wall counters display
function updateWallCounters() {
blueWallsUsedDisplay.textContent = players.blue.usedWalls;
redWallsUsedDisplay.textContent = players.red.usedWalls;
}
// Set message display
function setMessage(msg, color = 'green') {
messageDisplay.textContent = msg;
messageDisplay.style.color = color;
}
// Disable action buttons
function disableButtons() {
floodButton.disabled = true;
countButton.disabled = true;
passButton.disabled = true;
}
// Enable action buttons
function enableButtons() {
floodButton.disabled = false;
countButton.disabled = false;
passButton.disabled = false;
}
// Cell click handler
function onCellClick(e) {
const cell = e.currentTarget;
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
if (turnPhase === 1) {
// Place marker
// Remove existing marker if any
if (players[currentPlayer].markerPosition) {
const prevPos = players[currentPlayer].markerPosition;
const prevMarkerContainer = document.getElementById(`marker-${prevPos.row}-${prevPos.col}`);
if (prevMarkerContainer) {
const existingMarker = prevMarkerContainer.querySelector(`span[data-player="${currentPlayer}"]`);
if (existingMarker) {
prevMarkerContainer.removeChild(existingMarker);
}
}
}
// Update marker container without affecting numbers
const markerContainer = document.getElementById(`marker-${row}-${col}`);
// Add the marker for the current player
const existingMarker = markerContainer.querySelector(`span[data-player="${currentPlayer}"]`);
if (!existingMarker) {
const marker = document.createElement('span');
marker.textContent = players[currentPlayer].marker;
marker.style.color = players[currentPlayer].color;
marker.dataset.player = currentPlayer;
markerContainer.appendChild(marker);
}
// Update marker position for the player
players[currentPlayer].markerPosition = { row, col };
// Enable action buttons
enableButtons();
// Move to next turn phase
turnPhase = 2;
setMessage('Marker placed. Choose an action.');
} else if (turnPhase === 2) {
if (actionMode === 'flood') {
const success = performFloodFill(row, col);
if (success) {
actionMode = null;
endTurn();
} else {
// Do not end turn, allow player to retry
setMessage('Flood Fill failed. The area is not fully enclosed. Please try again or choose to pass the turn.', 'red');
}
} else if (actionMode === 'count') {
placeCount(cell);
}
}
}
// Corner click handler
function onCornerClick(e) {
if (turnPhase !== 2) {
return;
}
const corner = e.currentTarget;
const row = parseInt(corner.dataset.row);
const col = parseInt(corner.dataset.col);
if (wallMode) {
// Second corner click
const dist = Math.abs(firstCorner.row - row) + Math.abs(firstCorner.col - col);
if ((firstCorner.row === row || firstCorner.col === col) && (dist === 1 || dist === 2)) {
const success = drawWallBetweenCorners(firstCorner, { row, col }, currentPlayer);
if (success) {
setMessage('Wall drawn successfully.', 'green');
players[currentPlayer].usedWalls += 1;
updateWallCounters();
if (actionMode === 'flood') {
// During flood, allow drawing only one optional wall
optionalWallDrawn = true;
// Remain in action mode 'flood' to allow flood fill
} else {
// Normally, end the turn after drawing a wall
endTurn();
}
} else {
setMessage('Failed to draw wall. It might already exist.', 'red');
}
// Reset wall drawing state
wallMode = false;
firstCorner.element.style.backgroundColor = '#666';
firstCorner = null;
} else {
setMessage('Second corner must be adjacent horizontally or vertically.', 'red');
}
} else {
// First corner click
if (actionMode === 'flood' && optionalWallDrawn) {
// Already drew optional wall during flood
setMessage('Optional wall already drawn for this flood action.', 'red');
return;
}
// Check if corner is adjacent to the current player's marker position
const markerPos = players[currentPlayer].markerPosition;
if (markerPos && isCornerAdjacentToCell(row, col, markerPos.row, markerPos.col)) {
wallMode = true;
firstCorner = { row, col, element: corner };
corner.style.backgroundColor = players[currentPlayer].color;
if (actionMode === 'flood') {
setMessage('Select the second corner to draw the optional wall.', 'blue');
} else {
setMessage('Select the second corner to draw the wall.', 'blue');
}
} else {
setMessage('Select a corner adjacent to your marker cell.', 'red');
}
}
}
// Wall click handler (to prevent walls from being clickable)
function onWallClick(e) {
// Currently, walls are drawn via corner clicks. Clicking walls themselves does nothing.
// If additional functionalities are desired (e.g., removing walls), implement here.
}
// Check if corner is adjacent to cell
function isCornerAdjacentToCell(cornerRow, cornerCol, cellRow, cellCol) {
const cellCorners = getCellCorners(cellRow, cellCol);
return cellCorners.some(c => c.row === cornerRow && c.col === cornerCol);
}
// Get corners of a cell
function getCellCorners(cellRow, cellCol) {
const topLeft = { row: cellRow, col: cellCol };
const topRight = { row: cellRow, col: cellCol + 1 };
const bottomLeft = { row: cellRow + 1, col: cellCol };
const bottomRight = { row: cellRow + 1, col: cellCol + 1 };
return [topLeft, topRight, bottomLeft, bottomRight];
}
// Draw wall between two corners
// Returns true if wall was successfully drawn, false otherwise
function drawWallBetweenCorners(corner1, corner2, player) {
let wallsDrawn = [];
if (corner1.row === corner2.row) {
// Horizontal wall(s)
const minCol = Math.min(corner1.col, corner2.col);
const maxCol = Math.max(corner1.col, corner2.col);
for (let col = minCol; col < maxCol; col++) {
const hWall = document.querySelector(`.h-wall[data-row="${corner1.row}"][data-col="${col}"]`);
if (hWall) {
if (isWallPresent(hWall)) {
// Wall already exists
return false;
}
hWall.style.backgroundColor = players[player].color;
wallsDrawn.push(hWall);
}
}
} else if (corner1.col === corner2.col) {
// Vertical wall(s)
const minRow = Math.min(corner1.row, corner2.row);
const maxRow = Math.max(corner1.row, corner2.row);
for (let row = minRow; row < maxRow; row++) {
const vWall = document.querySelector(`.v-wall[data-row="${row}"][data-col="${corner1.col}"]`);
if (vWall) {
if (isWallPresent(vWall)) {
// Wall already exists
return false;
}
vWall.style.backgroundColor = players[player].color;
wallsDrawn.push(vWall);
}
}
} else {
// Diagonal walls are not allowed
return false;
}
// If all walls drawn successfully, update counts
if (wallsDrawn.length > 0) {
return true;
} else {
return false;
}
}
// Check if a wall is already present (not disabled)
function isWallPresent(wallElement) {
// A wall is present if its background color is not transparent and not black (disabled)
const bgColor = window.getComputedStyle(wallElement).backgroundColor;
return bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent' && bgColor !== 'black';
}
// Flood button handler
floodButton.addEventListener('click', () => {
if (actionMode !== null) return;
actionMode = 'flood';
optionalWallDrawn = false;
setMessage('Flood mode activated. Optionally draw one wall adjacent to your marker, then click on a cell to perform flood fill.', 'blue');
// Enable wall drawing; Flood mode manages walls differently
});
// Count button handler
countButton.addEventListener('click', () => {
if (actionMode !== null) return;
actionMode = 'count';
setMessage('Count mode activated. Click on cells to place counts. Click Pass Turn when done.', 'blue');
});
// Pass button handler
passButton.addEventListener('click', () => {
endTurn();
});
// Reset button handler
resetButton.addEventListener('click', () => {
resetGame();
});
// Perform Flood Fill
function performFloodFill(startRow, startCol) {
const visited = new Set();
const fillColor = currentPlayer === 'blue' ? 'filled' : 'filled-red';
const queue = [{ row: startRow, col: startCol }];
let isEnclosed = true;
while (queue.length > 0) {
const { row, col } = queue.shift();
const key = `${row},${col}`;
if (visited.has(key)) continue;
visited.add(key);
// Check if cell is on boundary WITHOUT a wall blocking the flood
if (
(row === 0 && (!isWallPresent(getCellWalls(row, col).top))) ||
(row === N - 1 && (!isWallPresent(getCellWalls(row, col).bottom))) ||
(col === 0 && (!isWallPresent(getCellWalls(row, col).left))) ||
(col === N - 1 && (!isWallPresent(getCellWalls(row, col).right)))
) {
isEnclosed = false;
}
// Check all four directions
const walls = getCellWalls(row, col);
// Top
if (!walls.top || !isWallPresent(walls.top)) {
if (row > 0) {
queue.push({ row: row - 1, col });
}
}
// Bottom
if (!walls.bottom || !isWallPresent(walls.bottom)) {
if (row < N - 1) {
queue.push({ row: row + 1, col });
}
}
// Left
if (!walls.left || !isWallPresent(walls.left)) {
if (col > 0) {
queue.push({ row, col: col - 1 });
}
}
// Right
if (!walls.right || !isWallPresent(walls.right)) {
if (col < N - 1) {
queue.push({ row, col: col + 1 });
}
}
}
if (isEnclosed) {
// Fill the area
visited.forEach(key => {
const [row, col] = key.split(',').map(Number);
const cell = document.querySelector(`.cell[data-row="${row}"][data-col="${col}"]`);
if (cell) {
cell.classList.add(fillColor);
}
});
setMessage('Area is enclosed. Flood Fill completed!', 'green');
return true;
} else {
setMessage('Area is not fully enclosed. Flood Fill failed.', 'red');
return false;
}
}
// Get walls around a cell
function getCellWalls(row, col) {
const walls = {};
// Top wall
walls.top = document.querySelector(`.h-wall[data-row="${row}"][data-col="${col}"]`);
// Bottom wall
walls.bottom = document.querySelector(`.h-wall[data-row="${row + 1}"][data-col="${col}"]`);
// Left wall
walls.left = document.querySelector(`.v-wall[data-row="${row}"][data-col="${col}"]`);
// Right wall
walls.right = document.querySelector(`.v-wall[data-row="${row}"][data-col="${col + 1}"]`);
return walls;
}
// Check if wall is present (colored)
function isWallPresent(wall) {
if (!wall) return false;
const bgColor = window.getComputedStyle(wall).backgroundColor;
// Transparent walls have RGBA(0, 0, 0, 0) or 'transparent'
return bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent';
}
// Place count on cell
function placeCount(cell) {
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
const numberContainer = document.getElementById(`number-${row}-${col}`);
if (numberContainer.textContent) {
setMessage('Cell already has a number.', 'red');
return;
}
const number = countNumber[currentPlayer]++;
numberContainer.textContent = number;
numberContainer.style.color = players[currentPlayer].color;
setMessage('Count placed.', 'green');
}
// End turn
function endTurn() {
// Reset variables
turnPhase = 1;
actionMode = null;
wallMode = false;
firstCorner = null;
optionalWallDrawn = false;
// Disable action buttons
disableButtons();
// Swap player
currentPlayer = currentPlayer === 'blue' ? 'red' : 'blue';
updateStatus();
setMessage('Turn passed to the next player.', 'blue');
}
// Initialize the game
resetGame();
</script>
</body>
</html>
Detailed Breakdown of Changes:
-
Responsive Design & Touch-Friendly Corners:
- CSS Variables: Defined root CSS variables (
--wall-size, --cell-size, --corner-size, etc.) to control the sizing of various elements responsively.
- Grid Layout: Updated the
#game-container to use relative units (vw, vh, and %) ensuring the game board scales with the viewport.
- Touch-Friendly Corners: Increased the padding and size of
.corner elements to make them easier to tap on touchscreens.
- Media Queries: Added a media query for screens narrower than 600px to further adjust sizes for better mobile compatibility.
-
Flood Fill Logic Fix:
performFloodFill Function: Modified to return a boolean (true if the flood fill was successful and the area is enclosed, false otherwise).
- Flood Button Handler: Updated the handler to call
endTurn() only if the flood fill is successful. If the flood fill fails, the turn does not pass, allowing the player to retry or choose another action.
-
Fog of War & Terrain Generation:
Future Enhancements:
- Bonus Features Generation: As mentioned in your note, future implementations can extend the
generateTerrainFeatures function to include generating bonus features on square fields.
- Wall Removal & Editing: Currently, walls can only be added. Implementing wall removal or editing could provide more flexibility in gameplay.
- Enhanced Fog Logic: Introducing dependencies or restrictions based on game rules when clearing fog patches to add strategic depth.
Testing the Updated Code:
Ensure to test the updated web app across various devices and screen sizes to confirm the responsiveness and touch interactions. Pay special attention to how fog patches overlay and interact with the underlying game grid. Adjust the CSS variables as necessary to fine-tune the sizing for different devices.
Feel free to reach out if you need further assistance or additional features!