post #1 of 1
Thread Starter 
First things first, I have a general idea of what I should be doing but I just can't seem to get the 'Occupy' function and place the Knight on the board. I've tried for a week and it still doesn't want to work. Can anyone help?

Here is my code:

Each code box is its own class:
Class Square:
Code:
package knightsTour;

public class Square {
        
        Square up;
        Square down;
        Square left;
        Square right;
        boolean occupy;
        int ID;
        static int count;
        
        public Square()
        {
                occupy = false;
                up = null;
                down = null;
                left = null;
                right = null;
                count++;
                ID = count;
        }
        
}

Class Board:
Code:
package knightsTour;

public class Board {
        
        public static Square topCorner = new Square();
        public static Square rowHold = topCorner;
        public static Square temp = topCorner;
        
        public Board(){
                
        for(int x = 0; x < 8; x++){
                if(x == 0){
                        for(int y = 0; y < 7; y++){
                                temp.right = new Square();
                                temp.right.left = temp;
                                temp = temp.right;
                                }
                        }
                
        else{
                temp = rowHold;
                temp.down = new Square();
                temp.down.up = temp;
                temp = temp.down;
                rowHold = temp;
                for(int y = 0; y < 7; y++){
                temp.right = new Square();
                temp.right.left = temp;
                temp.up.right.down = temp.right;
                temp.right.up = temp.up.right;
                temp = temp.right;
                }
                }
                }
        }
        
        public static void occupy(){
                
        }
        
        public static void remove(){
                
        }
        
        public static void display(){
        rowHold = topCorner;
        temp = rowHold;
        while(rowHold != null){
        while(temp != null){
        if(temp.ID < 10)
        System.out.print(temp.ID + " ");
        else
        System.out.print(temp.ID + " ");
        temp = temp.right;
        }
        System.out.println();
        rowHold = rowHold.down;
        temp = rowHold;
        }
        }
        }

Class Knight:
Code:
package knightsTour;

public class Knight {
        
        public static int positionX;
        public static int positionY;
        public static int KnightCount = 0;
        public static int KnightID;
        
        public Knight(int x, int y){
        positionX = 0;
        positionY = 0;
        KnightCount++;
        KnightID = KnightCount;
        }

        public static void tour(Knight startKnight){
        if(KnightID == 63)
        System.out.println("Finished");
        
        if(KnightID < 64){
                
                if(Knight.positionX + 1 < 8 && Knight.positionY - 2 > 0){
                Knight newKnight = new Knight(positionX+1,positionY-2);
                KnightID++;
                tour(newKnight);
                }
        
                if(Knight.positionX + 1 < 8 && Knight.positionY + 2 < 8){
                Knight newKnight = new Knight(positionX+1,positionY+2);
                KnightID++;
                tour(newKnight);
                }
        
                if(Knight.positionX - 1 > 0 && Knight.positionY - 2 > 0){
                Knight newKnight = new Knight(positionX-1,positionY-2);
                KnightID++;
                tour(newKnight);
                }
        
                if(Knight.positionX - 1 > 0 && Knight.positionY + 2 < 8){
                Knight newKnight = new Knight(positionX-1,positionY+2);
                KnightID++;
                tour(newKnight);
                }
        
                if(Knight.positionX - 2 > 0 && Knight.positionY + 1 < 8){
                Knight newKnight = new Knight(positionX+1,positionY+2);
                KnightID++;
                tour(newKnight);
                }
        
                if(Knight.positionX + 2 < 8 && Knight.positionY + 1 < 8){
                Knight newKnight = new Knight(positionX+1,positionY+2);
                KnightID++;
                tour(newKnight);
                }
        
                if(Knight.positionX - 2 > 0 && Knight.positionY - 1 > 0){
                Knight newKnight = new Knight(positionX+1,positionY+2);
                KnightID++;
                tour(newKnight);
                }
        
                if(Knight.positionX + 2 < 8 && Knight.positionY - 1 > 0){
                Knight newKnight = new Knight(positionX+1,positionY+2);
                KnightID++;
                tour(newKnight);
                }
                }
        }
}

Class Main:
Code:
package knightsTour;

public class Main {
        
        public static void main(String[] args) {
                Board Board = new Board();
                Board.display();
                Knight startKnight = new Knight(0,0);
                Knight.tour(startKnight);
        }

}
Swag's Venus
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k @ 4.8GHz Asus Maximus V Gene EVGA GTX 680 G.Skill RipJaws X 16GB (2x8GB) 
Hard DriveHard DriveCoolingOS
Samsung 830 Crucial M4 Noctua NH-D14 SE2011 Windows 7 
MonitorKeyboardPowerCase
Dell 23" Ducky TKL OCN-Version Corsair AX650 Corsair C70 (Military Green) 
MouseMouse PadAudioAudio
Razer DeathAdder BE Steelseries QCK+ Beats Pro Bose Surround Sound 5.1 
Audio
Beats Tours 
  hide details  
Reply
Swag's Venus
(17 items)
 
  
CPUMotherboardGraphicsRAM
i5 3570k @ 4.8GHz Asus Maximus V Gene EVGA GTX 680 G.Skill RipJaws X 16GB (2x8GB) 
Hard DriveHard DriveCoolingOS
Samsung 830 Crucial M4 Noctua NH-D14 SE2011 Windows 7 
MonitorKeyboardPowerCase
Dell 23" Ducky TKL OCN-Version Corsair AX650 Corsair C70 (Military Green) 
MouseMouse PadAudioAudio
Razer DeathAdder BE Steelseries QCK+ Beats Pro Bose Surround Sound 5.1 
Audio
Beats Tours 
  hide details  
Reply