How to use keyPressed to display "correct" letters?

Hi, I am very new to processing and am trying to make a Hangman style game where users fill in the blanks.

When the correct letter is pressed (keyPressed) then it appears and when an incorrect letter is pressed, the word "incorrect" comes up. I don't even know where to start on how to make sure my sketch / processing:

  1. Knows which are the right letters and how to make them appear
  2. Knows which are the wrong letters and how to display the "incorrect" banner

Can anyone help me with this?

Answers

  • edited June 2016
    1. to know which are the right letters. you need to have the right letters stored in a variable somewhere. A string object makes sense for that. Let's say it's String solution = "donkey"; or make it whatever you want. To make them appear, you should use the text function.
    2. you should think about making a keyPressed() method instead of just using the variable keyPressed. they are two different things. Much like you have a setup() and draw() function, make a keyPressed() function in your sketch. void keyPressed() { }. Within this function do some tests. if(someCondition) { doSomethingElse();} . Some Condition has to be does the key that was pressed == to some letter that is contained in your string. Some things to note. A string is an object, but key is a char. Strings have methods though to let you figure out what character is there. it's called charAt(). What i would do is that each time a key is pressed, check to see if that key is equal to any of the characters within your string. if it is , you got it correct, if not, you didn't.
  • edited June 2016

    Look at keyPressed() and key

    basically you have a solution word e.g.

    String Solution = "TIGER";

    Now let's say User entering T

    Have a var to count how many letters have been entered by user countInput

    Now say

    if(key==Solution.charAt(countInput)) 
    
        letterToDisplay += key;
    
         else wrongWordsCounter++; // count
    

    Use the last var to decide how many parts of hangman you'll draw

  • mine is not fully correct

  • Don't you have to loop through the characters?

    If you have, like Chrisir said a String solution = "TIGER"; And want to check if the entered character T is in the word, you have a loop like this:

    for(int i=0; i<solution.length; i++){
      if(key==solution.charAt(i)) character[i] = true;
    }
    

    The idea is that you have a boolean for each character in the word, when the character's boolean is set to true, you draw that character on the screen. So now you have checked if the entered character is in the word, but you still have to check if the entered character is not in the word... We can do that by adding a few lines.

    for(int i=0; i<solution.length; i++){
      if(i=0) false = -1;
      if(key==solution.charAt(i)) character[i] = true;
      else false += 1;
      if(false==solution.length) falseChar();
    }
    

    Now we start by setting a counter to 0 and then if the entered character doesn't match any of the characters in the solution, then we run a falseChar() function that you have to create.

  • @Stacey :

    // Hangman 
    
    String solution="TIGER";          // solution 
    String showLettersFound="_____";  // to display the ______ line
    String showWrongLetters="";       // wrong letters
    String allLetters="";             // journal of all letters
    
    // our counts. countBad determines the drawing of hangman; 
    // when countGood is as high as the length of solution, user wins
    int countGood, countBad; 
    
    void setup() {
      size(700, 700);
      background(111);
    
      // make it upper case
      solution=trim(solution.toUpperCase());
    }
    
    void draw() {
    
      background(111); 
    
      // general layout -------------------
    
      // (remark: "solution" gets displayed only for fun, remove it later)  
      text("solution " + solution 
        +"\n"
        +"display for user  "+showLettersFound
        +"\n"+
        "wrong letters "+showWrongLetters
        +"\n"+countBad, 
        40, 40);
    
      // draw hangman
    }//func
    
    // ------------------------------------------------
    
    void keyPressed() {
    
      char userInput=' ';
    
      userInput=key;
    
      // test intput -----------------
    
      // Has there been an input? 
      if (userInput != ' ') {
    
        //yes
    
        // make it upper case first 
        String buffer=userInput+"";
        buffer=buffer.toUpperCase();
        userInput=buffer.charAt(0);
        allLetters+=buffer;
    
        // is the entered letter in the word at all?
        if (solution.indexOf(userInput) > -1) {
    
          // yes
    
          // replace the _ with the right letter in showLettersFound
          // big problem: what if e.g. E is twice or three times in the solution??? 
          // we must replace each _ and also count each : countGood++;
          int pos=solution.indexOf(userInput); 
    
          StringBuffer buffer2 = new StringBuffer(showLettersFound);
          buffer2.setCharAt(pos, userInput);
          showLettersFound=buffer2.toString();
          countGood++;
        } else {
    
          //no, the letter is wrong 
          showWrongLetters+=buffer;
          countBad++;
        }// else
    
        // reset input 
        userInput=' ';
      }//if
      //
    } // func 
    
  • there are some tricks used, but don't be discouraged:

    lines 53 to 56 just make all inputs like a to A

    lines 69 to 71 just insert the correct letter in the word _____ with the found (already guessed correctly) letters "showLettersFound", so _____ becomes G etc.

Sign In or Register to comment.