public PuzzleNode right() { int[] tempSequence; tempSequence = this.Getsequence(); PuzzleNode tempNode = null; for (int c1 = 0; c1 < 8; c1++) { if (tempSequence[c1] == 8) { if (c1 % 3 < 2) //if the zero(i.e empty tile) is not on the most right columen //c1+4(due to c1 +1(make index start from 1) +3 (make index more than to 3 to avoid 2%3 =0 { //swap the position of empty tile and tile right to it tempSequence[c1] = tempSequence[c1 + 1]; tempSequence[c1 + 1] = 8; tempNode = new PuzzleNode(this, tempSequence); break; } } } return(tempNode); }
public PuzzleNode(PuzzleNode N, int[] sequence) { ParentNode = N; //as arrays are passed by reference so changing the passed array will change this //internal array which thing we don't want to occure sequence.CopyTo(tilesequence, 0); NodeHashe = GetNodeHash(); }
void SolutionFound(PuzzleNode lastNode) { while (lastNode.getNodeParent() != null) { NodeStack.Push(lastNode); lastNode = lastNode.getNodeParent(); } MessageBox.Show("solved by BfS algorithm in " + Searched.Count + " nodes and number of steps is " + NodeStack.Count); }
public string GetNodeHash(PuzzleNode node) { string tempStr = ""; int[] tempArr = node.Getsequence(); for (int c1 = 0; c1 < 9; c1++) { tempStr += tempArr [c1]; } return(tempStr); }
public DFS(int[] seq) { seq.CopyTo(testsequence, 0); firstNode = new PuzzleNode(null, testsequence); DFSstack.Push(firstNode); while (DFSstack.Count > 0 && !solved) { currentNode = DFSstack.Pop(); if (Searched.Contains(currentNode.GetNodeHash())) { currentNode = null; continue; } else { Searched.Add(currentNode.GetNodeHash(), currentNode); } childNodes[0] = currentNode.left(); childNodes[1] = currentNode.right(); childNodes[2] = currentNode.up(); childNodes[3] = currentNode.down(); for (int c1 = 0; c1 < 4; c1++) { if (childNodes[c1] != null) { if (childNodes[c1].CheckIfSolved()) { //call solution has been found //Console.WriteLine("soultion found ,number of searched nodes{0}",Searched.Count); solved = true; SolutionFound(childNodes[c1]); break; } else { DFSstack.Push(childNodes[c1]); } } } } if (!solved) { MessageBox.Show("not solved in " + Searched.Count + " nodes"); } }
public bool EqualNodes(PuzzleNode n1, PuzzleNode n2) { bool equalSequence = false; if (n1 != null && n2 != null) { equalSequence = true; int[] temp1 = n1.Getsequence(); int[] temp2 = n2.Getsequence(); for (int c1 = 0; c1 < 9; c1++) { if (temp1[c1] != temp2[c1]) { equalSequence = false; break; } } } return(equalSequence); }
public PuzzleNode down() { int[] tempSequence; tempSequence = this.Getsequence(); PuzzleNode tempNode = null; for (int c1 = 0; c1 < 9; c1++) { if (tempSequence[c1] == 8) { if (c1 / 3 < 2) //if the zero(i.e empty tile) is not on the most lower row { //swap the position of empty tile and tile lower to it tempSequence[c1] = tempSequence[c1 + 3]; tempSequence[c1 + 3] = 8; tempNode = new PuzzleNode(this, tempSequence); break; } } } return(tempNode); }
public PuzzleNode left() { int[] tempSequence; tempSequence = this.Getsequence(); PuzzleNode tempNode = null; for (int c1 = 0; c1 < 9; c1++) { if (tempSequence[c1] == 8) { if (c1 % 3 > 0) //if the nine (i.e empty tile) is not on the most left columen //c1+4(due to c1 +1(make index start from 1) +3 (make index more than to 3 to avoid 2%3 =0 { //c1 -1 to see if it the tile next to the most right one //swap the position of empty tile and tile left to it tempSequence[c1] = tempSequence[c1 - 1]; tempSequence[c1 - 1] = 8; tempNode = new PuzzleNode(this, tempSequence); break; } } } return(tempNode); }
static Constants() { sortedNode = new PuzzleNode(null, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }); }