我建立一个统一的游戏(理论上)通过使用布尔元素的2D数组来处理,然后处理它以决定游戏中是否应该存在该空间中的块,但是我遇到了问题统一实际创建所述数组。目前我的代码是:
void Start () {
System.Random rnd = new System.Random ();
int MapCol = 9;
int MapRow = 9;
int [,] MapArray = new int [MapCol, MapRow];
foreach (int i in MapArray)
MapArray ["{0} ", i] = rnd.Next (0, 2);
}
我的问题正在出现,因为官方微软对这个问题的回答只解释了如何在2D数组上使用foreach来查看单个文本元素,而不是整数。
我也在尝试将指南推送到post中生成的地图上,这似乎不起作用,因为可能会生成不符合这些指南的无限地图,并且可能永远无法完成地图。目前这些指南是一个块必须触及两个相邻的块,并且一个块不能触及同一个角落的3个块,即
- 1 1 0
- 1 1 0此位置不允许
- 0 0 0
同时
- 0 1 0
- 1 1 1允许此展示位置。
- 0 0 0
帮助将不胜感激!
编辑: 所以,要进一步解释生成条件。我希望每个“1”触及另外两个1的最小值,这里被认为触摸的是4个基本方向,上,下,左和右。与另一个1的对角线接触很好,除非该对角线被另外的2“1”包围。所以不应该有4个块组合在一起的安排。所以模式:
1 1
1 1
不应该出现在网格中
答案 0 :(得分:0)
在处理多维数组时,最好坚持使用嵌套的for
循环,如下所示:
for (int i = 0; i < MapArray.GetLength(0); i++) {
for (int j = 0; j < MapArray.GetLength(1); j++) {
MapArray[i, j] = rnd.Next(0, 2);
}
}
关于你的第二个问题,你能详细说明一下情况吗?我完全不明白。
答案 1 :(得分:0)
以下算法符合指南,并根据提供的种子生成一种可能的解决方案 -
// 2D array Map
var blocks = [];
// size of 2D array Map
var sizeX = 9,
sizeY = 9;
// seed - as a starting point in 2d array within range of array size-1. eg. (2,1) , (5,5)
var seedX = 8,
seedY = 8;
// used to hold 2D array elements to be processed
var processX = [];
var processY = [];
// string used for output
var str = "";
// initialize 2D array with value 2 and set seed to block as 1
function initializeMap() {
for (var row = 0; row < sizeX; row++) {
blocks[row] = [];
for (var col = 0; col < sizeY; col++) {
blocks[row][col] = 2;
}
}
blocks[seedX][seedY] = 1;
}
// print the 2D array map with guidelines
function printMap() {
str = "1. Every 1 is surrounded by atleast two 1s.<br/>";
str += "2. No 1 is surrounded by three 1s in the same corner.<br/><br/>";
for (var row = 0; row < sizeX; row++) {
for (var col = 0; col < sizeY; col++) {
str += blocks[row][col] + " ";
}
str += "\n<br/>";
}
document.getElementById("result").innerHTML = str;
}
// Add current element's unprocessed siblings to processing stack and set its value to -1
function pushSiblingsToProcess(x, y) {
for (var row = x - 1; row <= x + 1; row++) {
if (row >= 0 && row < sizeY) {
for (var col = y - 1; col <= y + 1; col++) {
if (col >= 0 && col < sizeX && blocks[row][col] > 1) {
processX.push(row);
processY.push(col);
blocks[row][col] = -1;
}
}
}
}
}
// sets the current element to make is either block as 1 or empty as 0
function setBlock(x, y) {
var totalAdj = 0;
var prev = false;
if ((x - 1) >= 0 && blocks[x - 1][y] == 1) {
totalAdj += 1;
prev = true;
}
if ((y - 1) >= 0 && blocks[x][y - 1] == 1) {
totalAdj += 1;
if (prev && (x - 1) >= 0 && blocks[x - 1][y - 1] == 1) {
blocks[x][y] = 0;
return;
}
prev = true;
} else {
prev = false;
}
if ((x + 1) < sizeX && blocks[x + 1][y] == 1) {
totalAdj += 1;
if (prev && (y - 1) >= 0 && blocks[x + 1][y - 1] == 1) {
blocks[x][y] = 0;
return;
}
prev = true;
} else {
prev = false;
}
if ((y + 1) < sizeY && blocks[x][y + 1] == 1) {
totalAdj += 1;
if (prev && (x + 1) <= sizeX && blocks[x + 1][y + 1] == 1) {
blocks[x][y] = 0;
return;
}
prev = true;
} else {
prev = false;
}
if (prev && (x - 1) >= 0 && (y + 1) < sizeY && blocks[x - 1][y + 1] == 1 && blocks[x - 1][y] == 1) {
blocks[x][y] = 0;
return;
}
blocks[x][y] = 1;
}
// called recursively to set blocks until processing stack has elements to process
function generateMap() {
if (processX.length <= 0) {
return;
} else {
var currX = processX.pop();
var currY = processY.pop();
setBlock(currX, currY);
pushSiblingsToProcess(currX, currY);
generateMap();
}
}
initializeMap();
pushSiblingsToProcess(seedX, seedY);
generateMap();
printMap();