Simple Pathsearcher

Here a simple Pathsearcher I wrote out of fun.

Finds a Path (if the random start/goal location have one) in time.
Sometimes fast, sometime slow.

Its an active search, not precalculating the path.
Just takes off and looks for it, avoiding traps



/**
 * Silly simple Pathsearcher by Damocles
 */
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;

public class Pathsearch extends JFrame
{

	int curX=0;
	int curY=0;
	
	
	int SIZE_X=30;
	int SIZE_Y=30;
	
	int[][] passable = new int[SIZE_X][SIZE_Y];
	int[][] field = new int[SIZE_X][SIZE_Y];
	int scaling=10;
	int tx=10;
	int ty=40;
	
	int startX=25;
	int startY=25;
	int goalX=1;
	int goalY=1;
	
	
	public Pathsearch()
	{

		this.setVisible(true);
		this.setSize(SIZE_X*10+20, SIZE_Y*10+50);
		
		setDefaultCloseOperation(3);

		this.setLocationRelativeTo(null);
		
		
		Random ran=new Random();
		for(int xx=0;xx<SIZE_X;xx++)
		{
			for(int yy=0;yy<SIZE_Y;yy++)
			{
				passable[xx][yy]=ran.nextInt(4)==0?1:0;
			}
			
		}
		
		
		
		this.repaint();
		
		
		//simple mover
		
		curX=startX;
		curY=startY;
		
		
		System.out.println(0 % 2 );
		
		while(true)
		{
			
			int gx=curX;
			int gy=curY;
			double minDist=1000000;
			
			
			for(int x=-1;x<2;x++)
			{
				for(int y=-1;y<2;y++)
				{
					int nx=curX+x;
					int ny=curY+y;
					
					
					if(nx>=0 && nx<SIZE_X && ny>=0 && ny<SIZE_Y && !(x==0 && y==0) && !(Math.abs(x)==1 && Math.abs(y)==1))
					{
						if(passable[nx][ny]==0)
						{
						double dx=nx-goalX;
						double dy=ny-goalY;
						double dist=Math.sqrt((dx*dx) + (dy*dy));
						
						dist+=field[nx][ny]; //add this memory field distance
						
						//ok closer
						if(dist<minDist) 
							{
							 gx=nx;
							 gy=ny;
							 minDist=dist;
							}
						}
					}
					
					
				}
			}
			
			//set new posititon
			curX=gx;
			curY=gy;
			
			field[gx][gy]+=50;
		
			if(curX==goalX && curY==goalY) break;
			
			
			
			try
			{
				Thread.sleep(100);
			} catch (InterruptedException e)
			{
				
				e.printStackTrace();
			}
			this.repaint();
			
		}
		
		
	}
	
	
	BufferedImage buffer=new BufferedImage(400,400,1);
   public void paint(Graphics g)
   {
  	 Graphics g2=g;
  	 
  	 	g=buffer.getGraphics();
      g.setColor(new Color(255,255,255));
      g.fillRect(0, 0, 400, 400);
      
      
      g.translate(tx, ty);
      
      
      g.setColor(new Color(230,230,230));
   
      for(int xx=0;xx<SIZE_X;xx++)
      {
            g.drawLine(xx*scaling, 0, xx*scaling, SIZE_Y*scaling);
      }
      for(int xx=0;xx<SIZE_Y;xx++)
      {
            g.drawLine(0, xx*scaling, SIZE_X*scaling, xx*scaling);
      }
      
      
      
      //draw inpassable fields
      g.setColor(new Color(100,100,100));
      for(int xx=0;xx<SIZE_X;xx++)
      {
         for(int yy=0;yy<SIZE_Y;yy++)
         {
            if(passable[xx][yy]!=0) g.fillRect(xx*scaling, yy*scaling, scaling, scaling);
         }
      }
      
                //draw start / goal
      g.setColor(new Color(250,100,100));
      g.fillRect(startX*scaling, startY*scaling, scaling, scaling);
      g.setColor(new Color(100,100,250));
      g.fillRect(goalX*scaling, goalY*scaling, scaling, scaling);

      //draw mover
      
      g.setColor(new Color(100,250,100));
      g.fillRect(curX*scaling, curY*scaling, scaling, scaling);
      
      g2.drawImage(buffer, 0, 0, null);
   }
   
	
	public static void main(String[] args)
	{
		new Pathsearch();

	}

}