import java.awt.*;
import java.util.*;
import java.applet.*;

//--------------------------------------------------------------------------
// applet
//--------------------------------------------------------------------------

public class
maze extends Applet implements Runnable
{
	Thread thread;
	private int timeout = 100;
	private int dx, dy;
	
	private T_board board;
	private T_mouse my_mouse;
	private T_mouse rats[];
	private T_cheese cheeses[];
	private T_zap zap;
	
	private T_counter hi_score;
	private T_counter score;
	private T_clock clock;

  	private Image offScreenImage;
  	private Dimension offScreenSize;
  	private Graphics offScreenGraphics;
	
	private long begin_time;
	private boolean game_begin_f;
	private boolean game_on_f;
	private boolean game_over_f;
	
	
	public void
	init()
	{
		int i;
		
		dx = 0;
		dy = 0;
		
    	Dimension d = size();
		d.width = 270;
		d.height = 210;
      	offScreenImage = createImage(d.width, d.height);
      	offScreenSize = d;
      	offScreenGraphics = offScreenImage.getGraphics();

		board = new T_board();
		board.init(this);
		
		my_mouse = new T_mouse(this, false, 6, 6);
		rats = new T_mouse[2];
		rats[0] = new T_mouse(this, true, 10, 5);
		rats[1] = new T_mouse(this, true, 3, 8);

		cheeses = new T_cheese[3];
		for(i=0; i<3; i++){
			cheeses[i] = new T_cheese(this, i==0);
		}
		zap = new T_zap(this);
		
		hi_score = new T_counter(this, 7*13+5, 10*13+3);
		hi_score.clear();
		score = new T_counter(this, 7*13-3, 13*13+3);
		score.clear();
		clock = new T_clock(this);
		
		game_begin_f = true;
		game_on_f = false;
		game_over_f = false;
	}
	
		
	public void 
	paint(Graphics g)
	{		
    	g.drawImage(offScreenImage, 0, 0, null);
	}

	
  	public void 
	update(Graphics g)
	{
		int i;
		
		board.draw(offScreenGraphics);
		my_mouse.draw(offScreenGraphics);
		for(i=0; i<2; i++){
			rats[i].draw(offScreenGraphics);
		}
		for(i=0; i<3; i++){
			cheeses[i].draw(offScreenGraphics);
		}
		zap.draw(offScreenGraphics);
		hi_score.draw(offScreenGraphics);
		score.draw(offScreenGraphics);
		clock.draw(offScreenGraphics);

		paint(g);
	}


	public boolean 
	keyDown(java.awt.Event evt, int ch) 
	{
//		System.out.print("Char: " + ch + ".\n");

		if(ch == 10){
			if(game_over_f){
				game_over_f = false;
				game_begin_f = true;
			}
		}
		if(ch == (int)('w') || ch == (int)('W')){
			dx = 0;
			dy = -1;
		}
		if(ch == (int)('a') || ch == (int)('A')){
			dx = -1;
			dy = 0;
		}
		if(ch == (int)('s') || ch == (int)('S')){
			dx = 1;
			dy = 0;
		}
		if(ch == (int)('z') || ch == (int)('Z')){
			dx = 0;
			dy = 1;
		}
    	return (true);
  	}

	public void 
	start()
	{
		if(thread == null){
			thread = new Thread(this);
			thread.start();
		}
	}
	
	
	public void 
	stop()
	{
		if(thread != null){
			thread.stop();
			thread = null;
		}
	}
	
	
	public void 
	run() 
	{
		int i,j;
		int mx, my;
		int cx, cy;
		int rx, ry;
		long time;
		
		while(true) {
			try {
				thread.sleep(timeout);
			}
			catch (InterruptedException e){
			}
			
			if(game_begin_f){
				score.clear();
				for(i=0; i<3; i++){
					cheeses[i].hide();
				}
				zap.hide();
				my_mouse.set_coordinates(6, 6);
				rats[0].set_coordinates(10, 5);
				rats[1].set_coordinates(3, 8);

				begin_time = System.currentTimeMillis();
				game_begin_f = false;
				game_on_f = true;
			}
			if(game_on_f){
				for(i=0; i<3; i++){
					cx = cheeses[i].get_x();
					cy = cheeses[i].get_y();
	//				System.out.print("x: " + cx + " y: "+ cy + "\n");
					if(cheeses[i].is_hidden()){
						cx = (int)(Math.random()*18);
						cy = (int)(Math.random()*13);
						while(!board.empty_location(cx, cy)){
							cx = (int)(Math.random()*18);
							cy = (int)(Math.random()*13);
						}
						cheeses[i].set_coordinates(cx, cy);
					}
				}
				my_mouse.update_coordinates(dx, dy, board);
				
				mx = my_mouse.get_x();
				my = my_mouse.get_y();
				for(i=0; i<3; i++){
					cx = cheeses[i].get_x();
					cy = cheeses[i].get_y();
					if((mx == cx) && (my == cy)){
						cheeses[i].hide();
						if(i == 0){
							score.add(2);
						}
						else{
							score.add(1);
						}
					}
				}
				for(i=0; i<2; i++){
					rats[i].new_direction(board);
					
					rx = rats[i].get_x();
					ry = rats[i].get_y();
					for(j=0; j<3; j++){
						cx = cheeses[j].get_x();
						cy = cheeses[j].get_y();
						if((rx == cx) && (ry == cy)){
							cheeses[j].hide();
						}
					}
					if((mx == rx) && (my == ry)){
						zap.set_coordinates(mx, my);
						game_on_f = false;
						game_over_f = true;
					}
				}
				
				time = System.currentTimeMillis() - begin_time;
				clock.set_value((int)(time*8/120000));
				if(time > 120000){
					game_on_f = false;
					game_over_f = true;
				}
//				System.out.print("time: " + time/1000 + "\n");
			}
			if(game_over_f){
				time = System.currentTimeMillis() - begin_time;
				clock.set_value((int)(time/1000));
				if(score.get_value() > hi_score.get_value()){
					hi_score.set_value(score.get_value());
				}
			}
			
			repaint();
		}
	}
}