Thank you for the answer
I will make 1 single 2d array c[][] then, but I need a little help to do this. This is the code I have for my c, c2 and c3 varible:
(The c, c2 and c3 variables explodes a string into chars.)
I am sorry for poor communication.
I have created the 2D array “letters[][]” but the thing is that I do not know how I can change the thing my code does into your code?
In my code (as you probably can see) I want to replace the first letter of the word with “-”, when the key pressed is equal to the front letter of the string. I want to do this by using 2d char-arrays and not the variables c, c2 and c3 which I did before
final String[] words = { "write", "code", "smarter" };
void setup() {
noLoop();
}
void draw() {
println(words);
}
void keyTyped() {
println(key);
for (int i = 0; i < words.length; ++i) {
final String word = words[i];
final int len = word.length() - 1;
final int idx = min(word.lastIndexOf('-') + 1, len);
final char ch = word.charAt(idx);
println(len, idx, ch);
if (ch == key) words[i] = word.replaceFirst(str(ch), "-");
if (words[i].endsWith("-")) println("Index", i, "got no more letters!");
}
redraw();
}
exactly
If I when want the word to randomly change into another word from the string array when you have written all letters in the word, how would you set this up?
Thank you very much, this works just as intended
I just have one question left:
How would you set the for-loop/code up when the string-array (words) is 2D (words) instead of 1D?
Lets say this is the 2D string-array:
Here I first want to select a random word from the first index of words ({“write”,“code”…) and when you have typed the random selected word correct on your computer keyboard, the word will change to another word from the first index of the string-array. When you have done this 4 times, the words should change to only be taken form the second index of the array ({“ok”,“I”,…). I hope you understand, and have a little time to help me?
What about when you think of a solution and post your attempt here?
Then we can help you with where you stuck.
Otherwise, we would do your work. What you ask here is a project, an entire Sketch, not a question about an isolated data structure.
Anyway
My guess would be you could make a IntList 0,1,2… and then use shuffle() on it (see reference: shuffle() / Reference / Processing.org) and use it as indexes.
When you increment the index “indexForIntList” you use for IntList, you don’t have to use random() because the list is shuffled already. (Explanation: this approach is easier than to use random() every time and check whether this number has already been used and if so, use random() again…).
Then you receive the input. Word is guessed: Increase the index “indexForIntList”.
Once “indexForIntList” is >=words2[indexForWords2].length you increase indexForWords2
Here is a quick demo for the pure data structure and IntList
You can of course not use it for your Quiz since it doesn’t have the structure:
ask user,
receive input,
go to next word…
A for-loop and while-loop won’t help your there, maybe states…
String[][] words2 = { // They don't have the same length!
{"write", "code", "smarter", "and", "better"}, // 5
{"ok", "i", "will", "do", "so"},
{"thank", "you", "very", "much"}, // 4
};
int indexForWords2=0; // index #1 in String[][] words2
// list for index # 2 in String[][] words2
IntList listOfIndexes = new IntList(); // https://www.processing.org/reference/IntList_shuffle_.html
void setup() {
size(200, 200);
while (indexForWords2 < words2.length) {
makeListOfIndexes();
for (int indexForIntList : listOfIndexes) {
println( words2[indexForWords2][indexForIntList] );
}//for
indexForWords2++;
println("----");
}//while
}
void makeListOfIndexes() { // list of random numbers
listOfIndexes.clear();
for (int i=0; i<words2[indexForWords2].length; i++) {
listOfIndexes.append(i);
}
listOfIndexes.shuffle();
}
//
Thank you very much @GoToLoop !
I used your code and have know made it work exactly as I want
The only thing I am having af little trouble with is that i want to put the “word”-string into a string-array (words):
String[] words = {CHAR,CHAR,CHAR};
I then will be able to draw 3 different “word”, and not only one at a time.