Spielfeld

package blatt3;

public class MyField implements Field {
	private Element[][] field;
	private int shipCount = 0;
	
	public MyField(int width, int height){
		field = new Element[height][width];
		
		for(int i=0; i<height; i++){
			for(int j=0; j<width; j++) field[i][j] = new Water();
		}
	}

	 private void testPosition(int x, int y, int length, int direction) throws Exception{
                if(x>=0 && x < field[0].length && y>=0 && y < field.length ){
					switch(direction){
                                case 0:
                                        if(x+length > field[0].length-1) throw new Exception("The last position of the ship is placed outside of the playground");
										for(int i=0; i<length; i++){
						                    if(field[y][x+i] instanceof Ship) throw new Exception("A ship was already placed.");
                                        }
                                        break;
                                case 1:
                                        if(y+length > field.length-1) throw new Exception("The last position of the ship is placed outside of the playground");
										for(int i=0; i<length; i++){
                                                if(field[y+i][x] instanceof Ship) throw new Exception("A ship was already placed.");
                                        }
                                        break;
                                default: throw new Exception("Illegal direction");
                        }
                }
				else throw new Exception("Illegal starting point");
        		
	}
	
	/* (non-Javadoc)
	 * @see blatt3.Field#placeShip(int, int, int, int)
	 */
	public void placeShip(int x, int y, int length, int direction) throws Exception{
		testPosition(x,y,length,direction);
		switch(direction){
			case 0:
				for(int i=0; i<length; i++){
					field[y][x+i] = new Ship();
				}
				break;
			case 1:
				for(int i=0; i<length; i++){
					field[y+i][x] = new Ship();
				}
				break;
		}
		shipCount+=length;
	}
		
	/* (non-Javadoc)
	 * @see blatt3.Field#toString()
	 */
	public String toString(){
		StringBuffer buf = new StringBuffer();
		for(int i=0; i<field.length; i++){
			for(int j=0; j<field[0].length; j++) buf.append(field[i][j].toString());
			buf.append("\n");
		}
			
			
		return buf.toString();	
	}
	
	/* (non-Javadoc)
	 * @see blatt3.Field#placeBomb(int, int)
	 */
	public boolean placeBomb(int x, int y) throws Exception{
		boolean res = false;
		if(x>=0 && x < field[0].length && y>=0 && y < field.length ){
			if(field[y][x] instanceof Ship && !field[y][x].isMarked()) {
				shipCount--;
				res = true;
			}
			
			field[y][x].mark();
			return res;
		}
		else throw new Exception("Position outside of the playground.");
	}
	
	/* (non-Javadoc)
	 * @see blatt3.Field#finished()
	 */
	public boolean isFinished(){
		return shipCount==0;
	}
	

}

Abstrakte Element-Klasse

package blatt3;

public abstract class Element {
	
	private boolean mark;
	
	public Element(){
		mark = false;
	}
	
	public void mark(){
		mark = true;
	}
	public boolean isMarked(){
		return mark;
	}
	
	public abstract String toString();
	
}

Wasser

package blatt3;

public class Water extends Element {
	
	public Water(){
		super();
	}
	

	public String toString() {
		return isMarked()? "W": "X";
	}
}

Schiff

package blatt3;

public class Ship extends Element {

	public Ship(){
		super();
	}
	
	public String toString() {
		return isMarked()? "S": "X";
	}

}