mardi 28 mai 2019

Hello, can someone help me to add fisher yates shuffle on my code?

this is my code , i don't know where to put the fisher yates shuffle algorithm in my code. can you help me?

i'm using firebase for my database. the button actually is 4 button not 2 button, but i cut it because it's to long to post in here.

public class AlfabetQuizActivity extends AppCompatActivity {

Button c1,c2;
TextView question, score;
private String answer;
private int skor=0, count=0;
private DatabaseReference qref, aref, c1ref, c2ref;

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

    c1 = (Button) findViewById(R.id.choice1Button);
    c2 = (Button) findViewById(R.id.choice2Button);

    question = (TextView) findViewById(R.id.questionTxt);
    score = (TextView) findViewById(R.id.scoreTxt);

    updateQuestion();

    c1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(c1.getText().toString().equals(answer)){
                Toast.makeText(getApplicationContext(), "Jawaban Benar", Toast.LENGTH_SHORT).show();
                c1.setBackgroundColor(Color.GREEN);
                skor = skor +1;
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        c1.setBackgroundColor(Color.parseColor("#F9CDAD"));
                        updateQuestion();
                        updateScore(skor);
                    }
                },1500);
            }
            else {
                Toast.makeText(getApplicationContext(), "Jawaban Salah", Toast.LENGTH_SHORT).show();
                c1.setBackgroundColor(Color.RED);

                if(c2.getText().toString().equals(answer)){
                    c2.setBackgroundColor(Color.GREEN);
                }

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        c1.setBackgroundColor(Color.parseColor("#F9CDAD"));
                        c2.setBackgroundColor(Color.parseColor("#F9CDAD"));
                    }
                },1500);
            }
        }
    });

    c2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(c2.getText().toString().equals(answer)){
                Toast.makeText(getApplicationContext(), "Jawaban Benar", Toast.LENGTH_SHORT).show();
                c2.setBackgroundColor(Color.GREEN);
                skor = skor +1;
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        c2.setBackgroundColor(Color.parseColor("#F9CDAD"));
                        updateQuestion();
                        updateScore(skor);
                    }
                },1500);
            }
            else {
                Toast.makeText(getApplicationContext(), "Jawaban Salah", Toast.LENGTH_SHORT).show();
                c2.setBackgroundColor(Color.RED);

                if(c1.getText().toString().equals(answer)){
                    c1.setBackgroundColor(Color.GREEN);
                }

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        c1.setBackgroundColor(Color.parseColor("#F9CDAD"));
                        c2.setBackgroundColor(Color.parseColor("#F9CDAD"));
                    }
                },1500);
            }
        }
    });
}

private void updateScore(int skorr){
    score.setText(""+ skor);
}

private void updateQuestion(){

    qref = FirebaseDatabase.getInstance("https://learnhangul-ed611.firebaseio.com/").getReference("AlfabetQuiz/");
    qref.child(count+"/question").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String q = dataSnapshot.getValue(String.class);
            question.setText(q);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    c1ref = FirebaseDatabase.getInstance("https://learnhangul-ed611.firebaseio.com/").getReference("AlfabetQuiz/");
    c1ref.child(count+"/choice1").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String ch1 = dataSnapshot.getValue(String.class);
            c1.setText(ch1);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    c2ref = FirebaseDatabase.getInstance("https://learnhangul-ed611.firebaseio.com/").getReference("AlfabetQuiz/");
    c2ref.child(count+"/choice2").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String ch2 = dataSnapshot.getValue(String.class);
            c2.setText(ch2);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    aref = FirebaseDatabase.getInstance("https://learnhangul-ed611.firebaseio.com/").getReference("AlfabetQuiz/");
    aref.child(count+"/answer").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            answer = dataSnapshot.getValue(String.class);

        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
    count = count +1;

}

}

thankyou for helping me.




Aucun commentaire:

Enregistrer un commentaire