All pastes #2089127 Raw Edit

Miscellany

public text v1 · immutable
#2089127 ·published 2011-10-11 17:27 UTC
rendered paste body
public class Boat {
    @SuppressWarnings("unused")
    private boolean Sunk = false;
    private String Ship;
    private Position Pos;
    private String Orientation;
    private int hits[];
    private Position locs[];

    Boat(String ship, Position pos, String orientation){
        Ship = ship;
        Pos = pos;
        Orientation = orientation;
        int a = size();
        hits = new int[a];
        locs = new Position[a];

        if(Orientation == "vertical"){
            for(int count = 0; count<size();count++){
                Position fill = new Position((Pos.row()+count),Pos.column()-1);
                locs[count] = fill;
            }
        }else if(Orientation == "horizontal"){
            for(int count = 0; count<size();count++){
                Position fill = new Position((Pos.row()),Pos.column()+count-1);
                locs[count] = fill;
            }
        }
    }
    public String name(){
        return Ship;
    }
    public char abbreviation(){
		if(Ship.equals("aircraft carrier"))
			return 'A';
		else if(Ship.equals("battleship"))
			return 'B';
		else if(Ship.equals("cruiser"))
			return 'C';
		else if(Ship.equals("destroyer"))
			return 'D';
		else if(Ship.equals("submarine"))
			return 'S';
		return 'x';
	}
    public int size(){
        if(Ship == "aircraft carrier"){
            return 5;
        }else if(Ship == "battleship"){
            return 4;
        }else if(Ship == "cruiser"){
            return 3;
        }else if(Ship == "destroyer"){
            return 2;
        }else if(Ship == "submarine"){
            return 3;
        }else{
            return 0;
        }
    }
    public boolean onBoat(Position pos){
        for(int count = 0; count < locs.length;count++){
            if(locs[count].column()==pos.column() && locs[count].row()==pos.row()){
                return true;
            }
        }
        return false;
    }
    public boolean isHit(Position pos){     

        if(Orientation == "vertical"){
            int target = pos.row();
            int boatStart = Pos.row();
            int boatEnd = boatStart+(size()-1);

            if(Pos.column()==pos.column()){
                if(target >= boatStart && target <= boatEnd){
                    target = target-boatStart;
                    if(hits[target]==1){
                        return true;
                    }
                }
            }

        }else if(Orientation == "horizontal"){
            int target = pos.column();
            int boatStart = Pos.column();
            int boatEnd = boatStart+(size()-1);
            if(Pos.row()==pos.row()){
                if(target >= boatStart && target <= boatEnd){
                    target = target-boatStart;
                    if(hits[target]==1){
                        return true;
                    }
                }
            }
        }

        return false;
    }
    public void hit(Position pos){
        if(onBoat(pos)==true && isHit(pos)==false){
            if(Orientation == "vertical"){
                hits[pos.row()-Pos.row()] = 1;
            }else if(Orientation == "horizontal"){
                hits[pos.column()-Pos.column()] = 1;
            }
        }
    }
    public boolean sunk(){
        for(int count=0; count<hits.length;count++){
            if(hits[count] == 0){
                return false;
            }
        }
        Sunk = true;
        return true;
    }
    public Position position(){
        return Pos;
    }
    public String direction(){
        return Orientation;
    }
}