Make a spot the difference app on Android
The specifications of the app introduced in this article are
--Integers from 0 to 7 are randomly placed in 4 places --Do not line up the same integers in the choices --If 0 is selected, true will be displayed in Log. --If you select anything other than 0, false will be displayed in Log. --If there is no 0 in the choices Select THERE IS NO ZERO and true will flow in the Log
It is like this ~~ Currently, it's just a visual acuity test ~~ If you change the number part to an image, it will be a spot the difference game.
public class MainActivity extends AppCompatActivity {
private final int TOTAL_QUESTION_COUNT = 8;
TextView mTextView;
Button[] mButtons = new Button[4];
Button mNoAnsButton;
int[] mQuestions = new int[4];
HashMap<String, String> mButtonIndexs = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Hi!", Toast.LENGTH_SHORT).show();
initViews();
storeIndexWithID();
mButtons[0].setText("PUSH");
setOnClicks();
}
private void initViews() {
mTextView = (TextView) findViewById(R.id.main_text);
mNoAnsButton = (Button) findViewById(R.id.no_answer_button);
mButtons[0] = (Button) findViewById(R.id.button1);
mButtons[1] = (Button) findViewById(R.id.button2);
mButtons[2] = (Button) findViewById(R.id.button3);
mButtons[3] = (Button) findViewById(R.id.button4);
}
private void storeIndexWithID() {
mButtonIndexs.put(String.valueOf(R.id.button1) , String.valueOf(0));
mButtonIndexs.put(String.valueOf(R.id.button2) , String.valueOf(1));
mButtonIndexs.put(String.valueOf(R.id.button3) , String.valueOf(2));
mButtonIndexs.put(String.valueOf(R.id.button4) , String.valueOf(3));
}
private void setOnClicks() {
for (int i = 0; i < mButtons.length; i++) {
final Button button = mButtons[i];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Ans", "check:" + checkAnswer(button));
updateQuestions();
updateButtons();
}
});
}
mNoAnsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Ans", "check (NoAns):" + !existsZero(mQuestions));
updateQuestions();
updateButtons();
}
});
}
private void updateButtons() {
for (int j = 0; j < mButtons.length; j++) {
mButtons[j].setText(String.valueOf(mQuestions[j]));
}
}
private void updateQuestions() {
mQuestions = getRandomArray();
setRandomAnswerOption(mQuestions);
}
private boolean existsZero(int[] array) {
boolean exist = false;
for (int number : array) if (number == 0) exist = true;
return exist;
}
private boolean checkAnswer(Button button) {
boolean isOk = false;
if (mQuestions[Integer.parseInt(mButtonIndexs.get(String.valueOf(button.getId())))] == 0) isOk = true;
return isOk;
}
private int[] getRandomArray() {
int[] randomArray = new int[4];
for (int i = 0; i < randomArray.length; i++) {
randomArray[i] = getRandomNumber(1, TOTAL_QUESTION_COUNT);
for (int j = 0; j < i; j++) {
if (randomArray[i] == randomArray[j])
i--; // If it is duplicated, create number again
}
}
return randomArray;
}
// min ~ max - 1
private int getRandomNumber(int min, int max) {
if (max < 1) return 0;
return new Random().nextInt(max - min) + min;
}
private void setRandomAnswerOption(int[] array) {
if (getRandomNumber(0, 100) < 50) {
array[getRandomNumber(0, array.length)] = 0;
}
}
}
(Update) 2017/03/07 The method name and variable name have been changed. Divided some processing.
Recommended Posts