package game;

import exception.PlacementFailedException;

public abstract class Board {
	private Square[][] board;
	private Move currentMove;

	public Board(int size) {
		board = new Square[size][size];

		initializeBoard();
	}

	public void initializeBoard() {
		for (int row = 0; row < board.length; row++)
			for (int col = 0; col < board[row].length; col++)
				board[row][col] = new Square(' ');
	}

	public void setMove(Move move) {
		currentMove = move;
	}

	public Move getMove() {
		return currentMove;
	}

	public void attemptPlace() {
		if (canPlace()) {
			place();
		} else {
			throw new PlacementFailedException("The piece cannot be placed on square \""
					+ currentMove.getCoordinate().toAlphaNumericalString() + '\"');
		}
	}

	public void place() {
		int row = currentMove.getCoordinate().getCol();
		int col = currentMove.getCoordinate().getRow();
		PieceType piece = currentMove.getPiece();
		board[row][col].setChar(piece.getChar());
	}

	public abstract boolean canPlace();

	public boolean isEmpty(Coordinate c) {
		return board[c.getCol()][c.getRow()].getChar() == ' ';
	}

	public String toString() {
		String out = " ";
		out += labelEachColumn() + "\n";
		out += printAllRows() + "\n";

		return out;
	}

	protected String labelEachColumn() {
		String out = "";
		for (int col = 1; col <= board.length; col++)
			out += " " + labelColumn(col);

		return out;
	}

	protected String labelColumn(int col) {
		return "" + (col % 10 == 0 ? col / 10 : col % 10);
	}

	protected String printAllRows() {
		String out = "";
		for (int row = 0; row < board[0].length; row++) {
			out += printRowLabel(row) + " ";
			out += printRow(row) + "\n";
			out += printRowDivider(!isBottomRow(row)) + "\n";
		}
		return out;
	}

	protected String printRowLabel(int row) {
		return "" + (char) ('A' + row);
	}

	protected String printRow(int row) {
		String out = "" + board[row][0].getChar();
		for (int col = 1; col < board.length; col++) {
			out += "|" + board[row][col].getChar();
		}
		return out;
	}

	protected String printRowDivider(boolean shouldPrint) {
		if (shouldPrint) {
			String out = "  -";
			for (int col = 1; col < board.length; col++)
				out += "--";
			return out;
		} else {
			return "";
		}
	}

	protected boolean isBottomRow(int row) {
		return board.length - 1 == row;
	}

	public abstract char gameEnd();

	public boolean squareExists(Coordinate out) {
		return out.getCol() >= 0 && out.getCol() < board[0].length && out.getRow() >= 0 && out.getRow() < board.length;
	}

	public char[][] getCharBoard() {
		char[][] out = new char[board.length][board[0].length];
		for (int row = 0; row < board.length; row++)
			for (int col = 0; col < board[row].length; col++)
				out[row][col] = board[row][col].getChar();
		return out;
	}
}
@@ -206,16 +206,39 @@
 %5Bsize%5D;%0D
+%0A%09%09currentMove = null;%0D
 %0A%0D%0A%09%09ini
@@ -510,63 +510,8 @@
 %0D%0A%0D%0A
-%09public Move getMove() %7B%0D%0A%09%09return currentMove;%0D%0A%09%7D%0D%0A%0D%0A
 %09pub
@@ -526,20 +526,19 @@
  attempt
-Plac
+Mov
 e() %7B%0D%0A%09
@@ -542,23 +542,24 @@
 %0D%0A%09%09if (
-canPlac
+validMov
 e()) %7B%0D%0A
@@ -561,20 +561,26 @@
 ) %7B%0D%0A%09%09%09
-plac
+executeMov
 e();%0D%0A%09%09
@@ -589,16 +589,51 @@
 else %7B%0D%0A
+%09%09%09// TODO change failure message%0D%0A
 %09%09%09throw
@@ -799,20 +799,26 @@
 ic void 
-plac
+executeMov
 e() %7B%0D%0A%09
@@ -1026,37 +1026,87 @@
 lic 
-abstract boolean canPlac
+boolean validMove() %7B%0D%0A%09%09return squareExists(currentMove.getCoordinat
 e()
+)
 ;%0D
+%0A%09%7D%0D
 %0A%0D%0A%09
@@ -2945,9 +2945,66 @@
 t;%0D%0A%09%7D%0D%0A
+%0D%0A%09public Move getMove() %7B%0D%0A%09%09return currentMove;%0D%0A%09%7D%0D%0A%0D%0A
 %7D

change Board