/**
 * Board.java
 *
 *
 * Caitlin Ross 
 * 
 * 100735219
 */

public class Board {
	
	private int size;
	private char[][][] pieces;	//third part of array has 2 levels
								//0: where the boats are
								//1: 0 if there hasn't been a hit
								//	 1 if there has been a hit, but ship hasn't been sunk,
								//	 2 if ship has been sunk
	//private boolean[][] isHitYet;
	
    public Board(int aSize) {
    	size = aSize;
    	pieces = new char[size][size][2];
    	//isHitYet = new boolean[size][size];
    	
    	for(int i=0; i<size; i++){
    		for(int j=0; j<size; j++){
    			pieces[i][j][0] = '~';
    			pieces[i][j][1] = '0';
    		}
    	}
    }
    
    public void setPiece(int row, int column, char newPiece){pieces[row][column][0] = newPiece;}
    public void hitPiece(int row, int column){pieces[row][column][1] = '1';}
    public void sinkPiece(int row, int column){pieces[row][column][1] = '2';}
    
    public char getPiece(int row, int column){return pieces[row][column][0];}
    public boolean isPieceHit(int row, int column){
    	if (pieces[row][column][1] == '1' || pieces[row][column][1] == '2')
    		return true;
    	else
    		return false;
    }
    public boolean isPieceSunk(int row, int column){
    	if (pieces[row][column][1] == '2')
    		return true;
    	else
    		return false;
    }
    public int getSize(){return size;}
    
    public void displayRow(int row, int whichBoard){
    	System.out.print(row + " ");
    	for (int i=0; i<size; i++){
    		if(whichBoard == 0){		//0 for the user
    			if (pieces[row][i][1] == '0')
    				System.out.print(pieces[row][i][0] + " ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] == '~')
    				System.out.print(". ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] != '~')
    				System.out.print("X ");
    		}
    		else if (whichBoard ==1){		//1 for the computer
    			if (pieces[row][i][1] == '0')
    				System.out.print("~ ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] == '~')
    				System.out.print(". ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] != '~')
    				System.out.print("X ");
    			else if (pieces[row][i][1] == '2')
    				System.out.print(pieces[row][i][0] + " ");
    		}
    	}
    }
    public void displayCheatRow(int row, int whichBoard){
    	System.out.print(row + " ");
    	for (int i=0; i<size; i++){
    		if(whichBoard == 0){		//0 for the user
    			if (pieces[row][i][1] == '0')
    				System.out.print(pieces[row][i][0] + " ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] == '~')
    				System.out.print(". ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] != '~')
    				System.out.print("X ");
    		}
    		else if (whichBoard ==1){		//1 for the computer
    			if (pieces[row][i][1] == '0' || pieces[row][i][1] == '2')
    				System.out.print(pieces[row][i][0] + " ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] == '~')
    				System.out.print(". ");
    			else if (pieces[row][i][1] == '1' && pieces[row][i][0] != '~')
    				System.out.print("X ");
    				
    		}
    	}
    }
    
    public boolean isValidMove(int row, int column){
    	if (row>=size || row<0 || column>=size || column<0)
    		return false;
    	if (pieces[row][column][1] == '1' || pieces[row][column][1] == '2')
    		return false;
    	return true;
    }
    
}