Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Chess

 

Chess has many chess classes

 

 

 

 

 

chessboard.h

 

#ifndef CHESSBOARD_H
#define CHESSBOARD_H
//---------------------------------------------------------------------------
#include "chessboard2d.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
typedef Board2d Board;
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSBOARD_H

 

 

 

 

 

chessboard.cpp

 

//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------

 

 

 

 

 

chessboard2d.h

 

#ifndef CHESSBOARD2D_H
#define CHESSBOARD2D_H
//---------------------------------------------------------------------------
#include <iosfwd>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/logic/tribool.hpp>
#include <boost/shared_ptr.hpp>
//---------------------------------------------------------------------------
#include "chessfwd.h"
#include "chesscolor.h"
#include "chessmove.h"
//#include "chesspiece.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
struct Piece;
//---------------------------------------------------------------------------
//Board has
//- a collection of Pieces
//- a history of Moves
struct Board2d
{
  typedef boost::shared_ptr<Piece> PiecePtr;
  typedef std::vector<PiecePtr> Pieces;

  Board2d();

  //bool CanDoMove(const Move& move) const;
  //void DoMove(const Move& move);

  //bool IsVisible(const Square& s);

  //const BoardVisibleType GetInSight(const Piece::Color color) const;
  //const Piece GetPiece(const int x, const int y) const;

  Color GetActivePlayer() const;
  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();

  Color GetColor(const Square& s) const;

  template<class T> bool IsType(const Square& s) const
  {
    return dynamic_cast<T*>(this->Find(s).get());
  }

  //const std::vector<Move> GetAllValidMoves(const int x, const int y) const;
  //const std::vector<Move> GetAllPossibleMoves(const Piece::Color whoseTurn) const;

  //void CoutPieces(const Piece::Color color) const;
  //void CoutSight(const Piece::Color color) const;

  //bool IsGameOver() const;
  //Piece::Color GetWinner() const;

  private:
  Pieces m_pieces;
  std::vector<Move> m_moves;

  //void SetPiece(const Piece& piece, const int x, const int y);

  const Pieces GetInitialSetup();
  const PiecePtr Find(const Square& s) const;

  //bool IsValidMove(const Move& move) const;

  //const std::vector<Move> GetAllValidMovesPawn(const int x, const int y) const;
  //const std::vector<Move> GetAllValidMovesKnight(const int x, const int y) const;
  //const std::vector<Move> GetAllValidMovesBishop(const int x, const int y) const;
  //const std::vector<Move> GetAllValidMovesRook(const int x, const int y) const;
  //const std::vector<Move> GetAllValidMovesQueen(const int x, const int y) const;
  //const std::vector<Move> GetAllValidMovesKing(const int x, const int y) const;

  //bool CanDoCastlingShort(const Piece::Color color) const;
  //bool CanDoCastlingLong(const Piece::Color color) const;
};
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSBOARD_H

 

 

 

 

 

chessboard2d.cpp

 

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <algorithm>
#include <cassert>
#include <iostream>
#include <stdexcept>
//---------------------------------------------------------------------------
//#include <boost/bind.hpp>
#include <boost/foreach.hpp>
//#include <boost/lambda/lambda.hpp>
//---------------------------------------------------------------------------
#include "chessboard.h"
//#include "chessmove.h"
#include "chesspiece.h"
#include "trace.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
Board2d::Board2d()
  : m_pieces(GetInitialSetup())
{

}
//---------------------------------------------------------------------------
///Find a chesspiece on a certain square
const Board2d::PiecePtr Board2d::Find(const Square& square) const
{
  auto p = std::find_if(m_pieces.begin(),m_pieces.end(),
    [square](const PiecePtr& p) { return p->GetSquare() == square; } );
  if (p == m_pieces.end()) return PiecePtr(new PieceEmpty);
  else return *p;
}
//---------------------------------------------------------------------------
Color Board2d::GetActivePlayer() const
{
  return (m_moves.size() % 2 ? Color::black : Color::white);
}
//---------------------------------------------------------------------------
Color Board2d::GetColor(const Square& s) const
{
  return this->Find(s)->GetColor();
}
//---------------------------------------------------------------------------
const Board2d::Pieces Board2d::GetInitialSetup()
{
  //C++0x initializer list
  Board2d::Pieces v =
  {
    PiecePtr(new PieceRook(Color::white,Square(0,0))),
    PiecePtr(new PieceKnight(Color::white,Square(1,0))),
    PiecePtr(new PieceBishop(Color::white,Square(2,0))),
    PiecePtr(new PieceQueen(Color::white,Square(3,0))),
    PiecePtr(new PieceKing(Color::white,Square(4,0))),
    PiecePtr(new PieceBishop(Color::white,Square(5,0))),
    PiecePtr(new PieceKnight(Color::white,Square(6,0))),
    PiecePtr(new PieceRook(Color::white,Square(7,0))),
    PiecePtr(new PiecePawn(Color::white,Square(0,1))),
    PiecePtr(new PiecePawn(Color::white,Square(1,1))),
    PiecePtr(new PiecePawn(Color::white,Square(2,1))),
    PiecePtr(new PiecePawn(Color::white,Square(3,1))),
    PiecePtr(new PiecePawn(Color::white,Square(4,1))),
    PiecePtr(new PiecePawn(Color::white,Square(5,1))),
    PiecePtr(new PiecePawn(Color::white,Square(6,1))),
    PiecePtr(new PiecePawn(Color::white,Square(7,1))),
    PiecePtr(new PieceRook(Color::black,Square(0,7))),
    PiecePtr(new PieceKnight(Color::black,Square(1,7))),
    PiecePtr(new PieceBishop(Color::black,Square(2,7))),
    PiecePtr(new PieceQueen(Color::black,Square(3,7))),
    PiecePtr(new PieceKing(Color::black,Square(4,7))),
    PiecePtr(new PieceBishop(Color::black,Square(5,7))),
    PiecePtr(new PieceKnight(Color::black,Square(6,7))),
    PiecePtr(new PieceRook(Color::black,Square(7,7))),
    PiecePtr(new PiecePawn(Color::black,Square(0,6))),
    PiecePtr(new PiecePawn(Color::black,Square(1,6))),
    PiecePtr(new PiecePawn(Color::black,Square(2,6))),
    PiecePtr(new PiecePawn(Color::black,Square(3,6))),
    PiecePtr(new PiecePawn(Color::black,Square(4,6))),
    PiecePtr(new PiecePawn(Color::black,Square(5,6))),
    PiecePtr(new PiecePawn(Color::black,Square(6,6))),
    PiecePtr(new PiecePawn(Color::black,Square(7,6)))
  };
  return v;
}
//---------------------------------------------------------------------------
/*
const boost::tribool Board2d::HasColor(
  const Square& s,
  const Color& c) const
{
  if (IsType<Empty>(s)) return boost::logic::indeterminate;
  const auto piece = this->Find(s).get();
  assert(piece);
  return boost::tribool(piece->GetColor() == c);
}
*/
//---------------------------------------------------------------------------
/*
const Piece Board2d::GetPiece(const int x, const int y) const
{
  return mPieces[x][y];
}
//---------------------------------------------------------------------------
void Board2d::SetPiece(const Piece& piece, const int x, const int y)
{
  mPieces[x][y] = piece;
  assert(this->GetPiece(x,y) == piece);
}
//---------------------------------------------------------------------------
bool Board2d::IsGameOver() const
{
  bool whiteHasKing = false;
  bool blackHasKing = false;

  for (int y=0; y!=8; ++y)
  {
    for (int x=0; x!=8; ++x)
    {
      const Piece piece = this->GetPiece(x,y);
      if (piece.IsNull()==false && piece.GetType()==Piece::king)
      {
        if (piece.GetColor() == Piece::white)
          whiteHasKing = true;
        else
          blackHasKing = true;
      }
    }
  }
  return (whiteHasKing == false || blackHasKing == false);
}
//---------------------------------------------------------------------------
Piece::Color Board2d::GetWinner() const
{
  assert(this->IsGameOver()==true);
  for (int y=0; y!=8; ++y)
  {
    for (int x=0; x!=8; ++x)
    {
      const Piece piece = this->GetPiece(x,y);
      if (piece.GetType()==Piece::king)
      {
        if (piece.GetColor() == Piece::white)
          return Piece::white;
        else
          return Piece::black;
      }
    }
  }
  assert(!"Should not get here");
  throw std::logic_error("Cannot find winner");
}
//---------------------------------------------------------------------------
bool Board2d::CanDoMove(const Move& move) const
{
  const Piece piece = this->GetPiece(move.GetX1(),move.GetY1());
  //Is there a chesspiece?
  if (piece.IsNull())
  {
    TRACE("Invalid move: source square is empty");
    return false;
  }
  //Is it of the right type?
  if (piece.GetType()!=move.GetType())
  {
    TRACE("Invalid move: difference between piece on square and in move");
    return false;
  }
  //Is it of the right color? ->Checked by ChessGame
  //Is the destination free or is there an enemy?
  const Piece destination = this->GetPiece(move.GetX2(),move.GetY2());
  if ( destination.IsNull()
    && move.GetIsCapture())
  {
    TRACE("Invalid move: cannot capture a free space");
    return false; //Free space && capture
  }
  if (!destination.IsNull())
  {
    //Cannot capture own pieces
    if (destination.GetColor()==piece.GetColor())
    {
      TRACE("Invalid move: cannot capture own piece");
      return false;
    }
    if (destination.GetColor()!=piece.GetColor()
      && move.GetIsCapture() == false)
    {
      TRACE("Invalid move: cannot move on enemy without capture");
      return false; //An enemy && no capture
    }
  }
  return this->IsValidMove(move);
}
//---------------------------------------------------------------------------
void Board2d::DoMove(const Move& move)
{
  assert(this->CanDoMove(move)==true);
  const Piece piece = this->GetPiece(move.GetX1(),move.GetY1());

  if (piece.GetType()==Piece::king
    && move.GetX1() == 4
    && (move.GetX2() == 2 || move.GetX2() == 6) )
  {
    const Piece::Color color = piece.GetColor();
    assert( (color == Piece::white && move.GetY1() == 0 && move.GetY2() == 0)
      ||    (color == Piece::black && move.GetY1() == 7 && move.GetY2() == 7) );
    const int y = move.GetY2();
    //Castling
    if (move.GetX2() == 2)
    {
      //Castling long
      this->SetPiece(Piece(color,Piece::king),2,y); //Move King
      this->SetPiece(Piece(color,Piece::rook),3,y); //Move rook
      this->SetPiece(Piece(),4,y);           //Erase old king
      this->SetPiece(Piece(),0,y);           //Erase old rook
    }
    else
    {
      //Castling short
      this->SetPiece(Piece(color,Piece::king),6,y); //Move King
      this->SetPiece(Piece(color,Piece::rook),5,y); //Move rook
      this->SetPiece(Piece(),4,y);           //Erase old king
      this->SetPiece(Piece(),7,y);           //Erase old rook
    }
  }
  else if (piece.GetType()==Piece::pawn
    && this->GetPiece(move.GetX2(),move.GetY2()).IsNull()==true
    && move.GetX1() != move.GetX2()
    && move.GetY1() != move.GetY2())
  {
    //En passant
    this->SetPiece(Piece(piece.GetColor(),Piece::pawn),move.GetX2(),move.GetY2()); //Move pawn
    this->SetPiece(Piece(),move.GetX1(),move.GetY1()); //Erase old pawn
    this->SetPiece(Piece(),move.GetX2(),move.GetY1()); //Capture pawn
  }
  else if (piece.GetType()==Piece::pawn
    && (move.GetY2() == 0 || move.GetY2() == 7) )
  {
    //Promotion to queen
    this->SetPiece(Piece(),move.GetX1(),move.GetY1()); //Overwrite old piece by NullType
    this->SetPiece(Piece(piece.GetColor(),Piece::queen),move.GetX2(),move.GetY2());
  }
  else
  {
    //Regular move (i.e. non-castling)
    this->SetPiece(Piece(),move.GetX1(),move.GetY1()); //Overwrite old piece by NullType
    this->SetPiece(piece,move.GetX2(),move.GetY2());
  }
  mMoves.push_back(move);
}
//---------------------------------------------------------------------------
bool Board2d::CanDoCastlingShort(const Piece::Color color) const
{
  //Determine the y
  const int y = (color == Piece::white ? 0 : 7);
  //King must be in place
  if(this->GetPiece(4,y).GetType() != Piece::king) return false;
  //Is the rook in place?
  if(this->GetPiece(7,y).GetType() != Piece::rook) return false;
  //Is nothing in between?
  if(this->GetPiece(5,y).IsNull() == false) return false;
  if(this->GetPiece(6,y).IsNull() == false) return false;

  typedef std::vector<Move>::const_iterator Iter;
  const Iter j = mMoves.end();
  for (Iter i = mMoves.begin(); i!=j; ++i)
  {
    //Check if king has moved
    if ( (*i).GetType() == Piece::king && (*i).GetY1() == y) return false;
    //Check if kingside rook has moved
    if ( (*i).GetType() == Piece::rook && (*i).GetY1() == y && (*i).GetX1() == 7) return false;
  }
  return true;
}
//---------------------------------------------------------------------------
bool Board2d::CanDoCastlingLong(const Piece::Color color) const
{
  //Determine the y
  const int y = (color == Piece::white ? 0 : 7);
  //King must be in place
  if(this->GetPiece(4,y).GetType() != Piece::king) return false;
  //Is the rook in place?
  if(this->GetPiece(0,y).GetType() != Piece::rook) return false;
  //Is nothing in between?
  if(this->GetPiece(1,y).IsNull() == false) return false;
  if(this->GetPiece(2,y).IsNull() == false) return false;
  if(this->GetPiece(3,y).IsNull() == false) return false;

  typedef std::vector<Move>::const_iterator Iter;
  const Iter j = mMoves.end();
  for (Iter i = mMoves.begin(); i!=j; ++i)
  {
    //Check if king has moved
    if ( (*i).GetType() == Piece::king && (*i).GetY1() == y) return false;
    //Check if kingside rook has moved
    if ( (*i).GetType() == Piece::rook && (*i).GetY1() == y && (*i).GetX1() == 0) return false;
  }
  return true;
}
//---------------------------------------------------------------------------
//Color denotes the player who's turn it is, i.e. the player looking at the board
void Board2d::CoutPieces(
  const Piece::Color color) const
{
  const int yBegin = (color == Piece::black ? 0 : 7);
  const int yEnd   = (color == Piece::black ? 8 : -1);
  const int yStep  = (color == Piece::black ? 1 : -1);
  const int xBegin = (color == Piece::white ? 0 : 7);
  const int xEnd   = (color == Piece::white ? 8 : -1);
  const int xStep  = (color == Piece::white ? 1 : -1);

  if (color == Piece::white)
    std::cout << "   A  B  C  D  E  F  G  H " << std::endl;
  else
    std::cout << "   H  G  F  E  D  C  B  A " << std::endl;

  for (int y=yBegin; y!=yEnd; y+=yStep)
  {
    std::cout << "  -------------------------" << std::endl;
    std::cout << (y+1) << " ";
    for (int x=xBegin; x!=xEnd; x+=xStep)
    {
      std::cout << "|" << this->GetPiece(x,y);
    }
    std::cout << "| " << (y+1) << std::endl;

  }
  std::cout << "  -------------------------" << std::endl;
  if (color == Piece::white)
    std::cout << "   A  B  C  D  E  F  G  H " << std::endl;
  else
    std::cout << "   H  G  F  E  D  C  B  A " << std::endl;

}
//---------------------------------------------------------------------------
//Color denotes the player who's turn it is, i.e. the player looking at the board
void Board2d::CoutSight(
  const Piece::Color color) const
{
  //
  const BoardVisibleType inSight = this->GetInSight(color);

  const int yBegin = (color == Piece::black ? 0 : 7);
  const int yEnd   = (color == Piece::black ? 8 : -1);
  const int yStep  = (color == Piece::black ? 1 : -1);
  const int xBegin = (color == Piece::white ? 0 : 7);
  const int xEnd   = (color == Piece::white ? 8 : -1);
  const int xStep  = (color == Piece::white ? 1 : -1);

  if (color == Piece::white)
    std::cout << "   A  B  C  D  E  F  G  H " << std::endl;
  else
    std::cout << "   H  G  F  E  D  C  B  A " << std::endl;

  for (int y=yBegin; y!=yEnd; y+=yStep)
  {
    std::cout << "  -------------------------" << std::endl;
    std::cout << (y+1) << " ";
    for (int x=xBegin; x!=xEnd; x+=xStep)
    {
      std::cout << "|" << (inSight[x][y]==true ? this->GetPiece(x,y) : Piece() );
    }
    std::cout << "| " << (y+1) << std::endl;

  }
  std::cout << "  -------------------------" << std::endl;
  if (color == Piece::white)
    std::cout << "   A  B  C  D  E  F  G  H " << std::endl;
  else
    std::cout << "   H  G  F  E  D  C  B  A " << std::endl;
}
//---------------------------------------------------------------------------
const Board2d::BoardVisibleType Board2d::GetInSight(const Piece::Color color) const
{
  BoardVisibleType inSight(boost::extents[8][8]);
  for (int y=0; y!=8; ++y)
  {
    for (int x=0; x!=8; ++x)
    {
      inSight[x][y] = false;
    }
  }

  for (int y=0; y!=8; ++y)
  {
    for (int x=0; x!=8; ++x)
    {
      //Get the piece there
      const Piece piece = this->GetPiece(x,y);
      //Empty or occupied by enemy? Then continue
      if (piece.IsNull()==true || piece.GetColor()!=color) continue;
      //Occupied by this color
      inSight[x][y] = true;
      //Then get all its valid moves
      const std::vector<Move> moves = this->GetAllValidMoves(x,y);
      const std::vector<Move>::const_iterator j = moves.end();
      for (std::vector<Move>::const_iterator i = moves.begin(); i!=j; ++i)
      {
        inSight[(*i).GetX2()][(*i).GetY2()] = true;
      }
      //Special treatment of pawns
      if (piece.GetType() == Piece::pawn)
      {
        if (piece.GetColor()== Piece::white)
        {
          //Always look a single square forward
          inSight[x][y+1] = true;
          //Look two squares forward, if the first is not occupied
          if (y==1 && GetPiece(x,y+1).IsNull()==true)
          {
            inSight[x][y+2] = true;
          }
          //Look sideways left
          if (x > 0 && GetPiece(x-1,y+1).IsNull()==true)
          {
            inSight[x-1][y+1] = true;
          }
          //Look sideways right
          if (x < 7 && GetPiece(x+1,y+1).IsNull()==true)
          {
            inSight[x+1][y+1] = true;
          }
          //Look sideways if en passant is possible
          if (y == 4                            //4th row
            && this->mMoves.back().GetType() == Piece::pawn //Black moved a pawn
            && this->mMoves.back().GetY1() == 6      //Black moved two places
            && this->mMoves.back().GetY2() == 4)     //Black moved two places
          {
            if (this->mMoves.back().GetX1() == x-1)
              inSight[x-1][y] = true;
            else if (this->mMoves.back().GetX1() == x+1)
              inSight[x+1][y] = true;
          }
        }
        else
        {
          //Always look a single square forward
          inSight[x][y-1] = true;
          //Look two squares forward, if the first is not occupied
          if (y==6 && GetPiece(x,y-1).IsNull()==true)
          {
            inSight[x][y-2] = true;
          }
          //Look sideways left
          if (x > 0 && GetPiece(x-1,y-1).IsNull()==true)
          {
            inSight[x-1][y-1] = true;
          }
          //Look sideways right
          if (x < 7 && GetPiece(x+1,y-1).IsNull()==true)
          {
            inSight[x+1][y-1] = true;
          }
          //Look sideways if en passant is possible
          if (y == 3                            //3rd row
            && this->mMoves.back().GetType() == Piece::pawn //White moved a pawn
            && this->mMoves.back().GetY1() == 1      //White moved two places
            && this->mMoves.back().GetY2() == 3)     //White moved two places
          {
            if (this->mMoves.back().GetX1() == x-1)
              inSight[x-1][y] = true;
            else if (this->mMoves.back().GetX1() == x+1)
              inSight[x+1][y] = true;
          }
        }
      }
    }
  }
  return inSight;
}
//---------------------------------------------------------------------------
bool Board2d::IsValidMove(const Move& move) const
{
  const std::vector<Move> moves = this->GetAllValidMoves(move.GetX1(), move.GetY1());
  if (std::find(moves.begin(),moves.end(),move)!=moves.end())
    return true;
  else
  {
    TRACE("Invalid move: IsValidMove");
    return false;
  }

}
//---------------------------------------------------------------------------
const std::vector<Move> Board2d::GetAllPossibleMoves(
  const Piece::Color whoseTurn) const
{
  std::vector<Move> allMoves;
  for (int y=0; y!=8; ++y)
  {
    for (int x=0; x!=8; ++x)
    {
      if (this->GetPiece(x,y).IsNull() == false
       && this->GetPiece(x,y).GetColor()==whoseTurn)
      {
        //Get all valid moves of this player's piece here
        const std::vector<Move> thisPieceMoves = GetAllValidMoves(x,y);
        //Append it to allMoves
        std::copy(
          thisPieceMoves.begin(),thisPieceMoves.end(),
          std::back_inserter(allMoves));
      }
    }
  }
  return allMoves;
}
//---------------------------------------------------------------------------
///GetAllValidMoves returns all valid move for a piece
///at a certain square. If there is no piece present,
///or if there are no valid moves, an empty std::vector
///is returned;
const std::vector<Move> Board2d::GetAllValidMoves(
  const int x, const int y) const
{
  const Piece piece = GetPiece(x,y);
  if (piece.IsNull())
  {
    return std::vector<Move>();
  }
  switch (piece.GetType())
  {
    case Piece::pawn  : return GetAllValidMovesPawn(x,y);
    case Piece::knight: return GetAllValidMovesKnight(x,y);
    case Piece::bishop: return GetAllValidMovesBishop(x,y);
    case Piece::rook  : return GetAllValidMovesRook(x,y);
    case Piece::queen : return GetAllValidMovesQueen(x,y);
    case Piece::king  : return GetAllValidMovesKing(x,y);
    default: assert(!"Should not get here");
  }
  assert(!"Should not get here");
  throw std::logic_error("Unknown EnumPieceType");
}
//---------------------------------------------------------------------------
const std::vector<Move> Board2d::GetAllValidMovesPawn(
  const int x, const int y) const
{
  const Piece piece = GetPiece(x,y);
  assert(piece.IsNull()==false);
  assert(piece.GetType() == Piece::pawn);

  std::vector<Move> moves;

  if (piece.GetColor()==Piece::white)
  {
    //Move single square forward
    if (GetPiece(x,y+1).IsNull()==true)
    {
      moves.push_back(
        Move(
          Piece::pawn,
          Square(x,y),
          false,
          Square(x,y+1)));
    }
    //Move two squares forward
    if (y==1
      && GetPiece(x,y+1).IsNull()==true
      && GetPiece(x,y+2).IsNull()==true)
    {
      moves.push_back(
        Move(
          Piece::pawn,
          Square(x,y),
          false,
          Square(x,y+2)));
    }
    //Capture left
    if (x > 0
      && GetPiece(x-1,y+1).IsNull()==false
      && GetPiece(x-1,y+1).GetColor()==Piece::black)
    {
      moves.push_back(
        Move(
          Piece::pawn,
          Square(x,y),
          true,
          Square(x-1,y+1)));
    }
    //Capture right
    if (x < 7
      && GetPiece(x+1,y+1).IsNull()==false
      && GetPiece(x+1,y+1).GetColor()==Piece::black)
    {
      moves.push_back(
        Move(
          Piece::pawn,
          Square(x,y),
          true,
          Square(x+1,y+1)));
    }
    //En passant
    if (y == 4
      && mMoves.back().GetY1() == 6 //Black had moved two squares
      && mMoves.back().GetY2() == 4)
    {
      if (mMoves.back().GetX1() == x - 1) //To the left
      {
        //Note: En passant is not regarded as a capture, as the spot moved to is empty
        moves.push_back(
          Move(
            Piece::pawn,
            Square(x,y),
            false, //note
            Square(x-1,y+1)));
      }
      else if (mMoves.back().GetX1() == x + 1) //To the right
      {
        //Note: En passant is not regarded as a capture, as the spot moved to is empty
        moves.push_back(
          Move(
            Piece::pawn,
            Square(x,y),
            false, // note
            Square(x+1,y+1)));
      }
    }
  }
  else
  {
    //Move single square forward
    if (GetPiece(x,y-1).IsNull()==true)
    {
      moves.push_back(
        Move(
          Piece::pawn,
          Square(x,y),
          false,
          Square(x,y-1)));
    }
    //Move two squares forward
    if (y==6
      && GetPiece(x,y-1).IsNull()==true
      && GetPiece(x,y-2).IsNull()==true)
    {
      moves.push_back(
        Move(
          Piece::pawn,
            Square(x,y),
            false,
            Square(x,y-2)));
    }
    //Capture left
    if (x > 0
      && GetPiece(x-1,y-1).IsNull()==false
      && GetPiece(x-1,y-1).GetColor()==Piece::white)
    {
      moves.push_back(
        Move(
          Piece::pawn,
          Square(x,y),
          true,
          Square(x-1,y-1)));
    }
    //Capture right
    if (x < 7
      && GetPiece(x+1,y-1).IsNull()==false
      && GetPiece(x+1,y-1).GetColor()==Piece::white)
    {
      moves.push_back(
        Move(
          Piece::pawn,
          Square(x,y),
          true,
          Square(x+1,y-1)));
    }
    //En passant
    if (y == 3
      && mMoves.back().GetY1() == 1  //White had moved two squares
      && mMoves.back().GetY2() == 3)
    {
      if (mMoves.back().GetX1() == x - 1) //To the left
      {
        //Note: En passant is not regarded as a capture, as the spot moved to is empty
        moves.push_back(
          Move(
            Piece::pawn,
            Square(x,y),
            false, //note
            Square(x-1,y-1)));
      }
      else if (mMoves.back().GetX1() == x + 1) //To the right
      {
        //Note: En passant is not regarded as a capture, as the spot moved to is empty
        moves.push_back(
          Move(
            Piece::pawn,
            Square(x,y),
            false, //note
            Square(x+1,y-1)));
      }
    }
  }
  return moves;
}
//---------------------------------------------------------------------------
const std::vector<Move> Board2d::GetAllValidMovesKnight(
  const int x, const int y) const
{
  const Piece piece = GetPiece(x,y);
  assert(piece.IsNull()==false);
  assert(piece.GetType() == Piece::knight);

  std::vector<Move> moves;

  //-2 -1
  if (x > 1 && y > 0)
  {
    //Is it a capture?
    if (GetPiece(x-2,y-1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x-2,y-1))); //No capture
    }
    else if (GetPiece(x-2,y-1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x-2,y-1))); //Capture of enemy
    }
  }
  //-1 -2
  if (x > 0 && y > 1)
  {
    //Is it a capture?
    if (GetPiece(x-1,y-2).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x-1,y-2))); //No capture
    }
    else if (GetPiece(x-1,y-2).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x-1,y-2))); //Capture of enemy
    }
  }
  //+2 -1
  if (x < 6 && y > 0)
  {
    //Is it a capture?
    if (GetPiece(x+2,y-1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x+2,y-1))); //No capture
    }
    else if (GetPiece(x+2,y-1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x+2,y-1))); //Capture of enemy
    }
  }
  //-1 +2
  if (x > 0 && y < 6)
  {
    //Is it a capture?
    if (GetPiece(x-1,y+2).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x-1,y+2))); //No capture
    }
    else if (GetPiece(x-1,y+2).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x-1,y+2))); //Capture of enemy
    }
  }
  //-2 +1
  if (x > 1 && y < 7)
  {
    //Is it a capture?
    if (GetPiece(x-2,y+1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x-2,y+1))); //No capture
    }
    else if (GetPiece(x-2,y+1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x-2,y+1))); //Capture of enemy
    }
  }
  //+1 -2
  if (x < 7 && y > 1)
  {
    //Is it a capture?
    if (GetPiece(x+1,y-2).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x+1,y-2))); //No capture
    }
    else if (GetPiece(x+1,y-2).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x+1,y-2))); //Capture of enemy
    }
  }

  //+2 +1
  if (x < 6 && y < 7)
  {
    //Is it a capture?
    if (GetPiece(x+2,y+1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x+2,y+1))); //No capture
    }
    else if (GetPiece(x+2,y+1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x+2,y+1))); //Capture of enemy
    }
  }
  //+1 +2
  if (x < 7 && y < 6)
  {
    //Is it a capture?
    if (GetPiece(x+1,y+2).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          false,
          Square(x+1,y+2))); //No capture
    }
    else if (GetPiece(x+1,y+2).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::knight,
          Square(x,y),
          true,
          Square(x+1,y+2))); //Capture of enemy
    }
  }

  return moves;
}
//---------------------------------------------------------------------------
const std::vector<Move> Board2d::GetAllValidMovesBishop(
  const int x, const int y) const
{
  const Piece piece = GetPiece(x,y);
  assert(piece.IsNull()==false);
  assert(piece.GetType() == Piece::bishop);

  std::vector<Move> moves;

  //+x +x
  for (int i=1; x+i<8 && y+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x+i,y+i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          false,
          Square(x+i,y+i))); //No capture
    }
    else if (GetPiece(x+i,y+i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          true,
          Square(x+i,y+i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //-x +x
  for (int i=1; x-i>-1 && y+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x-i,y+i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          false,
          Square(x-i,y+i))); //No capture
    }
    else if (GetPiece(x-i,y+i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          true,
          Square(x-i,y+i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //+x -x
  for (int i=1; x+i<8 && y-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x+i,y-i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          false,
          Square(x+i,y-i))); //No capture
    }
    else if (GetPiece(x+i,y-i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          true,
          Square(x+i,y-i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //-x -x
  for (int i=1; x-i>-1 && y-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x-i,y-i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          false,
          Square(x-i,y-i))); //No capture
    }
    else if (GetPiece(x-i,y-i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::bishop,
          Square(x,y),
          true,
          Square(x-i,y-i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  return moves;
}
//---------------------------------------------------------------------------
const std::vector<Move> Board2d::GetAllValidMovesRook(
  const int x, const int y) const
{
  const Piece piece = GetPiece(x,y);
  assert(piece.IsNull()==false);
  assert(piece.GetType() == Piece::rook);

  std::vector<Move> moves;

  //+x +0
  for (int i=1; x+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x+i,y+0).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          false,
          Square(x+i,y+0))); //No capture
    }
    else if (GetPiece(x+i,y+0).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          true,
          Square(x+i,y+0))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }
  //-x +0
  for (int i=1; x-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x-i,y+0).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          false,
          Square(x-i,y+0))); //No capture
    }
    else if (GetPiece(x-i,y+0).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          true,
          Square(x-i,y+0))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //+0 +x
  for (int i=1; y+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x+0,y+i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          false,
          Square(x+0,y+i))); //No capture
    }
    else if (GetPiece(x+0,y+i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          true,
          Square(x+0,y+i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //+0 -x
  for (int i=1; y-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x-0,y-i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          false,
          Square(x-0,y-i))); //No capture
    }
    else if (GetPiece(x-0,y-i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::rook,
          Square(x,y),
          true,
          Square(x-0,y-i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  return moves;
}
//---------------------------------------------------------------------------
const std::vector<Move> Board2d::GetAllValidMovesQueen(
  const int x, const int y) const
{
  const Piece piece = GetPiece(x,y);
  assert(piece.IsNull()==false);
  assert(piece.GetType() == Piece::queen);

  std::vector<Move> moves;


  //+x +x
  for (int i=1; x+i<8 && y+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x+i,y+i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x+i,y+i))); //No capture
    }
    else if (GetPiece(x+i,y+i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x+i,y+i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //-x +x
  for (int i=1; x-i>-1 && y+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x-i,y+i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x-i,y+i))); //No capture
    }
    else if (GetPiece(x-i,y+i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x-i,y+i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //+x -x
  for (int i=1; x+i<8 && y-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x+i,y-i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x+i,y-i))); //No capture
    }
    else if (GetPiece(x+i,y-i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x+i,y-i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //-x -x
  for (int i=1; x-i>-1 && y-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x-i,y-i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x-i,y-i))); //No capture
    }
    else if (GetPiece(x-i,y-i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x-i,y-i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //+x +0
  for (int i=1; x+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x+i,y+0).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x+i,y+0))); //No capture
    }
    else if (GetPiece(x+i,y+0).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x+i,y+0))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }
  //-x +0
  for (int i=1; x-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x-i,y+0).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x-i,y+0))); //No capture
    }
    else if (GetPiece(x-i,y+0).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x-i,y+0))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //+0 +x
  for (int i=1; y+i<8; ++i)
  {
    //Is it a capture
    if (GetPiece(x+0,y+i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x+0,y+i))); //No capture
    }
    else if (GetPiece(x+0,y+i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x+0,y+i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  //+0 -x
  for (int i=1; y-i>-1; ++i)
  {
    //Is it a capture
    if (GetPiece(x-0,y-i).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          false,
          Square(x-0,y-i))); //No capture
    }
    else if (GetPiece(x-0,y-i).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::queen,
          Square(x,y),
          true,
          Square(x-0,y-i))); //Capture of enemy
      break;
    }
    else
    {
      break; //Own color
    }
  }

  return moves;
}
//---------------------------------------------------------------------------
const std::vector<Move> Board2d::GetAllValidMovesKing(
  const int x, const int y) const
{
  const Piece piece = GetPiece(x,y);
  assert(piece.IsNull()==false);
  assert(piece.GetType() == Piece::king);

  std::vector<Move> moves;

  //+0 -1
  if (y > 0)
  {
    //Is it a capture
    if (GetPiece(x+0,y-1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x+0,y-1))); //No capture
    }
    else if (GetPiece(x+0,y-1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x+0,y-1))); //Capture of enemy
    }
  }
  //-1 -1
  if (y > 0 && x > 0)
  {
    //Is it a capture
    if (GetPiece(x-1,y-1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x-1,y-1))); //No capture
    }
    else if (GetPiece(x-1,y-1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x-1,y-1))); //Capture of enemy
    }
  }
  //+1 -1
  if (y > 0 && x < 7)
  {
    //Is it a capture
    if (GetPiece(x+1,y-1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x+1,y-1))); //No capture
    }
    else if (GetPiece(x+1,y-1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x+1,y-1))); //Capture of enemy
    }
  }


  //+0 +1
  if (y < 7)
  {
    //Is it a capture
    if (GetPiece(x+0,y+1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x+0,y+1))); //No capture
    }
    else if (GetPiece(x+0,y+1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x+0,y+1))); //Capture of enemy
    }
  }
  //-1 +1
  if (y < 7 && x > 0)
  {
    //Is it a capture
    if (GetPiece(x-1,y+1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x-1,y+1))); //No capture
    }
    else if (GetPiece(x-1,y+1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x-1,y+1))); //Capture of enemy
    }
  }
  //+1 +1
  if (y < 7 && x < 7)
  {
    //Is it a capture
    if (GetPiece(x+1,y+1).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x+1,y+1))); //No capture
    }
    else if (GetPiece(x+1,y+1).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x+1,y+1))); //Capture of enemy
    }
  }


  //-1 +0
  if (x > 0)
  {
    //Is it a capture
    if (GetPiece(x-1,y+0).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x-1,y+0))); //No capture
    }
    else if (GetPiece(x-1,y+0).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x-1,y+0))); //Capture of enemy
    }
  }
  //+1 +1
  if (x < 7)
  {
    //Is it a capture
    if (GetPiece(x+1,y+0).IsNull() == true)
    {
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          false,
          Square(x+1,y+0))); //No capture
    }
    else if (GetPiece(x+1,y+0).GetColor() != piece.GetColor())
    { //Is it an enemy?
      moves.push_back(
        Move(
          Piece::king,
          Square(x,y),
          true,
          Square(x+1,y+0))); //Capture of enemy
    }
  }

  //Can do castling?
  if (this->CanDoCastlingLong(piece.GetColor()))
    moves.push_back(
      Move(
        Piece::king,
        Square(x,y),
        false,
        Square(x-2,y)));
  if (this->CanDoCastlingShort(piece.GetColor()))
    moves.push_back(
      Move(
        Piece::king,
        Square(x,y),
        false,
        Square(x+2,y)));

  return moves;
}
//---------------------------------------------------------------------------
*/
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------

 

 

 

 

 

chessboardwidget.h

 

#ifndef CHESSBOARDWIDGET_H
#define CHESSBOARDWIDGET_H
//---------------------------------------------------------------------------
#include <boost/shared_ptr.hpp>
//---------------------------------------------------------------------------
#include "chessboard.h"
#include "widget.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
struct BoardWidget : public Widget
{
  BoardWidget(boost::shared_ptr<Board> board);


  boost::shared_ptr<Board> m_board;
};
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSBOARDWIDGET_H

 

 

 

 

 

chessboardwidget.cpp

 

#include "chessboardwidget.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------

 

 

 

 

 

chesscolor.h

 

#ifndef CHESSCOLOR_H
#define CHESSCOLOR_H
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
//C++0x enum class
enum class Color
{
  black, white, indeterminate
};
//---------------------------------------------------------------------------
} //~namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSCOLOR_H

 

 

 

 

 

chesscolor.cpp

 

#include "chesscolor.h"


 

 

 

 

 

chessfwd.h

 

#ifndef CHESSFWD_H
#define CHESSFWD_H

///Contains the CppChess forward declarations
namespace Chess
{
  struct Board1d;
  struct Board2d;
  struct Move;
  struct Piece;
  struct Square;
}

#endif // CHESSFWD_H

 

 

 

 

 

chessmove.h

 

#ifndef CHESSMOVE_H
#define CHESSMOVE_H
//---------------------------------------------------------------------------
#include <iosfwd>
//---------------------------------------------------------------------------
#include "chesssquare.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
struct Move
{
  Move(
    const Square& from,
    const Square& to);

  const Square& GetFrom() const;
  const Square& GetTo() const;

  private:
  Square m_from;
  Square m_to;
};
//---------------------------------------------------------------------------
bool operator==(const Move& lhs, const Move& rhs);
//std::ostream& operator<<(std::ostream& os,const Move& m);
//---------------------------------------------------------------------------
} //~namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSMOVE_H

 

 

 

 

 

chessmove.cpp

 

//---------------------------------------------------------------------------
#include <cassert>
#include <stdexcept>
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
//---------------------------------------------------------------------------
#include "chessmove.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
/*
Move::Move(
  const ChessPiece::Type type,
  const Square& from,
  const bool is_capture,
  const Square& to)
  : m_type(type),
    m_from(from),
    m_is_capture(is_capture),
    m_to(to)
{

}
//---------------------------------------------------------------------------
///Parses a Move from std::string.
///The parameter active_player is needed for castling
Move::Move(
  const std::string& s,
  const ChessPiece::Color active_player)
  : m_type(ParseType(s,active_player)),
    m_from(ParseFrom(s,active_player)),
    m_is_capture(ParseIsCapture(s,active_player)),
    m_to(ParseTo(s,active_player))
{
  if (s.empty()) throw std::logic_error("std::string to parse is empty");

  //Obtain chesstypes from the only upper case character at the first index
  //If there is no upper case, it is a pawn
  if (s=="o-o" || s=="O-O" || s == "0-0")
  {
    const int y = (active_player == ChessPiece::white ? 0 : 7);
    m_type = ChessPiece::king;
    m_from = Square(4,y);
    m_is_capture = false;
    m_to = Square(6,y);
    return;
  }
  if (s=="o-o-o" || s=="O-O-O" || s == "0-0-0")
  {
    const int y = (this->GetWhoseTurn() == ChessPiece::white ? 0 : 7);
    move = Move(
      ChessPiece::king,
      Square(4,y),
      false,
      Square(2,y));
    return true;
  }

  const ChessPiece::Type type = ChessPiece::GetType(s[0]);


  const int minSize = (move.GetType() == ChessPiece::pawn ? 5 : 6);
  if (static_cast<int>(s.size()) < minSize) return false;

  if (move.GetType() == ChessPiece::pawn)
  {
     if (s[2]==' ') move.capture = false;
     else if (s[2]=='x') move.capture = true;
     else return false;
  }
  else
  {
     if (s[3]==' ') move.capture = false;
     else if (s[3]=='x') move.capture = true;
     else return false;
  }

  const std::string from = (move.GetType() == ChessPiece::pawn ? s.substr(0,2) : s.substr(1,2) );
  const std::string to   = (move.GetType() == ChessPiece::pawn ? s.substr(3,2) : s.substr(4,2) );

  switch(from[0])
  {
    case 'a': move.x1 = 0; break;
    case 'b': move.x1 = 1; break;
    case 'c': move.x1 = 2; break;
    case 'd': move.x1 = 3; break;
    case 'e': move.x1 = 4; break;
    case 'f': move.x1 = 5; break;
    case 'g': move.x1 = 6; break;
    case 'h': move.x1 = 7; break;
    default: return false;
  }

  switch(from[1])
  {
    case '1': move.y1 = 0; break;
    case '2': move.y1 = 1; break;
    case '3': move.y1 = 2; break;
    case '4': move.y1 = 3; break;
    case '5': move.y1 = 4; break;
    case '6': move.y1 = 5; break;
    case '7': move.y1 = 6; break;
    case '8': move.y1 = 7; break;
    default: return false;
  }

  switch(to[0])
  {
    case 'a': move.x2 = 0; break;
    case 'b': move.x2 = 1; break;
    case 'c': move.x2 = 2; break;
    case 'd': move.x2 = 3; break;
    case 'e': move.x2 = 4; break;
    case 'f': move.x2 = 5; break;
    case 'g': move.x2 = 6; break;
    case 'h': move.x2 = 7; break;
    default: return false;
  }

  switch(to[1])
  {
    case '1': move.y2 = 0; break;
    case '2': move.y2 = 1; break;
    case '3': move.y2 = 2; break;
    case '4': move.y2 = 3; break;
    case '5': move.y2 = 4; break;
    case '6': move.y2 = 5; break;
    case '7': move.y2 = 6; break;
    case '8': move.y2 = 7; break;
    default: return false;
  }

  //if it contains exactly one 'x' it is a capture
  return true;

}
//---------------------------------------------------------------------------
const Square& Move::GetFrom() const
{
  return m_from;
}
//---------------------------------------------------------------------------
bool Move::GetIsCapture() const
{
  return m_is_capture;
}
//---------------------------------------------------------------------------
const Square& Move::GetTo() const
{
  return m_to;
}
//---------------------------------------------------------------------------
ChessPiece::Type Move::GetType() const
{
  return m_type;
}
//---------------------------------------------------------------------------
int Move::GetX1() const
{
  return GetFrom().GetX();
}
//---------------------------------------------------------------------------
int Move::GetY1() const
{
  return GetFrom().GetY();
}
//---------------------------------------------------------------------------
int Move::GetX2() const
{
  return GetTo().GetX();

}
//---------------------------------------------------------------------------
int Move::GetY2() const
{
  return GetTo().GetY();
}
//---------------------------------------------------------------------------
Square Move::ParseFrom(
  const std::string& s,
  const ChessPiece::Color active_player)
{
  assert(s.size() >= 2);
  char c = s[0];
  char d = s[1];
  if (c < 'a' || c > 'h')
  {
    assert(s.size() >= 3);
    c = s[1];
    d = s[2];
  }
  assert(c >= 'a' && c <= 'h');
  return Square(
    static_cast<int>(c - 'a'),
    boost::lexical_cast<int>(d));
}
//---------------------------------------------------------------------------
ChessPiece::Type Move::ParseType(
  const std::string& s,
    const ChessPiece::Color active_player)
{
  const char c = s[0];
  switch(c)
  {
    case 'O': //castling
    case 'o': //castling
      return ChessPiece::king  ;
    default : return ChessPiece::GetType(c);
  }
}
//---------------------------------------------------------------------------
bool Move::ParseIsCapture(
  const std::string& s,
  const ChessPiece::Color active_player)
{

}
//---------------------------------------------------------------------------
Square Move::ParseTo(
  const std::string& s,
  const ChessPiece::Color active_player)
{

}
//---------------------------------------------------------------------------
bool operator==(const Move& lhs, const Move& rhs)
{
  return (lhs.GetType()   == rhs.GetType()
    && lhs.GetFrom()      == rhs.GetFrom()
    && lhs.GetIsCapture() == rhs.GetIsCapture()
    && lhs.GetTo()        == rhs.GetTo());
}
//---------------------------------------------------------------------------
//std::ostream& operator<<(std::ostream& os,const Move& m)
//{
//
//}
//---------------------------------------------------------------------------
*/
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------

 

 

 

 

 

chesspiece.h

 

#ifndef CHESSPIECE_H
#define CHESSPIECE_H
//---------------------------------------------------------------------------
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
//---------------------------------------------------------------------------
#include "chesscolor.h"
#include "chesssquare.h"
namespace Chess { struct Board2d; }
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
//A Piece has
//- a position
//- a color
//A Piece needs
//- access to previous moves to determine en passant
struct Piece
{
  Piece(
    const Color color,
    const Square& square);

  //Base classes must have a public destructor:
  //Herb Sutter, Andrei Alexandrescu. C++ coding standards:
  //101 rules, guidelines, and best practices.
  //ISBN: 0-32-111358-6. Item 50: 'Make base class destructors
  //public and virtual, or protected and nonvirtual'.
  virtual ~Piece() {}

  Color GetColor() const;
  virtual const std::vector<Square> GetMoves(const Chess::Board2d& board) const = 0;
  const Square GetSquare() const;
  const std::pair<char,char> GetSymbol() const;

  //virtual void Move(const Square& to);

  private:

  ///The piece its color
  const Color m_color;

  virtual char GetTypeChar() const = 0;

  ///The piece its location
  Square m_square;
};
//---------------------------------------------------------------------------
struct PieceBishop : public Piece
{
  PieceBishop(
    const Color color,
    const Square& square);
  const std::vector<Square> GetMoves(const Chess::Board2d& board) const;
  char GetTypeChar() const { return 'B'; }

  private:
  ~PieceBishop() {}
  friend void boost::checked_delete<>(PieceBishop *);
};
//---------------------------------------------------------------------------
struct PieceEmpty : public Piece
{
  PieceEmpty();
  private:
  const std::vector<Square> GetMoves(const Chess::Board2d& board) const;
  char GetTypeChar() const { return ' '; }

  private:
  ~PieceEmpty() {}
  friend void boost::checked_delete<>(PieceEmpty *);
};
//---------------------------------------------------------------------------
struct PieceKing : public Piece
{
  PieceKing(
    const Color color,
    const Square& square);
  const std::vector<Square> GetMoves(const Chess::Board2d& board) const;
  void Move(const Square& to);
  char GetTypeChar() const { return 'K'; }

  private:
  ~PieceKing() {}
  friend void boost::checked_delete<>(PieceKing *);

  ///Keep track of whether king has moved for castling
  bool m_has_moved;

};
//---------------------------------------------------------------------------
struct PieceKnight : public Piece
{
  PieceKnight(
    const Color color,
    const Square& square);
  const std::vector<Square> GetMoves(const Chess::Board2d& board) const;
  char GetTypeChar() const { return 'N'; }

  private:
  ~PieceKnight() {}
  friend void boost::checked_delete<>(PieceKnight *);
};
//---------------------------------------------------------------------------
struct PiecePawn : public Piece
{
  PiecePawn(
    const Color color,
    const Square& square);
  const std::vector<Square> GetMoves(const Chess::Board2d& board) const;
  char GetTypeChar() const { return '.'; }

  private:
  ~PiecePawn() {}
  friend void boost::checked_delete<>(PiecePawn *);
};
//---------------------------------------------------------------------------
struct PieceQueen : public Piece
{
  PieceQueen(
    const Color color,
    const Square& square);
  const std::vector<Square> GetMoves(const Chess::Board2d& board) const;
  char GetTypeChar() const { return 'Q'; }

  private:
  ~PieceQueen() {}
  friend void boost::checked_delete<>(PieceQueen *);
};
//---------------------------------------------------------------------------
struct PieceRook : public Piece
{
  PieceRook(
    const Color color,
    const Square& square);
  const std::vector<Square> GetMoves(const Chess::Board2d& board) const;
  void Move(const Square& to);
  char GetTypeChar() const { return 'R'; }

  private:
  ~PieceRook() {}
  friend void boost::checked_delete<>(PieceRook *);

  ///Keep track of whether rook has moved for castling
  bool m_has_moved;

};
//---------------------------------------------------------------------------
//bool operator==(const Piece& lhs, const Piece& rhs);
//std::ostream& operator<<(std::ostream& os, const Piece& piece);
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSPIECE_H

 

 

 

 

 

chesspiece.cpp

 

//---------------------------------------------------------------------------
#include <cassert>
#include <iostream>
//---------------------------------------------------------------------------
#include "chesspiece.h"
#include "chesssquare.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
Piece::Piece(
  const Color color,
  const Square &square)
: m_color(color),
   m_square(square)
{

}
//---------------------------------------------------------------------------
Color Piece::GetColor() const
{
  return m_color;
}
//---------------------------------------------------------------------------
const Square Piece::GetSquare() const
{
  return m_square;
}
//---------------------------------------------------------------------------
const std::pair<char,char> Piece::GetSymbol() const
{
  char c = '*';
  switch (this->GetColor())
  {
    case Color::black: c = 'B'; break;
    case Color::indeterminate: c = ' '; break;
    case Color::white: c = 'W'; break;
  }
  return std::make_pair(c,GetTypeChar());

}
//---------------------------------------------------------------------------
PieceBishop::PieceBishop(
  const Color color,
  const Square& square)
  : Piece(color,square)
{
  assert(GetColor() != Color::indeterminate);
}
//---------------------------------------------------------------------------
const std::vector<Square> PieceBishop::GetMoves(const Chess::Board2d& board) const
{
  std::vector<Square> v;
  return v;
}
//---------------------------------------------------------------------------
PieceEmpty::PieceEmpty()
  : Piece(Color::indeterminate,Square(0,0))
{
  assert(GetColor() == Color::indeterminate);
}
//---------------------------------------------------------------------------
const std::vector<Square> PieceEmpty::GetMoves(const Chess::Board2d& board) const
{
  return std::vector<Square>();
}
//---------------------------------------------------------------------------
PieceKing::PieceKing(
  const Color color,
  const Square& square)
  : Piece(color,square)
{
  assert(GetColor() != Color::indeterminate);

}
//---------------------------------------------------------------------------
const std::vector<Square> PieceKing::GetMoves(const Chess::Board2d& board) const
{
  std::vector<Square> v;
  return v;
}
//---------------------------------------------------------------------------
PieceKnight::PieceKnight(
  const Color color,
  const Square& square)
  : Piece(color,square)
{
  assert(GetColor() != Color::indeterminate);

}
//---------------------------------------------------------------------------
const std::vector<Square> PieceKnight::GetMoves(const Chess::Board2d& board) const
{
  std::vector<Square> v;
  return v;
}
//---------------------------------------------------------------------------
PiecePawn::PiecePawn(
  const Color color,
  const Square& square)
  : Piece(color,square)
{
  assert(GetColor() != Color::indeterminate);

}
//---------------------------------------------------------------------------
const std::vector<Square> PiecePawn::GetMoves(const Chess::Board2d& board) const
{
  std::vector<Square> v;
  return v;
}
//---------------------------------------------------------------------------
PieceQueen::PieceQueen(
  const Color color,
  const Square& square)
  : Piece(color,square)
{
  assert(GetColor() != Color::indeterminate);

}
//---------------------------------------------------------------------------
const std::vector<Square> PieceQueen::GetMoves(const Chess::Board2d& board) const
{
  std::vector<Square> v;
  return v;
}
//---------------------------------------------------------------------------
PieceRook::PieceRook(
  const Color color,
  const Square& square)
  : Piece(color,square)
{
  assert(GetColor() != Color::indeterminate);

}
//---------------------------------------------------------------------------
const std::vector<Square> PieceRook::GetMoves(const Chess::Board2d& board) const
{
  std::vector<Square> v;
  return v;
}
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------




/*
std::ostream& operator<<(std::ostream& os, const Piece& piece)
{
  if (piece.IsNull()==true)
  {
    os << "  ";
  }
  else
  {
    os << piece.GetColor() << piece.GetType();
  }
  return os;
}
//---------------------------------------------------------------------------
Piece::Type Piece::GetType(const char c)
{
  switch(c)
  {
    case 'N': return Piece::knight;
    case 'B': return Piece::bishop;
    case 'R': return Piece::rook  ;
    case 'Q': return Piece::queen ;
    case 'K': return Piece::king  ;
    default : return Piece::pawn  ;
  }
}
//---------------------------------------------------------------------------
bool operator==(const Piece& lhs, const Piece& rhs)
{
  return (lhs.GetType() == rhs.GetType()
    && lhs.GetColor() == rhs.GetColor()
    && lhs.IsNull() == rhs.IsNull() );
}
//---------------------------------------------------------------------------
*/

 

 

 

 

 

chessresources.h

 

#ifndef SEARCHANDDESTROYCHESSRESOURCES_H
#define SEARCHANDDESTROYCHESSRESOURCES_H
//---------------------------------------------------------------------------
#include <boost/shared_ptr.hpp>
//---------------------------------------------------------------------------
namespace Chess { struct Piece; }
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
///Resources contains the resources filenames
struct Resources
{
  ///Find the filename for this piece
  const std::string Find(const boost::shared_ptr<const Chess::Piece>& piece) const;

  ///Get the Resources its only instance
  //static Resources * Get();

  //private:
  ///Resources its constructor, private because Resources is a Singleton
  //Resources();

  static const std::string GetBlackBishop() { return "bdd129.xpm"; }
  static const std::string GetBlackKing() { return "kdd129.xpm"; }
  static const std::string GetBlackKnight() { return "ndd129.xpm"; }
  static const std::string GetBlackPawn() { return "pdd129.xpm"; }
  static const std::string GetBlackQueen() { return "qdd129.xpm"; }
  static const std::string GetBlackRook() { return "rdd129.xpm"; }
  static const std::string GetBlackSquare() { return "wood_d.xpm"; }
  static const std::string GetWhiteBishop() { return "bld129.xpm"; }
  static const std::string GetWhiteKing() { return "kld129.xpm"; }
  static const std::string GetWhiteKnight() { return "nld129.xpm"; }
  static const std::string GetWhitePawn() { return "pld129.xpm"; }
  static const std::string GetWhiteQueen() { return "qld129.xpm"; }
  static const std::string GetWhiteRook() { return "rld129.xpm"; }
  static const std::string GetWhiteSquare() { return "wood_l.xpm"; }

  ///Resources its only instance
  //static Resources * m_instance;
};
//---------------------------------------------------------------------------
} //~ namespace SearchAndDestroyChess
//---------------------------------------------------------------------------
#endif // SEARCHANDDESTROYCHESSRESOURCES_H

 

 

 

 

 

chessresources.cpp

 

//---------------------------------------------------------------------------
#include <algorithm>
//---------------------------------------------------------------------------
#include <boost/filesystem.hpp>
//---------------------------------------------------------------------------
#include "chessresources.h"
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
//Resources * Resources::m_instance = 0;
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
const std::string Resources::Find(
  const boost::shared_ptr<const Chess::Piece>& piece) const
{

}
//---------------------------------------------------------------------------
/*
Resources * Resources::Get()
{
  if (m_instance == 0)
  {
    m_instance = new Resources;
  }
  return m_instance;
}
*/
//---------------------------------------------------------------------------
} //~namespace SearchAndDestroyChess
//---------------------------------------------------------------------------

 

 

 

 

 

chesssquare.h

 

#ifndef CHESSSQUARE_H
#define CHESSSQUARE_H
//---------------------------------------------------------------------------
#include <iosfwd>
#include <string>
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
///ChessSquare is the coordinat of a square on a chessboard
struct Square
{
  Square(const int x, const int y);
  int GetX() const { return m_x; }
  int GetY() const { return m_y; }
  const std::string ToStr() const;
  private:
  int m_x; //file
  int m_y; //rank

};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const Square& s);
bool operator==(const Square& lhs, const Square& rhs);
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSSQUARE_H

 

 

 

 

 

chesssquare.cpp

 

//---------------------------------------------------------------------------
#include <cassert>
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
//---------------------------------------------------------------------------
#include "chesssquare.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
Square::Square(const int x, const int y)
  : m_x(x), m_y(y)
{
  assert(m_x >= 0);
  assert(m_x  < 8);
  assert(m_y >= 0);
  assert(m_y  < 8);
}
//---------------------------------------------------------------------------
const std::string Square::ToStr() const
{
  const char c = 'a' + GetX();
  std::string s
    = c
    + boost::lexical_cast<std::string>(GetY());
  return s;
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const Square& s)
{
  os << s.ToStr();
  return os;
}
//---------------------------------------------------------------------------
bool operator==(const Square& lhs, const Square& rhs)
{
  return lhs.GetX() == rhs.GetX()
    && lhs.GetY() == rhs.GetY();
}
//---------------------------------------------------------------------------
} //~ namespace Chess
//---------------------------------------------------------------------------

 

 

 

 

 

chesssquareselector.h

 

#ifndef CHESSSQUARESELECTOR_H
#define CHESSSQUARESELECTOR_H
//---------------------------------------------------------------------------
#include <boost/scoped_ptr.hpp>
//---------------------------------------------------------------------------
#include "chesssquare.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
struct SquareSelector
{
  SquareSelector();
  SquareSelector(const SquareSelector& other);
  SquareSelector(const Square& s);
  SquareSelector& operator=(const SquareSelector& other);
  bool IsActive() const;
  const Square& Get() const;
  int GetX() const;
  int GetY() const;
  const std::string ToStr() const;

  private:
  boost::scoped_ptr<Square> m_square;
};
//---------------------------------------------------------------------------
} //~namespace Chess
//---------------------------------------------------------------------------
#endif // CHESSSQUARESELECTOR_H

 

 

 

 

 

chesssquareselector.cpp

 

#include <cassert>
//---------------------------------------------------------------------------
#include "chesssquareselector.h"
//---------------------------------------------------------------------------
namespace Chess {
//---------------------------------------------------------------------------
SquareSelector::SquareSelector()
{

}
//---------------------------------------------------------------------------
SquareSelector::SquareSelector(const SquareSelector& other)
  : m_square(new Square(other.Get()))
{

}
//---------------------------------------------------------------------------
SquareSelector::SquareSelector(const Square& s)
  : m_square(new Square(s))
{

}
//---------------------------------------------------------------------------
SquareSelector& SquareSelector::operator=(const SquareSelector& other)
{
  m_square.reset(new Square(other.Get()));
  return *this;
}
//---------------------------------------------------------------------------
const Square& SquareSelector::Get() const
{
  assert(m_square);
  return *m_square.get();
}
//---------------------------------------------------------------------------
int SquareSelector::GetX() const
{
  assert(m_square);
  return m_square->GetX();
}
//---------------------------------------------------------------------------
int SquareSelector::GetY() const
{
  assert(m_square);
  return m_square->GetY();
}
//---------------------------------------------------------------------------
///SquareSelector is active when m_square holds a square
bool SquareSelector::IsActive() const
{
  return m_square;
}
//---------------------------------------------------------------------------
const std::string SquareSelector::ToStr() const
{
  if (IsActive())
  {
    assert(m_square);
    return m_square->ToStr();
  }
  return "-";
}
//---------------------------------------------------------------------------
} //~namespace Chess
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml