0

私はある種の「邪悪なハングマン ゲーム」(気の利いたスタンフォード CS 演習) を作ろうとしています。このゲームの目的は、ユーザーが最後まで推測できないように、できるだけ多くの可能な単語の解決策を削除して「ごまかす」ことです。

可能性のある単語の多くを削除するように見えるループ(以下)を作成しましたが、何らかの理由でそれらのすべてを削除するわけではありません。入力は、約 120K の単語を含む Dictionary.txt ファイルです。

文字「a」を「推測」すると、「a」を含む単語の約60〜70%が取り除かれます(出力とtxtファイルの最初の数単語との比較に基づく推定)

File file = new File("dictionary.txt");
    Scanner textScan = new Scanner(file);

     List<String> wordList = new ArrayList<String>();

     while ( textScan.hasNext() )
        {
            word = textScan.next();
            wordList.add(word);

        }
     System.out.println("The ArrayList has " + wordList.size() + " objects stored in it.");


     Scanner textScan1 = new Scanner(file);

    for(int i = 0; i <= guessNumber; i++)

    {   
        Collections.sort(wordList);

        System.out.println("Type in your guess as a letter ");
        String guess = keyboard.next();
        guess = guess.toLowerCase();

     while ( textScan1.hasNext() )
    {
        String word1 = textScan1.next();
        if (wordLength != word1.length() && word1.contains(guess))
            {
            wordList.remove(word1);
            }


    }
    }

この時点で私のコードが少し乱雑であることは承知しています。プログラミングに関するすべてを改善しようとしているので、すべてのフィードバックは大歓迎です! そこにある必要のないものなどを含めているような気がします。

役立つ場合に備えて、以下のコード全体を投稿します。

import java.util.*;
import java.lang.*;
import java.io.*;




public class EvilHangman 

{


public static void main(String[] args) throws IOException
{
    // declaring variables
    int wordLength;
    int guessNumber;


    // initiate the scanner
    Scanner keyboard = new Scanner( System.in );




    // introduction and prompting the user for word length

    System.out.println("Welcome to Hangman. Let's play! ");
    System.out.println("Please enter the desired word length: ");
    wordLength = keyboard.nextInt();

    while(wordLength < 0 || wordLength > 26)
    {
        System.out.println("This is not a valid word length. ");
        System.out.println("Please enter the desired word length: ");
        wordLength = keyboard.nextInt();
    }

    // prompt the user for number of guesses 

    System.out.println("How many guesses do you want to have? ");
    guessNumber = keyboard.nextInt();

    while(guessNumber < 0)
    {
        System.out.println("Number of guesses has to be a postive integer. ");
        System.out.println("Please enter the desired number of guesses: ");
        guessNumber = keyboard.nextInt();
    }


    // count the number of words with the specified length

    /* int wordCount = 0;
    String word = null;
    while ( textScan.hasNext() )
    {
        word = textScan.next();
        if (word.length() == wordLength)
            {
            wordCount++;
            }


    }
    */


    // prompts the user whether he/she wants a running count of word length - using next() instead of nextLine() to clear buffer

    /* System.out.println("Do you want a running total of number of words remaining? ");
    String runningTotal = keyboard.next();

    if (runningTotal.equalsIgnoreCase("yes"))
        System.out.println("Words with that length: " + wordCount);
    */  

    // create a list (array) of all the words that matches the input length
    String word = null;


    File file = new File("dictionary.txt");
    Scanner textScan = new Scanner(file);

     List<String> wordList = new ArrayList<String>();

     while ( textScan.hasNext() )
        {
            word = textScan.next();
            wordList.add(word);

        }
     System.out.println("The ArrayList has " + wordList.size() + " objects stored in it.");


     Scanner textScan1 = new Scanner(file);

    for(int i = 0; i <= guessNumber; i++)

    {   
        Collections.sort(wordList);

        System.out.println("Type in your guess as a letter ");
        String guess = keyboard.next();
        guess = guess.toLowerCase();

     while ( textScan1.hasNext() )
    {
        String word1 = textScan1.next();
        if (wordLength != word1.length() && word1.contains(guess))
            {
            wordList.remove(word1);
            }


    }
    }

    System.out.println("The ArrayList has " + wordList.size() + " objects stored in it.");
    System.out.println(wordList);
4

1 に答える 1

0

最後に、それはスキャナーと関係があることがわかりました。ループ内で開始する必要がありました

for(int i = 1; i <= guessNumber; i++)

    {   
        Scanner textScan2 = new Scanner(file1);         
        System.out.println("Type in your guess as a letter ");
        String guess = keyboard.next();
        //System.out.print(guess);

     while ( textScan2.hasNext() )
    {

         String word1 = textScan2.next();
        if (wordLength != word1.length() || (word1.contains(guess)))
            {
            wordList.remove(word1);
            }





    }
    }
于 2013-03-22T03:37:30.097 に答える