0

これは私のクラスの課題です。ゲーム「Knights Tour」の解決策を見つけるプログラムを作成しようとしています。ここでプレイしてください: https://www.brainbashers.com/knight.asp

解決策が見つかるまでプログラムを再帰的に実行します。64!さまざまな組み合わせがあり、そのほとんどは解決策ではありません。java.lang.StackOverflowError: nullこの問題は、 (java.util.ArrayLists で)エラーをスローしたときに、約 30 の異なる組み合わせが見つかった後に発生します 。

問題は ArrayList にあります。他のフォーラムでは、多くのメモリを使用するのは "HashMash" であると言われています。2 つのアレイからやり直すべきかどうか迷っています。

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;
    }
}
4

0 に答える 0