如何替换主网格的特定位置(数组数组)?
我正在编码一个扫雷器,我得到了UI,我无法修改它。我已经为不同尺寸的网格制作了网格如果单元格与地雷位置匹配的电网替换电网,
则是代码:
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;
}
这是矿山守则生成器
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具有此属性:
type CellComponentType = {
position: [number, number];
cell: CellEnum;
};
最后,这些是不同的状态,如果有人可以提供帮助,
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
}
如果有人可以提供帮助,谢谢
I'm coding a minesweeper and I got the UI and I can't modify it. I've made the grid for different sizes but now I need to push the mines into it, I created a function that gives me an array of mines in differents positions taking as parameter the boardsize, but I cant figure how push this array of mines into the grid replacing the cells if it match with mines position
Here is the code:
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;
}
And this is the code of mines generator
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 has this attributes:
type CellComponentType = {
position: [number, number];
cell: CellEnum;
};
And finally these are the different status that could have a cell
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
}
I really appreciate if someone could help, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
positionMatch
-Method中遇到了问题。当您实际想比较数组的内容时,您正在比较数组本身。这样更改您的方法:要将地雷添加到该字段,您可以在矿井位置上循环并覆盖匹配的隐藏字段,然后再返回板子:
You have a problem in your
positionMatch
-method. You are comparing the array itself when you actually want to compare the content of the array. Change your method like this:To add the mines to the field you can loop over your mine positions and overwrite the matching hidden fields before returning the board:
在地雷上循环并更改相应的木板瓷砖:
我不知道您是如何设置板的,但通常我将其作为
y
作为行,x
as列,所以我在此处使用了板[y] [x]
。Loop over the mines and change the corresponding board tile:
I don't know how you've got your board setup but usually I have it as
y
as rows andx
as columns, so I've usedboard[y][x]
here.