/**
A rather silly sketch that draws random letters and try to make up a given tekst
character for character. All attempts are reported as postit labels at a random
place on the sketch
*/
// clear postit
boolean clear;
// allowed letters (modified in setup)
String alfa="abcdefghijklmnopqrstuvwxyzæøå";
// starting text
String target="complete nonsens";
// what we have found so far
String status="";
// number of attempts
int counter=0;
// the index of the letter we are looking for
int nextTargetIndex=0;
// should we clear postits for each letetr found
boolean clear_pr_char=false;
// need 3 textsizes
PFont bigF;
PFont smallF;
PFont mediumF;
void setup(){
size(600,400);
clear=false;
bigF=createFont("Arial",32);
smallF=createFont("Arial",12);
mediumF=createFont("Arial",18);
// modyfy to allow uppercase, digits and space
alfa=alfa+alfa.toUpperCase()+" 1234567890";
}
void draw(){
if(clear){
background(120);
clear=false;
}
counter++;
// draw postit
textFont(smallF);
float x=random(width);
float y=random(height);
fill(255,255,0);
rect(x,y,20,20);
fill(0);
int tix=int(random(alfa.length()));
textAlign(TOP);
char c=alfa.charAt(tix);
text(c,x+5,y+15);
// can we use this char: c
if( (nextTargetIndex < target.length()) && (target.charAt(nextTargetIndex)==c) ){
status+=c;
nextTargetIndex++;
// clear if wanted
if(clear_pr_char)
background(120);
}
// draw status, that is the part of the text found
if(status.length()>0){
fill(255);
textFont(bigF);
int dX=int(textWidth(status)+10);
rect(width/2-dX/2-5,height/2,dX,40);
fill(0);
textAlign(LEFT,TOP);
text(status, width/2-dX/2,height/2);
}
// draw resulting attempts for the whole tekst
if(nextTargetIndex >= target.length()){
textFont(mediumF);
String out=""+counter+" lapper";
int dX=int(textWidth(out+5));
fill(255);
rect(width/2-dX/2-5,height/2+50,dX,20);
textAlign(LEFT,TOP);
fill(0);
text(out, width/2-dX/2,height/2+50);
noLoop();
}
}
// you can clear whenever you want
void mouseClicked()
{
clear=true;
}
//*******************************
/* called from javascript */
void restart(String S,boolean prChar){
target=S;
status="";
nextTargetIndex=0;
clear=true;
counter=0;
loop();
clear_pr_char=prChar;
}