/*
 * =====================================================================================
 *
 *       Filename:  AbstractChessBoard.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  21/02/2011 08:18:15
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  John Ryland (jryland), jryland@xiaofrog.com
 *        Company:  InvertedLogic
 *
 * =====================================================================================
 */


#include <AbstractChessBoard.h>


AbstractChessBoard::AbstractChessBoard()
{
    pieces += new AbstractChessPiece( Rook,   White, 0, 0 );
    pieces += new AbstractChessPiece( Knight, White, 0, 1 );
    pieces += new AbstractChessPiece( Bishop, White, 0, 2 );
    pieces += new AbstractChessPiece( Queen,  White, 0, 3 );
    pieces += new AbstractChessPiece( King,   White, 0, 4 );
    pieces += new AbstractChessPiece( Bishop, White, 0, 5 );
    pieces += new AbstractChessPiece( Knight, White, 0, 6 );
    pieces += new AbstractChessPiece( Rook,   White, 0, 7 );
    for (int i = 0; i < 8; i++) {
        pieces += new AbstractChessPiece( Pawn, White, 1, i );
        pieces += new AbstractChessPiece( Pawn, Black, 6, i );
    }
    pieces += new AbstractChessPiece( Rook,   Black, 7, 0 );
    pieces += new AbstractChessPiece( Knight, Black, 7, 1 );
    pieces += new AbstractChessPiece( Bishop, Black, 7, 2 );
    pieces += new AbstractChessPiece( Queen,  Black, 7, 3 );
    pieces += new AbstractChessPiece( King,   Black, 7, 4 );
    pieces += new AbstractChessPiece( Bishop, Black, 7, 5 );
    pieces += new AbstractChessPiece( Knight, Black, 7, 6 );
    pieces += new AbstractChessPiece( Rook,   Black, 7, 7 );
}


AbstractChessBoard::~AbstractChessBoard()
{
    foreach (AbstractChessPiece *piece, pieces)
        delete piece;
    pieces.clear();
}


AbstractChessPiece *AbstractChessBoard::pieceAt(unsigned int row, unsigned int col)
{
    foreach (AbstractChessPiece *piece, pieces) {
        if ( piece->position.rank == row && piece->position.file == col ) {
            return piece;
        }
    }
    return 0;
}


ChessBoardPieceDescription AbstractChessBoard::at(unsigned int row, unsigned int col)
{
    ChessBoardPieceDescription desc;
    desc.rank = row;
    desc.file = col;
    desc.type = None;
    desc.side = White;

    foreach (AbstractChessPiece *piece, pieces) {
        if ( piece->position.rank == row && piece->position.file == col ) {
            desc.type = piece->type;
            desc.side = piece->color;
            return desc;
        }
    }

    return desc;
}


