Author | Message | Time |
---|---|---|
HdxBmx27 | [Edit] Fixed it, I think. Original post below, with fixed code. [hr] Im getting a bug when it attempts to exicute the vary last step of each simulation My GetMove() function is returning -1 (no moves) when it should return a move.. uncomment some of the "PrintAccess();" lines for some debugging. [code]/////////////////////////////// //Name: KnightsTour.java // //Auth: Hdx(Blake Hanson) // //Date: 01/11/06 // //Purp: To simulate the moves// // a Knight can take on the // // Chess Board. // /////////////////////////////// public class KnightsTour { private static int iMoves[][] = { { 2, -1}, { 1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}, { 1, 2}, { 2, 1} }; private static boolean bBoard[][] = new boolean[8][8]; private static int iAccess[][] = new int[8][8]; private static int iMoveAccess[] = new int[8]; public static void main(String sArgs[]) { System.out.println(" 0 1 2 3 4 5 6 7"); for (int X = 0; X <= 7; X++) { System.out.print(X + ": "); for (int Y = 0; Y <= 7; Y++){ Reset(); int iScore = StartSimulation(X, Y); System.out.print(iScore + " "); } System.out.println(""); } } /////////////////////////////////// //Name: GetAccess // //Auth: Hdx // //Date: 01/11/06 // //Params: int - Collom // // int - Row // //Note: This will find the number// // of remaing positions this // // square can be accessed from. // /////////////////////////////////// private static int GetAccess(int iCol, int iRow) { int iRet = 0; for (int X = 0; X <= 7; X++) { int iFromCol = iCol + iMoves[X][0]; int iFromRow = iRow + iMoves[X][1]; if (iFromCol >= 0 && iFromCol <= 7) { if (iFromRow >= 0 && iFromRow <= 7) { if (bBoard[iFromCol][iFromRow] == false) { iRet++; }//End False }//End Rows }//End Cols }//Next X //System.out.println(iCol + ", " + iRow + ": " + iRet); return iRet; } ////////////////////////////////////// //Name: FillAccess // //Auth: Hdx // //Date: 01/11/06 // //Params: None // //Note: This will fill the iAccess // // array with the correct number of// // places each sqare can be // // accessed from. // ////////////////////////////////////// private static void FillAccess() { for (int X = 0; X <= 7; X++) { for(int Y = 0; Y <= 7; Y++) { if (bBoard[X][Y] == false) { iAccess[X][Y] = GetAccess(X,Y); }else{ iAccess[X][Y] = 0; } }//End Y }//End X //PrintAccess(); } ///////////////////////////// //Name: GetMove // //Auth: Hdx // //Date: 01/11/06 // //Params: None // //Return: // // int - Move to make // ///////////////////////////// private static int GetMove() { int iSmallest = 0; for (int X = 0; X <= 7; X++) { if (iMoveAccess[X] > 0) { if (iMoveAccess[X] < iMoveAccess[iSmallest] || iMoveAccess[iSmallest] == 0) { iSmallest = X; } } } //System.out.println("GetMove: " + iMoveAccess[iSmallest]); if (iMoveAccess[iSmallest] == 0) iSmallest = -1; return iSmallest; } ////////////////////////// //Name: PrintAccess // //Auth: Hdx // //Date: 01/11/06 // //Params: None // //Return: None // //Note: This will print // // out a grid showing // // the access of each // // place. // ////////////////////////// private static void PrintAccess() { System.out.println(" 0 1 2 3 4 5 6 7"); for (int Z = 0; Z <= 7; Z++) { System.out.print(Z + ": "); for (int A = 0; A <= 7; A++) { System.out.print(iAccess[Z][A] + " "); } System.out.println(""); } } ///////////////////////// //Name: PrintGrid // //Auth: Hdx // //Date: 01/11/06 // //Params: None // //Return: None // //Note: This will print// // a grid showing the // // results of the run.// ///////////////////////// private static void PrintGrid() { System.out.println(" 0 1 2 3 4 5 6 7"); for (int Z = 0; Z <= 7; Z++) { System.out.print(Z + ": "); for (int A = 0; A <= 7; A++) { if (bBoard[Z][A] == true){ System.out.print("X "); }else{ System.out.print(" "); } }//Next A System.out.println(""); }//Next Z } ///////////////////////// //Name: StartSimulation// //Auth: Hdx // //Date: 01/11/06 // //Params: int - Column // // int - Row // //Returns: int - Tagged// //Note: This runs the // // simulation starting// // at the specifyed // // position. // ///////////////////////// private static int StartSimulation(int iCurrentCol, int iCurrentRow){ //System.out.println("Starting position: " + iCurrentRow + ", " + iCurrentCol); boolean bContinue = true; while(bContinue) { for (int X = 0; X <= 7; X++) { int iNextCol = iCurrentCol + iMoves[X][0]; int iNextRow = iCurrentRow + iMoves[X][1]; if (iNextCol >= 0 && iNextCol <= 7 && iNextRow >= 0 && iNextRow <= 7 && bBoard[iNextCol][iNextRow] == false) iMoveAccess[X] = iAccess[iNextCol][iNextRow]; else iMoveAccess[X] = 0; } int iMove = GetMove(); if (iMove == -1) { //-1 means there were no moves bContinue = false; }else if(iMove <= 7 && iMove >= 0) { iCurrentCol += iMoves[iMove][0]; iCurrentRow += iMoves[iMove][1]; FillAccess(); bBoard[iCurrentCol][iCurrentRow] = true; }else{ bContinue = false; System.out.print("Error, somehow GetMove() returned and invalid number: " + iMove); } } //PrintGrid(); int iTotal = 0; for (int X = 0; X <= 7; X++){ for (int Y = 0; Y <= 7; Y++){ if (bBoard[X][Y] == true) iTotal++; } } return iTotal; } //////////////////////// //Name: Reset // //Auth: Hdx // //Date: 01/11/06 // //Params: None // //Return: None // //Note: This resets // // both the Access // // table, and tagged // // list. // //////////////////////// private static void Reset() { for (int X = 0; X <= 7; X++) { for (int Y = 0; Y <= 7; Y++){ bBoard[X][Y] = false; } } FillAccess(); } }//End Class [/code] If anyone can figure out why in hell its doing this, Plz say so, Its prolly something obvious I jsut cant see it right now. Current output: [code] 0 1 2 3 4 5 6 7 0: 64 64 64 64 64 64 64 64 1: 64 64 64 64 64 64 64 64 2: 64 64 64 64 64 64 64 64 3: 64 64 64 64 64 64 64 64 4: 64 64 64 64 64 64 64 64 5: 64 64 64 64 64 64 64 64 6: 64 64 64 64 64 64 64 64 7: 64 64 64 64 64 64 64 64 Press any key to continue...[/code] ~-~(HDX)~-~ | January 13, 2006, 7:05 AM |
JoeTheOdd | [quote] ===============================================================[/quote] Eek, use [nobbc][hr][/nobbc] EDIT - Eek! This forum doesn't support nobbc. Anyhow, [hr.] | January 14, 2006, 8:36 PM |
HdxBmx27 | Whoot, upgraded it so that it ALWAYS compleets a full tour <3 Edited main post. ~-~(HDX)~-~ | January 14, 2006, 9:19 PM |