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

public class c_river extends Applet implements Runnable{

   int pm[] = { 1, 1, 1};
   int pf[] = { 1, 1, 1};
   int rf = 1;
   int rs[] = {0,0};
   int rix[] = {260,340,460,140};
   int riy[] = {0,0,200,200};
   int px,py;
   int c1,c2;
   int sn,pn,sx;
   Image pmi[] = new Image[3];
   Image pfi[] = new Image[3];
   Image raft,arr,arl;
   Button rep;
   Thread th = null;
   Image os;
   Graphics og;

   public void init(){
       setBackground(Color.white);
       os = createImage(600,200);
       og = os.getGraphics();
       add(rep = new Button("  replay  "));
       for(int k = 0;k < 3;k ++){
           pmi[k] = getImage(getDocumentBase(),"Picture/m"+(k+1)+".gif");
           pfi[k] = getImage(getDocumentBase(),"Picture/f"+(k+1)+".gif");
       }
       raft = getImage(getDocumentBase(),"Picture/raft.gif");
       arr = getImage(getDocumentBase(),"Picture/ar5.gif");
       arl = getImage(getDocumentBase(),"Picture/ar6.gif");
       addMouseListener(
           new MouseAdapter(){
               public void mousePressed(MouseEvent e){
                   px = e.getX();
                   py = e.getY();
                   c1 = 0; c2 = 0;
                   if((px > 238) && (px < 280) && (py > 157) && (py < 200)){
                       if((rs[0] != 0) || (rs[1] != 0)){
                           if((pm[0] != 1) && (pm[1] != 1) && (pm[2] != 1)){
                               c1 = 1;
                           }
                           for(int i = 0;i < 3;i ++){
                               if((pf[i] == 1) && (pm[i] != 1)){
                                   c2 = 1;
                               }
                           }
                           if((c1 == 1) || (c2 == 0)){                               
                               rf = -1;
                               repaint();
                           }
                       }
                   }
                   if((px > 320) && (px < 362) && (py > 157) && (py < 200)){
                       if((rs[0] != 0) || (rs[1] != 0)){
                           if((pm[0] != -1) && (pm[1] != -1) && (pm[2] != -1)){
                               c1 = 1;
                           }
                           for(int i = 0;i < 3;i ++){
                               if((pf[i] == -1) && (pm[i] != -1)){
                                   c2 = 1;
                               }
                           }
                           if((c1 == 1) || (c2 == 0)){                               
                               rf = 1;
                               repaint();
                           }
                       }
                   }
                   if((px > 40) && (px < 160) && (py > 20) && (py < 100)){
                       sn = (160 - px) / 40;
                       if((pf[sn] == -1) && (rf == -1)){
                           if(rs[0] == 0){
                               pf[sn] = 10;
                               rs[0] = -1 * (sn + 1);
                               repaint();
                           }else if(rs[1] == 0){
                               pf[sn] = 11;
                               rs[1] = -1 * (sn + 1);
                               repaint();
                           }
                       }
                   }
                   if((px > 440) && (px < 560) && (py > 20) && (py < 100)){
                       sn = (px - 440) / 40;
                       if((pf[sn] == 1) && (rf == 1)){
                           if(rs[0] == 0){
                               pf[sn] = 10;
                               rs[0] = -1 * (sn + 1);
                               repaint();
                           }else if(rs[1] == 0){
                               pf[sn] = 11;
                               rs[1] = -1 * (sn + 1);
                               repaint();
                           }
                       }
                   }
                   if((px > 20) && (px < 140) && (py > 100) && (py < 180)){
                       sn = (140 - px) / 40;
                       if((pm[sn] == -1) && (rf == -1)){
                           if(rs[0] == 0){
                               pm[sn] = 10;
                               rs[0] = sn + 1;
                               repaint();
                           }else if(rs[1] == 0){
                               pm[sn] = 11;
                               rs[1] = sn + 1;
                               repaint();
                           }
                       }
                   }
                   if((px > 460) && (px < 580) && (py > 100) && (py < 180)){
                       sn = (px - 460) / 40;
                       if((pm[sn] == 1) && (rf == 1)){
                           if(rs[0] == 0){
                               pm[sn] = 10;
                               rs[0] = sn + 1;
                               repaint();
                           }else if(rs[1] == 0){
                               pm[sn] = 11;
                               rs[1] = sn + 1;
                               repaint();
                           }
                       }
                   }
                   if((px > 200) && (px < 280) && (py > 60) && (py < 140)){
                       if(rf == -1){
                           sn = (px - 200) / 40;
                           if(rs[sn] != 0){
                               if(rs[sn] < 0){
                                   sx = -1;
                                   pn = -1 * rs[sn] - 1;
                                   pf[pn] = -1;
                                   rs[sn] = 0;
                                   repaint();
                               }else{
                                   sx = 1;
                                   pn = rs[sn] - 1;
                                   pm[pn] = -1;
                                   rs[sn] = 0;
                                   repaint();
                               }                               
                           }
                       }
                   }
                   if((px > 320) && (px < 400) && (py > 60) && (py < 140)){
                       if(rf == 1){
                           sn = (px - 320) / 40;
                           if(rs[sn] != 0){
                               if(rs[sn] < 0){
                                   sx = -1;
                                   pn = -1 * rs[sn] - 1;
                                   pf[pn] = 1;
                                   rs[sn] = 0;
                                   repaint();
                               }else{
                                   sx = 1;
                                   pn = rs[sn] - 1;
                                   pm[pn] = 1;
                                   rs[sn] = 0;
                                   repaint();
                               }                               
                           }
                       }
                   }
               }
           }
       );
       rep.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               for(int i = 0;i < 3;i ++){
                   pm[i] = 1;pf[i] = 1;rf = 1;rs[0] = 0;rs[1] = 0;
               }
           repaint();
           }
       });
   }

   public void paint(Graphics g){
       og.setColor(Color.white); 
       og.fillRect(0,0,600,200);
       og.setColor(new Color(88,226,243));
       og.fillPolygon(rix,riy,4);
       og.drawImage(raft,240 + rf * 60,120,this);
       og.drawImage(arr,238,157,this);
       og.drawImage(arl,320,157,this);
       for(int i = 0;i < 3;i ++){
           if(pf[i] < 10){
               og.drawImage(pfi[i],280 + pf[i] * (i * 40 + 160),20,this);
           }else if((pf[i] == 10) || (pf[i] == 11)){
               og.drawImage(pfi[i],260 + (pf[i] - 10) * 40 + rf * 60,60,this);
           }
           if(pm[i] < 10){
               og.drawImage(pmi[i],280 + pm[i] * (i * 40 + 180),100,this);
           }else if((pm[i] == 10) || (pm[i] == 11)){
               og.drawImage(pmi[i],260 + (pm[i] - 10) * 40 + rf * 60,60,this);
           }
       }
       g.drawImage(os,0,0,this);
   }
   public void start(){    
      if(th == null){
         th = new Thread(this);
         th.start();
      }
   }

   public void stop(){
      th = null;
   }

   public void run(){
      while(th != null){
         try{
            Thread.sleep(100);
         }
         catch (InterruptedException e){
         }
      }
   }

   public void update( Graphics g ){
      paint( g ) ;
   }
}