How to replace only one word when the assigned key is pressed?

Hello all! I am an "old" newbie in processing, meaning interested since a long time but did not manage to "get my hands dirty" with it since then. I am trying to programming an interactive poetry system, implying the use of a makey makey. The idea is that everytime you touch a letter, it will randomly display a word at the place where the first letter is placed vertically. The thing is that I did manage to have my words randomly choosen and displayed but two problems occured : 1) Every time another letter is hit, it erase the former word. I know that because of "background(0)" in the void draw() bloc, it is normal but if I remove it, than all words connected to the letter appears simultaneously. How to fix that, and make that when the relevant key is pressed, only this word is replace 2) when a key is pressed, every words assigned to this key are displayed before to stop on one word, probably because of the function random(), how do I fix that? 3) Other question : is it then better to use void keyPressed (), or the function keyPressed inside void draw() Here an extract of my testing code :

String [] words ={"alligator","attaque","array","avion","amour","tyran","talmudique","thalasso","tantrique","tyrannique","tu","labile","label","arraisonnement","condition","voix","charisme","cannibale","sciemment","collusion","calice","calvitie","fin"};
String textDisplay;
//  int y = (displayHeight / 8);  


void setup (){
  size (displayWidth, displayHeight);
  background (0);
  textSize (80);
  textAlign(CENTER);
}

  void draw () {
   int y = (displayHeight / 8);  
    if (keyPressed) {
      if (key == 'z' || key == 'Z') {
 //  int y = (displayHeight / 8);      
    background (0);
        fill (255);
        textDisplay = words[int(random(5))];
      text (textDisplay, width/2, y);
    }
     else {
 fill(0);
     }
     if (key == 'm' || key == 'M') {

       background (0);
        fill (255);
textDisplay = words[int(random(6, words.length))];
      text (textDisplay, width/2, 2*y);
          }
          else {
            fill (0);      }

} } I assume my code is pretty much rubbish, but be gentle, I am in the early experimentation level of Programmering!

Tagged:

Answers

  • edited April 2017

    Definitely use void keyPressed () for this purpose

    You need to learn some more advanced concepts

    You need class placedWord and an ArrayList of that class

    Then for loop over the ArrayList and display the content

    On keyPressed add to ArrayList

    The class holds the word as a String and a x,y position as start.

    Think of an underlying grid (like chess) to help you find the positions

    Find a way not to check each letter separately but in a for loop

  • Hey Chrisir, thanks for your answer. I was trying to avoid the use of classes, because I have it somehow hard to understand how it works. If I understand you correctly, I have to build a class for each letter/key pressed?

  • edited May 2017

    No it's Only one class for all words placed on the grid

    But you can also use 3 parallel arrays instead of the class which would be x[] y[] and textDisplay[]

  • edited May 2017

    @Pebpeb -- re:

    everytime you touch a letter, it will randomly display a word at the place where the first letter is placed vertically

    I want to make sure that I understand.

    1. are the letters being placed on a grid, like Scrabble or like a crossword puzzle? OR
    2. is each new word placed on the next line of a poem?

    This sketch always displays the same string, but pressing buttons adds new words (with line breaks) to the string each time:

    String [] words ={"alligator", "attaque", "array", "avion", "amour", "tyran", "talmudique", "thalasso", "tantrique", "tyrannique", "tu", "labile", "label", "arraisonnement", "condition", "voix", "charisme", "cannibale", "sciemment", "collusion", "calice", "calvitie", "fin"};
    String textDisplay = "";
    int TEXTSIZE = 30;
    void setup () {
      size (600, 400);
      background (0);
      textSize (TEXTSIZE);
      textAlign(LEFT);
      fill (255);
    }
    void draw () {
      background (0);
      translate(TEXTSIZE, TEXTSIZE);
      text(textDisplay, 0, 0);
    }
    void keyPressed() {
      if (key == 'z' || key == 'Z') {
        textDisplay = textDisplay + words[int(random(5))]+ '\n';
      }
      if (key == 'm' || key == 'M') {
        textDisplay = textDisplay + words[int(random(6, words.length))] + '\n';
      }
    }
    
  • @jeremydouglass the letters are placed vertically but everytime you hit the letter,you will have a new word which replacing the former one with the same letter on the beginning/on the same line. For example you have the word "plane": P -> key "p" is hitten, one out of five words will be displayed. Key is pressed one more time, it will display another word out of the five. L -> key "l" is hitten, one out of five words will be displayed. Key is pressed one more time, it will display another word out of the five.
    A -> and so on with "A" N -> and so on with "N" E -> and so on with "E"

    Everytime one of the assigned key is pressed, it has not to affect the other words, just changing the one of her line. Make it sens? Tell me if it does'nt.

    @Chrisir, is it the way you understood it?

  • String [] words ={"alligator", "attaque", "array", "avion", "amour", "tyran", "talmudique", "thalasso", "tantrique", 
      "tyrannique", "tu", "labile", "label", "arraisonnement", "condition", "voix", "charisme", "cannibale", "sciemment", 
      "collusion", "calice", "calvitie", "fin"};
    
    // array that we see
    String textDisplay[] = new String [200];
    
    // index for that array when inserting new word at the end
    int index=0;  // writing position
    
    int TEXTSIZE = 30;
    
    // ---------------------------------------------------
    // the two core functions 
    
    void setup () {
      size (600, 400);
      background (0);
      textSize (TEXTSIZE);
      textAlign(LEFT);
      fill (255);
    
      int i=0; 
      for (String s1 : textDisplay) {
        s1="";
        textDisplay[i]="";
        i++;
      }
    }
    
    void draw () {
      background (0);
    
      textSize(12);
      text("Please use letters a,c,l,t,v...", width-120, 17, 110, 200); 
    
      textSize (TEXTSIZE);
      translate(TEXTSIZE, TEXTSIZE);
      text(join(textDisplay, "\n"), 0, 0);
    }
    
    // ---------------------------------------------------
    // Input functions 
    
    void keyPressed() {
    
      String result=""; 
    
      // when the key is a normal letter, we start 
      if (key>32&&key<255) {
    
        // we try to get one word from the list "words" with that letter
        result=getWordWithCertainFirstLetter( key ) ;
    
        // none found? quit (this happens when no word with the letter exists in "words") 
        if (result.equals(""))
          return; // quit 
    
        // we try to get one line number from the display list with that letter 
        int lineNumber = getLineNumberWhenLetterAlreadyUsed(key); 
    
        // Is he yet unused? 
        if (lineNumber == -1) {
          // unused!  
          // adding new word at end 
          textDisplay[index] = result ;
          index++; // increase our writing position
        } else {
          // used! 
          // We want to replace the word with a new word that 
          // starts with the same letter
          grabTheNextWordFromWordsAndDisplayIt(lineNumber);
        }//else
      }//if
    }//func 
    
    // ---------------------------------------------------
    // Minor function
    
    void grabTheNextWordFromWordsAndDisplayIt(int lineNumber) {
    
      // Letter is already in use. We want to  
      // overwrite but we need a further, new word from 
      // "words" with same letter.
    
      // All words from "words" from that all letter.
      String[] all = getAllWordsWithCertainFirstLetter(key);
    
      // are we allowed to use that word? 
      boolean allowedToUse=false; // no, not yet
    
      // Loop over all potential words s1 from words. 
      for (String s1 : all) {
    
        // step 1: when the word s1 is found in textDisplay, we want to tak the next word in "all" after it   
        if (s1.toUpperCase().equals(textDisplay[lineNumber].toUpperCase())) {
          allowedToUse=true; // set to true
        }//if
    
        // step 2: when we allowed to use the word, we take the next word in "all"
        if (allowedToUse && !s1.toUpperCase().equals(textDisplay[lineNumber].toUpperCase())) {
          if (s1.equals("")) {
            return;//quit, no success
          }
          textDisplay[lineNumber]=s1.toLowerCase();
          return;//quit, success
        }//if
        //
      }//for
    }//func 
    
    // ---------------------------------------------------
    // These functions are even more minor / basic 
    
    int getLineNumberWhenLetterAlreadyUsed(char key2) {
    
      // returns a line number for textDisplay when the letter key2 is already
      // used as first letter in textDisplay
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      for (int i =0; i<textDisplay.length; i++) {
    
        if (textDisplay[i].equals(""))
          return -1;
    
        if (textDisplay[i].toUpperCase().charAt(0)==keyAsString.charAt(0)) {
          return i;
        }
      }
      // not found 
      return -1;
    }
    
    String getWordWithCertainFirstLetter(char key2) {
    
      // returns a word with first letter key2
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      for (String s1 : words) {
        if (s1.toUpperCase().charAt(0)==keyAsString.charAt(0)) {
          return s1;
        }
      }
      return "";
    }
    
    String[] getAllWordsWithCertainFirstLetter(char key2) {
    
      // returns a array word with first letter key2
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      String result=""; 
    
      for (String s1 : words) {
        if (s1.toUpperCase().charAt(0) == keyAsString.charAt(0)) {
          result+=s1.toUpperCase()+"#";
        }
      }
      // the last word in the array is always equal ""
      return split(result, "#");
    }
    //
    
  • edited May 2017

    I am still not sure what you mean.

    My sketch shows how you can hit the key l, v, a, etc. and hit a key multiple times so e.g. the word with "a" gets replaced but stays in the same line.

    ;-)

  • Woh ! Ok, I will need some time to process it & understand how you get there! But thanks!

    It is more or less what I meant, but there it involves a makey makey, I do have to assign the keyPressed function to another key then the first letter of the words btw, the ones which are embedded in the makey makey.

    I try to be clearer (I realise that it is not at all the case, sorry for that!): You have a word made of letters that you can touch. When one of the letter is touched, it displays on the screen one of the word out of the list beginning with the same letter that have been touch and place accordingly. (If there is to "A" in the word, one at the beginning, one at the end, it as then to display it on the screen vertically on the right place of the word which is ).

    That's why I thought the code so in the first place : "For a word like "ARRAY" : If the object (IRL) firstletter A is touched, then screen displays on y = firstposition one word which is in the array from 0 -> 5 and are assigned to key "Q", because my object (letter A) is connected to the slot "Q" on the makey makey". Then if first letter R is touched then screen will show one word out of the array from position 6 -> 11) , assigned to key "space", placed vertically on y = secondposition. If the first letter "A" is touched, then replace the word and only the word in the first position, If the fourth letter, which also happens to be a "A" is touched, then screen displays at the fourth place vertically (y = fourthposition) one word out of the array from position 21 -> 26.

    So how do I break it into functions?

  • edited May 2017

    Description

    Programming is probably first about being able to describe your goal and break it down into single steps. You are not very clear (for me).

    can you post a link to a description of a makey makey you are talking about?

    can you draw an image of the makey makey you mean?

    Questions

    Please answer these questions.

    You wrote:

    You have a word made of letters [let's say PLANER] that you can touch.

    Do you mean I touch it with the mouse?

    Do your initial letters look like this?

    P
    L
    A
    N
    E
    R
    

    Do they?

    They are vertical.

    Can you lay only left (or on both sides?)?

    P_____
    L________
    A_____
    N_______
    E________
    R_____
    

    If I click on "A" I get a list with all words starting with A and I can choose from the list with the mouse? Correct? Or is he doing this automatically?

    Do you plan a thing similar to a cross word? Or just one vertical word and many horizontal words ?

    Your fourth paragraph

    Your fourth paragraph (of 5) is unclear. Do you mean a coding going from Y to A and from Q to whatever? So if you press Q , the A is handled?

    Why?

    I wouldn't do this with ifs........ it would be long and repetitive...

    Instead make two parallel arrays of type char and once read the first, and if Q is found in array 1, A must be at same index in array 2 and you can use it.

    new version

    new version (treating only the first occurrence of A in the word), but do you mean this kind of process in principle?

    String [] words ={"alligator", "attaque", "array", "avion", "amour", "tyran", "talmudique", "thalasso", "tantrique", 
      "tyrannique", "tu", "labile", "label", "arraisonnement", "condition", "voix", "charisme", "cannibale", "sciemment", 
      "collusion", "calice", "calvitie", "fin", "rescue", "yes", "yen"};
    
    // array that we see
    String textDisplay[] = new String [200];
    
    // index for that array when inserting new word at the end
    int index=0;  // writing position
    
    int TEXTSIZE = 30;
    
    // ---------------------------------------------------
    // the two core functions 
    
    void setup () {
      size (600, 400);
      background (0);
      textSize (TEXTSIZE);
      textAlign(LEFT);
      fill (255);
    
      int i=0; 
      for (String s1 : textDisplay) {
        s1="";
        textDisplay[i]="";
        i++;
      }
    
      textDisplay[0]="A";
      textDisplay[1]="R";
      textDisplay[2]="R";
      textDisplay[3]="A";
      textDisplay[4]="Y";
    }
    
    void draw () {
      background (0);
    
      textSize(12);
      text("Please use letters a,c,l,t,v...", width-120, 17, 110, 200); 
    
      textSize (TEXTSIZE);
      translate(TEXTSIZE, TEXTSIZE);
      text(join(textDisplay, "\n"), 0, 0);
    }
    
    // ---------------------------------------------------
    // Input functions 
    
    void keyPressed() {
    
      String result=""; 
    
      // when the key is a normal letter, we start 
      if (key>32&&key<255) {
    
        // we try to get one word from the list "words" with that letter
        result=getWordWithCertainFirstLetter( key ) ;
    
        // none found? quit (this happens when no word with the letter exists in "words") 
        if (result.equals(""))
          return; // quit 
    
        // we try to get one line number from the display list with that letter 
        int lineNumber = getLineNumberWhenLetterAlreadyUsed(key); 
    
        // Is he yet unused? 
        if (lineNumber == -1) {
          // unused!  
          // adding new word at end 
          textDisplay[index] = result ;
          index++; // increase our writing position
        } else {
          // used! 
          // We want to replace the word with a new word that 
          // starts with the same letter
          grabTheNextWordFromWordsAndDisplayIt(lineNumber);
        }//else
      }//if
    }//func 
    
    // ---------------------------------------------------
    // Minor function
    
    void grabTheNextWordFromWordsAndDisplayIt(int lineNumber) {
    
      // Letter is already in use. We want to  
      // overwrite but we need a further, new word from 
      // "words" with same letter.
    
      // All words from "words" from that all letter.
      String[] all = getAllWordsWithCertainFirstLetter(key);
    
      // are we allowed to use that word? 
      boolean allowedToUse=false; // no, not yet
    
      if (textDisplay[lineNumber].toUpperCase().length()==1) {
        // we try to get one word from the list "words" with that letter
        String result=getWordWithCertainFirstLetter( key ) ;
        textDisplay[lineNumber]=result;
        return;
      }
    
      // Loop over all potential words s1 from words. 
      for (String s1 : all) {
    
        // step 1: when the word s1 is found in textDisplay, we want to take the next word in "all" after it   
        if (s1.toUpperCase().equals(textDisplay[lineNumber].toUpperCase())) {
          allowedToUse=true; // set to true
        }//if
    
        // step 2: when we allowed to use the word, we take the next word in "all"
        if (allowedToUse && !s1.toUpperCase().equals(textDisplay[lineNumber].toUpperCase())) {
          if (s1.equals("")) {
            return;//quit, no success
          }
          textDisplay[lineNumber]=s1.toLowerCase();
          return;//quit, success
        }//if
        //
      }//for
    }//func 
    
    // ---------------------------------------------------
    // These functions are even more minor / basic 
    
    int getLineNumberWhenLetterAlreadyUsed(char key2) {
    
      // returns a line number for textDisplay when the letter key2 is already
      // used as first letter in textDisplay
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      for (int i =0; i<textDisplay.length; i++) {
    
        if (textDisplay[i].equals(""))
          return -1;
    
        if (textDisplay[i].toUpperCase().charAt(0)==keyAsString.charAt(0)) {
          return i;
        }
      }
      // not found 
      return -1;
    }
    
    String getWordWithCertainFirstLetter(char key2) {
    
      // returns a word with first letter key2
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      for (String s1 : words) {
        if (s1.toUpperCase().charAt(0)==keyAsString.charAt(0)) {
          return s1;
        }
      }
      return "";
    }
    
    String[] getAllWordsWithCertainFirstLetter(char key2) {
    
      // returns a array word with first letter key2
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      String result=""; 
    
      for (String s1 : words) {
        if (s1.toUpperCase().charAt(0) == keyAsString.charAt(0)) {
          result+=s1.toUpperCase()+"#";
        }
      }
      // the last word in the array is always equal ""
      return split(result, "#");
    }
    //
    
  • here is a code for a letter grid when you click mouse on the letter

    // make a grid of rectangles by using nested for-loops. 
    
    Box[] boxes = new Box[5*15]; // must match the for-loop in setup()
    
    // text on the right side 
    boolean textRightOn=true;
    String textRightSide="No Letter pressed."; 
    
    void setup() { 
    
      size(1200, 600); 
    
      // Create a grid of boxes / cells 
      int x = 10;  // dist left  
      int y = 10;  // dist top
      int k=0;
    
      for (int i = 0; i < 15; i += 1) { 
        for (int j = 0; j < 5; j += 1) {
    
          color strokeColor1 = color (0, 0, 255);   
    
          boxes[k] = new Box(x+i*53, y+j*53, 
            50, 50, 
            strokeColor1, 
            i, j, 
            "");
    
          k++;
        } // for
      } // for
    
      // place some letters 
      k=0; 
      boxes[k].textBox="A";
      k++;
      boxes[k].textBox="R";
      k++;
      boxes[k].textBox="R";
      k++;
      boxes[k].textBox="A";
      k++;
      boxes[k].textBox="Y";
    } // setup()
    
    /////////////
    
    void draw() { 
      background(255);
    
      // display the boxes 
      for (int i = 0; i < boxes.length; i++) { 
        boxes[i].display();
      } // for
    
      // display text on the right side 
      fill(0); 
      text(textRightSide, 
        width-99, 99);
    } // draw()
    
    // -------------------------------------------------
    // mouse functions 
    
    void mousePressed() {
      // function gets called automatically on mouse pressed;
      // check the boxes 
      for (int i = 0; i < 25; i++) {
        // if mouse is inside
        if (boxes[i].mouseOver()) {
          // so something
          textRightOn=true;
          textRightSide="Pressed "+boxes[i].textBox;
          // leave the for-loop 
          break;
        } // if
      } // for
    } // function
    
    void mouseReleased () {
      // function gets called automatically on mouse released;
      //
    } // function
    
    // ==============================
    
    class Box {
    
      float x;
      float y; 
    
      float w=50;
      float h=50; 
    
      color colorBox;
    
      int column;
      int row;
    
      String textBox;
    
      // constr 
      Box ( float x, float y, 
        float w, float h, 
        color colorBox, 
        int column, int row, 
        String textBox) {
        this.x=x;
        this.y=y;
    
        this.w=w;
        this.h=h;
    
        this.colorBox=colorBox;
    
        this.column=column;
        this.row=row;
    
        this.textBox = textBox;
      } // constr 
    
      void display() {
        // displays the cell.
    
        stroke(colorBox);
        noFill();
    
        rect (x, y, w, h);
        fill(0);
        // text (column+"/"+row, x+5, y+15);
        textAlign(CENTER, CENTER); 
        text (textBox, (x+w/2), (y+h/2));
      } // method
    
      boolean mouseOver() {
        // returns whether the mouse is over this rect or not
        boolean result = false; 
        result = mouseX>=x && mouseX<=x+w && 
          mouseY>=y && mouseY<=y+h;
        return result;
      } // method
      //
    } // class 
    //
    
  • Description

    Programming is probably first about being able to describe your goal and break it down into single steps. You are not very clear (for me).

    Yep, I clearly have to improve that ;)

    Answers :

    • link to a makey makey picture :

    http://tienda.bricogeek.com/kits-electronica-para-montar/527-kit-makey-makey.html

    • no, you can actually touch the letters, here is an image of my "device" : http://hpics.li/6b41270

      the letters are then connected on the "makey makey" on slots which are assigned with certain keys of the keyboard, the electronic board (aka "makey makey") is then link to the computer via USB.

    • my initials letters are the way you see it on the picture. There are no letters displayed on screen before someone touch them.

    • you are right about the way the words should be displayed on the screen (tought I want them to be on the center, but that is not the issue here).

    • if you touch "A", then one word which is beginning with "A" in the list is randomly choosen and displayed after the "A". But for a certain "A". The idea is to get certain words, which happened to be beginning with the same letter, but it is actually not really that relevant. What is relevant is the _position__ of the letter in the word. The words to be displayed are actually not random : I write five to ten sentences which a meaning of themselves and beginning with the letter of the original word (ex : planer : Paradise Later A New Economic Realm/ Paris lately avoiding Nuclear Essais Realities / etc..). So when someone touch the letter "P", it displays "Paradise" or "Paris" (randomly), but if the word was initially "PAPER", the second "P" will not be displaying those two words when the second "P" of the word is touched, but the words that I wrote before.

    • the fact that "Q" is handed if "A "is stroked is because of the way the makey makey works.

    I hope it is clearer.... And I go back to some early tutorials of "object orienting programming" and processing!

  • @Chrisir. I did not see your other post before posting, I'll look into that also, many thanks!

  • new version with a Q to Y translator and a form to store entire sentences

    String [] words ={"alligator", "attaque", "array", "avion", "amour", "tyran", "talmudique", "thalasso", "tantrique", 
      "tyrannique", "tu", "labile", "label", "arraisonnement", "condition", "voix", "charisme", "cannibale", "sciemment", 
      "collusion", "calice", "calvitie", "fin", "rescue", "yes", "yen"};
    
    // array that we see
    String textDisplay[] = new String [200];
    
    // index for that array when inserting new word at the end
    int index=0;  // writing position
    
    int TEXTSIZE = 30;
    
    // two parallel arrays; the pairs Q-A, Y-R, N-T must match and belong together (on makey makey)  
    String[] receivingLetters={"Q", "S", "N", "P"};
    String[] translateLetters={"A", "R", "T", "Y"};
    
    String[][] sentences = {
      {    "Paradise", "Later", "A", "New", "Economic", "Realm"  }, 
      {    "Paris", "lately", "avoiding", "Nuclear", "Essais", "Realities"     }
    };
    
    // ---------------------------------------------------
    // the two core functions 
    
    void setup () {
      size (600, 400);
      background (0);
      textSize (TEXTSIZE);
      textAlign(LEFT);
      fill (255);
    
      int i=0; 
      for (String s1 : textDisplay) {
        s1="";
        textDisplay[i]="";
        i++;
      }
    
      textDisplay[0]="A";
      textDisplay[1]="R";
      textDisplay[2]="R";
      textDisplay[3]="A";
      textDisplay[4]="Y";
    }
    
    void draw () {
      background (0);
    
      textSize(12);
      text("Please use letters q, n, y, p.....", width-120, 17, 110, 200); 
    
      textSize (TEXTSIZE);
      translate(TEXTSIZE, TEXTSIZE);
      text(join(textDisplay, "\n"), 0, 0);
    }
    
    // ---------------------------------------------------
    // Input functions 
    
    void keyPressed() {
    
      String result=""; 
    
      // when the key is a normal letter, we start 
      if (key>32&&key<255) {
    
        String keyAsString=key+"";
        keyAsString=keyAsString.toUpperCase();
    
        // search
        String keyAsString2="";
    
        // match Q to A etc. 
        for (int i = 0; i < receivingLetters.length; i++) {
          if (receivingLetters[i].equals(keyAsString)) {
            keyAsString2=translateLetters[i];
            break;
          }
        }
    
        // fail? 
        if (keyAsString2.equals("")) 
          return; // fail
    
        // we try to get one word from the list "words" with that letter
        result=getWordWithCertainFirstLetter( keyAsString2.charAt(0) ) ;
    
        // none found? quit (this happens when no word with the letter exists in "words") 
        if (result.equals(""))
          return; // quit 
    
        // we try to get one line number from the display list with that letter 
        int lineNumber = getLineNumberWhenLetterAlreadyUsed(keyAsString2.charAt(0) ); 
    
        // Is he yet unused? 
        if (lineNumber == -1) {
          // unused!  
          // adding new word at end 
          textDisplay[index] = result ;
          index++; // increase our writing position
        } else {
          // used! 
          // We want to replace the word with a new word that 
          // starts with the same letter
          grabTheNextWordFromWordsAndDisplayIt(lineNumber, keyAsString2 );
        }//else
      }//if
    }//func 
    
    // ---------------------------------------------------
    // Minor function
    
    void grabTheNextWordFromWordsAndDisplayIt(int lineNumber, String keyAsString2) {
    
      // Letter is already in use. We want to  
      // overwrite but we need a further, new word from 
      // "words" with same letter.
    
      // All words from "words" from that all letter.
      String[] all = getAllWordsWithCertainFirstLetter(keyAsString2.charAt(0));
    
      // are we allowed to use that word? 
      boolean allowedToUse=false; // no, not yet
    
      if (textDisplay[lineNumber].toUpperCase().length()==1) {
        // we try to get one word from the list "words" with that letter
        String result=getWordWithCertainFirstLetter( keyAsString2.charAt(0) ) ;
        textDisplay[lineNumber]=result;
        return;
      }
    
      // Loop over all potential words s1 from words. 
      for (String s1 : all) {
    
        // step 1: when the word s1 is found in textDisplay, we want to take the next word in "all" after it   
        if (s1.toUpperCase().equals(textDisplay[lineNumber].toUpperCase())) {
          allowedToUse=true; // set to true
        }//if
    
        // step 2: when we allowed to use the word, we take the next word in "all"
        if (allowedToUse && !s1.toUpperCase().equals(textDisplay[lineNumber].toUpperCase())) {
          if (s1.equals("")) {
            return; // quit, no success
          }
          textDisplay[lineNumber]=s1.toLowerCase();
          return; // quit, success
        }//if
        //
      }//for
      //
    }//func 
    
    // ---------------------------------------------------
    // These functions are even more minor / basic 
    
    int getLineNumberWhenLetterAlreadyUsed(char key2) {
    
      // returns a line number for textDisplay when the letter key2 is already
      // used as first letter in textDisplay
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      for (int i =0; i<textDisplay.length; i++) {
    
        if (textDisplay[i].equals(""))
          return -1;
    
        if (textDisplay[i].toUpperCase().charAt(0)==keyAsString.charAt(0)) {
          return i;
        }
      }
      // not found 
      return -1;
    }
    
    String getWordWithCertainFirstLetter(char key2) {
    
      // returns a word with first letter key2
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      for (String s1 : words) {
        if (s1.toUpperCase().charAt(0)==keyAsString.charAt(0)) {
          return s1;
        }
      }
      return "";
    }
    
    String[] getAllWordsWithCertainFirstLetter(char key2) {
    
      // returns a array word with first letter key2
    
      String keyAsString=key2+"";
      keyAsString=keyAsString.toUpperCase();
    
      String result=""; 
    
      for (String s1 : words) {
        if (s1.toUpperCase().charAt(0) == keyAsString.charAt(0)) {
          result+=s1.toUpperCase()+"#";
        }
      }
      // the last word in the array is always equal ""
      return split(result, "#");
    }
    //
    
Sign In or Register to comment.