4

我正在研究填字算法来开发一个单词应用程序。在 StackOverflow 上进行了大量谷歌搜索或搜索后,我能够达到这一点。但是我无法理解 Java 中算法的正确实现。下面是我使用的类。

public class Crosswords {

    char[][] cross;
    int rows;
    int cols;
    char[][] numberGrid;
    boolean startword;
    final char DEFAULT = ' ';

    public Crosswords() {
        rows = 50;
        cols = 50;
        cross = new char[rows][cols];
        numberGrid = new char [rows][cols];
        for (int i = 0; i < cross.length;i++){
            for (int j = 0; j < cross[i].length;j++){
                cross[i][j] = DEFAULT;
            }
        }
    }

    public Crosswords(int ros, int colls) {
        rows = ros;
        cols = colls;
        cross = new char[rows][cols];
        numberGrid = new char [rows][cols];
        for (int i = 0;i < cross.length; i++){
            for (int j = 0; j < cross[i].length; j++){
                cross[i][j] = DEFAULT;
            }
        }
    }


    public String toString() {
             String s = new String();
            //String d = new String();
        for (int i = 0; i < rows; i++) {
         for (int j = 0; j < cols; j++){
          s = s + cross[i][j] + " ";
            }
          s = s + "\n";
        }
        return s;
    }


    public void addWordh(String s, int r, int c) {

        int i = 0;

        int j = 0;

        boolean b = true;

        boolean intersectsWord = true;


        if (s.length() > cols) {

            System.out.println(s + " is longer than the grid. Please try another word.");

            return;

        }

        if (c + s.length() > cols) {

            System.out.println(s + " is too long. Please try another word.");

            return;

        }

        if ((r - 2) >= 0) {

            if ((cross[r - 1][c - 1 + s.length()] == DEFAULT) || (cross[r - 1][c - 1 + s.length()] == '*')) {
                intersectsWord = false;
            }

            else { intersectsWord = true;}

            if (intersectsWord == true) {
                System.out.println("The word " + s + " intersects the beginning of another word!");
                return;
            }
        }

        for (i = 0; i < s.length(); i++) {

            if ((cross[r - 1][c - 1 + i] == DEFAULT) || (cross[r - 1][c - 1 + i] == s.charAt(i))) {

                b = true;

            }

            else {

                b = false;

                System.out.println("Unable to add " + s + ". Please try another word.");

                return;}

        }

        if (b == true) {

            if ((s.length() <= cols) && (c + s.length() <= cols) &&

                    (cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {

                while (j < s.length()) {


                    cross[r - 1][c - 1 + j] = s.charAt(j);

                    if (j==0){
                        startword = true;
                    }

                    cross[rows - 1 - (r - 1)][cols - 1 - (c - 1 + j)] = '*';

                    j++;

                }

            }

        }

    }

    public void addWordv(String s, int r, int c) {

        int i = 0;

        int j = 0;

        boolean b = true;

        boolean intersectsWord = true;

        if (s.length() > rows) {

            System.out.println(s + " is longer than the grid. Please try another word.");

        }

        if (r + s.length() > rows) {

            System.out.println(s + " is too long. Please try another word.");

        }

        else {

            if ((r - 2) >= 0) {

                if ((cross[r - 2][c - 1] == DEFAULT) || (cross[r - 2][c - 1] == '*')) {

                    intersectsWord = false;

                }

                else { intersectsWord = true;}

                if (intersectsWord == true) {

                    System.out.println("The word " + s + " intersects the end of another word!");

                    return;

                }

            }
            if ((cross[r - 1 + s.length()][c - 1] == DEFAULT) || (cross[r - 1 + s.length()][c - 1] == '*')) {
                intersectsWord = false;
            }

            else { intersectsWord = true;}

            if (intersectsWord == true) {
                System.out.println("The word " + s + " intersects the end of another word!");
                return;
            }


            for (i = 0; i < s.length(); i++) {

                if ((cross[r - 1 + i][c - 1] == DEFAULT) || (cross[r - 1 + i][c - 1] == s.charAt(i))) {

                    b = true;

                }

                else {

                    b = false;

                    System.out.println("Unable to add " + s + ". Please try another word.");

                    return;}

            }

            if (b == true) {

                if ((s.length() <= rows) && (r + s.length() <= cols) &&

                        (cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {

                    while (j < s.length()) {

                        cross[r - 1 + j][c - 1] = s.charAt(j);

                        if (j==0){
                            startword = true;
                        }

                        cross[rows - 1 - (r - 1 + j)][cols - 1 - (c - 1)] = '*';

                        j++;

                    }

                }

            }
        }

    }

    public void setNumberGrid(){
        numberGrid = new char [rows][cols];
        for (int i = 0; i < cross.length; i++){
            for (int j=0; j < cross[rows].length; j++){
                if (cross[i][j] == DEFAULT){
                    numberGrid[i][j] = (char) 0;
                }
                else if (startword == true){
                    numberGrid[i][j] = (char) -2;
                }
                else {
                    numberGrid[i][j] = (char) -1;
                }
            }
            int count = 1;
            for (i=0; i < cross.length; i++){
                for (int j=0; j < cross[rows].length; j++){
                    if (numberGrid[i][j] == -2){
                        numberGrid[i][j] = (char)count;
                        count++;
                    }
                }
            }
        }
    }

    public String printNumberGrid() {
        for (int i=0; i < cross.length; i++){
            for (int j=0; j < cross[rows].length; j++){
                if (numberGrid[i][j] == (char)-1){
                    numberGrid[i][j] = ' ';
                }
                else if (numberGrid[i][j] == (char)0){
                    numberGrid[i][j] = '#';
                }
            }
        }
        String d = new String();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++){
                d = d + numberGrid[i][j] + " ";
            }
            d = d + "\n";
        }
        return d;
    }



    public static void main(String[] args) {
        Crosswords g = new Crosswords();
        g.addWordv("rawr", 4, 5);
        g.addWordh("bot", 5, 4);
        g.addWordv("raw", 7, 5);
        g.addWordh("cat", 4, 5);
        g.addWordh("bass", 6, 10);
        System.out.println(g);

        Crosswords c = new Crosswords(20, 20);

        c.addWordh("HELLO", 1, 1);

        c.addWordv("HAPLOID", 1, 1);
        c.addWordh("COMPUTER", 3, 12);

        c.addWordv("CAT", 2, 11);

        c.addWordv("WOAH", 2, 20);
        c.addWordh("PARKING", 20, 5);

        c.addWordv("ARK", 17, 6);
        c.addWordh("AHOY", 6, 18);
        c.addWordv("AHOY", 18, 10);
        c.addWordv("ADVANTAGE", 2, 12);
        c.addWordv("INTERNAL", 2, 18);
        c.addWordh("BANTER", 7, 11);
        c.addWordv("BEAGLE", 5, 12);
        c.addWordh("BASE", 8, 3);
        c.addWordv("BALL", 8, 3);
        c.addWordh("LEFT", 10, 3);
        c.addWordv("SAFE", 8, 5);
        System.out.print(c);
    }
}

正如您在 Main 方法中看到的那样,我添加了单词,但也给出了行号和列号来放置诸如c.addWordv("Safe",8,5);这样的单词。其中 8 和 5 是列号。

现在的问题是我如何实现填字算法,它只取单词并将它们随机放置在板上而不取行号和列号。提前致谢

编辑:
我想修改这个类算法,我不必放弃行号和列号..

4

2 回答 2

2

//伪代码

如果填字游戏的大小是 maxSize 并且任何单词的长度都存储在 wordLength 中,那么您可以使用如下随机方法 int maxSize=20; 诠释字长=4;

    Random random =new Random();
    int r,c;
    //for horizontal
     r=random.nextInt(maxSize-wordLength);
     c=random.nextInt(maxSize);
    //for vertical
     r=random.nextInt(maxSize);
     c=random.nextInt(maxSize-wordLength);

您可以存储行和列并生成新的(如果它已经存在)。

于 2016-07-18T07:20:33.803 回答
1

好的,首先恭喜你获得了优秀的 JAVA 代码,但你必须记住,作为操作系统的 Android 与 Java 不同。首先,所有作为“主”类的类都不使用public static void main(String[] args). 他们扩展了活动。这public static void main(string args)对于启动您的应用程序无效,这意味着代码永远不会运行!

public class Crosswords extends Activity{

    char[][] cross;
    int rows;
    int cols;
    char[][] numberGrid;
    boolean startword;
    final char DEFAULT = ' ';

    public Crosswords() {
        rows = 50;
        cols = 50;
        cross = new char[rows][cols];
        numberGrid = new char [rows][cols];
        for (int i = 0; i < cross.length;i++){
            for (int j = 0; j < cross[i].length;j++){
                cross[i][j] = DEFAULT;
            }
        }
    }

    public Crosswords(int ros, int colls) {
        rows = ros;
        cols = colls;
        cross = new char[rows][cols];
        numberGrid = new char [rows][cols];
        for (int i = 0;i < cross.length; i++){
            for (int j = 0; j < cross[i].length; j++){
                cross[i][j] = DEFAULT;
            }
        }
    }


    public String toString() {
             String s = new String();
            //String d = new String();
        for (int i = 0; i < rows; i++) {
         for (int j = 0; j < cols; j++){
          s = s + cross[i][j] + " ";
            }
          s = s + "\n";
        }
        return s;
    }


    public void addWordh(String s, int r, int c) {

        int i = 0;

        int j = 0;

        boolean b = true;

        boolean intersectsWord = true;


        if (s.length() > cols) {

            System.out.println(s + " is longer than the grid. Please try another word.");

            return;

        }

        if (c + s.length() > cols) {

            System.out.println(s + " is too long. Please try another word.");

            return;

        }

        if ((r - 2) >= 0) {

            if ((cross[r - 1][c - 1 + s.length()] == DEFAULT) || (cross[r - 1][c - 1 + s.length()] == '*')) {
                intersectsWord = false;
            }

            else { intersectsWord = true;}

            if (intersectsWord == true) {
                System.out.println("The word " + s + " intersects the beginning of another word!");
                return;
            }
        }

        for (i = 0; i < s.length(); i++) {

            if ((cross[r - 1][c - 1 + i] == DEFAULT) || (cross[r - 1][c - 1 + i] == s.charAt(i))) {

                b = true;

            }

            else {

                b = false;

                System.out.println("Unable to add " + s + ". Please try another word.");

                return;}

        }

        if (b == true) {

            if ((s.length() <= cols) && (c + s.length() <= cols) &&

                    (cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {

                while (j < s.length()) {


                    cross[r - 1][c - 1 + j] = s.charAt(j);

                    if (j==0){
                        startword = true;
                    }

                    cross[rows - 1 - (r - 1)][cols - 1 - (c - 1 + j)] = '*';

                    j++;

                }

            }

        }

    }

    public void addWordv(String s, int r, int c) {

        int i = 0;

        int j = 0;

        boolean b = true;

        boolean intersectsWord = true;

        if (s.length() > rows) {

            System.out.println(s + " is longer than the grid. Please try another word.");

        }

        if (r + s.length() > rows) {

            System.out.println(s + " is too long. Please try another word.");

        }

        else {

            if ((r - 2) >= 0) {

                if ((cross[r - 2][c - 1] == DEFAULT) || (cross[r - 2][c - 1] == '*')) {

                    intersectsWord = false;

                }

                else { intersectsWord = true;}

                if (intersectsWord == true) {

                    System.out.println("The word " + s + " intersects the end of another word!");

                    return;

                }

            }
            if ((cross[r - 1 + s.length()][c - 1] == DEFAULT) || (cross[r - 1 + s.length()][c - 1] == '*')) {
                intersectsWord = false;
            }

            else { intersectsWord = true;}

            if (intersectsWord == true) {
                System.out.println("The word " + s + " intersects the end of another word!");
                return;
            }


            for (i = 0; i < s.length(); i++) {

                if ((cross[r - 1 + i][c - 1] == DEFAULT) || (cross[r - 1 + i][c - 1] == s.charAt(i))) {

                    b = true;

                }

                else {

                    b = false;

                    System.out.println("Unable to add " + s + ". Please try another word.");

                    return;}

            }

            if (b == true) {

                if ((s.length() <= rows) && (r + s.length() <= cols) &&

                        (cross[r - 1][c - 1] == s.charAt(0)) || (cross[r - 1][c - 1] == DEFAULT)) {

                    while (j < s.length()) {

                        cross[r - 1 + j][c - 1] = s.charAt(j);

                        if (j==0){
                            startword = true;
                        }

                        cross[rows - 1 - (r - 1 + j)][cols - 1 - (c - 1)] = '*';

                        j++;

                    }

                }

            }
        }

    }

    public void setNumberGrid(){
        numberGrid = new char [rows][cols];
        for (int i = 0; i < cross.length; i++){
            for (int j=0; j < cross[rows].length; j++){
                if (cross[i][j] == DEFAULT){
                    numberGrid[i][j] = (char) 0;
                }
                else if (startword == true){
                    numberGrid[i][j] = (char) -2;
                }
                else {
                    numberGrid[i][j] = (char) -1;
                }
            }
            int count = 1;
            for (i=0; i < cross.length; i++){
                for (int j=0; j < cross[rows].length; j++){
                    if (numberGrid[i][j] == -2){
                        numberGrid[i][j] = (char)count;
                        count++;
                    }
                }
            }
        }
    }

    public String printNumberGrid() {
        for (int i=0; i < cross.length; i++){
            for (int j=0; j < cross[rows].length; j++){
                if (numberGrid[i][j] == (char)-1){
                    numberGrid[i][j] = ' ';
                }
                else if (numberGrid[i][j] == (char)0){
                    numberGrid[i][j] = '#';
                }
            }
        }
        String d = new String();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++){
                d = d + numberGrid[i][j] + " ";
            }
            d = d + "\n";
        }
        return d;
    }



    public void onCreate(Bundle sis) {
        super.onCreate(sis);

        Crosswords g = new Crosswords();
        g.addWordv("rawr", 4, 5);
        g.addWordh("bot", 5, 4);
        g.addWordv("raw", 7, 5);
        g.addWordh("cat", 4, 5);
        g.addWordh("bass", 6, 10);
        System.out.println(g);

        Crosswords c = new Crosswords(20, 20);

        c.addWordh("HELLO", 1, 1);

        c.addWordv("HAPLOID", 1, 1);
        c.addWordh("COMPUTER", 3, 12);

        c.addWordv("CAT", 2, 11);

        c.addWordv("WOAH", 2, 20);
        c.addWordh("PARKING", 20, 5);

        c.addWordv("ARK", 17, 6);
        c.addWordh("AHOY", 6, 18);
        c.addWordv("AHOY", 18, 10);
        c.addWordv("ADVANTAGE", 2, 12);
        c.addWordv("INTERNAL", 2, 18);
        c.addWordh("BANTER", 7, 11);
        c.addWordv("BEAGLE", 5, 12);
        c.addWordh("BASE", 8, 3);
        c.addWordv("BALL", 8, 3);
        c.addWordh("LEFT", 10, 3);
        c.addWordv("SAFE", 8, 5);
        System.out.print(c);
    }
}

因为您的代码在初始化方面是错误的,所以以后真的会惹恼您。你必须记住,创建一个 java 项目是不够的,因为它永远不会在 Android 上运行。所以这里有一些提醒:

  • 像这样的类(不同的屏幕)扩展了 Activity 并使用 onCreate 来设置屏幕的内容
  • 通过创建更多的活动,如下所示,它将导致堆栈溢出错误(新类 -> oncreate -> 新类 -> onCreate -> 崩溃)
  • 自定义画布扩展了 SurfaceView(画布是您可以在上面绘制的东西)
  • 使用 Java 进行初始化的方法不适用于 android。您需要一个 Android Manifest 并声明活动、权限
  • 你需要一个 Android 项目和 Android SDK(我推荐 Android Studio)

此外,上面的代码还没有准备好部署,但它以初始化(有错误)为例。我强烈建议您学习一些Android 教程,因为 Android 与“常规”Java 非常不同。上面的代码是为了帮助您入门

于 2016-07-18T08:05:40.440 回答