dimanche 19 février 2017

Android Guessing Game [duplicate]

This question is an exact duplicate of:

I was wondering if anyone had any ideas about my android guessing game. Everything works fine except for the fact that on the third guess (the user can guess three times), as opposed to telling the user they got it right, it tells them they guessed incorrectly three times and that the number was 'X', which is the number they guessed. It should only do this when all three answers are wrong.

I'm not sure where to start in correcting it. Any Java geniuses out there who can spot what I need to change or add?

Thanks in anticipation

public class Task1Activity extends AppCompatActivity {

int attempts = 0;
final int maxAttempts = 1;
Random randGen = new Random();
int ranNum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.task1_layout);

    final TextView textResponse = (TextView) findViewById(R.id.txtResponse);
    final TextView guessText = (TextView) findViewById(R.id.txtAnswer);
    final EditText userGuess = (EditText) findViewById(R.id.etNumber);
    Button pressMe = (Button) findViewById(R.id.btnGuess);

    randGen = new Random();
    // Generate number once
    ranNum = randGen.nextInt(20);

    final Toast toast = Toast.makeText(getApplicationContext(), "Please guess between 0 and 20", Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 350);


    // When the button is clicked, it shows the text assigned to the txtResponse TextView box
    pressMe.setOnClickListener(new View.OnClickListener() {


   @Override
   public void onClick(View v) {
       final AlertDialog.Builder alert = new AlertDialog.Builder(Task1Activity.this);
       alert.setTitle("Unlucky");
       alert.setCancelable(false);
       alert.setMessage("You have guessed incorrectly three times. " +
               "The answer was " + ranNum + ". " + "Would you like to play again?")
               //.setCancelable(true)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       //dialog.dismiss();
                       Intent i = new Intent(Task1Activity.this, Task1Activity.class);
                       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(i);

                   }
               });

                alert
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int i) {
                        //Task1Activity.this.finish();
                       dialog.dismiss();
                       finishAffinity();
                   };
               });

       final AlertDialog.Builder alert2 = new AlertDialog.Builder(Task1Activity.this);
       alert2.setTitle("You Did It!");
       alert2.setCancelable(false);
       alert2.setMessage("The answer was " + ranNum + ". " + "Would you like to play again?")
               //.setCancelable(true)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       //dialog.dismiss();
                       Intent i = new Intent(Task1Activity.this, Task1Activity.class);
                       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(i);

                   }
               });

       alert2
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int i) {
                       //Task1Activity.this.finish();
                       dialog.dismiss();
                       finishAffinity();
                   };
               });



                       int userNumber = Integer.parseInt(userGuess.getText().toString());

                       if (userNumber > 19) {
                           //guessText.setText("Please guess between 0 and 20");
                           //guessText.setBackgroundColor(Color.WHITE);
                           toast.show();
                       } else if (userNumber < ranNum) {
                           guessText.setText("Your answer is too low. Guess again!");
                           guessText.setBackgroundColor(Color.YELLOW);
                       } else if (userNumber > ranNum) {
                           guessText.setText("Your answer is too high.  Guess again!");
                           guessText.setBackgroundColor(Color.RED);
                       } else if (userNumber == ranNum) {
                           ranNum = randGen.nextInt(20);
                           //guessText.setText("You did it!");
                           //guessText.setBackgroundColor(Color.WHITE);
                           alert2.show();
                       }

       if (attempts++ > maxAttempts) {
           alert.show();
           //guessText.setText("You have guessed incorrectly three times.  The answer was " + ranNum);
       } else {
           String randText = "";


                       randText = Integer.toString(ranNum);
                       textResponse.setText("");

                       userGuess.setText("");


                   }
               }
           }
    );

}
}

Aucun commentaire:

Enregistrer un commentaire