单词搜索拼图生成器出界

时间:2014-02-06 12:04:09

标签: puzzle

在生成20 x 20的单词搜索难题时遇到问题。拼图是从单词列表和随机放置的单词生成的。问题是有些单词被放置在网格之外!如下所示:

Screenshot

我没有随机填充剩余的按钮,以便清楚地了解正在发生的事情。 以下是生成拼图的代码。

package wordsearch;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class PuzzleGenerator {

    Character[][] thePuzzle = null;
    int _rows;
    int _cols;

    ArrayList<String> _wordList;

    private Random random = new Random();

    public PuzzleGenerator(int Rows, int Cols, ArrayList<String> wordList) {
        thePuzzle = new Character[Rows][Cols];

        this._rows = Rows;
        this._cols = Cols;
        this._wordList = wordList;

        for (int i = 0; i < this._rows; i++) {
            for (int j = 0; j < this._cols; j++) {
                thePuzzle[i][j] = '-';
            }
        }

        addWords();
    }

    public Character[][] getPuzzle(){
        return thePuzzle;
    }

    private char getCharAt(int x, int y) {
        char result = this.thePuzzle[x][y];
        return result;
    }

    private int getRows(PuzzleGenerator aPuzzle) {
        return this._rows;
    }

    private int getCols(PuzzleGenerator aPuzzle) {
        return this._cols;
    }

    private void addWords() {

        while (_wordList.size() != 0) {
            //put the first word from the list into a string
            String aWord = (String) _wordList.get(0);
            //remove the word from the list
            _wordList.remove(0);
            int numTries = 0;

            while (numTries < _rows * _cols) {
                //Choose a random location on the puzzle to start placement of word
                int startRow = random.nextInt(_rows);
                int startCol = random.nextInt(_cols);

                if (this.move(aWord, startRow, startCol, -1, 0)) {
                    break;
                }
                if (this.move(aWord, startRow, startCol, -1, 1)) {
                    break;
                }
                if (this.move(aWord, startRow, startCol, 0, 1)) {
                    break;
                }
                if (this.move(aWord, startRow, startCol, 1, 1)) {
                    break;
                }
                if (this.move(aWord, startRow, startCol, 1, 0)) {
                    break;
                }
                if (this.move(aWord, startRow, startCol, 1, -1)) {
                    break;
                }
                if (this.move(aWord, startRow, startCol, 0, -1)) {
                    break;
                }
                if (this.move(aWord, startRow, startCol, -1, -1)) {
                    break;
                }

                if (++numTries == _rows * _cols) {
                    System.err.println(aWord);
                }
            }
        }
    }
    private boolean move(String theWord, int startRow, int startCol, int dx, int dy) {
        boolean result = true;
        //the initial char in question for placement
        int p = 0;
        int r = startRow;
        int c = startCol;

        while (p < theWord.length()) {
            if (this.thePuzzle[startRow][startCol] == '-' || this.thePuzzle[startRow][startCol] == theWord.charAt(p)) {
                //This is the wrapping logic
                startRow = ((startRow + dx + getRows(this)) % getRows(this));
                startCol = ((startCol + dy + getCols(this)) % getCols(this));

                if (r == startRow && c == startCol) {
                    result = false;
                    break;
                }

                p++;
            } else {
                result = false;
                break;
            }
        }

        if (result == true) {
            place(theWord, r, c, dx, dy);
        }
        return result;
    }
    private void place(String theWord, int startRow, int startCol, int dx, int dy) {
        //the initial char in question for placement
        int p = 0;

        while (p < theWord.length()) {

            //********COMMENTED OUT DUE TO HANGING ISSUES**************
            thePuzzle[startRow][startCol] = (char) theWord.charAt(p);

            //This is the wrapping logic
            startRow = ((startRow + dx + getRows(this)) % getRows(this));
            startCol = ((startCol + dy + getCols(this)) % getCols(this));
            p++;
        }
    }

    protected void fillPuzzle() {
        for (int i = 0; i < _rows; i++) {
            for (int j = 0; j < _cols; j++) {
                if (thePuzzle[i][j] == '-') {

                    //randomly generate a capital char (65-90) for fill
                    Character aChar;
                    do {
                        aChar = (char) (random.nextInt(90) + 65);
                    } while ((int) aChar < 65 || (int) aChar > 90);

                    thePuzzle[i][j] = aChar;
                }
            }
        }
    }

    protected void printPuzzle(String puzzleName) {
        BufferedWriter out2 = null;
        try {
            //now add the array to the file created above
            out2 = new BufferedWriter(new FileWriter(puzzleName + ".pz", true));
            for (int i = 0; i < getRows(this); i++) {
                for (int j = 0; j < getCols(this); j++) {
                    out2.write(thePuzzle[i][j] + " ");
                }
                out2.write("\n");
            }
            out2.close();
        } catch (IOException e) {
            e.getMessage();
        }
    }

    public void loadPuzzletoGrid() {
        //fillPuzzle();
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                WordSearchPuzzle.grid[i][j].setText((thePuzzle[i][j]).toString());
            }
        }

    }
}

1 个答案:

答案 0 :(得分:2)

这部分代码当前导致单词在到达网格边缘时换行。

// This is the wrapping logic
startRow = ((startRow + dx + getRows(this)) % getRows(this));
startCol = ((startCol + dy + getCols(this)) % getCols(this));

if (r == startRow && c == startCol) {
  result = false;
  break;
}

相反,您希望move()方法在到达边缘时失败,如下所示:

startRow = startRow + dx;
startCol = startCol + dy;

if (startRow < 0 || startRow >= getRows(this) || startCol < 0 || startCol >= getCols(this)) {
  $result = false;
  break;
}