PFont font;//Create a Font named font
String s;// String that contains operator symbol
int opp;// Used to choose the operator from 1 to 4
int x,y,ans;// x and y being the randomly generated values and ans contains its answer, according to their operator
int count=1;//is used as the power of 10
int uans=0;//User's answer
String ua="";//User's Answer
void setup(){
softkey("Submit");//To create a softkey label named Submit
font = loadFont("ArialMT-48.mvlw",#FFFFFF);
textFont(font);
opp = random(1,4);//To randomly generate operator
x=random(1,100);//To randomly generate number
y=random(1,100);//To randomly generate number
}
void draw(){
background(0);
fill(255,0,0);
rect(0,0,width/3,height/4);
rect(2*width/3,0,width,height/4);
if(opp==1){//Addition operator
s = "+";
ans=x+y;
}
if(opp==2){//Subtraction Operator
s = "-";
ans=x-y;
}
if(opp==3){//Multiplication Operator
s = "X";
ans=x*y;
}
if(opp==4){//Division Operator
s = "/";
ans=x/y;
}
text("" + x, width/10, height/6);
text("" + y, 3*width/4, height/6);
text(s, width/2-15, 20, width/2, height/2);
text("" + uans, 0, height/2);
text(ua, 0, 5*height/6);
}
void keyPressed(){
if (key =='0'){
uans = (uans*pow(10,count)) + 0 ;
}
if (key =='1'){
uans = (uans*pow(10,count)) + 1 ;
}
if (key =='2'){
uans = (uans*pow(10,count)) + 2 ;
}
if (key =='3'){
uans = (uans*pow(10,count)) + 3 ;
}
if (key =='4'){
uans = (uans*pow(10,count)) + 4 ;
}
if (key =='5'){
uans = (uans*pow(10,count)) + 5 ;
}
if (key =='6'){
uans = (uans*pow(10,count)) + 6 ;
}
if (key =='7'){
uans = (uans*pow(10,count)) + 7 ;
}
if (key =='8'){
uans = (uans*pow(10,count)) + 8 ;
}
if (key =='9'){
uans = (uans*pow(10,count)) + 9 ;
}
if (key =='*'){
uans = uans;
}
if (key =='#'){
uans = uans*-1;
}
}
void softkeyPressed(String label) {
if (label.equals("Submit")) {
if(uans==ans){
ua="Correct";
}
else
ua="Wrong";
}
}
brrrrrrrrrrr
ReplyDelete