Estoy codificando un buscaminas y obtuve la interfaz de usuario y no puedo modificarla. Hice la cuadrícula para diferentes tamaños, pero ahora necesito empujar las minas hacia ella, creé una función que me da una matriz de minas en diferentes posiciones tomando como parámetro el tamaño del tablero, pero no puedo entender cómo empujar esta matriz de minas en la cuadrícula reemplazando las celdas si coincide con la posición de las minas
Aquí está el código:
import { CellEnum } from "../types"; import { CellComponentType } from "../UI/components/CellComponentType"; import { getMinePositions, positionMatch } from "./mines"; export function def(boardSize: number, minesInGame: number) { let board: CellEnum[][] = []; const minePosition = getMinePositions(boardSize, minesInGame); for (let xAxis = 0; xAxis < boardSize; xAxis++) { const row = []; for (let yAxis = 0; yAxis < boardSize; yAxis++) { let tile: CellEnum = CellEnum.Hidden row.push(tile); } board.push(row); } return board; }Y este es el código del generador de minas.
import { CellComponentType } from "../UI/components/CellComponentType"; import { CellEnum } from "../types"; function randomNumber(size: number) { return Math.floor(Math.random() * size); } function positionMatch(a: CellComponentType, b: CellComponentType) { return a.position === b.position; } function getMinePositions(boardSize: number, minesInGame: number) { const positions: CellComponentType[] = []; while (positions.length < minesInGame) { let position: CellComponentType = { position: [randomNumber(boardSize), randomNumber(boardSize)], cell: CellEnum.Mine }; if (!positions.some(positionMatch.bind(null, position))) { positions.push(position); } } return positions; } export { getMinePositions, positionMatch };CellComponentType tiene estos atributos:
type CellComponentType = { position: [number, number]; cell: CellEnum; };Y finalmente estos son los diferentes estados que podría tener un celular
enum CellEnum { Hidden = -1, Cero = 0, One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Flag = 9, Mine = 10, ClickedMine = 11 }Realmente aprecio si alguien puede ayudar, gracias
Tiene un problema en su método positionMatch . Está comparando la matriz en sí cuando en realidad desea comparar el contenido de la matriz. Cambia tu método así:
function positionMatch(a: CellComponentType, b: CellComponentType) { return a.position[0] === b.position[0] && a.position[1] === b.position[1]; }Para agregar las minas al campo, puede recorrer las posiciones de sus minas y sobrescribir los campos ocultos correspondientes antes de devolver el tablero:
for(let mine of minePosition) { board[mine.position[1], mine.position[0]] = mine.cell; }Recorra las minas y cambie la ficha del tablero correspondiente:
const mines = getMinePositions(); mines.forEach((mine) => { const [x, y] = mine.position; board[y][x] = mine.cell; }); No sé cómo tienes configurado tu tablero, pero normalmente lo tengo como y como filas y x como columnas, así que he usado board[y][x] aquí.