import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class f_color extends Applet{

   int ld[][] ={{40,40,300,40},{40,60,280,60},{60,80,260,80},{100,100,220,100},
                {140,120,180,120},{140,120,180,120},{140,120,180,120},{60,140,140,140},
                {60,140,140,140},{60,140,140,140},{140,160,180,160},{120,180,200,180},
                {80,200,240,200},{60,220,260,220},{40,240,300,240},{40,40,40,240},
                {60,80,60,220},{80,60,80,80},{80,140,80,200},{100,100,100,140},
                {100,200,100,220},{120,80,120,100},{120,140,120,180},{140,120,140,160},
                {140,180,140,200},{160,60,160,80},{160,100,160,120},{160,160,160,180},
                {160,200,160,220},{180,120,180,160},{180,180,180,200},{200,80,200,100},
                {200,140,200,180},{220,100,220,140},{220,200,220,220},{240,60,240,80},
                {240,140,240,200},{260,80,260,140},{280,60,280,140},{300,40,300,240},
                {180,140,280,140},{260,160,260,220},{260,160,300,160}};
   int pn = 1;
   int cn;
   int dx,dy;
   int px,py;
   int ec[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
   int td[][] = {{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                 { 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 1},
                 { 2, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 5, 1},
                 { 2, 6, 6, 9, 9, 9,10,10,10, 8, 8, 5, 1},
                 { 2, 6, 6, 9, 9,11,11,10,10, 8, 8, 5, 1},
                 { 2,12,13,13,14,11,11,15,16,16, 1, 1, 1},
                 { 2,12,13,13,14,14,15,15,16,16, 1, 2, 2},
                 { 2,12,13,13,13,17,17,16,16,16, 1, 2, 2},
                 { 2,12,12,18,18,18, 0, 0, 0, 1, 1, 2, 2},
                 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}};
   int ecn[] = {5,7,6,5,5,3,6,6,6,6,6,4,4,6,5,5,6,6,5};
   int en[][] = {{1,2,16,17,18},{2,3,4,5,8,16,0},{1,3,6,12,18,0},
                 {1,2,4,6,7},{1,3,5,7,8},{1,4,8},{2,3,7,9,12,13},
                 {3,4,6,8,9,10},{1,4,5,7,10,16},{6,7,10,11,13,14},
                 {7,8,9,11,15,16},{9,10,14,15},{2,6,13,18},
                 {6,9,12,14,17,18},{9,11,13,15,17},{10,11,14,16,17},
                 {8,10,15,17,0,1},{14,15,13,16,18,0},{12,13,17,0,2}};
   int fg = 0;
   Button rep;

   public void init(){
       setBackground(Color.white);
       addMouseListener(
            new MouseAdapter(){
                 public void mousePressed(MouseEvent e){
                      dx = e.getX();
                      dy = e.getY();
                      px = dx / 20 -2;
                      py = dy / 20 -2;
                      if((dx > 60) && (dx < 100) && (dy > 260) && (dy < 300)){
                            pn = 1;
                            repaint();
                      }
                      if((dx > 120) && (dx < 160) && (dy > 260) && (dy < 300)){
                            pn = 2;
                            repaint();
                      }
                      if((dx > 180) && (dx < 220) && (dy > 260) && (dy < 300)){
                            pn = 3;
                            repaint();
                      }
                      if((dx > 240) && (dx < 280) && (dy > 260) && (dy < 300)){
                            pn = 4;
                            repaint();
                      }
                      if((dx > 40) && (dx < 300) && (dy > 40) && (dy < 240)){
                         if(ec[td[py][px]] == pn){
                               ec[td[py][px]] = 0;
                               repaint();
                         }else{
                               for(int c = 0;c < ecn[td[py][px]];c ++){
                                   if(pn == ec[en[td[py][px]][c]]){
                                         fg = 1;
                                   }
                               }
                               if(fg == 0){
                                  ec[td[py][px]] = pn;
                                  repaint();
                               }
                         fg = 0; 
                         }
                     }
                 }
            }
        );

        setLayout(new FlowLayout());
        add(rep = new Button("  replay  "));
        rep.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
              for(int s = 0;s < 19;s ++){
                  ec[s] = 0;
              }
              repaint();
           }
        });
   }

   public void paint(Graphics g){

       g.setColor(new Color(127,74,181));
       g.fillRoundRect(pn * 60 - 3,257,46,46,15,15);
       g.setColor(Color.blue);
       g.fillRoundRect(60,260,40,40,15,15);
       g.setColor(Color.green);
       g.fillRoundRect(120,260,40,40,15,15);
       g.setColor(Color.red);
       g.fillRoundRect(180,260,40,40,15,15);
       g.setColor(Color.yellow);
       g.fillRoundRect(240,260,40,40,15,15);
       for(int i = 0;i < 13;i ++){
          for(int j = 0;j < 10;j ++){
             cn = ec[td[j][i]];
             if(cn == 1){g.setColor(Color.blue);
             }else if(cn == 2){g.setColor(Color.green);
             }else if(cn == 3){g.setColor(Color.red);
             }else if(cn == 4){g.setColor(Color.yellow);
             }else{g.setColor(Color.white);
             }
             g.fillRect(i * 20 + 40,j * 20 + 40,20,20);
          }
       }
       g.setColor(Color.black);
       for(int k = 0;k < 48;k ++){
          g.drawLine(ld[k][0],ld[k][1],ld[k][2],ld[k][3]);
       }
   }
}