better way to write this code
in
Programming Questions
•
3 years ago
Hi, for the purpose of learning,
I made a program that random outputs one of the pre-defined available chars.
As soon as the word " waanzin" is made with it then it tells how many times a char was sended before this combination was founded. The program works so that's not the problem, but i'm just wondering how you guys would do it.
Like what names would you use, or will u use functions etc.
This because my code is confusing in my eyes for such a simple task and i think it can be done easier and i'm just wondering how.
It would be nice if some of you guys will make an example.
I made a program that random outputs one of the pre-defined available chars.
As soon as the word " waanzin" is made with it then it tells how many times a char was sended before this combination was founded. The program works so that's not the problem, but i'm just wondering how you guys would do it.
Like what names would you use, or will u use functions etc.
This because my code is confusing in my eyes for such a simple task and i think it can be done easier and i'm just wondering how.
It would be nice if some of you guys will make an example.
- char[] elements = { ' ', 'w', 'a', 'n', 'z', 'i' };
String word = " waanzin";
char[] equalCheckChars;
char[] lastChars;
int end;
int count = 0;
void setup() {
equalCheckChars = word.toCharArray();
lastChars = new char[equalCheckChars.length];
end = lastChars.length-1;
frameRate(99999);
}
void draw() {
count++;
for(int i = 0; i < lastChars.length-1; i++) {//move all stored chars by 1 position
lastChars[i] = lastChars[i+1];
}
int index = int(random(elements.length));//pick random char
lastChars[end] = elements[index];//store last added char
//print(elements[index]); //prints all chars to console
for(int i = 0; i < lastChars.length; i++){
if(lastChars[i] == equalCheckChars[i]){
if( i+1 == equalCheckChars.length){//last char is also equal so there is a hit
println("\n "+count+" ticks");
count = 0;//reset
// exit();
}
}
else{
break; //no reason to continue check if the characters are not equal
}
}
}
1