package ie.gmit.sw.ai;

import ie.gmit.sw.ai.traversers.*;
import ie.gmit.sw.ai.node.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GameRunner implements KeyListener{
	public static final int MAZE_DIMENSION = 100;
	private static final int IMAGE_COUNT = 14;
	private GameView view;
	private Maze model;
	private int currentRow;
	private int currentCol;
	
	public GameRunner() throws Exception{
		model = new Maze(MAZE_DIMENSION);
    	view = new GameView(model);

    	currentRow=model.getPlayer().getRow();
        currentCol=model.getPlayer().getCol();

    	
    	Sprite[] sprites = getSprites();
    	view.setSprites(sprites);
    	
    	//placePlayer();
    	
    	Dimension d = new Dimension(GameView.DEFAULT_VIEW_SIZE, GameView.DEFAULT_VIEW_SIZE);
    	view.setPreferredSize(d);
    	view.setMinimumSize(d);
    	view.setMaximumSize(d);
    	
    	JFrame f = new JFrame("GMIT - B.Sc. in Computing (Software Development)");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.addKeyListener(this);
        f.getContentPane().setLayout(new FlowLayout());
        f.add(view);
        f.setSize(1000,1000);
        f.setLocation(100,100);
        f.pack();
        f.setVisible(true);

        //setting test goal node
        //model.getMaze()[3][3].setGoalNode(true);

		//Traversator t = new BruteForceTraversator(true);
        //Traversator t = new BestFirstTraversator(model.getMaze()[1][69]);

        //Traversator t = new DepthLimitedDFSTraversator(10);

        //transverse from node 0 0 //can change 0 0 to sprites location to search from their location
        //t.traverse(model.getMaze(), model.getMaze()[20][20]);
        
	}

	// need to update maze.set()
	private void placePlayer(){   	
    	//currentRow = (int) (MAZE_DIMENSION * Math.random());
    	//currentCol = (int) (MAZE_DIMENSION * Math.random());
    	//model.set(currentRow, currentCol, new Node(currentRow, currentCol, 5)); //A Spartan warrior is at index 5
       // model.setPlayer(currentRow, currentCol);
    	//updateView();
	}
	
	private void updateView(){
		view.setCurrentRow(currentRow);
		view.setCurrentCol(currentCol);
	}

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT && currentCol < MAZE_DIMENSION - 1) {
        	if (isValidMove(currentRow, currentCol + 1)) currentCol++;   		
        }else if (e.getKeyCode() == KeyEvent.VK_LEFT && currentCol > 0) {
        	if (isValidMove(currentRow, currentCol - 1)) currentCol--;	
        }else if (e.getKeyCode() == KeyEvent.VK_UP && currentRow > 0) {
        	if (isValidMove(currentRow - 1, currentCol)) currentRow--;
        }else if (e.getKeyCode() == KeyEvent.VK_DOWN && currentRow < MAZE_DIMENSION - 1) {
        	if (isValidMove(currentRow + 1, currentCol)) currentRow++;        	  	
        }else if (e.getKeyCode() == KeyEvent.VK_Z){
        	view.toggleZoom();
        }else{
        	return;
        }
        
        updateView();       
    }
    public void keyReleased(KeyEvent e) {} //Ignore
	public void keyTyped(KeyEvent e) {} //Ignore

    
	private boolean isValidMove(int row, int col){
		if (row <= model.size() - 1 && col <= model.size() - 1 && model.get(row, col).getId() == -1){


			model.set(currentRow, currentCol, model.get(row, col));

            model.get(currentRow, currentCol).setRow(currentRow);
            model.get(currentRow, currentCol).setCol(currentCol);

			model.set(row, col, model.getPlayer());

			model.get(row,col).setRow(row);
            model.get(row,col).setCol(col);

			return true;
		}else{
			return false; //Can't move
		}
	}
	
	private Sprite[] getSprites() throws Exception{
		//Read in the images from the resources directory as sprites. Note that each
		//sprite will be referenced by its index in the array, e.g. a 3 implies a Bomb...
		//Ideally, the array should dynamically created from the images... 
		Sprite[] sprites = new Sprite[IMAGE_COUNT];
		sprites[0] = new Sprite("Hedge", "resources/hedge.png");
		sprites[1] = new Sprite("Sword", "resources/sword.png");
		sprites[2] = new Sprite("Help", "resources/help.png");
		sprites[3] = new Sprite("Bomb", "resources/bomb.png");
		sprites[4] = new Sprite("Hydrogen Bomb", "resources/h_bomb.png");
		sprites[5] = new Sprite("Spartan Warrior", "resources/spartan_1.png", "resources/spartan_2.png");
		sprites[6] = new Sprite("Black Spider", "resources/black_spider_1.png", "resources/black_spider_2.png");
		sprites[7] = new Sprite("Blue Spider", "resources/blue_spider_1.png", "resources/blue_spider_2.png");
		sprites[8] = new Sprite("Brown Spider", "resources/brown_spider_1.png", "resources/brown_spider_2.png");
		sprites[9] = new Sprite("Green Spider", "resources/green_spider_1.png", "resources/green_spider_2.png");
		sprites[10] = new Sprite("Grey Spider", "resources/grey_spider_1.png", "resources/grey_spider_2.png");
		sprites[11] = new Sprite("Orange Spider", "resources/orange_spider_1.png", "resources/orange_spider_2.png");
		sprites[12] = new Sprite("Red Spider", "resources/red_spider_1.png", "resources/red_spider_2.png");
		sprites[13] = new Sprite("Yellow Spider", "resources/yellow_spider_1.png", "resources/yellow_spider_2.png");
		return sprites;
	}
	
	public static void main(String[] args) throws Exception{
		new GameRunner();
	}
}
@@ -21,73 +21,8 @@
 i;%0A%0A
-import ie.gmit.sw.ai.traversers.*;%0Aimport ie.gmit.sw.ai.node.*;%0A%0A
 impo
@@ -521,22 +521,16 @@
 Col();%0A%0A
-    %09%0A
     %09Spr
@@ -600,36 +600,8 @@
   %09%0A
-    %09//placePlayer();%0A    %09%0A
     
@@ -1140,838 +1140,11 @@
 e);%0A
-%0A        //setting test goal node%0A        //model.getMaze()%5B3%5D%5B3%5D.setGoalNode(true);%0A%0A%09%09//Traversator t = new BruteForceTraversator(true);%0A        //Traversator t = new BestFirstTraversator(model.getMaze()%5B1%5D%5B69%5D);%0A%0A        //Traversator t = new DepthLimitedDFSTraversator(10);%0A%0A        //transverse from node 0 0 //can change 0 0 to sprites location to search from their location%0A        //t.traverse(model.getMaze(), model.getMaze()%5B20%5D%5B20%5D);%0A        %0A%09%7D%0A%0A%09// need to update maze.set()%0A%09private void placePlayer()%7B   %09%0A    %09//currentRow = (int) (MAZE_DIMENSION * Math.random());%0A    %09//currentCol = (int) (MAZE_DIMENSION * Math.random());%0A    %09//model.set(currentRow, currentCol, new Node(currentRow, currentCol, 5)); //A Spartan warrior is at index 5%0A       // model.setPlayer(currentRow, currentCol);%0A    %09//updateView();%0A%09%7D%0A%09
+%09%7D%0A
 %0A%09pr

Remove redundant code