Java填字游戏:有什么方法可以计算打印出的单词前后的空格?

时间:2019-02-17 12:07:12

标签: java puzzle crossword

Words:测试文件

我正在为我的学校设置程序,但遇到了麻烦。您能帮我找到一种方法来在打印出的单词前后打印空格吗?

public class Main {

    public static void main(String[] args) {
        System.out.println("crossword generator ver. 1.0");
        File wordlist = new File("words.txt");
        try {
            Scanner s = new Scanner(wordlist);
            String words[] = new String[1000000];
            int lineNr = 0;
            while (s.hasNext() && lineNr < 1000000) {
                words[lineNr] = s.nextLine();
                lineNr++;
            }
            System.out.println("Wordlist succesfully loaded");
            Random r = new Random();
            String solution = words[r.nextInt(lineNr)];
            System.out.println("Solution = " + solution);
            for (int i = 0; i<solution.length(); i++){
                char c = solution.charAt(i);
                String word;
                do{
                    word = words[r.nextInt(lineNr)];                    
                } while(word.indexOf(c) == -1);
                System.out.printf("(%c): %s \n", c ,word);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:1)

您已经有了其中的关键要素:indexOf()

创建空间数量有些棘手:创建与indexOf相同的数量确实与我们需要的相反。首先,我们必须计算最高的indexOf,以便我们可以在每个单词的前面减去在当前单词的indexOf内创建那个的空格。

我们必须记住单词,因为我们经历了整个循环两次。

下面的解决方案有点脏-更好的方法是为随机单词的实例(带有小写的版本和indexOf)创建一个新类,这也可以容纳 List 有效的indexOf位置,这样您就不必总是使用字符的第一次出现。

这只是成为前进道路上的垫脚石。还有很多事情要做,例如您可以决定只使用小写单词,然后在最终输出中将“热”字符变成大写。

此代码忽略大写/小写,因此,如果您的解决方案词以大写字母开头,则不会被锁定在某些随机词中。这里实现的方式也很脏。

加载列表,顺便说一句,可以大大简化,如下所示。这样也可以避免不必要的大单词列表数组(否则有时可能太小)。

public static void main(String[] args) {

    System.out.println("\ncrossword generator ver. 1.0");

    // Load word list.
    final List<String> wordList;
    try {
        final File wordListFile = new File("words.txt");
        wordList = Files.readAllLines(wordListFile.toPath(), StandardCharsets.UTF_8);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    System.out.println("\nWord list successfully loaded.");


    // Pick solution word.
    final int wordCount = wordList.size();
    final Random rand = new Random();
    final String solution = wordList.get(rand.nextInt(wordCount));
    final String solutionLC = solution.toLowerCase(); // So that we won't depend on upper/lower case.
    final int solutionLen = solution.length();
    System.out.println("\nSolution = " + solution + "\n");


    // Choose words whose characters are in the solution word.
    final String[] chosenWords = new String[solutionLen];
    int highestIndex = 0;
    for (int i = 0; i < solutionLen; i++) {
        final char c = solutionLC.charAt(i);
        String word;
        int indexOfChar;
        do {
            word = wordList.get(rand.nextInt(wordCount));
            indexOfChar = word.toLowerCase().indexOf(c);
        } while (indexOfChar < 0);
        chosenWords[i] = word;
        highestIndex = Math.max(highestIndex, indexOfChar);
    }


    // Print crossword excerpt.
    for (int i = 0; i < solutionLen; i++) {
        final char cLC = solutionLC.charAt(i);
        final char c = solution.charAt(i);
        final int indexOfChar = chosenWords[i].toLowerCase().indexOf(cLC);
        System.out.println("(" + c + "): " + createStringOfIdenticalCharacters(highestIndex - indexOfChar,
                                                                               ' ') + chosenWords[i]);
    }

}


public static String createStringOfIdenticalCharacters(final int count,
                                                       final char c) {
    final char[] retPreliminary = new char[count];
    Arrays.fill(retPreliminary, c);
    return new String(retPreliminary);
}

示例输出:

crossword generator ver. 1.0

Word list successfully loaded.

Solution = councilor

(c):            Corcyra
(o):        Harbour
(u):      nonillustrative
(n):           unexiled
(c):       sepulchering
(i):        Torrington
(l):         builtin
(o):           nonnarcissistic
(r): Balsamodendron