第二场比赛后,琐事游戏的得分变得不准确

时间:2013-03-08 22:43:24

标签: java android algorithm

我有一个琐事游戏是10个定时问题。一个错误的答案应该输出0分。正确答案应该输出转换为100分制的20,000毫秒的剩余时间。

现在游戏适用于第一款游戏。然后,如果用户继续第二次播放,则每个问题的分数变得不正确。例如,不正确的答案开始具有从1到100的各种点,并且正确的答案有时会有0分,有时也会出现错误的总分。所以我猜测一些变量可能没有在第二场比赛中重置,所以它导致积分稍微偏离。

我发布了所有处理算法的代码。我的问题是,无论用户在关闭应用程序之前玩了多少次,我都可以让这段代码输出正确的答案。

QuestionView.java

public class QuestionView extends Activity  {

    int correctAnswers = 0;
    int wrongAnswers = 0;
    int answer = 0;
    int i = 0;

    long score = 0;

    long startTime = 20000;
    long interval = 1000;
    long points;

    boolean timerHasStarted = false;

    String category;

    Button answer1, answer2, answer3, answer4;
    TextView question, pointCounter, questionNumber, timeCounter, timeremaining;
    AdView adView;

    ArrayList<Question> queries;
    public static ArrayList<Long> pointsPerQuestion = new ArrayList<Long>(10);
    Timer cdTimer;

    ProgressBar bar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.questionview);

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);
        answer3 = (Button)findViewById(R.id.answer3);
        answer4 = (Button)findViewById(R.id.answer4);

        question = (TextView)findViewById(R.id.question);

        category = getIntent().getStringExtra("category");
        queries = getIntent().getParcelableArrayListExtra("queries");

        questionNumber = (TextView)findViewById(R.id.questionnumber);
        timeremaining = (TextView)findViewById(R.id.timeremaining);

        cdTimer = new Timer(startTime, interval);

        bar = (ProgressBar)findViewById(R.id.progressbar);
        bar.setIndeterminate(false);
        bar.setMax(20000);

        tracker.sendView("/QuestionView - Started Quiz");

        loadQuestion();
    }

    public void loadQuestion() {

        if(i == 10) {

            cdTimer.cancel();
            endQuiz();

        } else {

            if(!timerHasStarted) {
                cdTimer.start();
                timerHasStarted = true;
            } else {
                cdTimer.start();
                timerHasStarted = false;
            }

            answer = queries.get(i).getCorrectAnswer();

            question.setText(queries.get(i).getQuery());

            answer1.setText(queries.get(i).getA1());
            answer2.setText(queries.get(i).getA2());
            answer3.setText(queries.get(i).getA3());
            answer4.setText(queries.get(i).getA4());

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(0);
                    if(answer == 0) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });

            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(1);
                    if(answer == 1) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });

            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(2);
                    if(answer == 2) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });

            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(3);
                    if(answer == 3) {
                        correctAnswers++;
                        correct();
                    } else {
                        wrongAnswers++;
                        incorrect();
                    }
                }
            });
        } 
    }

    public ArrayList<Question> getQueries() {
        return queries;
    }

    public static ArrayList<Long> getPointsPerQuestion() {
        return pointsPerQuestion;
    }

    public void correct() {
        pointsPerQuestion.add(points); 
        score = score + points;
        i++;
        loadQuestion();
    }

    public void incorrect() {
        long zero = 0;
        pointsPerQuestion.add(zero);
        i++;
        loadQuestion();
    }

    public class Timer extends CountDownTimer {

        public Timer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            points = 0;
            if(i >= 9) {
                cdTimer.cancel();
                pointsPerQuestion.add(points);
                endQuiz();
            } else {
                wrongAnswers++;
                incorrect();
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {
            bar.setProgress((int) millisUntilFinished);
            points = millisUntilFinished / 200;
            timeremaining.setText("Score remaining: " + points);
            if(i < 10) {
                questionNumber.setText("Question " + (i + 1) + " of 10");
            }
        }
    }

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtra("correctAnswers", correctAnswers);
        intent.putExtra("wrongAnswers", wrongAnswers);
        intent.putExtra("score", score);
        intent.putExtra("pointsPerQuestion", pointsPerQuestion);
        intent.putParcelableArrayListExtra("queries", queries);
        intent.putExtra("category", category);
        startActivity(intent);
    }
}

这是一个Question对象:

exodus.add(new Question("6th", "3rd", "5th", "4th", 2, "Name that plague:  All livestock dies", -1, "Exodus 9:6"));

在我的Results课程中,显示​​每个问题的分数,这是显示分数的行:

scoreTV.setText("You received " + QuestionView.getPointsPerQuestion().get(x) + " out of a possible 100 points.");

其他所有内容都在我QuestionView之上。如果您还需要别的东西,请告诉我。

前3个字符串是答案A(索引0),B(索引1),C(索引2)和D(索引3)。第一个int是正确的答案。第五个字符串是问题。第二个int设置为用户在测验期间选择的答案。最后一个字符串是找到答案的圣经经文。

0 个答案:

没有答案