dimanche 27 septembre 2015

Using a loop, and conditionals to change score in a game

I used a loop to generate 1 numbers onto the text file. I start to have problems on when the user starts to play the game.

I'm going to go right ahead and give my pseudo code so you can understand what I want to do...

  1. Randomly generate 10 numbers between 0 and 10
  2. Store these numbers into a file called mysteryNumbers.txt, a number per line
  3. Prompt the user to enter a number between 0 and 10
  4. Check this number against the first number in the file
  5. If the user guesses right, subtract 10 from the score (at the start, the score is 0)
  6. If the user guesses wrong, add the correct amount of score to the current score of the user (at the start, the score is 0)
  7. Prompt the user to enter a number between 0 and 10
  8. Check this number against the second number in the file
  9. If the user guesses right, subtract 10 from the score
  10. If the user guesses wrong, add the correct amount of score to the current score of the user
  11. Prompt the user to enter a number between 0 and 10
  12. Check this number against the third number in the file
  13. If the user guesses right, subtract 10 from the score
  14. If the user guesses wrong, add the correct amount of score to the current score of the user
  15. … etc. …
  16. Display the score of the user: of course, the lower the better!

-I also organized the code below following the pseudocode outline.

    /************************************************************************************
    PrintWriter mywriter = new PrintWriter("mysteryNumber.txt");
    int randomNumber;
    int i = 0;
    i=1;
    while (i<=10) {
    Random randomGen = new Random();
        randomNumber= randomGen.nextInt(11);
    // then store (write) it in the file
    mywriter.println(randomNumber);
        i = i + 1;
    }
    //numbers are now generated onto text file...   

    /************************************************************************************
     * 3.   Prompt the user to enter a number between 0 and 10                          */
    Scanner myscnr= new Scanner (System.in);

    System.out.print ("Please enter a number between 0 and 10: ");

    int userNumber= myscnr.nextInt();

    /************************************************************************************
     * 4.   Check this number against the first number in the file                      */
    // to check the user's number against the file's, you need to be able to read
    // the file. 
    FileReader fr = new FileReader("./mysteryNumber.txt");
    BufferedReader textReader = new BufferedReader(fr);
    int numberInFile;
    // the number in your file is as follows:
    numberInFile = Integer.valueOf(textReader.readLine());

    /************************************************************************************
     * 5.   If the user guesses right, subtract 10 from the score                       */
    /************************************************************************************
     * 6.   If the user guesses wrong, add the correct amount of score to the current   *
     * score of the user (at the start, the score is 0                                  */  


    /************************************************************************************
     * 7.   Prompt the user to enter a number between 0 and 10                          */
    //Sytem.out.println ("Enter a number between 0");

    /************************************************************************************
     * 8.   Check this number against the second number in the file                     */

    etc etc...

I'm confused starting on comment section 5. I know I have to create a new integer for score, but after that I'm lost! I tried working in an if else statemnent but I couldn't get it going..




Aucun commentaire:

Enregistrer un commentaire