我制作了一个翻转游戏,我正在尝试制作一种方法来提示玩家他们的下一步行动。基本上,我正在尝试使用广度优先搜索来解决游戏,然后向玩家展示下一步行动。但是,我的算法存在一些问题。Exit()
s 只是为了帮助我调试。无论如何,该算法成功地将当前游戏状态添加isWhite
到gameStates
并检查它是否是一个解决方案。当我尝试添加到队列时,问题就来了。我的代码从来没有达到第二个Exit()
,我不知道为什么。任何帮助,将不胜感激。谢谢。
编辑:问题已解决。现在我只需要弄清楚一旦找到解决方案该怎么做......
private void hint()
{
Queue<bool[,]> statesToCheck = new Queue<bool[,]>();
List<bool[,]> closedStates = new List<bool[,]>();
bool[,] rootState = new bool[4, 4];
rootState = (bool[,])isWhite.Clone();
statesToCheck.Enqueue((bool[,])rootState.Clone());
//closedStates.Add((bool[,])rootState.Clone());
while (statesToCheck.Count() > 0)
{
bool solved = true;
//checks for solution state
rootState = statesToCheck.Dequeue();
for (int row = 0; row < GAME_SIZE; row++)
for (int col = 0; col < GAME_SIZE; col++)
{
if (!rootState[row, col])
solved = false;
}
if (solved)
{
//add something to do
System.Diagnostics.Debug.Write("It worked");
return;
}
else
{
closedStates.Add((bool[,])rootState.Clone());
}
//simulates clicking each space and adds each one to statesToCheck
for (int row = 0; row < GAME_SIZE; row++)
for (int col = 0; col < GAME_SIZE; col++)
{
bool[,] stateToChange = new bool[4, 4];
stateToChange = (bool[,])rootState.Clone();
flip(stateToChange, row, col);//gets next gameState (next node in a typical breadth-first search)
if (!closedStates.Contains(stateToChange))
{
//closedStates.Add((bool[,])rootState.Clone());
statesToCheck.Enqueue((bool[,])stateToChange.Clone());
}
}
}