Mystery Game #22

← Back to Games List
// Grid Toggle Puzzle const gridSize = 5; const states = new Array(gridSize * gridSize).fill(false); let moves = 0; let isActive = true; function initializeGrid() { // Create random starting pattern for (let i = 0; i < gridSize * gridSize; i++) { if (Math.random() < 0.3) { toggleCell(i % gridSize, Math.floor(i / gridSize), false); } } } function toggleCell(x, y, countMove = true) { if (!isActive) return; // Toggle center cell const idx = y * gridSize + x; states[idx] = !states[idx]; // Toggle adjacent cells if (x > 0) states[y * gridSize + (x-1)] = !states[y * gridSize + (x-1)]; if (x < gridSize-1) states[y * gridSize + (x+1)] = !states[y * gridSize + (x+1)]; if (y > 0) states[(y-1) * gridSize + x] = !states[(y-1) * gridSize + x]; if (y < gridSize-1) states[(y+1) * gridSize + x] = !states[(y+1) * gridSize + x]; if (countMove) { moves++; checkWin(); } } function checkWin() { if (states.every(state => !state)) { isActive = false; showMessage('Puzzle Solved in ' + moves + ' moves!'); } } function render(ctx) { const cellSize = 80; const padding = 2; // Draw grid for (let y = 0; y < gridSize; y++) { for (let x = 0; x < gridSize; x++) { const idx = y * gridSize + x; ctx.fillStyle = states[idx] ? '#ffeb3b' : '#333'; ctx.fillRect(x * cellSize + padding, y * cellSize + padding, cellSize - padding * 2, cellSize - padding * 2); } } // Draw moves counter ctx.fillStyle = 'white'; ctx.font = '20px Arial'; ctx.fillText('Moves: ' + moves, 10, gridSize * cellSize + 30); if (!isActive) { ctx.fillStyle = 'white'; ctx.font = '30px Arial'; ctx.fillText('Complete!', 10, gridSize * cellSize + 70); } } function handleClick(x, y) { const cellSize = 80; const gridX = Math.floor(x / cellSize); const gridY = Math.floor(y / cellSize); if (gridX >= 0 && gridX < gridSize && gridY >= 0 && gridY < gridSize) { toggleCell(gridX, gridY); } } canvas.addEventListener('click', (e) => { const rect = canvas.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; handleClick(x, y); });
# Grid Toggle Puzzle Implementation import pygame import random import numpy as np from dataclasses import dataclass from typing import List @dataclass class GridState: size: int states: np.ndarray moves: int = 0 is_active: bool = True class TogglePuzzle: def __init__(self): pygame.init() # Game settings self.grid_size = 5 self.cell_size = 80 self.padding = 2 self.width = self.grid_size * self.cell_size self.height = self.grid_size * self.cell_size + 100 # Extra space for text # Colors self.colors = { 'background': (0, 0, 0), 'cell_off': (51, 51, 51), 'cell_on': (255, 235, 59), # #ffeb3b 'text': (255, 255, 255) } # Initialize display self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption('Grid Toggle') self.clock = pygame.time.Clock() # Game state self.state = GridState( size=self.grid_size, states=np.zeros((self.grid_size, self.grid_size), dtype=bool) ) self.initialize_grid() def initialize_grid(self): """Create random starting pattern""" for i in range(self.grid_size * self.grid_size): if random.random() < 0.3: x = i % self.grid_size y = i // self.grid_size self.toggle_cell(x, y, count_move=False) def toggle_cell(self, x: int, y: int, count_move: bool = True): """Toggle cell and its adjacent cells""" if not self.state.is_active: return # Toggle center cell self.state.states[y, x] = not self.state.states[y, x] # Toggle adjacent cells if x > 0: self.state.states[y, x-1] = not self.state.states[y, x-1] if x < self.grid_size - 1: self.state.states[y, x+1] = not self.state.states[y, x+1] if y > 0: self.state.states[y-1, x] = not self.state.states[y-1, x] if y < self.grid_size - 1: self.state.states[y+1, x] = not self.state.states[y+1, x] if count_move: self.state.moves += 1 self.check_win() def check_win(self): """Check if all cells are turned off""" if not self.state.states.any(): # All False self.state.is_active = False def handle_input(self) -> bool: """Handle mouse input""" for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() grid_x = mouse_x // self.cell_size grid_y = mouse_y // self.cell_size if (0 <= grid_x < self.grid_size and 0 <= grid_y < self.grid_size): self.toggle_cell(grid_x, grid_y) return True def draw(self): """Draw the game state""" # Clear screen self.screen.fill(self.colors['background']) # Draw grid for y in range(self.grid_size): for x in range(self.grid_size): color = (self.colors['cell_on'] if self.state.states[y, x] else self.colors['cell_off']) pygame.draw.rect(self.screen, color, (x * self.cell_size + self.padding, y * self.cell_size + self.padding, self.cell_size - self.padding * 2, self.cell_size - self.padding * 2)) # Draw moves counter font = pygame.font.Font(None, 36) moves_text = font.render(f'Moves: {self.state.moves}', True, self.colors['text']) self.screen.blit(moves_text, (10, self.grid_size * self.cell_size + 10)) # Draw completion message if not self.state.is_active: complete_text = font.render( f'Puzzle Solved in {self.state.moves} moves!', True, self.colors['text']) text_rect = complete_text.get_rect( center=(self.width//2, self.grid_size * self.cell_size + 50)) self.screen.blit(complete_text, text_rect) pygame.display.flip() def run(self): """Main game loop""" running = True while running: running = self.handle_input() self.draw() self.clock.tick(60) pygame.quit() if __name__ == '__main__': game = TogglePuzzle() game.run()
Play Game