JS - 多维数组和匹配3系统

时间:2015-07-24 13:07:28

标签: arrays multidimensional-array unity3d unityscript

我在Unity中制作了一个匹配3的游戏(我使用 UnityScript (Javascript for Unity)),基于俄罗斯方块游戏玩法(多维数据集像Tetris一样被删除以创建行和列的彩色多维数据集) ,我需要找到一种方法来检查一行(或列)中是否有3个彩色立方体。

游戏逻辑实际上非常简单,但我对Cubes Comparison有疑问。

1。存储值

我创建了一个多维数组,并使用循环推送每个多维数据集的值:

    cells[0, 0] // this is the Cube n°0, and it's X Position
    cells[0, 1] // this is the Cube n°0, and it's Y Position
    cells[0, 2] // this is the Cube n°0, and it's ID
    cells[0, 3] // this is the Cube n°0, and it's Color

    cells[1, 0] // this is the Cube n°1, and it's X Position
    cells[1, 1] // this is the Cube n°1, and it's Y Position
    cells[1, 2] // this is the Cube n°1, and it's ID
    cells[1, 3] // this is the Cube n°1, and it's Color
…

// etc…

所以,如果我想要 Cube 1 X Pos。,我必须调用“ cells [0,0]; ”。 这完全正常。

2。匹配3逻辑

找到3个立方体垂直匹配的逻辑很简单(它位于“扫描”所有立方体的“for”循环中):

if ( cells[row,cell] == cells[row,cell+1] == cells[row,cell+2] ) // The Problem is here, see my question below
{
    print("Congrats, you have stacked 3 cubes in a column !");
    // + Here I add my function to Destroy cubes and Collapse row(s)/col(s)
}

正如您在我的脚本中看到的那样,行:“if(cells [row,cell] == cells [row,cell + 1] == cells [row,cell + 2])”是问题所在。

我(真的)想要做的事情:

如果“CELL_A与Y位置= 3”等于“CELL_B且Y位置= 2 + 1”等于“CELL_C与Y位置= 1 + 2“,那就是匹配。

3。问题

所以,我可以完美地搜索带有“for”循环的Vertical Cubes,但我的问题在于:“ cells [row,cell] == cells [row,cell + 1] == cells [行,单元格+ 2] ”。

因为如果我不是“ cell + 1 ”是“ Cell Pos Y值+ 1 ”,那么找不到该值,但存储在数组中的下一个值: Y位置,或 CubeID ,或颜色ID ......

因为如果cell = 1 ,而我不是“ cell + 2 ”,则cell + 2 = cells [0,2] ,结果是:“立方体n°0,它的ID ”。

4。问题

如何为 Cells 数组的 Second int 添加值?

而不是:

cells [0,(1 + 1)] =多维数据集ID(因为结果为:cells [0,2])

我想:

细胞[0,(1 + 1)] = “立方体0:Y位置” +1

所以:

当我搜索“ if(cells [row,cell + 1] == 1)”时,我发现多维数据集的数字 1 as Pos Y Value

我希望我的解释足够好,如果您需要更多细节,请告诉我。在此先感谢您的帮助,对不起我的英语不好。

PS:如果你有任何其他算法可以胜任这项工作,我会很乐意接受它!感谢。

1 个答案:

答案 0 :(得分:1)

如果你想看看这三个是否都等于一个值你可以试试这个:

OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());

除非您循环遍历行,然后跨越您的列应该能够相互检查相同的列。所以细胞[row,0]&单元格[row,1]。如果由于某种原因在循环遍历行的同时循环,则应该只需要该列的变量。

writeStream(out);