Bad points counting

Put your problem here if it does not fit any of the other categories.

Bad points counting

Postby Nabukodonosor » Sun Mar 10, 2013 1:58 am

OK, here's my whole game class. The thing is...I have a quiz activity with 4 possible answers. When a user answers correct it pops up theme.dialog activity that says "correct", and if the answer is wrong other activity says "it wrong, correct answer is:...". Either way it adds point (negative points if the answer is correct and after 5 negatives the games is finished) and load next question. Questions are time limited to 15 seconds. But what if a user didn't answer the question? To solve that I placed the same routine in my onFinish() method of MyCount class. But, there's my problem...after each passed question, at first I get 2 negative points insted one, after second passed question, I get 6 negative points and the two popups and two sounds (I have a sound too), after 3rd 14 negative points and god knows how many popups and after that my game crashes. If I delete counter (brojacPogresnihOdgovora++;) from my onFinish() method, I get none of negative points. Without this, the points counting works like a charm (if you click every question and don't pass any). I can't figure it out. How to get one negative point when I'm wrong and one negative if I pass the question, and not both if I pass?

Anyway, here's my class:

Code: Select all
public class NeogranicenoPetGresaka extends Activity implements OnClickListener{

    MyCount brojacVremena = new MyCount(6000, 1000);

LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();

    private String generateWhereClause(){
        StringBuilder result = new StringBuilder();
        for (Long l : mAnsweredQuestions){
            result.append(" AND _ID <> " + l);
        }
        return result.toString();
    }

    Button bIzlazIzKviza, bOdgovor1, bOdgovor2, bOdgovor3, bOdgovor4;
    TextView question, netacniOdg, score, countdown;
    int counter = 0;
    int brojacPogresnihOdgovora = 0;
    int brojacTacnihOdgovora = 0;
    public static String tacanOdg;

    Runnable mLaunchTask = new Runnable() {
        public void run() {
            nextQuestion();
            brojacVremena.start();
        }
     };
    Runnable mLaunchTaskFinish = new Runnable() {
         public void run() {
            brojacVremena.cancel();
            finish();
         }
      };


    private class Answer {
        public Answer(String opt, boolean correct) {
            option = opt;
            isCorrect = correct;
        }

        String option;
        boolean isCorrect;
    }
    Handler mHandler = new Handler();

    final OnClickListener clickListener = new OnClickListener() {
        public void onClick(View v) {

            Answer ans = (Answer) v.getTag();
            if (ans.isCorrect) {
                brojacVremena.cancel();
                brojacTacnihOdgovora = brojacTacnihOdgovora + 5;
                Intent i = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
                startActivity(i);
                mHandler.postDelayed(mLaunchTask,1200);
            }
        else{
            brojacVremena.cancel();
            brojacPogresnihOdgovora++;
            Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
            i.putExtra("tacanOdgovor", tacanOdg);
            startActivity(i);
            mHandler.postDelayed(mLaunchTask,2200);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.neograniceno);

        Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
        Typeface pitanje = Typeface.createFromAsset(getAssets(), "myriad.ttf");
        bIzlazIzKviza = (Button) findViewById(R.id.bIzlazIzKvizaN);
        netacniOdg = (TextView) findViewById(R.id.tvBrojPitanjaN);
        question = (TextView) findViewById(R.id.tvPitanjeN);
        bOdgovor1 = (Button) findViewById(R.id.bOdgovorN1);
        bOdgovor2 = (Button) findViewById(R.id.bOdgovorN2);
        bOdgovor3 = (Button) findViewById(R.id.bOdgovorN3);
        bOdgovor4 = (Button) findViewById(R.id.bOdgovorN4);
        score = (TextView) findViewById(R.id.tvSkor2N);
        countdown = (TextView) findViewById(R.id.tvCountdownN);
        bOdgovor1.setTypeface(dugmad);
        bOdgovor2.setTypeface(dugmad);
        bOdgovor3.setTypeface(dugmad);
        bOdgovor4.setTypeface(dugmad);
        bIzlazIzKviza.setTypeface(dugmad);
        netacniOdg.setTypeface(dugmad);
        question.setTypeface(pitanje);
        score.setTypeface(dugmad);
        countdown.setTypeface(dugmad);

        nextQuestion(); //startuje prvo pitanje!
        brojacVremena.start();  //startuje brojac vremena
    }


    public class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {

            Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
            i.putExtra("tacanOdgovor", tacanOdg);
            startActivity(i);
            mHandler.postDelayed(mLaunchTask,2200);

            brojacPogresnihOdgovora++;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            countdown.setText("" + millisUntilFinished / 1000);
        }
    }

        public void onClick(View v) {
        // TODO Auto-generated method stub


    }
    @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
        }

    public void nextQuestion() {

        brojacVremena.start();

        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();

        try{    //Pokusava da otvori db

            mDbHelper.open();  //baza otvorena

            Cursor c = mDbHelper.getTestData(generateWhereClause());
            mAnsweredQuestions.add(c.getLong(0));

            List<Answer> labels = new ArrayList<Answer>();

            labels.add(new Answer(c.getString(2), true));
            labels.add(new Answer(c.getString(3), false));
            labels.add(new Answer(c.getString(4), false));
            labels.add(new Answer(c.getString(5), false));

            Collections.shuffle(labels);

            tacanOdg = c.getString(2);

            if(brojacPogresnihOdgovora < 20){


        question.setText(c.getString(1));

        bOdgovor1.setText(labels.get(0).option);
        bOdgovor1.setTag(labels.get(0));
        bOdgovor1.setOnClickListener(clickListener);

        bOdgovor2.setText(labels.get(1).option);
        bOdgovor2.setTag(labels.get(1));
        bOdgovor2.setOnClickListener(clickListener);

        bOdgovor3.setText(labels.get(2).option);
        bOdgovor3.setTag(labels.get(2));
        bOdgovor3.setOnClickListener(clickListener);

        bOdgovor4.setText(labels.get(3).option);
        bOdgovor4.setTag(labels.get(3));
        bOdgovor4.setOnClickListener(clickListener);

        netacniOdg.setText("" + brojacPogresnihOdgovora);
        score.setText("Score: " + brojacTacnihOdgovora);
            }
            else{
                brojacVremena.cancel();
                Intent i = new Intent(getApplicationContext(), Rezultat.class);
                i.putExtra("noviRezultat", brojacTacnihOdgovora);
                startActivity(i);

                mHandler.postDelayed(mLaunchTaskFinish,4000);

            }
        }
        finally{    // kada zavrsi sa koriscenjem baze podataka, zatvara db
            mDbHelper.close();
        }

    bIzlazIzKviza.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            finish();   
        }
    });
    }
}



Thanks.
Nabukodonosor
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 22, 2013 1:56 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 23 guests