package tank;

/*
 * Copyright (c) 2013 midgardabc.com
 */


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

/**
 * @version 3.0
 */
public class BattleFieldTemplate2 extends JPanel {

	final boolean COLORDED_MODE = true;
	
	final int BF_WIDTH = 576;
	final int BF_HEIGHT = 576;
	
	// 1 - top, 2 - bottom, 3 - left, 4 - right
		
	
	int bulletX     = -100;
	int bulletY     = -100;
	int bulletSpeed =  5;
	
	int tankX = 128;
	int tankY = 512;
	int speed = 10;
	int defaultStep = 1;
	int endOffield=8*64-defaultStep;  //��������� ������� ����
	int beginOffield=defaultStep;		//��������� ������� ����
	int targetX = 0;
	int targetY = 0;
	int tankDirection = 4;

	String[][] battleField = new String[][]{
			{" "," "," "," "," "," "," "," "," "},
			{" ","B","B","B"," ","B","B","B"," "},
			{" "," ","B"," "," "," ","B"," "," "},
			{"B"," ","B"," ","B"," ","B"," ","B"},
			{"B"," "," "," ","B"," "," "," ","B"},
			{"B"," "," ","B","B","B"," "," ","B"},
			{" "," "," "," "," "," "," "," "," "},
			{" ","B","B","B"," ","B","B","B"," "},
			{" "," "," "," "," "," "," "," "," "}
	};

	/**
	 * Write your code here.
	 */
	void runTheGame() throws Exception {
		moveToQuadrant(6, 2);
		Thread.sleep(1000);
		/*moveToQuadrant(3, 3);
		Thread.sleep(2000);
		moveToQuadrant(1, 4);
		Thread.sleep(2000);
		moveToQuadrant(9, 8);*/
		turn(4);
		System.out.println(getQuadrant(tankX,tankY));
		while (true){
		fire();
		
		}
	}
	
	boolean processInterception() {
		String str = getQuadrant(bulletX,bulletY);
		int v=0,h=0;
		v = Integer.valueOf(String.valueOf(str.charAt(0)));
		h = Integer.valueOf(String.valueOf(str.charAt(2)));
		if (!battleField[v][h].trim().isEmpty()) {
			battleField[v][h] = " ";
			bulletX = -100;
			bulletY = -100;
			repaint();
			//Thread.sleep(bulletSpeed);
			return true;
		}
		return false;
	}
	
	static String getQuadrantXY(int v, int h){
		//������ ���������� �������� ������ ���� ��������� � ������� String 64_192;
		return String.valueOf((v-1)*64)+"_"+String.valueOf((h-1)*64);
	}
	static String getQuadrant(int x,int y){
		return y / 64 + "_" + x / 64;
		
	}
	
	void fire() throws Exception {
		bulletX     = tankX+25;
		bulletY     = tankY+25;
		if (tankDirection == 4){
				while (bulletX<BF_WIDTH&&!processInterception()){bulletX++;repaint();Thread.sleep(bulletSpeed);}
				bulletX = tankX+25;
			}
		if (tankDirection == 3){
				while (bulletX>0&&!processInterception()){bulletX--;repaint();Thread.sleep(bulletSpeed);}
				bulletX = tankX+25;
				}
		if (tankDirection == 2){
				while (bulletY<BF_HEIGHT&&!processInterception()){bulletY++;repaint();Thread.sleep(bulletSpeed);}
				bulletY = tankY+25;
			}
		if (tankDirection == 1){
				while (bulletY>0&&!processInterception()){bulletY--;repaint();Thread.sleep(bulletSpeed);}
				bulletY = tankY+25;
			}
		
	}


	
	void move(int direction) throws InterruptedException {
		turn(direction);
		if (direction==4){moveRight(defaultStep);}
		if (direction==3){moveLeft(defaultStep);}
		if (direction==2){moveDown(defaultStep);}
		if (direction==1){moveUp(defaultStep);}
				
	}
	void turn(int direction) throws InterruptedException{
		tankDirection = direction;
		}
	void moveRight(int defaultStep) throws InterruptedException {
		int beginX=tankX;
		boolean normalSpeed=slowDown();
		System.out.println("������� �� : "+tankX+":"+tankY);
		while (tankX<=(beginX+64-defaultStep)&&tankX<=endOffield) {tankX += defaultStep; repaint();Thread.sleep(speed);}
		slowUp(normalSpeed);
	}
	void moveLeft(int defaultStep) throws InterruptedException {
		int beginX=tankX;
		boolean normalSpeed=slowDown();
		System.out.println( "������  �� : "+tankX+":"+tankY);
		while (tankX>=(beginX-64+defaultStep)&&tankX>=beginOffield) {tankX -= defaultStep; repaint();Thread.sleep(speed);} 
		slowUp(normalSpeed);
	}
	void moveUp(int defaultStep) throws InterruptedException {
		int beginY=tankY;
		boolean normalSpeed=slowDown();
		System.out.println(   "�����   �� : "+tankX+":"+tankY);
		while (tankY>=(beginY-64+defaultStep)&&tankY>=beginOffield) {tankY -= defaultStep; repaint();Thread.sleep(speed);} 
		slowUp(normalSpeed);
	}
	void moveDown(int defaultStep) throws InterruptedException {
		int beginY=tankY;
		boolean normalSpeed=slowDown();
		System.out.println( "����    �� : "+tankX+":"+tankY);
		while (tankY<=(beginY+64-defaultStep)&&tankY<=endOffield) {tankY += defaultStep; repaint();Thread.sleep(speed);} 
		slowUp(normalSpeed);
	}
	boolean slowDown(){
		boolean normalSpeed=true;
		if (Math.abs(targetX-tankX)<=64&&Math.abs(targetY-tankY)<=64){
			speed=speed*2;
			normalSpeed=false; 
		}
		return normalSpeed;
	}
	void slowUp (boolean normalSpeed){
		if (!normalSpeed){speed=speed/2;}
	}	
	void moveToQuadrant(int v, int h) throws InterruptedException{
		int indexPoint=0;
		boolean targetXbingo=false;
		boolean targetYbingo=false;
		String getStr=getQuadrantXY(v, h);
		if (!getStr.contains("_")){System.out.println("����� moveTo - ������� ������ ����������"); return;}
		indexPoint=getStr.indexOf("_");
		targetY=Integer.parseInt(getStr.substring(0,indexPoint));//int ���������� ���� �������� ��������
		targetX=Integer.parseInt(getStr.substring(indexPoint+1));
		while (true) {
			if (tankX != targetX){if (tankX < targetX){move(4);}else{move(3);}}else{targetXbingo=true;}
			if (targetXbingo) {System.out.println("�������!");break;}
		}
		while (true) {
			if (tankY != targetY){if (tankY < targetY){move(2);}else{move(1);}}else{targetYbingo=true;}
			if (targetYbingo) {System.out.println("�������!");break;}
		}
		
	}

	// Magic bellow. Do not worry about this now, you will understand everything in this course.
	// Please concentrate on your tasks only.
	
	
	public static void main(String[] args) throws Exception {
		BattleFieldTemplate2 bf = new BattleFieldTemplate2();
		bf.runTheGame();
	}

	public BattleFieldTemplate2() throws Exception {
		JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
		frame.setLocation(750, 150);
		frame.setMinimumSize(new Dimension(BF_WIDTH + 17, BF_HEIGHT + 38));
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.getContentPane().add(this);
		frame.pack();
		frame.setVisible(true);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		int i = 0;
		Color cc;
		for (int v = 0; v < 9; v++) {
			for (int h = 0; h < 9; h++) {
				if (COLORDED_MODE) {
					if (i % 2 == 0) {
						cc = new Color(252, 241, 177);
					} else {
						cc = new Color(233, 243, 255);
					}
				} else {
					cc = new Color(180, 180, 180);
				}
				i++;
				g.setColor(cc);
				g.fillRect(h * 64, v * 64, 64, 64);
			}
		}
		
		for (int j = 0; j < battleField.length; j++) {
			for (int k = 0; k < battleField.length; k++) {
				if (battleField[j][k].equals("B")) {
					String coordinates = getQuadrantXY(j + 1, k + 1);
					int separator = coordinates.indexOf("_");
					int y = Integer.parseInt(coordinates.substring(0, separator));
					int x = Integer.parseInt(coordinates.substring(separator + 1));
					g.setColor(new Color(0, 0, 255));
					g.fillRect(x, y, 64, 64);
				}
			}
		}
		
		g.setColor(new Color(255, 0, 0));
		g.fillRect(tankX, tankY, 64, 64);
		
		g.setColor(new Color(0, 255, 0));
		if (tankDirection == 1) {
			g.fillRect(tankX + 20, tankY, 24, 34);
		} else if (tankDirection == 2) {
			g.fillRect(tankX + 20, tankY + 30, 24, 34);
		} else if (tankDirection == 3) {
			g.fillRect(tankX, tankY + 20, 34, 24);
		} else {
			g.fillRect(tankX + 30, tankY + 20, 34, 24);
		}
		
		g.setColor(new Color(255, 255, 0));
		g.fillRect(bulletX, bulletY, 14, 14);
	}

}