Javascript 确保相邻节点不共享相同的值
我正在创建一个游戏-一个由颜色组成的网格,玩家可以向上、向下、向左或向右导航。如果指示牌上写着“去红场”,玩家必须这样做。玩家必须遵循的路径由随机路径生成算法确定,该算法遵循以下规则: 游戏规则:Javascript 确保相邻节点不共享相同的值,javascript,random,colors,Javascript,Random,Colors,我正在创建一个游戏-一个由颜色组成的网格,玩家可以向上、向下、向左或向右导航。如果指示牌上写着“去红场”,玩家必须这样做。玩家必须遵循的路径由随机路径生成算法确定,该算法遵循以下规则: 游戏规则: 这是一个帮助人们练习外语的游戏 给玩家一个颜色网格,玩家在下一个给定颜色之后从左到右穿过随机生成的路径(如果随机生成路径上的下一个颜色是红色,他们将被提示进入红场)-显然,稍后提示将使用外语 例如,“Rojo”出现时,他们必须去红场 这将是基于时间的。。。如果你在同一块瓷砖上停留太久,你会受到伤害
- 这是一个帮助人们练习外语的游戏
- 给玩家一个颜色网格,玩家在下一个给定颜色之后从左到右穿过随机生成的路径(如果随机生成路径上的下一个颜色是红色,他们将被提示进入红场)-显然,稍后提示将使用外语
- 例如,“Rojo”出现时,他们必须去红场
- 这将是基于时间的。。。如果你在同一块瓷砖上停留太久,你会受到伤害
- 如果你踩错了颜色,你会受到伤害
- 由于迷宫是从左到右生成的(请参见下面的算法信息),因此它们可以选择移动到北、东或南节点
- 从左到右,表示在第0列选择一个随机行
- 路径必须从第0列连接到最后一列
- 这条路有风,但不能往回走,这意味着它可以向上、向下和向右循环
- 相互相邻的节点不能共享相同的颜色
- 当前节点北部、东部和南部的节点(例如灰色节点)不能共享相同的颜色
- 颜色提示(下一个要移动到的单元格)是在生成随机路径时预先生成的。。。每种颜色在网格创建时都指定给一个单元格。。。随机生成的路径存储这些颜色的顺序/路径。。。所以我可以把下一种颜色传给玩家让他们踩上去
var lastUsedColor = "";
for (var row = 0; row <= bridgeSegment.settings.rows-1; row++) {
this.grid[row] = [];
for (var col = 0; col <= bridgeSegment.settings.cols-1; col++) {
if (lastUsedColor != "") delete this.colors[lastUsedColor];
var colorIndex = Math.floor(Math.random() * (Object.keys(this.colors).length - 0) + 0);
var keyName = Object.keys(this.colors)[colorIndex];
image = (this.maze == 1) ? image : keyName;
if (lastUsedColor != "") this.colors[lastUsedColor] = lastUsedColor;
lastUsedColor = keyName;
var myCell = me.pool.pull("mazetile", x, y, {
name : "mazetile",
type : "mazetile",
image : image,
row : row,
col : col,
width: 64,
height : 64,
spritewidth : 64
});
me.game.world.addChild(myCell);
this.grids[row][col] = myCell;
}
这行吗?我不确定我是否完全理解这个问题,但我认为这是正确的。我正在创建一个名为
grid
的数组,并用随机颜色填充它,每次检查它是否与附近的颜色不同
编辑:不,这还不行。我会试着把它修好。
好吧,我想我修好了
grid = [];
for(var i=0;i<=totalRows;row++)
{
grid[i]=[]
for(var j=0;j<=totalCols;j++)
{
do
{
var colorIndex = Math.floor(Math.random() * (Object.keys(this.colors).length - 0) + 0);
var keyName = Object.keys(this.colors)[colorIndex];
grid[i][j]=keyName;
var nearSameColor=false;
for(var k=i-2;k<=i+2;k++)
{
if(k<0||k>totalRows)
continue;
for(var l=j-2;l<=l+2;l++)
{
if(l<0||l>totalCols||(k==i&&l==j))
continue;
nearSameColor=nearSameColor||grid[i+k][j+l]==grid[i][i];
}
}
}while(nearSameColor)
}
}
for (var row = 0; row <= totalRows; row++) {
this.containerOfGrids[i][row] = [];
for (var col = 0; col <= totalCols; col++) {
image = grid[row][col];
//other code
grid=[];
对于(var i=0;i,如果我理解正确,您不仅关心不将同一颜色放置在其旁边,而且关心所有方向上的两个步骤
当您为瓷砖(a,b)上色时,您可以制定规则,而不是从对象中移除颜色。如果a-2a+2和y-2
然后,您必须在放置每种颜色时检查其规则(每种颜色可以同时有多个活动规则,如果0,0和3,0都是红色,则移动到下一行时仍需要第一个规则)
如果不知道如何实现颜色和网格,就很难进行更详细的描述
编辑再次阅读,注意到我写了y-2要避免相邻的瓷砖颜色相同还是相邻的瓷砖颜色相同?@KSFT节点随机路径上的相邻节点不能共享相同的颜色。但为了简单起见,我们使用c假设相邻的节点不能共享相同的颜色。我不确定它们是否相同。在您给出的示例图像中,没有两个相邻的节点具有相同的颜色,但您说“这很糟糕”。@KSFT抱歉,我弄糊涂了。请最后添加rule@RokoC.Buljan参见游戏规则:)很抱歉,你应该解释你所做的改变,以及为什么你认为它们可能会有帮助。mentioned@Growler我试着解释一下。你想让我详细解释一下吗?
firstCell : function () {
//choose intitial random starting cell
if (game.data.bridgeSegments[this.bridgeIndex].settings.realname == "bridge_shape_1") {
var row = Math.floor(Math.random() * (this.containerOfGrids[this.bridgeIndex].length - 0) + 0);
var col = 0;
this.gridLen = this.containerOfGrids[this.bridgeIndex][row].length;
}
else {
var row = 0;
var col = Math.floor(Math.random() * (this.containerOfGrids[this.bridgeIndex][row].length - 0) + 0);
this.gridLen = this.containerOfGrids[this.bridgeIndex].length;
}
this.containerOfGrids[this.bridgeIndex][row][col].firstCell = true;
this.currColor = this.containerOfGrids[this.bridgeIndex][row][col].color;
this.containerOfGrids[this.bridgeIndex][row][col].renderable.alpha = 1;
this.myDirectionsText.settings.text = this.containerOfGrids[this.bridgeIndex][row][col].color;
},
//passing in collided cell, cell row and col from the Tile Class
nextCell : function (cell, row, col) {
if (cell.col < this.gridLen) {
this.checkValidCells(row, col);
this.checkValidMove(cell, this.currColor);
var randomNumber = Math.floor(Math.random() * (this.list.length));
this.currColor = (this.list.length > 0) ? this.list[randomNumber].color : "RED";
this.myDirectionsText.settings.text = this.currColor;
}
},
checkValidMove : function (cell, color) {
if (cell.color == color) {
cell.renderable.image = (game.data.bridgeSegments[this.bridgeIndex].settings.view == "topdown") ? me.loader.getImage("MyFlatTile3") : me.loader.getImage("Tile128Green");
}
else {
game.data.health -=10;
cell.renderable.image = (game.data.bridgeSegments[this.bridgeIndex].settings.view == "topdown") ? me.loader.getImage("MyFlatTile3") : me.loader.getImage("Tile128Red");
}
cell.renderable.alpha = 1;
},
checkValidCells : function (row, col) {
this.list = [];
if (row - 1 >= 0) {
if (!this.containerOfGrids[this.bridgeIndex][row-1][col].explored) {
this.list.push(this.containerOfGrids[this.bridgeIndex][row-1][col]);
}
}
if (col - 1 >= 0) {
if (!this.containerOfGrids[this.bridgeIndex][row][col-1].explored) {
this.list.push(this.containerOfGrids[this.bridgeIndex][row][col-1]);
}
}
if (col + 1 < this.containerOfGrids[this.bridgeIndex][row].length && this.containerOfGrids[this.bridgeIndex][row][col + 1] != "undefined") {
if (!this.containerOfGrids[this.bridgeIndex][row][col+1].explored) {
this.list.push(this.containerOfGrids[this.bridgeIndex][row][col+1]);
}
}
if (row + 1 < this.containerOfGrids[this.bridgeIndex].length && this.containerOfGrids[this.bridgeIndex][row+1][col] != "undefined") {
if (!this.containerOfGrids[this.bridgeIndex][row+1][col].explored) {
this.list.push(this.containerOfGrids[this.bridgeIndex][row+1][col]);
}
}
}
if (!this.explored) {
nextCell(this, this.row, this.col);
this.explored=true;
}
grid = [];
for(var i=0;i<=totalRows;row++)
{
grid[i]=[]
for(var j=0;j<=totalCols;j++)
{
do
{
var colorIndex = Math.floor(Math.random() * (Object.keys(this.colors).length - 0) + 0);
var keyName = Object.keys(this.colors)[colorIndex];
grid[i][j]=keyName;
var nearSameColor=false;
for(var k=i-2;k<=i+2;k++)
{
if(k<0||k>totalRows)
continue;
for(var l=j-2;l<=l+2;l++)
{
if(l<0||l>totalCols||(k==i&&l==j))
continue;
nearSameColor=nearSameColor||grid[i+k][j+l]==grid[i][i];
}
}
}while(nearSameColor)
}
}
for (var row = 0; row <= totalRows; row++) {
this.containerOfGrids[i][row] = [];
for (var col = 0; col <= totalCols; col++) {
image = grid[row][col];
//other code