Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jabriko/6c423e52db8182407aa3f167733772d0 to your computer and use it in GitHub Desktop.
Save Jabriko/6c423e52db8182407aa3f167733772d0 to your computer and use it in GitHub Desktop.
MainActivity.java
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button answer1, answer2, answer3, answer4;
TextView score, question;
private Questions mQuestions = new Questions();
private String mAnswer;
private int mScore = 0;
private int mQuestionsLenght = mQuestions.mQuestions.length;
Random r = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLenght));
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer1.getText().equals(mAnswer)) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLenght));
} else {
gameOver();
}
}
});
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer2.getText().equals(mAnswer)) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLenght));
} else {
gameOver();
}
}
});
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer3.getText().equals(mAnswer)) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLenght));
} else {
gameOver();
}
}
});
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer4.getText().equals(mAnswer)) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLenght));
} else {
gameOver();
}
}
});
}
private void updateQuestion(int num) {
question.setText(mQuestions.getQuestion(num));
answer1.setText(mQuestions.getChoice1(num));
answer2.setText(mQuestions.getChoice2(num));
answer3.setText(mQuestions.getChoice3(num));
answer4.setText(mQuestions.getChoice4(num));
mAnswer = mQuestions.getCorrectAnswer(num);
}
private void gameOver(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setMessage("You failed! Score is : " + mScore + " points.")
.setCancelable(false)
.setPositiveButton("Start over!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
})
.setNegativeButton("Exit!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment