1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| package com.ponroy.florian.topquiz.controller;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.ponroy.florian.topquiz.R;
import com.ponroy.florian.topquiz.model.Question;
import com.ponroy.florian.topquiz.model.QuestionBank;
import java.util.Arrays;
public class GameActivity extends AppCompatActivity implements View.OnClickListener {
private Image mImageView;
private TextView mQuestionTextView;
private Button mAnswerButton1;
private Button mAnswerButton2;
private Button mAnswerButton3;
private Button mAnswerButton4;
private QuestionBank mQuestionBank;
private Question mCurrentQuestion;
private int mScore;
private int mNumberOfQuestions;
public static final String BUNDLE_EXTRA_SCORE = "BUNDLE_EXTRA_SCORE";
public static final String BUNDLE_STATE_SCORE = "currentScore";
public static final String BUNDLE_STATE_QUESTION = "currentQuestion";
private boolean mEnableTouchEvents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
System.out.println("GameActivity::onCreate()");
mQuestionBank = this.generateQuestions();
if (savedInstanceState != null) {
mScore = savedInstanceState.getInt(BUNDLE_STATE_SCORE);
mNumberOfQuestions = savedInstanceState.getInt(BUNDLE_STATE_QUESTION);
} else {
mScore = 0;
mNumberOfQuestions = 10;
}
mEnableTouchEvents = true;
// Wire widgets
mImageView = (image) findViewById(@+id/activity_game_question_img);
mQuestionTextView = (TextView) findViewById(R.id.activity_game_question_text);
mAnswerButton1 = (Button) findViewById(R.id.activity_game_answer1_btn);
mAnswerButton2 = (Button) findViewById(R.id.activity_game_answer2_btn);
mAnswerButton3 = (Button) findViewById(R.id.activity_game_answer3_btn);
mAnswerButton4 = (Button) findViewById(R.id.activity_game_answer4_btn);
// Use the tag property to 'name' the buttons
mAnswerButton1.setTag(0);
mAnswerButton2.setTag(1);
mAnswerButton3.setTag(2);
mAnswerButton4.setTag(3);
mAnswerButton1.setOnClickListener(this);
mAnswerButton2.setOnClickListener(this);
mAnswerButton3.setOnClickListener(this);
mAnswerButton4.setOnClickListener(this);
mCurrentQuestion = mQuestionBank.getQuestion();
this.displayQuestion(mCurrentQuestion);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(BUNDLE_STATE_SCORE, mScore);
outState.putInt(BUNDLE_STATE_QUESTION, mNumberOfQuestions);
super.onSaveInstanceState(outState);
}
@Override
public void onClick(View v) {
int responseIndex = (int) v.getTag();
if (responseIndex == mCurrentQuestion.getAnswerIndex()) {
// Good answer
Toast.makeText(this, "Bonne réponse", Toast.LENGTH_SHORT).show();
mScore=mscore + 5;
} else {
// Wrong answer
Toast.makeText(this, "Mauvaise réponse!", Toast.LENGTH_SHORT).show();
mScore--;
}
mEnableTouchEvents = false;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mEnableTouchEvents = true;
// If this is the last question, ends the game.
// Else, display the next question.
if (--mNumberOfQuestions == 0) {
// End the game
endGame();
} else {
mCurrentQuestion = mQuestionBank.getQuestion();
displayQuestion(mCurrentQuestion);
}
}
}, 2000); // LENGTH_SHORT is usually 2 second long
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return mEnableTouchEvents && super.dispatchTouchEvent(ev);
}
private void endGame() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Bien joué!")
.setMessage("Ton score est" + mScore)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// End the activity
Intent intent = new Intent();
intent.putExtra(BUNDLE_EXTRA_SCORE, mScore);
setResult(RESULT_OK, intent);
finish();
}
})
.setCancelable(false)
.create()
.show();
}
private void displayQuestion(final Question question) {
mImageView.setImage(question.getQuestion());
mQuestionTextView.setText(question.getQuestion());
mAnswerButton1.setText(question.getChoiceList().get(0));
mAnswerButton2.setText(question.getChoiceList().get(1));
mAnswerButton3.setText(question.getChoiceList().get(2));
mAnswerButton4.setText(question.getChoiceList().get(3));
}
private QuestionBank generateQuestions() {
Question question1 = new Question("scr="@drawable/image_question1", "Quel est le nom du président français actuel?",
Arrays.asList("François Hollande", "Emmanuel Macron", "Jacques Chirac", "François Mitterand"),
1);
Question question2 = new Question("Combien de pays y a-t-il dans l'Union européenne?",
Arrays.asList("15", "24", "28", "32"),
2);
Question question3 = new Question("Qui est le créateur du système d'exploitation Android?",
Arrays.asList("Andy Rubin", "Steve Wozniak", "Jake Wharton", "Paul Smith"),
0);
return new QuestionBank(Arrays.asList(question1,
question2,
question3));
}
@Override
protected void onStart() {
super.onStart();
System.out.println("GameActivity::onStart()");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("GameActivity::onResume()");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("GameActivity::onPause()");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("GameActivity::onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("GameActivity::onDestroy()");
}
} |
Partager