0

This is an assignment for one of my classes, I'm trying to create a program that will find a solution to the game "Knights Tour". Play it here: https://www.brainbashers.com/knight.asp

I have my program run recursively until it finds a solution. There is 64! different combinations, most of which are not solutions. The problem occurs after it finds about 30 different combinations when it throws the error java.lang.StackOverflowError: null (in java.util.ArrayLists).

The problem lies in the ArrayList, other forums say it is the "HashMash" that uses a lot of memory. I'm wondering if I should just start over with 2 Arrays?

import chn.util.*;
import java.util.*;
import java.awt.geom.*;
import apcslib.*;

public class Knight {
    public static void main(String[] args) {
        Solve Do = new Solve();
        Do.Boundries();
        Do.Display();
    }
}

class Solve {
    int[][] Grid = new int[8][8];
    int MaxR = Grid.length;
    int MaxC = Grid[1].length;
    ArrayList<Point> Point = new ArrayList<Point>(0);
    int nump = 0;
    int move = 1;
    int Random = 0;
    int Row = (int) (Math.random() * 7);
    int Col = (int) (Math.random() * 7);
    int Counter = 1;

    void Boundries() {
        // System.out.println("Cord: "+Row + ", "+Col);

        //Point0
        if (Row > 0 && Col > 1) {
            if (Grid[Row - 1][Col - 2] == 0) {
                Point.add(new Point(Row - 1, Col - 2));
                nump++;
            }
        }
        //Point1
        if (Row > 1 && Col > 0) {
            if (Grid[Row - 2][Col - 1] == 0) {
                Point.add(new Point(Row - 2, Col - 1));
                nump++;
            }
        }

        //Point2
        if (Row < 6 && Col > 0) {
            if (Grid[Row + 2][Col - 1] == 0) {
                Point.add(new Point(Row + 2, Col - 1));
                nump++;
            }
        }
        //Point3
        if (Row < 7 && Col > 1) {
            if (Grid[Row + 1][Col - 2] == 0) {
                Point.add(new Point(Row + 1, Col - 2));
                nump++;
            }
        }

        //Point5
        if (Row < 7 && Col < 6) {
            if (Grid[Row + 1][Col + 2] == 0) {
                Point.add(new Point(Row + 1, Col + 2));
                nump++;
            }
        }
        //Point6
        if (Row < 6 && Col < 7) {
            if (Grid[Row + 2][Col + 1] == 0) {
                Point.add(new Point(Row + 2, Col + 1));
                nump++;
            }
        }

        //Point7
        if (Row > 0 && Col < 6) {
            if (Grid[Row - 1][Col + 2] == 0) {
                Point.add(new Point(Row - 1, Col + 2));
                nump++;
            }
        }
        //Point8
        if (Row > 1 && Col < 7) {
            if (Grid[Row - 2][Col + 1] == 0) {
                Point.add(new Point(Row - 2, Col + 1));
                nump++;
            }
        }

        //for (int i = 0;i<nump;i++)
        // System.out.println(Point.get(i).xcord + ","+Point.get(i).ycord);

        if (nump != 0) {
            Random = (int) (Math.random() * nump);
            //for (int p=3;p<6;p++){
            int p = 0;
            int q = 7;
            for (int z = 1; z < nump; z++) {
                if (Point.get(z).xcord == p || Point.get(z).xcord == q || Point.get(z).ycord == p || Point.get(z).ycord == q) {
                    p++;
                    q--;
                    Random = z;
                }
            }
            //}
            nump = 0;
            Grid[Point.get(Random).xcord][Point.get(Random).ycord] = move;
            move++;
            Row = Point.get(Random).xcord;
            Col = Point.get(Random).ycord;
            Point = new ArrayList<Point>(0);
            Boundries();
        } else {
            System.out.println("\fNot a Solution: " + move + " moves");
            System.out.println("Amount of tries: " + Counter);
        }
        if (move < 64) {
            for (int t = 0; t < nump; t++)
                Point.remove(t);

            Display();
            Counter++;
            move = 0;
            nump = 0;
            Row = (int) (Math.random() * 7);
            Col = (int) (Math.random() * 7);
            //Point = new ArrayList<Point>(0);
            Grid = new int[8][8];
            Delay();
            Boundries();
        }
    }

    public static void gc() {
        System.gc();
    }

    void Delay() {
        try {
            Thread.sleep(100); //1000 milliseconds is one second.
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

    void Display() {
        {
            int row, col;
            System.out.println();
            for (row = 0; row < 8; row++) {
                for (col = 0; col < 8; col++)
                    System.out.print((Format.left(Grid[row][col], 3) + " "));

                System.out.println();
            }
            System.out.println();
        }
    }
}

class Point {
    int xcord = 0;
    int ycord = 0;

    Point(int x, int y) {
        xcord = x;
        ycord = y;
    }
}
false
  • 10,264
  • 13
  • 101
  • 209
Greg M
  • 398
  • 1
  • 9
  • 1
    Please post the stacktrace. – Tunaki Jun 03 '16 at 20:59
  • How deep do you think your recursion goes? 64! is a VERY large number. – FredK Jun 03 '16 at 21:30
  • A chessboard is 8x8, so the stack shouldn't be any higher than 64. Also, it's not an `OutOfMemoryException`, so the problem is how you handle the recursion. Obviously, you are visiting some positions several times, else you wouldn't have this stack overflow. – T. Claverie Jun 03 '16 at 21:37

0 Answers0