static PuzzleState MoveSpace(PuzzleState startPuzzle , int toIndex , bool[] keepOutMap , List<int> root ) { int[] rootMap =new int[16]; int fromIndex = startPuzzle.IndexOf(0); if (MakeRoot(rootMap, keepOutMap, fromIndex, toIndex) == false) { #if DEBUG Console.WriteLine("No Root Space {0:D} to {1:D}", fromIndex, toIndex); PuzzleState f1 = new PuzzleState(rootMap); f1.Print(); #endif return null; } PuzzleState tempPuzzle = startPuzzle; int c = rootMap[fromIndex] - 1; while (c > 0) { int mv = -1; int nextIndex = fromIndex; if ((fromIndex & 3) > 0) { if (rootMap[fromIndex - 1] == c) { mv = MOVE_LEFT; // space move nextIndex = fromIndex - 1; // space index } } if ((fromIndex & 3) < 3) { if (rootMap[fromIndex + 1] == c) { mv = MOVE_RIGHT; nextIndex = fromIndex + 1; } } if (fromIndex > 3) { if (rootMap[fromIndex - 4] == c) { mv = MOVE_UP; nextIndex = fromIndex - 4; } } if (fromIndex < 12) { if (rootMap[fromIndex + 4] == c) { mv = MOVE_DOWN; nextIndex = fromIndex + 4; } } tempPuzzle = tempPuzzle.Move(mv); root.Add(tempPuzzle.NumberOf(fromIndex)); fromIndex = nextIndex; c -= 1; } return tempPuzzle; }
static PuzzleState MoveNumber(PuzzleState startPuzzle ,bool[] keepOutMap,int number ,int toIndex , List<int> root ) { int[] rootMap = new int[16]; int fromIndex = startPuzzle.IndexOf(number); if (MakeRoot(rootMap, keepOutMap, fromIndex, toIndex) == false) { #if DEBUG Console.WriteLine("No Root {0:D} from {1:D} to {2:D}", number, fromIndex, toIndex); #endif return null; } PuzzleState tempPuzzle = startPuzzle; int c = rootMap[fromIndex] - 1; while (c > 0) { int mv = -1; int nextIndex = fromIndex; //Console.WriteLine("} head of MN num={0:D} fromIndex={1:D}", number, fromIndex); //tempPuzzle.Print(); if ((fromIndex & 3) > 0) { if (rootMap[fromIndex - 1] == c) { mv = MOVE_RIGHT; // number move is reverse space move nextIndex = fromIndex - 1; // number index. not space index. } } if ((fromIndex & 3) < 3) { if (rootMap[fromIndex + 1] == c) { mv = MOVE_LEFT; nextIndex = fromIndex + 1; } } if (fromIndex > 3) { if (rootMap[fromIndex - 4] == c) { mv = MOVE_DOWN; nextIndex = fromIndex - 4; } } if (fromIndex < 12) { if (rootMap[fromIndex + 4] == c) { mv = MOVE_UP; nextIndex = fromIndex + 4; } } keepOutMap[fromIndex] = true; tempPuzzle = MoveSpace(tempPuzzle, nextIndex, keepOutMap, root); keepOutMap[fromIndex] = false; if (tempPuzzle == null) { #if DEBUG Console.WriteLine("cannot move space to {0:D}", nextIndex); PuzzleState f2 = new PuzzleState(rootMap); f2.Print(); Console.WriteLine("formIndex={0:D} c={1:D} mv={2:D}", fromIndex, c, mv); #endif return null; } tempPuzzle = tempPuzzle.Move(mv); root.Add(tempPuzzle.NumberOf(nextIndex)); fromIndex = nextIndex; c -= 1; } return tempPuzzle; }