473,408 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Knight's Tour Project

Hello,
I know this is a popular problem with a lot of solutions, but I don't want to be pointed to a post by someone else with THEIR solution. I want to figure out how to make my own code work. So, any help you could provide would be wonderful!

So, what I'm trying to do is solve the Knight's Tour problem (without a GUI) and print the result as follows:
[1][0][3][0][0][0][0][0]
[8][0][43][0][0][0][0][0]
[0][2][0][0][0][0][0][0]
[0][18][0][64][0][0][0][0]
[0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0]
Now, of course this is not the correct answer since there are many unvisited squares (denoted by 0's) and many numbers are absent. The numbers are supposed to represent the order of the steps it takes to fill the grid.

Here is my code. It is relatively short so it's worth just following it since I can't pinpoint where the problem is.

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2.  
  3.  
  4. public class Knight
  5. {
  6.     private int currentX;
  7.     private int currentY;
  8.     private int tempX;
  9.     private int tempY;
  10.     private int counter;
  11.     private int[][] kmoves = {{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
  12.     private int[][] board = new int[8][8];
  13.     private boolean[][] boolboard = new boolean[8][8];
  14.     private ArrayList <Integer> possibleX = new ArrayList();
  15.     private ArrayList <Integer> possibleY = new ArrayList();
  16.     private int[][] access = {     
  17.                                             {2,3,4,4,4,4,3,2},
  18.                                             {3,4,6,6,6,6,4,3}, 
  19.                                             {4,6,8,8,8,8,6,4}, 
  20.                                             {4,6,8,8,8,8,6,4}, 
  21.                                             {4,6,8,8,8,8,6,4}, 
  22.                                             {4,6,8,8,8,8,6,4}, 
  23.                                             {3,4,6,6,6,6,4,3},
  24.                                             {2,3,4,4,4,4,3,2}
  25.                                     };
  26.  
  27.     public Knight()
  28.     {
  29.         currentX = 0;
  30.         currentY = 0;
  31.         counter = 1;
  32.         for(int i = 0; i < 8; i++)
  33.         {
  34.             for(int j = 0; j < 8; j++)
  35.             {
  36.                 board[i][j] = 0;
  37.             }
  38.         }
  39.         for(int i = 0; i < 8; i++)
  40.         {
  41.             for(int j = 0; j < 8; j++)
  42.             {
  43.                 boolboard[i][j] = false;
  44.             }
  45.         }        
  46.     }
  47.  
  48.     public void getMoveLocations()
  49.     {
  50.         for(int i = 0; i < kmoves.length; i++)
  51.         {
  52.             if(currentY + kmoves[i][1] < 8 && currentY + kmoves[i][1] >=0 && currentX + kmoves[i][0] < 8 && currentX + kmoves[i][0] >= 0 && (boolboard[currentY + kmoves[i][1]][currentX + kmoves[i][0]] == false))
  53.             {
  54.                 possibleX.add(currentX + kmoves[i][0]);
  55.                 possibleY.add(currentY + kmoves[i][1]);
  56.             }
  57.         }        
  58.     }
  59.  
  60.     public void selectMoveLocation()
  61.     {
  62.         int i = 0;
  63.         int lowX = 4;
  64.         int lowY = 4;
  65.  
  66.         while(i < possibleX.size())
  67.         {
  68.             if(access[possibleY.get(i)][possibleX.get(i)] <= access[lowY][lowX])
  69.             {
  70.                 lowX = possibleX.get(i);
  71.                 lowY = possibleY.get(i);
  72.             }
  73.             i++;
  74.         }
  75.  
  76.         int p = 0;
  77.  
  78.         while(p < possibleX.size())
  79.         {
  80.             access[possibleY.get(p)][possibleX.get(p)]--;
  81.             p++;
  82.         }
  83.  
  84.         tempX = currentX;    //remembers old x value
  85.         tempY = currentY;    //remembers old y value
  86.         currentX = lowX;    //x value to move to
  87.         currentY = lowY;    //y value to move to
  88.     }
  89.  
  90.     public void makeMove()
  91.     {
  92.         board[tempY][tempX] = counter;
  93.         boolboard[tempY][tempX] = true;
  94.         counter++;
  95.     }
  96.  
  97.     public void printBoard()
  98.     {
  99.         for(int i = 0; i < 8; i++)
  100.         {
  101.             System.out.println("");
  102.             for(int j = 0; j < 8; j++)
  103.             {
  104.                 System.out.print("[" + board[j][i] + "]");
  105.             }
  106.         }
  107.     }
  108. }
  109.  
And, here's my tester file:

Expand|Select|Wrap|Line Numbers
  1. public class KnightRunner
  2. {
  3.  
  4.     public static void main(String[] args)
  5.     {
  6.         int x = 0;
  7.         Knight mine = new Knight();
  8.         while(x < 64)
  9.         {
  10.             mine.getMoveLocations();
  11.             mine.selectMoveLocation();
  12.             mine.makeMove();
  13.             x++;
  14.         }
  15.         mine.printBoard();
  16.     }
  17.  
  18. }
  19.  
I would really like to get this program running properly. What is frustrating is that I am so sure I have accounted for everything, yet there is obviously still some problem. Thanks in advance!
Dec 6 '09 #1
0 2907

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: googlegroups111 | last post by:
This is an EXCERPT from the story of the Philip Bartlett the Knight in Shining Armour from the days of hoary olde England, a story adapted by an author who shall remain nameless..... One day...
3
by: Ekhaat | last post by:
Hi I followed the Web Matrix guided tour and came to the "ASP.NET Pages with Data (Microsoft Access)" part. There is really not much you can do wrong there, but for some reason, the INSERT...
0
by: James | last post by:
I've tried an XML service in a VB.NET Guided Tour sample. In Step 6-5, I couldn't create a new category called "TaskManagement" in the "Performance Counter" path. It doesn't show...
9
by: whitehatmiracle | last post by:
im stuck, thers a prob with the backtrack function. #include <iostream.h> #include <conio.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define size 5
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
17
by: foahchon | last post by:
Hi, I'm trying to write a program to solve (or whatever) the Knight's Tour. I know this is a pretty common problem, but I haven't found a solution that I can really compare mine to and isolate what...
18
by: Bert | last post by:
This is a past question from a programming competition. On a chess board (8 squares by 8 squares), there's is a knight sitting on b1 (2 from the left of the bottom row). To cut the crap, find out...
3
by: sliever | last post by:
hey! to anyone out there who might be able to help me with my problem about the knight's tour using php pls. help me.. i've come up with a long solution... ?> $chessboard=array ( ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.