
Trap a cat 🐈 A web game bring me back to 2008
Trying to demo my first web game, trap a cat comes to my mind out of the blue. It was one of my favorite casual games. I love cat, I love how randomly they move, like the game’s main idea.
- The game is played on an n*n grid
- The cat starts in the center
- Players can click cells to block them
- The cat moves randomly among available adjacent cells
- The goal is to trap the cat before it reaches the edge of the grid
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
// Add this new function to check if the cat can reach the border
function canReachBorder(startX, startY) {
// Create a visited array to keep track of checked cells
let visited = Array(GRID_SIZE).fill().map(() => Array(GRID_SIZE).fill(false));
// Queue for BFS (Breadth-First Search)
let queue = [{x: startX, y: startY}];
visited[startY][startX] = true;
while (queue.length > 0) {
let current = queue.shift();
// If we reached the border, return true
if (current.x === 0 || current.x === GRID_SIZE-1 ||
current.y === 0 || current.y === GRID_SIZE-1) {
return true;
}
// Check all adjacent cells
let possibleMoves = getPossibleMoves(current.x, current.y);
for (let move of possibleMoves) {
if (!visited[move.y][move.x]) {
visited[move.y][move.x] = true;
queue.push(move);
}
}
}
// If we've checked all reachable cells and haven't found the border
return false;
}