lundi 23 janvier 2017

Android - How to generate questions for a maths game

I am new to android programming so my code probably wont make sense as it is a mess so i apologize in advance. I am currently creating my own version of an app as it is required in my assignment to design our own maths game.

So far my app is working as i want it to. I have a textView which displays to the user the question example: 7 + 7 and user has four buttons with one displaying the correct answer and three displaying the incorrect answer. the issues I am having is:

  1. how do i get the three incorrect answers to display a similar number to the answer to make it harder? Example: the answer is 14 but i want the wrong answers as close to the correct answer, example: 12,13,15.

  2. the other issue is on the buttons that display the incorrect answer, how do i get it to stop duplicating?

I need this to work on addition, subtraction, multiplication, and division. All these operators have their own activities.

Here is a snippet of the code. Like i said its messy and will be the worst code you ever read :/ lol I tried haha.....

public class SubtractionActivity extends AppCompatActivity {


private int level = 0, answer = 0, operator = 0, num1 = 0, num2 = 0;
private String[] Operator = {"-"};

private final int SUBTRACT_OPERATOR = 0;


ArrayList<Integer> answers = new ArrayList<Integer>();


//creating Integer to keep track of the number of questions user got up to.
//creating score Integer that holds the value of the users score
//create Integer that connects to the while loop below. (Check Comment)
int highScoreeasy = 0;
int highScoremedium = 0;
int highScorehard = 0;
int Score = 0;
int incorrectAnswer;
int answersLocation;
SharedPreferences easyprefs;
SharedPreferences mediumprefs;
SharedPreferences hardprefs;




CountDownTimer countDownTimer;
int ctnTime;
Boolean gameOver = false;
MediaPlayer mPlayer;


Button button0;
Button button1;
Button button2;
Button button3;
Button plyAgain;

RelativeLayout subtractionLayout;
TextView score;
TextView timer;
TextView hghScore;
TextView subtractionQuestions;






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


    easyprefs = this.getSharedPreferences("easyPrefsKey", Context.MODE_PRIVATE);
    mediumprefs = this.getSharedPreferences("mediumPrefsKey", Context.MODE_PRIVATE);
    hardprefs = this.getSharedPreferences("hardPrefsKey", Context.MODE_PRIVATE);

    mPlayer = new MediaPlayer();
    button0 = (Button) findViewById(R.id.btn01);
    button1 = (Button) findViewById(R.id.btn11);
    button2 = (Button) findViewById(R.id.btn21);
    button3 = (Button) findViewById(R.id.btn31);
    plyAgain = (Button)findViewById(R.id.btnrestartId1);


    timer = (TextView) findViewById(R.id.timerId1);
    score = (TextView) findViewById(R.id.scorePoints1);
    hghScore = (TextView) findViewById(R.id.hghScorePoints1);
    subtractionQuestions = (TextView) findViewById(R.id.subquestionsId1);
    subtractionLayout = (RelativeLayout) findViewById(R.id.subtractionLayoutId);


    Bundle extras = getIntent().getExtras();
    if (extras != null){
        int passedLevel = extras.getInt("Level", -1);
        if (passedLevel >= 0){
            level = passedLevel;

        }
    }


    if (level == 0 ){
        easyQuestions();
        timerSixtySec();
        button0.setText("");
        button1.setText("");
        button2.setText("");
        button3.setText("");
        score.setText("0");
        hghScore.setText("");
        timer.setText("10s");
        plyAgain.setVisibility(View.INVISIBLE);
        plyAgain.setClickable(gameOver);


        button0.setClickable(gameOver);
        button1.setClickable(gameOver);
        button2.setClickable(gameOver);
        button3.setClickable(gameOver);




    }else if (level == 1){
        mediumQuestions();
        timerSixtySec();
        button0.setText("");
        button1.setText("");
        button2.setText("");
        button3.setText("");
        score.setText("0");
        hghScore.setText("");
        timer.setText("10s");
        plyAgain.setVisibility(View.INVISIBLE);
        plyAgain.setClickable(gameOver);


        button0.setClickable(gameOver);
        button1.setClickable(gameOver);
        button2.setClickable(gameOver);
        button3.setClickable(gameOver);


    }else if (level == 2){
        hardQuestions();
        timerSixtySec();
        button0.setText("");
        button1.setText("");
        button2.setText("");
        button3.setText("");
        score.setText("0");
        hghScore.setText("");
        timer.setText("10s");
        plyAgain.setVisibility(View.INVISIBLE);
        plyAgain.setClickable(gameOver);


        button0.setClickable(gameOver);
        button1.setClickable(gameOver);
        button2.setClickable(gameOver);
        button3.setClickable(gameOver);


    }



}

public void chooseAnswer(View v){

    //now we to compare the tag of the button to the location of the correct answer so we can determine if the user got the answer correct or not.
    // use an if statement to get the tag of the buttons, convert them toString, also check if it is equal answerLocation, also convert that toString.
    if (v.getTag().toString().equals(Integer.toString(answersLocation))) {

        Score = Score + 10;
        score.setText(Integer.toString(Score));



        mPlayer.reset();
        mPlayer.release();
        mPlayer = MediaPlayer.create(this, R.raw.correct);
        mPlayer.start();
        if (level == 0){
            easyQuestions();
        }else if (level == 1){
            mediumQuestions();
        }else if (level == 2){
            hardQuestions();
        }




    }else{

        mPlayer.reset();
        mPlayer.release();
        mPlayer = MediaPlayer.create(this, R.raw.wrong);
        mPlayer.start();


    }

}

public void easyQuestions(){


    // rand.nextInt will now create the random generator to randomize whole numbers.
    Random rand = new Random();

    operator = rand.nextInt(Operator.length);

    //give the rand a value of 51 to generate numbers from 0 to 50. value is 51 as numbers start from 0.
    num1 = (rand.nextInt(10) + 1) + 5;
    num2 = (rand.nextInt(10) + 1) + 5;


    if(operator == SUBTRACT_OPERATOR){
        while(num2>num1){
            num1 = (rand.nextInt(10) + 1) + 5;
            num2 = (rand.nextInt(10) + 1) + 5;
        }
    }

    switch(operator) {
        case SUBTRACT_OPERATOR:
            answer = num1 - num2;
            break;
    }

    // this will update the addition TextView of the value a - b. for example, " 7 - 12 " .
    //we have to set a and b to a String as they are Integer values.
    subtractionQuestions.setText(num1+" "+Operator[operator]+" "+num2);

    //create the location of the correct answer to be another random number between 0 to 3.
    answersLocation = rand.nextInt(4);

    // our arrayList is getting added to each time we generate new answers. once they have selected their answer, use answers.Clear() to reset it.
    answers.clear();

    // create a loop which will create a value for the buttons within the gridlayout for the correct answer to randomly appear on any of them.
    // the outcome should either display the correct answer and generate three incorrect answers.
    for (int i = 0; i < 4; i++ ){


        //now we want to use an if statement within for loop to check if i == to answersLocation and if so to get the answer from a + b.
        if (i == answersLocation){
            answers.add(num1 - num2);
        }else{
            //this will generate three incorrect answers on the buttons within the gridlayout.
            incorrectAnswer = (rand.nextInt(10) + 1) + 5;
            if (incorrectAnswer == incorrectAnswer){
                incorrectAnswer = (rand.nextInt(10) + 1) + 5;
            }

            //and if i != answersLocation, generate an incorrect answer from 0 to 100.
            /* also we dont want an incorrect answer to be the same as the correct answer as if user was to click it, it will come up as incorrect.
            so create a while loop that checks if incorrectAnswer is == to the answer of a + b, and if it is to generate another number between 0 to 100
             */
            while (incorrectAnswer == num1 - num2 && incorrectAnswer == incorrectAnswer){
                incorrectAnswer = (rand.nextInt(10) + 1) + 5;
            }

            // now add incorrect answers to our arrayList out side of the while loop.
            answers.add(incorrectAnswer);
        }



    }

    //what this code is doing is setting the text to the buttons within the gridLayout with our answers ArrayList.
    button0.setText(Integer.toString(answers.get(0)));
    button1.setText(Integer.toString(answers.get(1)));
    button2.setText(Integer.toString(answers.get(2)));
    button3.setText(Integer.toString(answers.get(3)));





}

public void timerSixtySec(){
    countDownTimer = new CountDownTimer(3100,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            subtractionQuestions.setText(millisUntilFinished / 1000 + "s");


        }

        @Override
        public void onFinish() {

            button0.setClickable(true);
            button1.setClickable(true);
            button2.setClickable(true);
            button3.setClickable(true);

            if (level == 0){
                easyQuestions();
            }else if (level == 1){
                mediumQuestions();
            }else if (level == 2){
                hardQuestions();
            }


            countDownTimer = new CountDownTimer(10999,100) {
                @Override
                public void onTick(long millisUntilFinished) {
                    timer.setText(millisUntilFinished / 1000 + "s");
                }

                @Override
                public void onFinish() {
                    timer.setText("0s");
                    subtractionQuestions.setText("Game over...");
                    button0.setClickable(gameOver);
                    button1.setClickable(gameOver);
                    button2.setClickable(gameOver);
                    button3.setClickable(gameOver);

                    plyAgain.setClickable(true);
                    plyAgain.setVisibility(View.VISIBLE);



                }
            }.start();

        }
    }.start();

    plyAgain.setClickable(false);

}

Also i am not quite sure whats happening here > (rand.nextInt(10) + 1) + 5; is this displaying 1 to 15 insted of 0 to 14?

any help is great. Thanks :)




Aucun commentaire:

Enregistrer un commentaire