True and false keycodes for game (Quiz / true or false game)

@Zayba please, please format code when you paste it to the forum. Highlight your code and press the </> button. Or put three ` marks on their own before and after your code blocks.

Continuing the discussion from True and false keycodes for game:

Thank you so much Lexyth, but I am a little confused, where are those lines in my code? I don’t really know where they are.

void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if ( keyCode == ENTER ) { //THIS ONE MAKES SENSE, IF YOU HAVE TO TYPE IN THE WORD „True“ or „False“
    //What happens when you press ENTER

    checkAnswer(myText, correctAnswer);
    myText = "";
  } else if (key == 't') { //THIS ONE
    println("t");


    totalCorrect++; //THIS ONE IS WRONG, SINCE YOU WANT TO SET TOTALCORRECT IN THE NEXT METHOD (checkAnswer())


    checkAnswer(myText, correctAnswer);
  } else if (key == 'f') { //AND THIS ONE ARE REDUNDANT IF YOU ALREADY TYPED YOUR ANSWER


    totalWrong++; //THIS ONE IS ALSO WRONG, SAME REASON AS BEFORE


    checkAnswer(myText, correctAnswer);
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
    myText = myText + key;
  }
}

If you adjust the Code, totalCorrect will increment if you have a right answer, Else totalWrong will increment.

Also, using t/f keys doesn‘t do anything in your Code right now, since they both only use the text you entered (which is already happening if you press Enter, you t/f do the same as Enter). You‘d need to add a functionality to set your answer to „true“ if t is pressed and „False“ if f is pressed, but you can keep that for later. But you can‘t just increment totalCorrect only because you pressed the t key… if the correct answer is false and you press t, that‘s wrong, so totalWrong should increase, but that‘s not what your Code does.

How it should be : (This is not valid Code)

Answer True && CorrectAnswer True && Enter = totalCorrect++; //You answered true and the expected correct answer is true, so total correct increments
T+T = Correct;
// I‘ll abbreviate it with AT, AF, ACT, ACF. Should be obvious what they stand for. 

AF && CAF && Enter = totalCorrect++;
F+F = correct;

AT && CAF && Enter = totalWrong++; //you answered true, but the correct answer is False, so total wrong Increments
T+F = wrong;

AF && CAT && Enter = totalWrong++;
F+T= wrong;

What your Code does (doesn‘t work correctly) (Again, not valid Code!):

AT && CAT && KeyT = totalCorrect + 2; //even though it should only give one point

AT && CAT && KeyF = totalCorrect++ and totalWrong++; //even though it should be correct...

AT && CAF && KeyT = totalCorrect++ and totalWrong++; //even though it should be wrong...

AF && CAT && KeyF = totalWrong++ and totalCorrect++// you get the point

//there are still some more possibilities, but i think it‘s obvious where the problem lies.

1 Like

Thank you Lexyth, this piece of code makes a lot more sense than mine did, but Processing is pulling up random errors and i have no clue what they mean.

String myText;
int totalWrong = 0;
int totalCorrect = 0;
int question = 0;
String correctAnswer;
String WrongAnswer;
int Answer;
int CorrectAnswer;

// ----------------------------------------------------------------------

void setup() {
  size(800, 800);
  fill(0);
  myText = "Welcome to the Quiz Game";
  textSize(40);
}

void draw() {
  background(255);

  //Set up the screen for the user.
  text("", 0, 300, 800, 700);
  text(question, 20, 80);
  text("Correct:", 600, 40);
  text(totalCorrect, 620, 80);
  text("Incorrect:", 600, 140);
  text(totalWrong, 620, 180);

  //Display the user input text

  text(myText, 50, 650);

  //OPTIONAL: To improve your code: maybe make a "displayQuestion function.

  // ---------------------------SET UP THE QUIZ HERE -----------------------------

  if (question ==0) {
    //Intro
    //Put some user facing (external) documentation here.
    text("Press Enter to Begin", 50, 250, 700, 250);
    correctAnswer = "";
  } else if (question == 1) {

    text("Question:lightning never strikes in the same place twice. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 2) {

    text("Question: if you cry in space, the tears just stick onto your face. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 3) {

    text("Question: If you cut an earthworm in half, both halves can regrow their body. True or False? ", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 4) {

    text("Question: Humans can distinguish between over a trillion different smells. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 5) {

    text("Question: Adults have fewer bones than babies do. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 6) {

    text("Question: Goldfish only have a memory span of 3 seconds.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 7) {

    text("Question: there are more cells of bacteria in your body than there are human cells.True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 8) {

    text("Question: Your fingernails and hair keep growing after you die. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 9) {

    text("Question: Water spirals down the plughole in opposite directions in the northern and southern hemispheres.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 10) {

    text("Question: Humans can't breathe and swallow at the same time. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 11) {

    text("Question: A penny dropped from a skyscraper can reach sufficient velocity to kill a pedestrian below. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else {
    text("You are done!", 50, 250, 700, 250);
    text("You got " + totalCorrect + " correct answers.", 50, 300, 700, 250);
  }

  //---------------------------------------------------------------------------
}

//Use this function to enter text into the myText string.
void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if ( keyCode == ENTER ) {
    //What happens when you press ENTER
   
    checkAnswer(myText, correctAnswer);
   
    
    myText = "";
  } 
    
    checkAnswer(myText, correctAnswer);
      
    checkAnswer(myText, correctAnswer);{
  }  if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
       myText = myText + key;
  }
}
 Answer TRUE && CorrectAnswer TRUE && ENTER= totalCorrect++;
    TRUE+TRUE= Correct;
    Answer FALSE && CorrectAnswer FALSE && ENTER=totalCorrect++;
    FALSE+FALSE= correct;
    Answer TRUE && correctAnswer FALSE && ENTER=totalWrong++;
    TRUE+FALSE= wrong;
    Answer FALSE && correctAnswer TRUE && ENTER=totalWrong++;
    FALSE + TRUE= wrong;





void checkAnswer(String answer, String correctAnswer) {

  answer = answer.toLowerCase();
  correctAnswer = correctAnswer.toLowerCase();

  if (question!=0) {

    //Note: You could use .contains() instead.
    if (answer.equals(correctAnswer)) {

   
   if (answer.equals(correctAnswer)) {

  if ( correctAnswer.equals("False")  )
totalCorrect++;
else totalWrong++;
}else{
if ( correctAnswer.equals("True") )
totalCorrect++;
 totalWrong++;

  totalWrong++;
  
}
}
}
  

  
  question++;
}`Preformatted text`

Uhm… that might‘ve been a bit confusing to write that in Coded format… that‘s not Code! It‘s a representation of what happens in your Code vs how it should be… the Code you can use is only the first bit, edited to what the comments within tell you.

Click on a error message. The editor will scroll to the line where the error happens. Read the error carefully, as if it is advice (it usually is).

The class “Answer” does not exist.

It looks like you cut-paste Lexyth’s pseudocode – but that isn’t real code, it is just a code-like description of “what your code does” and what it could do – Edit: use the first code blocks comments as a guide to modify your own code

1 Like

oops sorry i got very confused i thought that piece code would need to be put into my code, my bad.
But i am still not getting it, how can i convert my code in a way that works? How can I convert it in a way where the scores increment correctly?

Lexyth described above how to do it

Print it and work through it

The first Block of Code in my post is exactly the mousePressed method that you have.

I added comments behind the parts that tell you what they do and if they are wrong. Just delete the parts that are wrong. (2 Lines…)

As for the other parts with comments, you can delete the 2 key methods if (key == ‚t‘) and if (key == ‚f‘), since they work the same way as Enter.

The only way to make those methods have sense is to add functionality that sets the answers to either „true“ or „false“ is you press the t or f keys. But you don’t have that, so they don‘t make sense for now.

Hmm – I think that perhaps Zayba is very new, and pseudocode is too confusing in this case when the basic syntax hasn’t been mastered yet – instead, providing small fragments of actual code (or links to reference pages with actual code examples) might be more helpful…

2 Likes

thank you Lexyth i took out those parts but i am wondering if i make my code like this, will i be able to use key f and t?

String myText;
int totalWrong = 0;
int totalCorrect = 0;
int question = 0;
String correctAnswer;
String WrongAnswer;



// ----------------------------------------------------------------------

void setup() {
  size(800, 800);
  fill(0);
  myText = "Welcome to the Quiz Game";
  textSize(40);
}

void draw() {
  background(255);

  //Set up the screen for the user.
  text("", 0, 300, 800, 700);
  text(question, 20, 80);
  text("Correct:", 600, 40);
  text(totalCorrect, 620, 80);
  text("Incorrect:", 600, 140);
  text(totalWrong, 620, 180);

  //Display the user input text

  text(myText, 50, 650);

  //OPTIONAL: To improve your code: maybe make a "displayQuestion function.

  // ---------------------------SET UP THE QUIZ HERE -----------------------------

  if (question ==0) {
    //Intro
    //Put some user facing (external) documentation here.
    text("Press Enter to Begin", 50, 250, 700, 250);
    correctAnswer = "";
  } 
 
  else if (question == 1) {
text("Question:lightning never strikes in the same place twice. True or False?", 50, 250, 700, 250); }
 else if (question==1 && keyPressed){
   correctAnswer = "f"; totalCorrect=+1;
 }else if (question==1 && keyPressed){
   correctAnswer = "t"; totalWrong++;
 }
  
  else if (question == 2) {
 text("Question: if you cry in space, the tears just stick onto your face. True or False?", 50, 250, 700, 250);
   }else if (question==2 && keyPressed){
     correctAnswer = "t"; totalCorrect++;
  } else if (question==2 && keyPressed){
     correctAnswer = "f"; totalWrong++;
  }
  
    
  else if (question == 3) {
    text("Question: If you cut an earthworm in half, both halves can regrow their body. True or False? ", 50, 250, 700, 250);
    }else if (question==3 && keyPressed){
     correctAnswer = "f"; totalCorrect++;
  } else if (question==3 && keyPressed){
     correctAnswer = "t"; totalWrong++;
  }
    
   else if (question == 4) {

    text("Question: Humans can distinguish between over a trillion different smells. True or False?", 50, 250, 700, 250);
   }else if (question==4 && keyPressed){
     correctAnswer = "t"; totalCorrect++;
  } else if (question==4 && keyPressed){
     correctAnswer = "f"; totalWrong++;
  }
  
  else if (question == 5) {

    text("Question: Adults have fewer bones than babies do. True or False?", 50, 250, 700, 250);
   }else if (question==5 && keyPressed){
     correctAnswer = "t"; totalCorrect++;
  } else if (question==5 && keyPressed){
     correctAnswer = "f"; totalWrong++;
  }
 
  
  else if (question == 6) {
  text("Question: Goldfish only have a memory span of 3 seconds.True or False?", 50, 250, 700, 250);
  }else if (question==6 && keyPressed){
     correctAnswer = "f"; totalCorrect++;
  } else if (question==6 && keyPressed){
     correctAnswer = "t"; totalWrong++;
  }
  
  
  else if (question == 7) {
  text("Question: there are more cells of bacteria in your body than there are human cells.True or False?", 50, 250, 700, 250);
 }else if (question==7 && keyPressed){
  correctAnswer = "t"; totalCorrect++;
  }else if (question==7 && keyPressed){
     correctAnswer = "f"; totalWrong++;
  }
    
   else if (question == 8) {
     text("Question: Your fingernails and hair keep growing after you die. True or False?", 50, 250, 700, 250);
  }else if (question==8 && keyPressed){
     correctAnswer = "f"; totalCorrect++;
  } else if (question== 8 && keyPressed){
     correctAnswer = "t"; totalWrong++;
  }
    
    
  else if (question == 9) {
    text("Question: Water spirals down the plughole in opposite directions in the northern and southern hemispheres.True or False?", 50, 250, 700, 250);
   }else if (question==9 && keyPressed){
     correctAnswer = "f"; totalCorrect++;
  } else if (question==9 && keyPressed){
     correctAnswer = "t"; totalWrong++;
  }
 
  
  else if (question == 10) {
 text("Question: Humans can't breathe and swallow at the same time. True or False?", 50, 250, 700, 250);
  }else if (question==10 && keyPressed){
     correctAnswer = "t"; totalCorrect++;
  }else if (question==10 && keyPressed){
  correctAnswer = "f"; totalWrong++;
  }
  
  else if (question == 11) {

    text("Question: A penny dropped from a skyscraper can reach sufficient velocity to kill a pedestrian below. True or False?", 50, 250, 700, 250);
   }else if (question==11 && keyPressed){
     correctAnswer = "f"; totalCorrect++;
   }else if (question==11 && keyPressed){
     correctAnswer = "t"; totalWrong++;
  }
  
  else {
    text("You are done!", 50, 250, 700, 250);
    text("You got " + totalCorrect + " correct answers.", 50, 300, 700, 250);
  }

  //---------------------------------------------------------------------------
}

//Use this function to enter text into the myText string.
void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if ( keyCode == ENTER ) {
    //What happens when you press ENTER
    checkAnswer(myText, correctAnswer);
    myText = "";
  }
    
    checkAnswer(myText, correctAnswer);
  
  if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
    myText = myText + key;
  }
}

void checkAnswer(String answer, String correctAnswer) {

  answer = answer.toLowerCase();
  correctAnswer = correctAnswer.toLowerCase();

  if (question!=0) {

    //Note: You could use .contains() instead.
    if (answer.equals(correctAnswer)) {

      if ( correctAnswer.equals("False")  )
        totalCorrect++;
      else totalWrong++;
    } else {
      if ( correctAnswer.equals("True")  )
        totalCorrect--;
      else totalWrong--;

      totalWrong=0+1;
    }
  }
  question++;
}
//
1 Like

thank you jeremy douglass i am in my third week of coding and i am very confused so i came online to ask experts for some help.

1 Like

I think you should decide; either user types t OR true

I would not support both ways

You can, but you don‘t have to.
Without key f and t, you Type in „True“ or „False“ and then press Enter, so the key‘s are obsolete.

If you go without the Typing and without enter, you can very simply set the text of your answer to „True“ if you press ‚t‘ and „False“ if you press ‚f‘.

You can also use both at the same time, but you need to have that functionality for the t and f keys, Else it‘s not going to work.

1 Like

i figured it out thank you Lexyth, Chrisir, and jeremydouglassfor all your help!!:slight_smile: :upside_down_face:

3 Likes

Congratulations! Now that you have it working, a next step might be to consider making a copy of your program and restructuring it to separate the code and the data. Here is your data:

“Lightning never strikes in the same place twice.”, “False”
“If you cry in space, the tears just stick onto your face.”, “True”
“If you cut an earthworm in half, both halves can regrow their body.”, “False”
“Humans can distinguish between over a trillion different smells.”, “True”

You really only need a counter and a single function to display that:

text(myQuestions[current], 50, 250, 700, 250);

and a single key method to check it, something like (untested):

void keyPressed() {
  if (key==myAnswers[current]) {
    totalCorrect++;
  } else {
    totalWrong++;
  }
  current++;
}

The example I’ve given is as if you use parallel arrays of questions and answers – String[] myQuestions, and boolean[] myAnswers, for example – although you could store your answers as boolean (True) or char (f) or even String – however you like.

– but you could also use a HashMap (advanced) or a Table (more advanced) or create your own Question class (even more advanced). In any case, you can edit your question data separately from your code – and even load it from a text file. To add 100 new questions would be separate from your functions, and wouldn’t require 500 lines of new code.

3 Likes

ok i will give it a try on my next assignment because I already handed the other one in but thanks again for the tip jeremydouglass! :slight_smile: :smiley:

2 Likes

hey , Just wondering how did you get the code to work regarding the score jumping by two or one each time

1 Like