import java.applet.Applet;
import java.awt.*;

public class Othello03 extends Applet implements Runnable
{
	static final int BOXWID = 64;
	static final int NONE = 0;
	static final int BLACK = 1;
	static final int WHITE = 2;
	static final int OUT = 3;
	MediaTracker mt = new MediaTracker(this);
	int[][] box = new int[10][10];
	Image[][] stone = new Image[3][8];
	Image offImage;
	Graphics offScreen;
	Graphics screen;
	Thread thread;
	int cursorX, cursorY;
	int lastX, lastY;
	static final int[] dx = {0, 1, 1, 1, 0, -1, -1, -1};
	static final int[] dy = {-1, -1, 0, 1, 1, 1, 0, -1};
	static final int[] otherSide = {NONE, WHITE, BLACK, NONE};
	int nextTurn;
	boolean[][] rotate = new boolean[9][9];
	boolean[][] lastRotate = new boolean[9][9];
	boolean rotating = false;
	int count = 0;
	Dimension mySize;

	public void init() {
		mySize = size();
		screen = getGraphics();
		offImage = createImage(mySize.width, mySize.height);
		offScreen = offImage.getGraphics();
		for (int i = 0; i < 10; i++) {
			box[0][i] = OUT;
			box[9][i] = OUT;
			box[i][0] = OUT;
			box[i][9] = OUT;
		}
		/*
		box[4][4] = WHITE;
		box[4][5] = BLACK;
		box[5][4] = BLACK;
		box[5][5] = WHITE;
		*/
		for (int x = 1; x <= 8; x++) {
			for (int y = 1; y <= 8; y++) {
				box[x][y] = (int)(Math.random()*3);
			}
		}
		nextTurn = BLACK;
		for (int n = 0; n < 8; n++) {
			stone[WHITE][n] = getImage(getCodeBase(), "../../image/25_0"+(n+1)+".GIF");
			mt.addImage(stone[WHITE][n], 0);
			stone[BLACK][n] = getImage(getCodeBase(), "../../image/26_0"+(n+1)+".GIF");
			mt.addImage(stone[BLACK][n], 0);
		}
		try {
			mt.waitForID(0);
		} catch (InterruptedException e) {
		}
	}

	public void paint(Graphics g) {
		g.drawImage(offImage, 0, 0, null);
	}

	public synchronized void update(Graphics g) {
		if (rotating) {
			for (int x = 1; x <=8; x++) {
				for (int y = 1; y <= 8; y++) {
					if (box[x][y] != NONE) {
						if (rotate[x][y]) {
							offScreen.drawImage(stone[box[x][y]][count], (x-1)*BOXWID, (y-1)*BOXWID, null);
							lastRotate[x][y] = true;
						} else if (lastRotate[x][y]) {
							offScreen.drawImage(stone[box[x][y]][0], (x-1)*BOXWID, (y-1)*BOXWID, null);
							lastRotate[x][y] = false;
						}
					}
				}
			}
		}
		paint(g);
	}

	public void start() {
		updateOffScreen();
		repaint();
		if (thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	}

	public void stop() {
		if (thread != null) {
			thread.stop();
		}
		thread = null;
	}

	boolean isPosibleToPlace(int x, int y, int color) {
		if (box[x][y] == NONE) {
			for (int i = 0; i < 8; i++) {
				int wx = x+dx[i];
				int wy = y+dy[i];
				if (box[wx][wy] == otherSide[color]) {
					do {
						wx += dx[i];
						wy += dy[i];
					} while (box[wx][wy] == otherSide[color]);
					if (box[wx][wy] == color) {
						return true;
					}
				}
			}
		}
		return false;
	}

	void updateRotate(int x, int y, int color) {
		for (int i = 1; i <= 8; i++) {
			for (int j = 1; j <= 8; j++) {
				rotate[i][j] = false;
			}
		}
		if (box[x][y] == NONE) {
			for (int i = 0; i < 8; i++) {
				int wx = x+dx[i];
				int wy = y+dy[i];
				if (box[wx][wy] == otherSide[color]) {
					do {
						wx += dx[i];
						wy += dy[i];
					} while (box[wx][wy] == otherSide[color]);
					if (box[wx][wy] == color) {
						wx -= dx[i];
						wy -= dy[i];
						while (box[wx][wy] == otherSide[color]) {
							rotate[wx][wy] = true;
							wx -= dx[i];
							wy -= dy[i];
						}
					}
				}
			}
		}
	}

	synchronized void updateOffScreen() {
		offScreen.setColor(Color.black);
		offScreen.fillRect(0, 0, mySize.width, mySize.height);
		for (int x = 1; x <=8; x++) {
			for (int y = 1; y <= 8; y++) {
				if (box[x][y] != NONE) {
					offScreen.drawImage(stone[box[x][y]][0], (x-1)*BOXWID, (y-1)*BOXWID, null);
				} else if (x == cursorX && y == cursorY) {
					offScreen.setColor(Color.darkGray);
					offScreen.fill3DRect((cursorX-1)*BOXWID, (cursorY-1)*BOXWID, BOXWID, BOXWID, true);
				}
			}
		}
	}

	public void run() {
		int minX = 8;
		int minY = 8;
		int maxX = 1;
		int maxY = 1;
		while (true) {
			if (isPosibleToPlace(cursorX, cursorY, nextTurn)) {
				if (lastX != cursorX || lastY != cursorY) {
					updateRotate(cursorX, cursorY, nextTurn);
					count = 0;
					lastX = cursorX;
					lastY = cursorY;
				} else {
					if (++count >= 8) count = 0;
					minX = minY = 8;
					maxX = maxY = 1;
				}
				for (int x = 1; x <= 8; x++) {
					for (int y = 1; y <= 8; y++) {
						if (rotate[x][y]) {
							if (x < minX) minX = x;
							if (y < minY) minY = y;
							if (x > maxX) maxX = x;
							if (y > maxY) maxY = y;
						}
					}
				}
				rotating = true;
				repaint((minX-1)*BOXWID, (minY-1)*BOXWID, (maxX-minX+1)*BOXWID, (maxY-minY+1)*BOXWID);
			} else {
				if (rotating) {
					count = 0;
					rotating = false;
					updateOffScreen();
					repaint();
				}
			}
			try {
				thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
	}

	public synchronized boolean mouseMove(Event evt, int x, int y) {
		int wx = x/BOXWID+1;
		int wy = y/BOXWID+1;
		if (wx != cursorX || wy != cursorY) {
			if (box[cursorX][cursorY] == NONE) {
				offScreen.setColor(Color.black);
				offScreen.fillRect((cursorX-1)*BOXWID, (cursorY-1)*BOXWID, BOXWID, BOXWID);
			}
			if (box[wx][wy] == NONE) {
				offScreen.setColor(Color.darkGray);
				offScreen.fill3DRect((wx-1)*BOXWID, (wy-1)*BOXWID, BOXWID, BOXWID, true);
			}
			cursorX = wx;
			cursorY = wy;
			screen.drawImage(offImage, 0, 0, null);
		}
		return true;
	}

	public synchronized boolean mouseExit(Event evt, int x, int y) {
		if (box[cursorX][cursorY] == NONE) {
			offScreen.setColor(Color.black);
			offScreen.fillRect((cursorX-1)*BOXWID, (cursorY-1)*BOXWID, BOXWID, BOXWID);
		}
		screen.drawImage(offImage, 0, 0, null);
		cursorX = cursorY = 0;
		return true;
	}
}

