/* * ChessBoard.java * * Created on February 24, 2005, 2:00 AM */ package chess; /** * * @author Mike */ public class ChessBoard { protected ChessPiece board[][] = new ChessPiece[8][8]; // board always this size /** Creates a new instance of ChessBoard * Default version creates starting setup */ public ChessBoard() { // first for convenience make all spaces empty /* for (ChessPiece row[] : board) { for (ChessPiece curr : row) { curr = new ChessPiece(); // default is empty } } **/ for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++ ) { board[row][col] = new ChessPiece(); } } // now put the pieces in board[0][0] = new ChessPiece(Color.white,Piece.rook); board[0][1] = new ChessPiece(Color.white,Piece.knight); board[0][2] = new ChessPiece(Color.white,Piece.bishop); board[0][3] = new ChessPiece(Color.white,Piece.queen); board[0][4] = new ChessPiece(Color.white,Piece.king); board[0][5] = new ChessPiece(Color.white,Piece.bishop); board[0][6] = new ChessPiece(Color.white,Piece.knight); board[0][7] = new ChessPiece(Color.white,Piece.rook); // could have placed pawns easily with a loop board[1][0] = new ChessPiece(Color.white,Piece.pawn); board[1][1] = new ChessPiece(Color.white,Piece.pawn); board[1][2] = new ChessPiece(Color.white,Piece.pawn); board[1][3] = new ChessPiece(Color.white,Piece.pawn); board[1][4] = new ChessPiece(Color.white,Piece.pawn); board[1][5] = new ChessPiece(Color.white,Piece.pawn); board[1][6] = new ChessPiece(Color.white,Piece.pawn); board[1][7] = new ChessPiece(Color.white,Piece.pawn); board[7][0] = new ChessPiece(Color.black,Piece.rook); board[7][1] = new ChessPiece(Color.black,Piece.knight); board[7][2] = new ChessPiece(Color.black,Piece.bishop); board[7][3] = new ChessPiece(Color.black,Piece.queen); board[7][4] = new ChessPiece(Color.black,Piece.king); board[7][5] = new ChessPiece(Color.black,Piece.bishop); board[7][6] = new ChessPiece(Color.black,Piece.knight); board[7][7] = new ChessPiece(Color.black,Piece.rook); board[6][0] = new ChessPiece(Color.black,Piece.pawn); board[6][1] = new ChessPiece(Color.black,Piece.pawn); board[6][2] = new ChessPiece(Color.black,Piece.pawn); board[6][3] = new ChessPiece(Color.black,Piece.pawn); board[6][4] = new ChessPiece(Color.black,Piece.pawn); board[6][5] = new ChessPiece(Color.black,Piece.pawn); board[6][6] = new ChessPiece(Color.black,Piece.pawn); board[6][7] = new ChessPiece(Color.black,Piece.pawn); } /** * return the row corresponding to the passed index */ public ChessPiece [] getRow (int rowNum) { // really should check that passed index is ok return board[rowNum]; } /** * allow unconstrained move of a piece from one location to another. Doesn't check if the move is legal. Not for public consumption in general, but * probably ok for some in same package. Certainly could be used to implement moves for different kinds of pieces - should probably have made protected * Should check indices for being in legal range, but don't do right now */ public boolean movePiece(int startRow, int startCol, int endRow, int endCol) { // copy piece to new location board[endRow][endCol] = board[startRow][startCol]; // now clear old position board[startRow][startCol] = new ChessPiece(); // default peice is empty space return true; // for now assume successful } /** * Show board - use compressed form so that it looks like a board */ public String toString () { String res = ""; for (ChessPiece[] row : board) { for (ChessPiece curr : row) { res += curr.toShortString() + " "; } res += "\n"; } return res; } }