import java.applet.*;
import java.awt.*;
import java.net.URL;


public class snake extends Applet{
	private Font font;
	private Font font2;
	private Font font3;
	
	    int i=0;
		int j=0;
		int k=0;
		int l=0;
		int lx=0;
		int ly=0;
		int nx=0;
		int ny=0;
		int ox=0;
		int oy=0;
		double n;
		double m;
		int d=0;

		

	//vert walls
	private int a[][]= new int[10][10];
	//horiz walls
	private int b[][]= new int[10][10];
	//counter
	private int c[][]= new int[10][10];
	
		//snakearray
	private int s[]= new int[100];
	private int sx[]= new int[40];
	private int sy[]= new int[40];
	

	

	
	private Choice color_choices;
	private Image bup;
	private Image up;
	private Image down;
	private Image left;
	private Image right;
		//initialize images



	Color c1= new Color(0xCCFFFF); //sky blue
	Color c2= new Color(0x66FF00); //dark green
	//Color c2= new Color(0xCCFF00); //light green
	Color c3= new Color(0xFFFFCC); //light beije
	Color c4= new Color(0x660000); //brick
	
	
		
	
public void init() 
	{
	
	

        
	
	font=new Font("Helvetica",Font.BOLD,48);
	font2=new Font("Helvetica",Font.BOLD,20);
	font3=new Font("Helvetica",Font.BOLD,13);
	super.init();
	
	//initialize images
		URL codebase = getCodeBase();
	System.out.println(codebase);
bup= getImage(codebase,"bup.GIF");	
up= getImage(codebase,"up.GIF");	
down= getImage(codebase,"down.GIF");	
left= getImage(codebase,"left.GIF");	
right= getImage(codebase,"right.GIF");	

	
		
	    
	    resize(550, 450);



}
      
      
      	 	         //draw backgrpound
       				public void paint(Graphics g){Background(); }	
                    // end drawing background
	
	


       
int x=0;
int y=0;






    // Called when the user presses mouse button down
  
    public boolean mouseDown(Event e, int x, int y)
{ 

//new game button
if(x>105 && x<195 && y<95 &&y>75){


     	    Background();
     }
     //instructions button
     if(x>305 && x<395 && y<95 &&y>75){
		Graphics g = this.getGraphics();
		g.setColor(c3);
		g.fillRect(0+103,0+103,294,294);
		g.setColor(c4);
		for (i=0;i<7;i++){
	
				g.drawLine(50*i+100,0+100,50*i+100,300+100);
				g.drawLine(0+100,50*i+100,300+100,50*i+100);

		} 
		g.setColor(Color.black);
		g.setFont(font2);
		g.drawString("Instructions for Snakehunt",120,135);
		g.setFont(font3);
		i=45;
		g.drawString("Behind this grid lies a snake! His position",110,120+i);
		g.drawString("under the grid is completely random. You can",110,135+i);
		g.drawString("double-click at squares in this grid to reveal",110,150+i);
		g.drawString("parts of the snake. Each part of the snake is  ",110,165+i);
		g.drawString("numbered 1 through 36. Your goal is to ",110,180+i);
		g.drawString("uncover the square that hides the head of the",110,195+i);
		g.drawString("snake (number 1).",110,210+i);
		
		g.drawString("The appeal of snakehunt might be somewhat ",110,260+i);
		g.drawString("elusive if this is your first time playing.",110,275+i);
		g.drawString("After all you can win simply by poking random",110,290+i);
		g.drawString("squares until you reach the head. The fun ",110,305+i);
		g.drawString("lies in trying to invent a strategy to uncover",110,320+i);
		g.drawString("the head in the least amount of clicks.",110,335+i);
	

		
     }
     
//solution button
if(x>205 && x<295 && y<95 &&y>75){
Graphics g = this.getGraphics();
drawsnake();

}
//plays on grid
     //make sure in bounds
     if (x<100 || y<100 ){return true;}
x=((x-100)-(x-100)%50)/50;
    	y=((y-100)-(y-100)%50)/50;
    	if (x>5 || y>5 ){return true;}
 		
 		//reveal numbers
    	
        Graphics g = this.getGraphics();
       // g.setColor(c4);
       // g.fillRect(x*50+101,y*50+101,49,49);
        g.setColor(Color.black);
        g.setFont(font2);
        
        if(a[x+1][y+1]>=10 && c[x+1][y+1]==0){g.drawString(String.valueOf(a[x+1][y+1]),x*50+113,y*50+134);}
        if(a[x+1][y+1]<10 && c[x+1][y+1]==0){g.drawString(String.valueOf(a[x+1][y+1]),x*50+120,y*50+135);}
        //if same square clicked on again
        if(c[x+1][y+1]==1){
            c[x+1][y+1]=2;
            g.fillRect(x*50+105,y*50+105,40,40);
            g.setColor(Color.white);
        	if(a[x+1][y+1]>=10){g.drawString(String.valueOf(a[x+1][y+1]),x*50+113,y*50+134);}
            if(a[x+1][y+1]<10){g.drawString(String.valueOf(a[x+1][y+1]),x*50+120,y*50+135);}
       }
        
        //win detection
			if (a[x+1][y+1]==1 && c[x+1][y+1]==2) {
				drawsnake();}
	    //set counter to 1 unless its already 2(ie dark)
		if (c[x+1][y+1]<2){c[x+1][y+1]= 1;}
        return true;
}



private void Background(){



        // i have no idea why but this helps. Don't remove it
       Graphics g = this.getGraphics();
          g.drawImage (down,sx[0]*50+55,sy[0]*50+55,this);
	      g.drawImage (up,sx[0]*50+55,sy[0]*50+55,this);
	      g.drawImage (right,sx[0]*50+55,sy[0]*50+55,this);
	      g.drawImage (left,sx[0]*50+55,sy[0]*50+55,this);
    	//background color

		g.setColor(Color.white);
		g.fillRect(0,0,500,500);
		
		//brick square
		g.setColor(c4);
		g.fillRect(0+80,0,340,420);
		
		g.setColor(c3); //beije squares
		
		g.fillRect(0+100,0+100,300,300);
		g.fillRect(0+105,75,90,20);
        g.fillRect(0+100+105,75,90,20);
        g.fillRect(0+100+100+105,75,90,20);
        g.fillRect(0+110,10,279,58);

		// blackness
		g.setColor(Color.black);
		g.setFont(font3);
		g.drawString("New Game",115,90);
		g.drawString("Solution",220,90);
		g.drawString("Instructions",316,90);
		g.setFont(font);
		g.drawString("Snakehunt!",120,57);
		
		//draw snakeish border design
		g.fillRect(420,381,80,40);g.fillRect(0,381,80,40);g.fillRect(425,331,70,40);g.fillRect(0,331,70,40);
		g.fillRect(36,60,40,315);g.fillRect(425,60,40,315);g.fillRect(460,331,40,60);g.fillRect(0,331,40,60);
		
		g.drawImage (bup,36,30,this);
		g.drawImage (bup,425,30,this);

		// draw boxes
		for (i=0;i<3;i++){
		g.drawRect(0+105+i,75+i,90-2*i,20-2*i);
		g.drawRect(0+205+i,75+i,90-2*i,20-2*i);
		g.drawRect(0+305+i,75+i,90-2*i,20-2*i);
		g.drawRect(0+110+i,10+i,279-2*i,58-2*i);
	    }

		//thicken borders
		for (i=0;i<3;i++){
		
		g.drawLine(80,420-i,420,420-i);
		g.drawLine(80+i,0,80+i,420);
		g.drawLine(420-i,0,420-i,420);
		g.drawLine(80,i,420,i);
		
		
		g.drawLine(100,400-i,400,400-i);
		g.drawLine(100+i,100,100+i,400);
		g.drawLine(400-i,100,400-i,400);
		g.drawLine(100,100+i,400,100+i);
		

		}
		
		//draw grid
		for (i=0;i<7;i++){
	
		g.drawLine(50*i+100,0+100,50*i+100,300+100);
		g.drawLine(0+100,50*i+100,300+100,50*i+100);

		}
		
		for (l=1;l<35;){ 
		  l=0;
		
		//create

		 for (i=0;i<8;i++){  
		      
		      a[i][0]=100;a[i][7]=100;a[0][i]=100;a[7][i]=100;
		      
			     } 
			      
		  for (i=1;i<7;i++){  
		       for (j=1;j<7;j++){ 
		       a[i][j]=0;
		       c[i][j]=0;
		       }
		   
	      } 
m = 6*Math.random();
        if(m<6 && m>5)  ox=6;
        if(m<5 && m>4)  ox=5;
        if(m<4 && m>3)  ox=4;
        if(m<3 && m>2)  ox=3;
        if(m<2 && m>1)  ox=2;
        if(m<1 && m>0)  ox=1;
m = 6*Math.random();
        if(m<6 && m>5)  oy=6;
        if(m<5 && m>4)  oy=5;
        if(m<4 && m>3)  oy=4;
        if(m<3 && m>2)  oy=3;
        if(m<2 && m>1)  oy=2;
        if(m<1 && m>0)  oy=1;

nx=ox;ny=oy;
d=0;
a[ox][oy]=1;
		
		
		for (i=2;i<37;i++){
		
				
				m = 4*Math.random();
             
        if(m<4 && m>3)  nx=ox+1;
        if(m<3 && m>2)  nx=ox-1;
        if(m<2 && m>1)  ny=oy+1;
        if(m<1 && m>0)  ny=oy-1;
        
        
        
       
        if(nx>0 && nx<7 && ny>0 && ny<7 && a[nx][ny]==0)  {
        
        
        	
       	 	if(a[nx+nx-ox][ny+ny-oy]==0 || (a[nx+nx-ox][ny+ny-oy]*(a[nx+ny-oy][ny+nx-ox]+a[nx-ny+oy][ny-nx+ox])>0||d==0))  {
     	    
     	    
     	    
     	    for (k=1;k<7;k++){  
		      
		      d=d+a[k][1]+a[k][6]+a[1][k]+a[6][k];
		      
			     } 
	      
	      
	      
     	    a[nx][ny]=i;
        
     	    // g.setColor(c4);
		
			//if((nx-ox)+(ny-oy)>0){ g.fillRect(ox*50+75,oy*50+75,13+50*(nx-ox),13+50*(ny-oy));
	       
	       // }
			//if((nx-ox)+(ny-oy)<0) {g.fillRect(nx*50+75,ny*50+75,13+50*(ox-nx),13+50*(oy-ny));
			
			//}
			sx[l]=ox;sx[l+1]=nx;
			sy[l]=oy;sy[l+1]=ny;
			l++;
			lx++;
			g.setColor(Color.black);
     	    //g.drawString(String.valueOf(l),8,10*i);
     	   //g.drawString(String.valueOf(lx),25,10*i);
     	    
     	    j=0;
		
			ox=nx;oy=ny;
        	}
        	else {nx=ox;ny=oy;j++;i--; if(j==20) i=37;}
		}
		else {nx=ox;ny=oy;j++;i--; if(j==20) i=37;}
			
			}
			
			}
		
		
		
		
		
}

        








//drawsnake function
private void drawsnake()
{
Graphics g = this.getGraphics();


g.setColor(c2);
		ox=sx[0];oy=sy[0];
		g.fillRect(sx[0]*50+55,sy[0]*50+55,40,40);
		// draw body
        for (i=0;i<36;i++){  
        
		       if((sx[i]-ox)+(sy[i]-oy)>0){ g.fillRect(ox*50+55,oy*50+55,40+50*(sx[i]-ox),40+50*(sy[i]-oy));}
		       if((sx[i]-ox)+(sy[i]-oy)<0){ g.fillRect(sx[i]*50+55,sy[i]*50+55,40+50*(ox-sx[i]),40+50*(oy-sy[i]));}
		       ox=sx[i];oy=sy[i];
	      }
	     
	      // paint numbers onto snake
	      g.setFont(font2);
	      for (i=1;i<7;i++){  
	      for (j=1;j<7;j++){  
	          //paint single click numbers
	          if (c[i][j]==1){
	          g.setColor(Color.black);
	           if (a[i][j]<10){g.drawString(String.valueOf(a[i][j]),i*50+69,j*50+80);}
	           else{g.drawString(String.valueOf(a[i][j]),i*50+65,j*50+80);}
	          }
	          //paint doubleclick numbers
	          if (c[i][j]==2){
	          g.setColor(Color.black);
	          g.fillRect(i*50+55,j*50+55,40,40);
	          g.setColor(Color.white);
	           if (a[i][j]<10){g.drawString(String.valueOf(a[i][j]),i*50+69,j*50+80);}
	           else{g.drawString(String.valueOf(a[i][j]),i*50+65,j*50+80);}
	          }

	      }
	      }
	      
	      //draw snake head
	       if (sy[0]-sy[1]==1){g.drawImage (down,sx[0]*50+55,sy[0]*50+55,this);}
	       if (sy[1]-sy[0]==1){g.drawImage (up,sx[0]*50+55,sy[0]*50+55,this);}
	       if (sx[0]-sx[1]==1){g.drawImage (right,sx[0]*50+55,sy[0]*50+55,this);}
	        if (sx[1]-sx[0]==1){g.drawImage (left,sx[0]*50+55,sy[0]*50+55,this);}
}
}
