我最近偶然发现了一个项目(使用A * alg的8-puzzle求解器),其中一些代码对我来说很奇怪,因为我从来没有见过它的喜欢。需要对这些代码位的解释
这条线是什么意思?这是什么 ?!
this[StateIndex]
这是什么表示法?我根本无法理解它! 我发布了一个样本,以便您几乎可以一起看到它。 还有一个问题,像StateNode一样实现一个类是否没有错?它只使用一个构造函数来初始化它的字段,但是最糟糕的是,它们都是公开的!如果他/她没有执行这项任务的功能?
public enum Direction
{
Up = 1, Down = 2, Left = 3, Right = 4, UpUp = 5, DownDown = 6, LeftLeft = 7, RightRight = 8, Stop = 9
}
class StateNode
{
public int Parent;
public List<int> Childs;
public Direction Move;
public Direction ParentMove;
public byte[,] State;
public byte Depth;
public byte NullRow;
public byte NullCol;
public StateNode()
{ }
public StateNode(int NewParent, Direction NewMove, Direction ParentMove, byte NewDepth, byte NewNullRow, byte NewNullCol)
{
this.Parent = NewParent;
this.State = new byte[5, 5];
this.Move = NewMove;
this.ParentMove = ParentMove;
this.Depth = NewDepth;
this.NullRow = NewNullRow;
this.NullCol = NewNullCol;
this.Childs = new List<int>();
}
}
class StateTree : List<StateNode>
{
public static long MakedNodes;
public static long CheckedNodes;
public static byte MaxDepth;
public List<int> Successor1(int StateIndex)
{
List<int> RetNodes = new List<int>();
StateNode NewState = new StateNode();
//Up
if (this[StateIndex].NullRow + 1 <= 3 && this[StateIndex].ParentMove != Direction.Up)
{
NewState = ChangeItemState(this[StateIndex], StateIndex, Direction.Up, Direction.Down, Convert.ToByte(this[StateIndex].Depth + 1), this[StateIndex].NullRow, this[StateIndex].NullCol, Convert.ToByte(this[StateIndex].NullRow + 1), this[StateIndex].NullCol);
this.Add(NewState);
RetNodes.Add(this.Count - 1);
StateTree.MakedNodes++;
this[StateIndex].Childs.Add(this.Count - 1);
if (NewState.Depth > StateTree.MaxDepth)
StateTree.MaxDepth = NewState.Depth;
}
//Down
//Left
//Right
return RetNodes;
}
}
如果您在理解特定语法时遇到困难,您并不需要发布整个代码。祝你好运! – SimpleVar 2012-07-06 13:05:17
也许我误解了,但'StateIndex'被用作'this'中的一个索引。它将检索位于阵列中该位置的项目。 – Silvermind 2012-07-06 13:07:02