/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gamelogic;

import java.util.List;
import alieninterfaces.*;

/**
 *
 * @author guberti
 */
public class ViewImplementation implements View {
    
    private final List<AlienContainer> aCs;
    private final int bottomX;
    private final int bottomY;
    private final int size;
    
    public ViewImplementation ()
    {
        this.aCs = null;
        this.bottomX = 0;
        this.bottomY = 0;
        this.size = 0;
    }
    
    public ViewImplementation (List<AlienContainer> aCs, int bottomX, int bottomY, int size) {
        this.aCs = aCs;
        this.bottomX = bottomX;
        this.bottomY = bottomY;
        this.size = size;
    }
    
    @Override
    public int getEnergyAtPos(int x, int y) throws CantSeeSquareException {
        if (bottomX + 2 * size < x ||
                bottomX > x ||
                bottomY + 2 * size < y ||
                bottomY > y) {
            throw new CantSeeSquareException();
        }
        for (AlienContainer aC : aCs) {
            if (aC.x == x && aC.y == y) {
                return aC.energy;
            }
        }
        return -1;
    }
    
    @Override
    public boolean isAlienAtPos(int x, int y) throws CantSeeSquareException {
        return getEnergyAtPos(x, y) > -1;
    }
    
    @Override
    public int[] getClosestAlienPos(int x, int y) {
        if (aCs == null)
        {
            // todo: get rid of this
            // this is a stand-in view until we have a real one. 
            // for now, NO VISIBLE ALIENS FOR YOU!!!
            int [] invalid_pos = {Integer.MAX_VALUE, Integer.MAX_VALUE};
            return invalid_pos;
        }
        
        
        if (aCs.isEmpty()) { // If there are no visible aliens
            //throw new NoVisibleAliensException();
            int [] invalid_pos = {Integer.MAX_VALUE, Integer.MAX_VALUE};
            return invalid_pos;
        }
        
        // Returns an array of two numbers corresponding to the x and y of the alien
        int alienX = aCs.get(0).x;
        int alienY = aCs.get(0).y;
        
        for (AlienContainer aC : aCs) {
            if (getPointDistance(x, y, aC.x, aC.y) < 
                    getPointDistance(x, y, alienX, alienY)) {
                alienX = aC.x;
                alienY = aC.y;
            }
        }
        
        int[] pos = {alienX, alienY};
        
        return pos;
    }
    
    private int getPointDistance(int x1, int y1, int x2, int y2) {
        // Square roots are expensive, and we only need to determine
        // which of two distances is the greatest, so we use the distance
        // formula but don't square root it because we can simply compare
        // the squares of distances instead
        
        int distance = (int) Math.pow(Math.abs(x1 - x2), 2);
        return distance + (int) Math.pow(Math.abs(y1 - y2), 2);
    }
}

@@ -1516,316 +1516,8 @@
 y) %7B
-%0A        if (aCs == null)%0A        %7B%0A            // todo: get rid of this%0A            // this is a stand-in view until we have a real one. %0A            // for now, NO VISIBLE ALIENS FOR YOU!!!%0A            int %5B%5D invalid_pos = %7BInteger.MAX_VALUE, Integer.MAX_VALUE%7D;%0A            return invalid_pos;%0A        %7D%0A 
     

Remove stand-in code for basic view functions