Отображение результатов для приложения викторины (Android Studio) - PullRequest
0 голосов
/ 17 февраля 2020

У меня есть приложение для викторины, которое работает правильно, но дело в том, что пользователь должен правильно ответить на все вопросы, чтобы выиграть игру (если игрок ошибается, игра будет окончена).

То, что я хотел сделать, - это получить ответы на вопросы, а затем в конце будет занятие, которое покажет, сколько игроков ответило, затем будут варианты повторить попытку и go вернуться в меню.

Это код игровой активности

public class MainGameActivity extends AppCompatActivity {
FButton buttonA, buttonB, buttonC, buttonD;
TextView questionText, triviaQuizText, timeText, resultText, coinText;
TriviaQuizHelper triviaQuizHelper;
TriviaQuestion currentQuestion;
List<TriviaQuestion> list;
int qid = 0;
int timeValue = 20;
int coinValue = 0;
CountDownTimer countDownTimer;
Typeface tb, sb;

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

    //Initializing variables
    questionText = (TextView) findViewById(R.id.triviaQuestion);
    buttonA = (FButton) findViewById(R.id.buttonA);
    buttonB = (FButton) findViewById(R.id.buttonB);
    buttonC = (FButton) findViewById(R.id.buttonC);
    buttonD = (FButton) findViewById(R.id.buttonD);
    triviaQuizText = (TextView) findViewById(R.id.triviaQuizText);
    timeText = (TextView) findViewById(R.id.timeText);
    resultText = (TextView) findViewById(R.id.resultText);
    coinText = (TextView) findViewById(R.id.coinText);

    //Setting typefaces for textview and buttons - this will give stylish fonts on textview and button etc
    tb = Typeface.createFromAsset(getAssets(), "fonts/TitilliumWeb-Bold.ttf");
    sb = Typeface.createFromAsset(getAssets(), "fonts/shablagooital.ttf");
    triviaQuizText.setTypeface(sb);
    questionText.setTypeface(tb);
    buttonA.setTypeface(tb);
    buttonB.setTypeface(tb);
    buttonC.setTypeface(tb);
    buttonD.setTypeface(tb);
    timeText.setTypeface(tb);
    resultText.setTypeface(sb);
    coinText.setTypeface(tb);

    //Our database helper class
    triviaQuizHelper = new TriviaQuizHelper(this);
    //Make db writable
    triviaQuizHelper.getWritableDatabase();

    //It will check if the ques,options are already added in table or not
    //If they are not added then the getAllOfTheQuestions() will return a list of size zero
    if (triviaQuizHelper.getAllOfTheQuestions().size() == 0) {
        //If not added then add the ques,options in table
        triviaQuizHelper.allQuestion();
    }

    //This will return us a list of data type TriviaQuestion
    list = triviaQuizHelper.getAllOfTheQuestions();

    //Now we gonna shuffle the elements of the list so that we will get questions randomly
    Collections.shuffle(list);

    //currentQuestion will hold the que, 4 option and ans for particular id
    currentQuestion = list.get(qid);

    //countDownTimer
    countDownTimer = new CountDownTimer(22000, 1000) {
        public void onTick(long millisUntilFinished) {

            //here you can have your logic to set text to timeText
            timeText.setText(String.valueOf(timeValue) + "\"");

            //With each iteration decrement the time by 1 sec
            timeValue -= 1;

            //This means the user is out of time so onFinished will called after this iteration
            if (timeValue == -1) {

                //Since user is out of time setText as time up
                resultText.setText(getString(R.string.timeup));

                //Since user is out of time he won't be able to click any buttons
                //therefore we will disable all four options buttons using this method
                disableButton();
            }
        }

        //Now user is out of time
        public void onFinish() {
            //We will navigate him to the time up activity using below method
            timeUp();
        }
    }.start();

    //This method will set the que and four options
    updateQueAndOptions();


}


public void updateQueAndOptions() {

    //This method will setText for que and options
    questionText.setText(currentQuestion.getQuestion());
    buttonA.setText(currentQuestion.getOptA());
    buttonB.setText(currentQuestion.getOptB());
    buttonC.setText(currentQuestion.getOptC());
    buttonD.setText(currentQuestion.getOptD());


    timeValue = 20;

    //Now since the user has ans correct just reset timer back for another que- by cancel and start
    countDownTimer.cancel();
    countDownTimer.start();

    //set the value of coin text
    coinText.setText(String.valueOf(coinValue));
    //Now since user has ans correct increment the coinvalue
    coinValue++;

}

//Onclick listener for first button
public void buttonA(View view) {
    //compare the option with the ans if yes then make button color green
    if (currentQuestion.getOptA().equals(currentQuestion.getAnswer())) {
        buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        //Check if user has not exceeds the que limit
        if (qid < list.size() - 1) {

            //Now disable all the option button since user ans is correct so
            //user won't be able to press another option button after pressing one button
            disableButton();

            //Show the dialog that ans is correct
            correctDialog();
        }
        //If user has exceeds the que limit just navigate him to GameWon activity
        else {

            gameWon();

        }
    }
    //User ans is wrong then just navigate him to the PlayAgain activity
    else {

        gameLostPlayAgain();

    }
}

//Onclick listener for sec button
public void buttonB(View view) {
    if (currentQuestion.getOptB().equals(currentQuestion.getAnswer())) {
        buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {
        gameLostPlayAgain();
    }
}

//Onclick listener for third button
public void buttonC(View view) {
    if (currentQuestion.getOptC().equals(currentQuestion.getAnswer())) {
        buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {

        gameLostPlayAgain();
    }
}

//Onclick listener for fourth button
public void buttonD(View view) {
    if (currentQuestion.getOptD().equals(currentQuestion.getAnswer())) {
        buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {
        gameLostPlayAgain();
    }
}

//This method will navigate from current activity to GameWon
public void gameWon() {
    Intent intent = new Intent(this, GameWon.class);
    startActivity(intent);
    finish();
}

//This method is called when user ans is wrong
//this method will navigate user to the activity PlayAgain
public void gameLostPlayAgain() {
    Intent intent = new Intent(this, PlayAgain.class);
    startActivity(intent);
    finish();
}

//This method is called when time is up
//this method will navigate user to the activity Time_Up
public void timeUp() {
    Intent intent = new Intent(this, Time_Up.class);
    startActivity(intent);
    finish();
}

//If user press home button and come in the game from memory then this
//method will continue the timer from the previous time it left
@Override
protected void onRestart() {
    super.onRestart();
    countDownTimer.start();
}

//When activity is destroyed then this will cancel the timer
@Override
protected void onStop() {
    super.onStop();
    countDownTimer.cancel();
}

//This will pause the time
@Override
protected void onPause() {
    super.onPause();
    countDownTimer.cancel();
}

//On BackPressed
@Override
public void onBackPressed() {
    Intent intent = new Intent(this, HomeScreen.class);
    startActivity(intent);
    finish();
}

//This dialog is show to the user after he ans correct
public void correctDialog() {
    final Dialog dialogCorrect = new Dialog(MainGameActivity.this);
    dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
    if (dialogCorrect.getWindow() != null) {
        ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
        dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
    }
    dialogCorrect.setContentView(R.layout.dialog_correct);
    dialogCorrect.setCancelable(false);
    dialogCorrect.show();

    //Since the dialog is show to user just pause the timer in background
    onPause();


    TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
    FButton buttonNext = (FButton) dialogCorrect.findViewById(R.id.dialogNext);

    //Setting type faces
    correctText.setTypeface(sb);
    buttonNext.setTypeface(sb);

    //OnCLick listener to go next que
    buttonNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //This will dismiss the dialog
            dialogCorrect.dismiss();
            //it will increment the question number
            qid++;
            //get the que and 4 option and store in the currentQuestion
            currentQuestion = list.get(qid);
            //Now this method will set the new que and 4 options
            updateQueAndOptions();
            //reset the color of buttons back to white
            resetColor();
            //Enable button - remember we had disable them when user ans was correct in there particular button methods
            enableButton();
        }
    });
}


//This method will make button color white again since our one button color was turned green
public void resetColor() {
    buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
}

//This method will disable all the option button
public void disableButton() {
    buttonA.setEnabled(false);
    buttonB.setEnabled(false);
    buttonC.setEnabled(false);
    buttonD.setEnabled(false);
}

//This method will all enable the option buttons
public void enableButton() {
    buttonA.setEnabled(true);
    buttonB.setEnabled(true);
    buttonC.setEnabled(true);
    buttonD.setEnabled(true);
    }


}

1 Ответ

0 голосов
/ 17 февраля 2020

Отредактировано

Просто удалите оболочку if else внутри всех кнопок, чтобы сохранить ее, не повторяя код. Я предполагаю, что экран, который показывает результат, обрабатывается внутри gameWon, и вы реализовали функциональность для inCorrectDialog

public void buttonA(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}
public void buttonB(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}

public void buttonC(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}

public void buttonD(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}

public void buttonPressed(Button button) {

 button.setButtonColor(ContextCompat.getColor(getApplicationContext(), R.color.lightGreen));
 if (qid < list.size() - 1) {
  disableButton();
  if (currentQuestion.getOptA().equals(currentQuestion.getAnswer())) {
   correctDialog();
  } else {
   inCorrectDialog();
  }

 } else {
  gameWon();
 }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...