Java密码游戏强调

时间:2018-03-22 13:24:31

标签: java

我正在尝试编写一个密码游戏。用户必须猜测报价。我以下划线的形式打印报价。例如,如果引用是"是或不是"我将打印" _ _ _ _ _ _ _ _ _ _ "。用户将输入要放置角色的角色和位置。例如,如果玩家输入了字母" r"在位置0,我们将得到以下输出" r _ _ _ _ _ _ r _ _"。 我的问题是,每次我要求用户猜一个字母时,都不显示播放器的先前猜测。我的代码如下:

public  class Cryptogram 
{ //get letter and delete letter methods? 
    //Where do we generate the crypto mapping

     String phrase;
     Integer freq;
    // ArrayList<Character> guess; 
     //char guess[];

    /**
     * read the file
     * return a random line 
     * @throws IOException 
     * */

    public Cryptogram ()
    {
        try 
        {
            phrase = readCryptogram();
            //guess = new ArrayList<Character>();   
            //char guess[] = new char [phrase.length()];

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        freq = 0;

    }

    public String readCryptogram() throws IOException 
    {
        FileInputStream fs= new FileInputStream("QUOTES.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fs));
        ArrayList<String> array = new ArrayList<>();
        String line;

        while((line = br.readLine()) != null)
              array.add(line);
        Random rand = new Random();
        int randomIndex = rand.nextInt(array.size());
        //int smth = array.size();
        //guess = new ArrayList<Character>(array.size());
        return array.get(randomIndex);                                                            
      }
        public void updateUnderscores(int posit, char lettr)
        {
            char guess[] = new char [phrase.length()];
//          for (int k=0; k<phrase.length(); k++)
//          {
//              guess[k] = 0;
//          }

             //guess = new char [phrase.length()];
            //System.out.print(guess.length);
            char  quote[] = phrase.toCharArray();
            for (int c = 0; c<quote.length; c++)
            {
                quote[c] = phrase.charAt(c);
            }

            char let = quote[posit];    
            for (int i=0; i<quote.length; i++)
            {
                if (Character.isAlphabetic(quote[i]))
                {
                    if (let==quote[i])
                    {
                        //System.out.print(lettr);
                        guess[i] = lettr;
                        //guess.add(i, lettr);
                        //System.out.println(guess);


                    }

                    else System.out.print("_  ");   
                    //guess[i] = ' _';

                }
                else System.out.print("  ");
            }



            //copyGuessedCr(guess);
            /*System.out.print("Guess arr: " );         
            for (int j=0; j<phrase.length(); j++)
            {
                System.out.print(guess[j]);
            }*/

       }

        /*public char[] copyGuessedCr(char guess[])
        {
            char gs[] = new char [phrase.length()];
            System.out.print("Guess arr: " );           
            for (int j=0; j<phrase.length(); j++)
            {
                if (guess[j]!=0)
                gs[j] = guess[j];
            }
            return gs;
        }

        public void printGuess()
        {
            char g[] = ;
            System.out.print("Guess arr: " );           
            for (int j=0; j<phrase.length(); j++)
            {
                System.out.print();
            }
        }*/

        public String getCryptogram()
        {

            return phrase;          
        }

        public void getFrequencies()
        {
            String qt = phrase;
            int cols = qt.length();
            int rows = 2;
            Object [][] arr =  new Object [cols][rows];


            qt=qt.toLowerCase(); //converting the string into lowercase
            int l=qt.length(); //finding the length of the string           
            char ch;

            /* Counting frequency of alphabets begins below */
            int count=0;
            for(char i='a'; i<='z'; i++)
                {
                    count = 0;
                    for(int j=0; j<l; j++)
                    {
                        ch=qt.charAt(j); //extracting characters of the string one by one
                        if(ch==i) //first checking the whole string for 'a', then 'b' and so on
                          count++; //increasing count of those aplhabets which are present in the string
                          //System.out.println(i+"\t\t"+count);
                    }
//                  if(count!=0)//printing only those alphabets whose count is not '0'
//                  {
//                      //System.out.println(i+"\t\t"+count);
//                      
//                  }
                    for (int k=0; k<l; k++)
                    {
                        if (qt.charAt(k) == i)
                        {

                            arr[k][1] = count; 

                        }
                    }                   
                }


            for (int i=0; i<cols; i++)
            {

                if (Character.isAlphabetic(qt.charAt(i)))
                {
                    arr[i][1] = "_ ";
                }
                else arr[i][1] = "   ";
            }

            for (int i=0; i<rows; i++)
            {
                for (int j=0; j<cols; j++)
                {
                    System.out.print(arr[j][i]);

                }
                System.out.println("\n");               
            }
        }

public class Main   
{   
    public static void main(String[] args) throws IOException   
    {
        Game gObj = new Game();
        players pl = new players();
        Cryptogram crObj = new Cryptogram();
        Scanner scanner = new Scanner(System.in);



        System.out.println("Type your name: " + "\n");
        String name = scanner.next();

        pl.findPlayer(name);


        System.out.println("Do you want to play? Y / N ");

        char ans = scanner.next().charAt(0);


        if (ans == 'Y')
        {           
            System.out.println("Cryptogram is out: "+crObj.getCryptogram());
            crObj.getFrequencies();

        }
        char letter;
        int position = -2;
        while (position != -1)
        {
            System.out.println("\n" + "Choose a position to fill a letter in: ");
            position = scanner.nextInt();
            System.out.println("Position " + position);

            System.out.println("\n" + "Choose a letter: ");
            letter = scanner.next().charAt(0);
            crObj.updateUnderscores(position, letter);


        }   

        scanner.close();

    }


}

1 个答案:

答案 0 :(得分:0)

当我做一个刽子手游戏时,我遇到了类似的问题。

我解决它的方法是使用一个单独的列表,其长度由所选单词或您的案例短语决定。然后列表中只填充“_”并在每次猜测后显示。

当用户猜测时,它会通过一个循环检查它,对着char数组中单词的每个字符进行检查。找到匹配后,修改了列表,并根据循环的迭代次数将“_”替换为正确的字符。

我无法记住我使用的确切代码,我目前正在工作但是当我回到家时会检查和修改。

我还将之前错误的猜测存储在每次猜测后添加的列表中。

希望这有帮助,我仍然有很多在编程中学习,所以如果解释有点令人困惑或者我使用了错误的短语,我会道歉。