gpt4 book ai didi

javascript - 确保邻居节点不共享相同的值

转载 作者:行者123 更新时间:2023-11-28 07:42:03 24 4
gpt4 key购买 nike

我正在创建一个游戏-玩家可以向上,向下,向左或向右导航的颜色网格。如果指示说“去红场”,则玩家必须这样做。玩家必须遵循的路径由遵循以下规则的随机路径生成算法确定:

游戏规则:


这是一个迷宫游戏,可以帮助人们练习外语
为玩家提供了颜色网格,他们沿着下一个给定的颜色遍历随机生成的从左到右的路径(如果随机生成的路径上的下一个颜色是红色,则会提示他们转到红色方块)-显然稍后提示将使用外语
例如,出现“ Rojo”,他们必须去红场
这将基于时间...如果您在同一块瓷砖上停留太长时间,则会造成损坏
如果踩错颜色,则会造成伤害
由于迷宫是从左到右生成的(请参见下面的算法信息),因此他们可以选择移动到北,东或南节点


逻辑规则:


从左到右,表示在第0列选择了一个随机行
路径必须从col 0连接到最后一列
这条路是风大的,但不能回头,这意味着它可以向上,向下和向右循环
彼此相邻的节点不能共享相同的颜色
当前节点的北,东,南节点(例如灰色节点)不能共享相同的颜色
颜色提示(下一个要移动的单元格)是在生成随机路径时预先生成的……每种颜色在网格创建时都分配给了一个单元格……并且随机生成的路径存储了这些颜色的顺序/路径继续...,这样我就可以将下一种颜色传递给播放器,以便他们继续使用。


路径生成算法信息:Random path generation algorithm

我正在生成随机颜色的网格。当然,这意味着它不可能是完全随机的,因为我需要确保玩家不能选择与邻居相同的颜色。

该图像描述了它:灰色单元格是玩家的起始位置...北,东和南邻居都共享相同的颜色...这很糟糕。



在游戏中



网格是彩色图块图像的网格。因此,生成网格后,不应更改图块“颜色”。在网格创建过程中,我为每个新单元创建了一个随机索引,并将单元颜色设置为与colorsObject [randomIndex]相等,以确保分配了随机颜色

为了解决这个问题,我尝试存储上次使用的颜色...,在单元格创建的下一次迭代中,我从colors对象中删除了该上次使用的颜色(出于我自己的原因需要一个对象,否则将使用数组) ),设置随机数以从缩短的颜色列表中进行选择,然后使用该颜色。然后将该lastUsedColor添加回该对象:

    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;
}


这在逐行的基础上起作用(意味着2个相邻的列不会共享相同的颜色),但是在下一行中,可能存在具有相同颜色的相邻节点。

我的下一个想法是首先生成所有带有颜色的图块图像...然后再次进行迭代以检查相邻的颜色...如果有,请换出新图像。但我想在生成一组图块时完成此操作,而不必执行另一个嵌套的for循环。



我尝试了roko建议的方法,“迷宫”现在可以正常工作,但是当玩家移至桥的下一部分(在网格中生成第二组图块时,请参见此图)我的意思是: http://i.imgur.com/Hlerxm3.png),他附近似乎没有颜色:



您可以看到它显示“黄色”,但他周围没有黄色选项。

代码:(省略代码以实际循环并生成网格,因为这不是问题)

run : function (maze) {
this.maze= maze;
this.list = [];
this.containerOfGrids = [];
this.myDirectionsText = new game.HUD.ScreenText(630, 10, 10, 10, {font: "32x32_font", text: "1"})

game.data.gameHUD.addChild(this.myDirectionsText);
for (var i = 0; i <= game.data.bridgeSegments.length-1; i++) {
this.containerOfGrids[i] = [];
this.createGrid(game.data.bridgeSegments[i], i);
}

//call to set initial color cell
this.firstCell();

},


首先调用它是为了选择第一个单元格。如果它是顶部桥段(桥形状1),我需要从第一列中的随机行开始...如果路径逐渐缩小时是其他网格,则需要一个随机列选择...

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]);
}
}

}


在我的tile类中,我在与当前单元格碰撞时开始下一个单元格:

        if (!this.explored) {
nextCell(this, this.row, this.col);
this.explored=true;
}

最佳答案

如果我理解正确,那么您不仅会担心不在自己旁边放置相同的颜色,还会在所有方向上两步走。

除了为对象着色外,您还可以在为图块(a,b)着色时制定规则。如果a-2 a + 2并且y-2
然后,您将需要检查每种颜色的规则(每种颜色可以同时有多个活动规则,如果0,0和3,0均为红色,则为fx,则需要第一个规则)仍然用于当您移至下一行时)。

在不知道如何实现颜色和网格的情况下,很难对其进行详细介绍。

编辑再读一遍​​,发现我写了y-2

关于javascript - 确保邻居节点不共享相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27892760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com