> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://cisc1600.sketchpad.cc/sp/pad/view/ro.Htw3CSmK3yO/rev.49
 * 
 * authors: 
 *   Mohamed Alguhiem

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/* @pjs preload="/static/uploaded_resources/p.20287/myimage.jpg"; */
/* @pjs preload="/static/uploaded_resources/p.20287/lost.jpg"; */

// ****************************************************** Declaring Variables  ******************************************************
PImage img,img2;
Rain r1;
color[] c2 = new color[20];
int count=1;
int numDrops = 50;
Rain[] drops = new Rain[numDrops];
int[][] b = new int [4][4];
// To Hold The COLOMN AND RAW Indexes when asking user questions 
int[] question = new int[2]; 
color[][] c = new color [4][4];
int padding = 20; 
int square = 150;
int length = padding*(b.length+1)+square*b.length;
int dead=1;
int score=0;
int tries = 3;
boolean status = true;
char choice;
boolean drawWarnning=false, wrongChoice=false,drawCongrats=false,titlescreen=true;
boolean startGame=false,getResult=true ,correctChoice=false,checkResult=false;
int wc =0; // counter to make the losing message blink

// ****************************************************** setup METHOD  ******************************************************
void setup(){
    // This image belongs to
    //http://www.ecards.co.uk/preview/477/52/Send-free-e-cards-here-and-gloat-like-there-is-no-tomorrow.html
    img = loadImage("/static/uploaded_resources/p.20287/lost.jpg");
    img2 = loadImage("/static/uploaded_resources/p.20287/myimage.jpg");
    // setting Random Colors For Winning The Game 
    for(int i=0;i<c2.length;i++)
        c2[i]=color(int(random(255)),int(random(255)),int(random(255)),int(random(255)));
    setBvalues();
    setCvalues();
    size(700,700);
    question[0]=int(random(4));
    question[1]=int(random(4));
    reset();
    textFont(createFont("Courier",40));
    background(0);
    stroke(0, 1, 0);
    for (int i = 0; i < drops.length; i++) {
        drops[i] = new Rain(); // Create each object
        r1 = new Rain();
    }
}



// ****************************************************** draw METHOD  ******************************************************
void draw(){
    if (titlescreen==true){
         titlescreen();
         stars();
     }
    else if (startGame==true)
        startGame();
    if (keyPressed) {
        if ((key == 'd' || key == 'D') && startGame==true){
            startGame=false;
            choice=' ';
            getResult=true;
        }
    }
    if(startGame==false && titlescreen==false && getResult==true){
        getResultes();
    }
    if( checkResult==true){
        checkResult();
    }
    if(correctChoice==true){
        drawCongrats=true;
        drawCongrats();
    }
    if(wrongChoice==true){
        drawWarnning=true;
        drawWarnning();
    }
    if(tries==0){
      gameOver();
    }
    if (keyPressed) {
        if ((key == 'A' || key == 'a') && correctChoice==true){
            dead=1;
            reset();
            correctChoice=false;
            titlescreen=true;
        }
    }
    if (keyPressed) {
         if ((key == 'B' || key == 'b')){
             if(tries>0 && wrongChoice==true){
                 background(255);
                 smooth();
                 noStroke();      
                 dead=1;
                 correctChoice=false;
                 titlescreen=true;
                 dead=1;
                 getResult=false;
                 checkResult=false;
                 wrongChoice=false;
              }
         }
    }
}


void gameOver(){
      background(255);
      rectt(0,0,width,height,0,color(255,199));// Transparent Rectangle
      textt("Gameover Click to restart ",0,height/2,width,50,color(0),30,CENTER);
}
// ****************************************************** drawWarnning METHOD  ******************************************************

void drawWarnning(){
  background(255);
  smooth();
  noStroke();
  if(tries>0){
      image(img, 0, 0, width, height);
      String s;
      if(tries>1)
          s="Tries";
      else
          s="Try";
      if(wc<100){
          textSize(25);
          textAlign(CENTER);
          fill(0);
          text("YOU HAVE "+tries+ " "+s+" LEFT" ,(width/2),(height-80));
          textSize(25);
          textAlign(CENTER);
          fill(0);
          text("PRESS \"B\" TO TRY AGAIN" ,(width/2),(height-40));
          wc++;
      }
      wc++;
      if (wc==150)wc=0;//reset counter of blink
  }
}
  
  
  // ****************************************************** drawWarnning METHOD  ******************************************************
void drawCongrats(){
    background(255);
    smooth();
    noStroke();
     //fill(255,80);
    textSize(30);
    fill(c2[3]);
    textAlign(CENTER);
    if(count<3000)
    {
        fill(#FFC0CB);
        count++;
        text("Congratulations You Won!",(width/2)-5,250);
    }
    textSize(40);
    fill(c2[3]);
    textAlign(CENTER);
    if(count<3000){
        fill(25);
        count++;
        textFont(createFont("Courier",40));
        text("Press \"A\" To Play Again",(width/2)-5,400);
    }
    fill(200,80);
    rect(0,0,700,700);
    //Loop through array to use objects.
    for (int i = 0; i < drops.length; i++)
        drops[i].fall();
}


// ****************************************************** reset METHOD  ******************************************************
void reset(){
    b = new int [4][4];
    for(int c=0;c<b.length;c++){
        for(int r=0; r<b[c].length;r++)
            b[c][r]=int(random(3))+1;
    }
    for(int i=0;i<c.length;i++){
        for(int r=0; r<c[i].length;r++)
            c[i][r]= color(int(random(255)),int(random(255)),int(random(255)));
    }
}


// ****************************************************** rectt METHOD  ******************************************************

 void rectt(float x,float y,float w, float h,float r, color c){
     fill(c);
     rect(x,y,w,h,r);
}



// ****************************************************** textt METHOD  ******************************************************
void textt(String t, float x, float y, float w, float h, color c, float s, int align){
    fill(c);
    textAlign(align);
    textSize(s);
    text(t,x,y,w,h);
}



//     ************************************************* titlescreen METHOD  ******************************************************
void titlescreen() {
    image(img2, -10, 300, 150, (height/2)+100);
    textSize(18);
    //background(#cccccc);
    fill(#000000);
    textAlign(CENTER);
    fill(100);
    text("Welcome To The Memory Game!",(width/2),150);
    text("Memorize The Shapes.",(width/2),200);
    text("After You Meomorize The Shape of Each Box Press D",(width/2),250);
    text("For Lines Input 1",(width/2),350);
    text("For  ellipses Input 2",(width/2),400);
    text("For triangles Input 3",(width/2),450);
    fill(204, 102, 0);
    rect((width/2)-70,(height-155), 140,50);
    //rect((width/2),(height-125),0,400,200);
    fill(255,0,0);
    text("Start Game",(width/2),(height-125));
    if(mousePressed && (mouseX >= ((width/2)-70)&& 
       mouseX <= ((((width/2)-70-15)+((height-125)))/2) &&
       mouseY >= (height-155) && mouseY <= (height-125)+22)) 
    {
           //dead=0;
           startGame=true;
           titlescreen=false;
           draw();
    }
}



//   *****************************************************    Game Starts Here   ******************************************************
void startGame(){
    background(255); 
    rectt(0,0,width,height,10,color(150));// padding color 150
    drawShapes();
    textt("score: "+score,10,5,100,50,color(0),10,LEFT);
}



//     ****************************************************** Drawing STARS  ******************************************************
void stars(){
   fill(0,55);
   rect(0,0,width,height);
   fill(255);
   ellipse(random(width),random(height),4,4);
}



//     *************************************************** Setting Random b[][] values at setup and reset ***********************************
void setBvalues(){
    for(int c=0;c<b.length;c++){
        for(int r=0; r<b[c].length;r++)
        b[c][r]=int(random(3));
    }
}



//     ****************************************************** Setting an Array of Random Colors  ********************************************
void setCvalues(){
    for(int i=0;i<c.length;i++){
        for(int r=0; r<c[i].length;r++)
        c[i][r]= color(int(random(255)),int(random(255)),int(random(255)));
    }
}



// ******************************************************  Drawing SHAPES     ******************************************************
void drawShapes(){
    stroke(0, 1, 0);
    for(int i=0;i<b.length;i++){
        for(int k=0; k<b[i].length;k++){
            float x,y;
            x= padding+(padding+square)*k ;
            y= padding+(padding+square)*i;
            rectt(x,y,square,square,5,color(200));
            if(b[i][k]>0)
            {
                float p = log(b[i][k])/log(2);
                rectt(x,y,square,square,5,color(190));
            }
            
            // ******************************************************  Drawing lines     ******************************************************
            if((b[i][k])==1){
                fill(0);
                line(10+(k+1)*padding + (k*150) +2, 10+(i+1)*padding + (i*150) + 2, 
                (k+1)*padding + (k*150) + (150-2)-10, (i+1)*padding + (i*150) + (150-2)-10);
            }
            //  ****************************************************** Drawing ellipses  ******************************************************
            else if(b[i][k]==2){
                fill(c[i][k]);
                ellipse((k+1)*padding + (k*150) + (150/2)+2, (i+1)*padding + (i*150) + (150/2),135, 135);
            }
            //  ****************************************************** Drawing Triangles  ******************************************************
             else if(b[i][k]==3){
                 fill(c[i][k]);
                 triangle ( 20+((k*150)+((150)/2)+(padding*k)),10+(i*150)+((padding*(i+1))),
                 (10+(k*150)+(padding*(k+1))),(i*150)+(padding*(i+1))+150-10,
                 (k*150)+((padding*(k+1)))+(150-10),(i*150)+(padding*(i+1))+150-10);
             }  
        }
    }
}



// ****************************************************** drawBackGround METHOD  ******************************************************
void drawBackGround(){
        for(int i=0;i<b.length;i++){
            for(int k=0; k<b[i].length;k++){
                float x,y;
                x= padding+(padding+square)*k ;
                y= padding+(padding+square)*i;
                rectt(x,y,square,square,5,color(200));
                    if(b[i][k]>0){
                        float p = log(b[i][k])/log(2);
                        rectt(x,y,square,square,5,color(245));//color(255-p*255/11,p*255/11,0)
                         //textt(""+b[i][k],x,y+52,square,square,color(0),40,CENTER);
                    }
              }
       }     
}  



// ****************************************************** getResultes METHOD  ******************************************************
void getResultes(){
    background(0);
    fill(255);
    stroke(0, 1, 0);
    rect(0,0,width,height);// padding color 150
    textSize(20);
    fill(30);
    textAlign(CENTER);
    text("Choose The Correct Shape of COLOMN \""+(question[1]+1)+"\" , RAW \""+(question[0]+1)+"\"",(width/2),50);
    text("Input 1: For Line Then Click \"UP\" Key",(width/2)-5,110);
    text("Input 2: For Ellipse Then Click  \"UP\" Key",(width/2)+20,150);
    text("Input 3: For Triangle Then Click  \"UP\" Key",(width/2)+26,190);
    strokeWeight(2);
    fill(125,155,(10+100));
    triangle ((width/2),(225),(width/3),(height/2)+45,2*(width/3),(height/2)+45);
    fill(175,50,(10+150));
    ellipse(150,height-150,200,200);
    strokeWeight(6);
    line((width/2)+20,(height/2)+100,(width-100)+20,(height-80)-20);
    strokeWeight(1);
    if (keyPressed) {
            if ((key == '1'))
                choice = 'l';
            else if ((key == '2'))
                choice = 'e';
            else if ((key == '3'))
                choice = 't';
            if (key == CODED) {
                if (keyCode == UP && (choice =='t' || choice =='l' || choice =='e')){
                    getResult=false;
                    checkResult=true;
                }
            }
    }
    if(choice == 't'){
        textSize(17);
        textAlign(CENTER);
        text("Current Choice is: Triangle !! Click Enter To Check Result",(width/2),430);
    }
    else if(choice == 'e'){
        textSize(17);
        textAlign(CENTER);
        fill(c[1][2]);
        text("Current Choice is: Ellipse !! Click Enter To Check Result",(width/2),430);
    }
    else if(choice == 'l'){
        textSize(17);
        textAlign(CENTER);
        fill(c[1][0]);
        text("Current Choice is: Line !! Click Enter To Check Result",(width/2),430);
    }
       // System.out.println(b[question[0]][question[1]]);
}


// ****************************************************** checkResult METHOD  ******************************************************
void checkResult(){
    //System.out.println(choice);
    //fill(c[1][2]);
    rect(0,0,width,height);// padding color 
    if((b[question[0]][question[1]]==1) && choice=='l'){
        checkResult=false;
        correctChoice=true;
        score++;
    }
    else if((b[question[0]][question[1]]==2) && choice=='e'){
        checkResult=false;
        correctChoice=true;
        score++;
    }
    else if((b[question[0]][question[1]]==3) && choice=='t'){
        checkResult=false;
        correctChoice=true;
        score++;
    }
    else{ 
        tries--;
        question[0]=int(random(4));
        question[1]=int(random(4));
        startGame=false;
        wrongChoice=true;
        checkResult=false;
        getResult=false;
        //drawWarnning();
    }
}


// ****************************************************** Rain METHOD  ******************************************************
// CREDIT FOR (RAIN AND FALL METHODS) CODE TO: 
// https://processing.org/discourse/beta/num_1275997615.html
// SOME OF THE RAIN CODE MIGHT BE MODIFIED TO MEET PROJECT NEEDS
// I also give Credit for the Declaration and setup for this Class
class Rain {
    float r = random(700);
    float y = random(-height);
    void fall() {
        y = y + 7;
        if(count<1000){
        fill(c2[0]);
        count++;}
        else if(count<2000){
            fill(c2[2]);
            count++;
        }
        else if(count<3000){
            fill(c2[3]);
            count++;
        }
        else if(count<4000){
            fill(c2[4]);
            count++;
        }
        else if(count<5000){
            fill(c2[5]);
            count++;
        }
        else if(count<7000){
            fill(c2[6]);
            count++;
        }
        else if(count<7000){
            fill(c2[7]);
            count++;
        }
        else
            count=0;
        ellipse(r, y, 10, 10);
        if(y>height){
            r = random(700);
            y = random(-200);
        }
    }
}