WORDSEARCH.TSX: import type React from "react" import { useState, useEffect,…
- User
WORDSEARCH.TSX:
import type React from "react" import { useState, useEffect, useCallback, useRef, useMemo } from "react" import { View, Text, TouchableOpacity, StyleSheet, SafeAreaView, Dimensions, Alert } from "react-native" import { useNavigation } from "@react-navigation/native"
const getRandomColor = () => { const letters = "0123456789ABCDEF" let color = "#" for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)] } return color }
type Difficulty = "fácil" | "intermedio" | "difícil" | "experto"
const LEVELS: Record<Difficulty, { size: number; words: string[] }> = { fácil: { size: 8, words: ["ESPAÑA", "MÉXICO", "PERÚ", "CHILE", "CUBA"], }, intermedio: { size: 10, words: ["COLOMBIA", "ARGENTINA", "ECUADOR", "CULTURA", "IDIOMA", "FIESTA", "TANGO"], }, difícil: { size: 12, words: ["VENEZUELA", "GUATEMALA", "TRADICIÓN", "CARNAVAL", "FLAMENCO", "MARIACHI", "ANDES", "SALSA"], }, experto: { size: 15, words: [ "LATINOAMÉRICA", "HISPANOHABLANTE", "PRECOLOMBINO", "BIODIVERSIDAD", "GASTRONOMÍA", "INDEPENDENCIA", "AMAZONIA", "PATAGONIA", "CARIBE", "INCA", ], }, }
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get("window")
const WordSearch: React.FC = () => { const navigation = useNavigation() const [difficulty, setDifficulty] = useState("fácil") const [grid, setGrid] = useState<string[][]>([]) const [foundWords, setFoundWords] = useState<{ word: string; color: string; found: boolean }[]>([]) const [foundCells, setFoundCells] = useState<{ [key: number]: string }>({}) const [showMenu, setShowMenu] = useState(true) const [timer, setTimer] = useState(0) const [score, setScore] = useState(0) const [selectedCells, setSelectedCells] = useState<number[]>([]) const gridRef = useRef<string[][]>([])
useEffect(() => { if (!showMenu) { initializeGrid(difficulty) setTimer(0) const interval = setInterval(() => { setTimer((prevTimer) => prevTimer + 1) }, 1000) return () => clearInterval(interval) } }, [showMenu, difficulty])
const initializeGrid = useCallback((currentDifficulty: Difficulty) => { const { size, words } = LEVELS[currentDifficulty] const newGrid: string[][] = Array(size) .fill(null) .map(() => Array(size).fill(""))
words.forEach((word) => placeWord(newGrid, word)) fillEmptyCells(newGrid) gridRef.current = newGrid setGrid(newGrid) setFoundWords(LEVELS[currentDifficulty].words.map((word) => ({ word, color: "", found: false }))) setFoundCells({}) setSelectedCells([]) setScore(0)}, [])
const placeWord = (grid: string[][], word: string) => { const directions = [ [0, 1], [1, 0], [1, 1], [-1, 1], [0, -1], [-1, 0], [-1, -1], [1, -1], ]
let placed = false while (!placed) { const direction = directions[Math.floor(Math.random() * directions.length)] const [dx, dy] = direction const startX = Math.floor(Math.random() * grid.length) const startY = Math.floor(Math.random() * grid.length) if (canPlaceWord(grid, word, startX, startY, dx, dy)) { for (let i = 0; i < word.length; i++) { grid[startX + i * dx][startY + i * dy] = word[i] } placed = true } } - Assistant
- User
Hay palabras que no me las tacha ayudame
- Assistant