//---------------------------------------------------------------------------
const double ChessGame::AttributeValue(
  const ChessMove& move) const
{
  double value = 0.0;
  const int y1 = move.y1;
  const int x1 = move.x1;
  const int y2 = move.y2;
  const int x2 = move.x2;
  const ChessPiece piece = mBoard.GetPiece(x1,y1);
  assert(piece.IsNull()==false);
  //const EnumChessPieceColor color = piece.GetColor();

  if (mBoard.GetPiece(x2,y2).IsNull()==false)
  {
    //An expensive piece should not take a cheaper piece
    //The more expensive the piece to move,
    //  the less motivated it will be to take a piece
    if (piece.GetType()==king  ) { value-=100.0; }
    if (piece.GetType()==queen ) { value-= 9.0; }
    if (piece.GetType()==rook  ) { value-= 4.0; }
    if (piece.GetType()==bishop) { value-= 2.0; }
    if (piece.GetType()==knight) { value-= 2.0; }
    if (piece.GetType()==pawn  ) { value-= 0.0; }
    //The more expensive the piece to take,
    //  the more motivated it will be to take it
    const ChessPiece victim = mBoard.GetPiece(x2,y2);
    if (victim.GetType()==king  ) { value+=100000.0; }
    if (victim.GetType()==queen ) { value+=10.0; }
    if (victim.GetType()==rook  ) { value+= 5.0; }
    if (victim.GetType()==bishop) { value+= 3.0; }
    if (victim.GetType()==knight) { value+= 3.0; }
    if (victim.GetType()==pawn  ) { value+= 1.0; }
  }
  return value;
}
//---------------------------------------------------------------------------
const bool ChessGame::CanDoMove(const ChessMove& move) const
{
  //Regular move
  const ChessPiece piece = mBoard.GetPiece(move.x1,move.y1);
  //Is it of the right color?
  if (piece.GetColor()!=mWhoseTurn) return false;
  //Is the captured piece of the other color?
  //->checked by ChessBoard
  //ChessBoard checks the rest
  return mBoard.CanDoMove(move);
}
Exemplo n.º 3
0
vector<BoardPosition> ChessPiece::checkMoves(vector<vector<BoardPosition> > jumps) const {
    vector<BoardPosition> moves;

    for(unsigned int i = 0; i < jumps.at(0).size(); i++) { // for(unsigned int i = 0; i < jumps.size(); i++)
        if(!this->outOfBounds(jumps.at(0).at(i))) {
            ChessPiece* piece = this->getBoard()->getAt(jumps.at(0).at(i));
            if(piece == NULL || piece->getColor() != this->getColor()) {
                moves.push_back(jumps.at(0).at(i));
            }
        }
    }

    return moves;
}
Exemplo n.º 4
0
void ChessBoard::ResolveLineOfSight(ChessPiece* movedPiece)
{
	Player* opponentPlayer;
	Player* currPlayer;
	if (movedPiece->isWhite())
	{
		opponentPlayer = _BlackPlayer;
		currPlayer = _WhitePlayer;
	}
	else
	{
		opponentPlayer = _WhitePlayer;
		currPlayer = _BlackPlayer;
	}
	//Will be handled here now lulz
	if (pieceToText(movedPiece) == 'K')
	{
		opponentPlayer->ClearAttackingKingPieces();
		ChessSquare KingLoc = currPlayer->GetKingLocation();
		for (int i = 0; i < opponentPlayer->GetNumPiecesLeft(); i++)
		{
			ChessPiece* currPiece = opponentPlayer->GetPiece(i);
			if (currPiece->HasLineToKing(KingLoc) )
				opponentPlayer->AddToKingAttacking(currPiece);
		}
		return;
	}
	
	ChessSquare KingLoc = opponentPlayer->GetKingLocation();
	
	if (!movedPiece->IsInKingArray() )
	{
		if (movedPiece->HasLineToKing(KingLoc) )
		{
			currPlayer->AddToKingAttacking(movedPiece);
		}
	}
	else 
	{
		if (movedPiece->HasLineToKing(KingLoc) )
		{
			//currPlayer->AddToKingAttacking(movedPiece);
		}
		else
			currPlayer->RemoveKingAttacking(movedPiece);
	}
	
}
Exemplo n.º 5
0
bool ChessMove::IsValidMovePawn(const ChessBoard& board) const{
    ChessPiece* pawn = board.GetPiece(startX, startY);
    ChessColor color = pawn->GetColor();
    int delta = GetDelta(color);
    int startingRow = GetStartingRow(color);
    ChessPiece* occupyingPiece = board.GetPiece(endX, endY);
    if (occupyingPiece){
        return abs(startX-endX)==1 && startY+delta==endY;
    } else if (endX==startX){
        if (startingRow==startY){
            return endY==startingRow+delta || (!board.GetPiece(endX, startingRow+delta) && endY==startingRow+delta*2);
        } else {
            return endY==startY+delta;
        }
    }
    return false;
}
Exemplo n.º 6
0
void GamePresenter::drawBoard() {
	SDL_RenderClear(main_renderer);
	SDL_RenderCopy(main_renderer,background,NULL,&fullscreen);
	SDL_Rect currentField;
	currentField.h = SCREEN_HEIGHT/8;
	currentField.w = SCREEN_WIDTH/8;
	currentField.y = 0;
	for(int a = 7; a >= 0; a--){
		currentField.x = 0;
		for(int b = 0; b<8; b++){
			ChessPiece* CurrentPiece = currentgame->getPieceAtLocation(a*8 + b);
			if (CurrentPiece)
				SDL_RenderCopy(main_renderer,piecesheet,&pieces[getSpriteForPiece(CurrentPiece->GetType(),CurrentPiece->GetColor())],&currentField);
			currentField.x += currentField.w;
		}
		currentField.y += currentField.h;
	}
	if (currentgame->isGameOver()){
		SDL_SetTextureAlphaMod(piecesheet,160);
		if (currentgame->getResult() == "White wins"){
			SDL_RenderCopy(main_renderer,piecesheet,&pieces[getSpriteForPiece('K','W')],&fullscreen);

		}
		if (currentgame->getResult() == "Black wins"){
			SDL_RenderCopy(main_renderer,piecesheet,&pieces[getSpriteForPiece('K','B')],&fullscreen);
		}
		if (currentgame->getResult() == "Draw"){
			SDL_Rect lefthalf;
			lefthalf.h=SCREEN_HEIGHT;
			lefthalf.w=SCREEN_WIDTH/2;
			lefthalf.x=0;
			lefthalf.y=0;
			SDL_Rect righthalf(lefthalf);
			righthalf.x=SCREEN_WIDTH/2;
			SDL_Rect halfwhiteking=pieces[getSpriteForPiece('K','W')];
			SDL_Rect halfblackking=pieces[getSpriteForPiece('K','B')];
			halfwhiteking.w/=2;
			halfblackking.w/=2;
			halfblackking.x+=halfblackking.w;
			SDL_RenderCopy(main_renderer,piecesheet,&halfwhiteking,&lefthalf);
			SDL_RenderCopy(main_renderer,piecesheet,&halfblackking,&righthalf);
		}
		SDL_SetTextureAlphaMod(piecesheet,255);
	}
	SDL_RenderPresent(main_renderer);
}
Exemplo n.º 7
0
bool ChessMove::IsValidCastleMove(const ChessBoard& board) const{
    ChessPiece* king = board.GetPiece(startX, startY);
    if (king->GetMoveCount() != 0) return false;
    if (abs(startX-endX) != 2 || startY != endY) return false;
    int x = 7;
    int delta = 1;
    if (startX > endX){
        x = 0;
        delta = -1;
    }
    ChessPiece* rook = board.GetPiece(x, startY);
    if (rook && rook->GetMoveCount() != 0) return false;
    ChessColor opposingColor = SwitchColor(king->GetColor());
    for (int i = startX; GetCondition(i, startX, x); i += delta){
        if ((board.GetPiece(i, startY) && i != startX)
          || board.UnderAttack(opposingColor,i,startY)) return false;
    }
    return true;
}
Exemplo n.º 8
0
int Chess::findBestMove(Player* player, int depth) {

	Player* otherPlayer = player;
	if (player == &_computer) {
		otherPlayer = &_player;
	}
	else {
		otherPlayer = &_computer;
	}

	if (depth == 0) {
		return (player->score(_board.tiles()) - otherPlayer->score(_board.tiles()));
	}

	for (auto cp : player->activeChessPieces(_board.tiles())) {
		for (auto pm : cp->possibleMoves(_board.tiles())) {
			// Make move
			ChessPiece* endPosPiece = pm.toTile->_currPiece;
			cp->move(pm.toTile, _board.tiles());
			player->_moves.push_back(Move(pm.fromTile, pm.toTile));

			int score = findBestMove(otherPlayer, depth - 1);
			if (score > player->_bestScore){
				player->_bestScore = score;
				player->_bestMove = player->_moves[0];
			}

			// Restore piece
			cp->_currTile = pm.fromTile;
			pm.fromTile->_currPiece = cp;
			pm.toTile->_currPiece = endPosPiece;
			if (endPosPiece != nullptr) {
				endPosPiece->setActive(true);
			}
			player->_moves.pop_back();
			// TODO: Change the way we update position of chesspieces everywhere.
			cp->setPosition(pm.fromTile->_pos.x + pm.fromTile->_size.x / 2, pm.fromTile->_pos.y + pm.fromTile->_size.y / 2);
		}
	}

	return (player->score(_board.tiles()) - otherPlayer->score(_board.tiles()));
}
Exemplo n.º 9
0
void ChessBoard::ResolveCastleLineOfSight(bool bIsWhite)
{
	Player* opponentPlayer;
	Player* currPlayer;
	if (bIsWhite)
	{
		opponentPlayer = _BlackPlayer;
		currPlayer = _WhitePlayer;
	}
	else
	{
		opponentPlayer = _WhitePlayer;
		currPlayer = _BlackPlayer;
	}
	
	ChessSquare OpponentKingLoc = opponentPlayer->GetKingLocation();
	for (int i = 0; i < currPlayer->GetNumPiecesLeft(); i++)
	{
		ChessPiece* currPiece = currPlayer->GetPiece(i);
		if (pieceToText(currPiece) == 'R')
		{
			if (currPiece->HasLineToKing(OpponentKingLoc) )
			{
				currPlayer->AddToKingAttacking(currPiece);
			}
		}
	}
	opponentPlayer->ClearAttackingKingPieces();
	ChessSquare KingLoc = currPlayer->GetKingLocation();
	for (int i = 0; i < opponentPlayer->GetNumPiecesLeft(); i++)
	{
		ChessPiece* currPiece = opponentPlayer->GetPiece(i);
		
		if (currPiece->HasLineToKing( KingLoc) )
			opponentPlayer->AddToKingAttacking(currPiece);
	}
				
	
}
Exemplo n.º 10
0
bool ChessMove::IsValidMove(const ChessBoard& board) const{
    if (endX < 0 || endX > 7 || endY < 0 || endY > 7)
        return false;
    ChessPiece* startPiece = board.GetPiece(startX, startY);
    ChessPiece* occupyingPiece = board.GetPiece(endX, endY);
    ChessColor color = startPiece->GetColor();
    if (occupyingPiece && occupyingPiece->GetColor() == color)
        return false;
    switch (startPiece->GetRank()){
    case PAWN:
        return IsValidMovePawn(board) || IsValidEnPassantMove(board, GetDelta(color), GetStartingRow(color));
    case ROOK:
        return IsValidMoveRook(board);
    case KNIGHT:
        return IsValidMoveKnight();
    case BISHOP:
        return IsValidMoveBishop(board);
    case QUEEN:
        return IsValidMoveBishop(board) || IsValidMoveRook(board);
    case KING:
        return IsValidMoveKing() || IsValidCastleMove(board);
    }
    return false;
}
Exemplo n.º 11
0
ChessMove ChessMove::fromString(const std::string& str)
{
   if(str.length()<4)
   {
      return ChessMove();
   }
   PieceType promotion = ptNone;
   if(str.length()>4)
   {
      ChessPiece p = ChessPiece::fromChar(str[4]);
      switch(p.type())
      {
         case ptQueen:
         case ptRook:
         case ptBishop:
         case ptKnight:
            promotion = p.type();
            break;
      }
   }
   CoordPair cpair = CoordPair::fromString(str.substr(0, 4));
   if(cpair==CoordPair()) return ChessMove();
   return ChessMove(cpair, promotion);
}
Exemplo n.º 12
0
bool ChessMove::IsValidEnPassantMove(const ChessBoard& board, int delta, int startingRow) const{
    if (startY != startingRow+delta*3 || abs(startX-endX) != 1 || startY+delta != endY) return false;
    if (board.GetPiece(endX, endY)) return false;
    ChessPiece* potentialPawn = board.GetPiece(endX, startY);
    return potentialPawn && potentialPawn->GetRank()==PAWN && potentialPawn->GetMoveCount()==1;
}
Exemplo n.º 13
0
vector<Possible_Move> Rook::move()
{
	ChessPiecePosition current_position;
	Possible_Move possible_move;
	///////////////////////////////////////
	vector< vector<ChessPiece*> > Board = ChessBoard::GetBoard();
	vector<Possible_Move> All_Possible_Positions;
	current_position = GetPosition();
	bool Current_White = GetWhite();
	//Right
	for(int x=current_position.x+1 ; x<8 ; x++)
	{
		ChessPiece NewPiece = *Board[current_position.y][x];
		ChessPiecePosition p;
		p.x=x;
		p.y=current_position.y;
		if(NewPiece.GetName() == "")
		{
			possible_move.position = p;
			possible_move.Action=GLOBALS::Action_Move;
			All_Possible_Positions.push_back(possible_move);
		}
		else
		{
			if(NewPiece.GetWhite() != Current_White)
			{
				possible_move.position=p;
				possible_move.Action=GLOBALS::Action_Attack;
				All_Possible_Positions.push_back(possible_move);
			}
			break;
		}
	}
	//Left
	for(int x=current_position.x-1 ; x>=0 ; x--)
	{
		ChessPiece NewPiece =* Board[current_position.y][x];
		ChessPiecePosition p;
		p.x=x;
		p.y=current_position.y;
		if(NewPiece.GetName() == "")
		{
			possible_move.position=p;
			possible_move.Action=GLOBALS::Action_Move;
			All_Possible_Positions.push_back(possible_move);
		}
		else
		{
			if(NewPiece.GetWhite() != Current_White)
			{
				possible_move.position=p;
				possible_move.Action=GLOBALS::Action_Attack;
				All_Possible_Positions.push_back(possible_move);
			}
			break;
		}
	}
	//Down
	for(int y=current_position.y+1 ; y<8 ; y++)
	{
		ChessPiece NewPiece = *Board[y][current_position.x];
		ChessPiecePosition p;
		p.x=current_position.x;
		p.y=y;
		if(NewPiece.GetName() == "")
		{
			possible_move.position=p;
			possible_move.Action=GLOBALS::Action_Move;
			All_Possible_Positions.push_back(possible_move);
		}
		else
		{
			if(NewPiece.GetWhite() != Current_White)
			{
				possible_move.position=p;
				possible_move.Action=GLOBALS::Action_Attack;
				All_Possible_Positions.push_back(possible_move);
			}
			break;
		}
	}
	//Up
	for(int y=current_position.y-1 ; y>=0 ; y++)
	{
		ChessPiece NewPiece = *Board[y][current_position.x];
		ChessPiecePosition p;
		p.x=current_position.x;
		p.y=y;
		if(NewPiece.GetName() == "")
		{
			possible_move.position=p;
			possible_move.Action=GLOBALS::Action_Move;
			All_Possible_Positions.push_back(possible_move);
		}
		else
		{
			if(NewPiece.GetWhite() != Current_White)
			{
				possible_move.position=p;
				possible_move.Action=GLOBALS::Action_Attack;
				All_Possible_Positions.push_back(possible_move);
			}
			break;
		}
	}
	return All_Possible_Positions;
}
Exemplo n.º 14
0
int ChessBoard::CalculateScore(const Chromosome* calcChromo)
{
	int score = 0;
	BlackPlayer* blackPlayer = _BlackPlayer;
	WhitePlayer* whitePlayer = _WhitePlayer;
	
	std::vector<const ChessPiece*> WhiteRooks;
	WhiteRooks.reserve(10);
	
	std::vector<const ChessPiece*> BlackRooks;
	BlackRooks.reserve(10);
	
	std::vector<const ChessPiece*> WhiteKnights;
	WhiteKnights.reserve(10);
	
	std::vector<const ChessPiece*> BlackKnights;
	BlackKnights.reserve(10);
	
	std::vector<const ChessPiece*> WhiteBishops;
	WhiteBishops.reserve(10);
	
	std::vector<const ChessPiece*> BlackBishops;
	BlackBishops.reserve(10);
	
	std::vector<const ChessPiece*> WhitePawns[8];
	
	std::vector<const ChessPiece*> BlackPawns[8];
	
	ChessPiece* WhiteKing;
	ChessPiece* BlackKing;
	
	ChessSquare WhiteKingLoc = GetKingLocation(false);
	ChessSquare BlackKingLoc = GetKingLocation(true);
	
	for (int i = 0; i < whitePlayer->GetNumPiecesLeft(); i++)
	{
		ChessPiece* currPiece = whitePlayer->GetPiece(i);
		switch (pieceToText(currPiece) )
		{
			case 'R':
				WhiteRooks.push_back(currPiece);
				if (currPiece->IsAttackingSquare(BlackKingLoc.row, BlackKingLoc.col))
					score += currPiece->GetMobilityScore(calcChromo, false);
				else
					score += currPiece->GetMobilityScore(calcChromo, true);
				break;
			case 'B':
				WhiteBishops.push_back(currPiece);
				if (currPiece->IsAttackingSquare(BlackKingLoc.row, BlackKingLoc.col))
					score += currPiece->GetMobilityScore(calcChromo, false);
				else
					score += currPiece->GetMobilityScore(calcChromo, true);
				break;
			case 'P':
				WhitePawns[currPiece->GetRow()].push_back(currPiece);
				break;
			case 'K':
				WhiteKing = currPiece;
				break;
			case 'N':
				WhiteKnights.push_back(currPiece);
				if (currPiece->IsAttackingSquare(BlackKingLoc.row, BlackKingLoc.col))
					score += currPiece->GetMobilityScore(calcChromo, false);
				else
					score += currPiece->GetMobilityScore(calcChromo, true);
				break;
			default:
				//Don't care about queens
				if (currPiece->IsAttackingSquare(BlackKingLoc.row, BlackKingLoc.col))
					score += currPiece->GetMobilityScore(calcChromo, false);
				else
					score += currPiece->GetMobilityScore(calcChromo, true);
				break;
		}
	}
	
	for (int i = 0; i < blackPlayer->GetNumPiecesLeft(); i++)
	{
		ChessPiece* currPiece = blackPlayer->GetPiece(i);
		switch (pieceToText(currPiece) )
		{
			case 'R':
				BlackRooks.push_back(currPiece);
				if (currPiece->IsAttackingSquare(WhiteKingLoc.row, WhiteKingLoc.col))
					score -= currPiece->GetMobilityScore(calcChromo, false);
				else
					score -= currPiece->GetMobilityScore(calcChromo, true);
				break;
			case 'B':
				BlackBishops.push_back(currPiece);
				if (currPiece->IsAttackingSquare(WhiteKingLoc.row, WhiteKingLoc.col))
					score -= currPiece->GetMobilityScore(calcChromo, false);
				else
					score -= currPiece->GetMobilityScore(calcChromo, true);
				break;
			case 'P':
				BlackPawns[currPiece->GetRow()].push_back(currPiece);
				break;
			case 'K':
				BlackKing = currPiece;
				break;
			case 'N':
				BlackKnights.push_back(currPiece);
				if (currPiece->IsAttackingSquare(WhiteKingLoc.row, WhiteKingLoc.col))
					score -= currPiece->GetMobilityScore(calcChromo, false);
				else
					score -= currPiece->GetMobilityScore(calcChromo, true);
				break;
			default:
				//Don't care about queens
				if (currPiece->IsAttackingSquare(WhiteKingLoc.row, WhiteKingLoc.col))
					score -= currPiece->GetMobilityScore(calcChromo, false);
				else
					score -= currPiece->GetMobilityScore(calcChromo, true);
				break;
		}
	}

	for (int i = blackPlayer->GetNumPiecesLeft() - 1; i >= 0; i--)
	{
		ChessPiece * currPiece = blackPlayer->GetPiece(i);
		score -= currPiece->GetScore();
		//currPiece = NULL;
		
		/*{
			if (currPiece->IsAttackingSquare(WhiteKing->GetRow(), WhiteKing->GetCol() ) )
			{
				//Player shouldn't capture king	
				score -= currPiece->GetMobilityScore(calcChromo, true);
			}
			else {
				score -= currPiece->GetMobilityScore(calcChromo, false);
			}

		}
		score -= currPiece->GetDefendingScore(calcChromo);
		score += currPiece->GetPieceUnderAttackScore(calcChromo);*/
	}
	
	for (int i = whitePlayer->GetNumPiecesLeft() -1 ; i >= 0; i--)
	{
		ChessPiece * currPiece = whitePlayer->GetPiece(i);
		score += currPiece->GetScore();
		//currPiece = NULL;
		
		/*{
			if (currPiece->IsAttackingSquare(BlackKing->GetRow(), BlackKing->GetCol() ) )
			{
				//Player shouldn't capture king
				score += currPiece->GetMobilityScore(calcChromo, true);
			}
			else {
				score += currPiece->GetMobilityScore(calcChromo, false);
			}
			
			
		}

		score += currPiece->GetDefendingScore(calcChromo);
		score -= currPiece->GetPieceUnderAttackScore(calcChromo);*/
	}
	
	if (WhiteBishops.size() > 1)
	{
		score += calcChromo->GetWeighting(BishopPair);
	}
	if (BlackBishops.size() > 1)
	{
		score -= calcChromo->GetWeighting(BishopPair);
	}
	
	for (int i = 0; i < WhiteRooks.size(); i++)
	{
		if (WhiteRooks[i]->GetCol() == 6)
		{
			score += calcChromo->GetWeighting(RookOnSeventhRow);
			break;
		}
	}
	for (int i = 0; i < BlackRooks.size(); i++)
	{
		if (BlackRooks[i]->GetCol() == 1)
		{
			score -= calcChromo->GetWeighting(RookOnSeventhRow);
			break;
		}
	}
	
	score += RookBattery(WhiteRooks) * calcChromo->GetWeighting(RookBatteryType);
	score -= RookBattery(BlackRooks) * calcChromo->GetWeighting(RookBatteryType);
	
	score -= dynamic_cast<King*>(WhiteKing)->CastleMissed() * calcChromo->GetWeighting(CastleMissed);
	score += dynamic_cast<King*>(BlackKing)->CastleMissed() * calcChromo->GetWeighting(CastleMissed);
	
	score -= UnMovedWhiteKnightBishop(WhiteBishops, WhiteKnights) * calcChromo->GetWeighting(UnMovedKnightBishop);
	score += UnMovedBlackKnightBishop(BlackBishops, BlackKnights) * calcChromo->GetWeighting(UnMovedKnightBishop);
	
	score += calcChromo->GetWeighting(CenterAttackWeighting) * CenterAttackScore();
	score += calcChromo->GetWeighting(OpponentSquareWeighting) * OpponentSquareScore();
	score += calcChromo->GetWeighting(CastleScoreWeighting) * CastleScore();
	
	score += PawnStructureScore(WhitePawns, BlackPawns, WhiteRooks, calcChromo);
	score -= PawnStructureScore(BlackPawns, WhitePawns, BlackRooks, calcChromo);
	
	score += calcChromo->GetWeighting(OpenFileRookWeighting) * OpenFileRookScore();
	//const int CheckmateWeight = 100000;
	//score += CheckmateWeight * CheckMateScore();
	
	if (score < -1000000000 || score > 0x7FFFFFFF)
		ChessAssert();
	
	WhiteRooks.clear();
	BlackRooks.clear();
	WhiteKnights.clear();
	BlackKnights.clear();
	WhiteBishops.clear();
	BlackBishops.clear();
	for (int i =0 ; i < 8; i++)
	{
		WhitePawns[i].clear();
		BlackPawns[i].clear();
	}
	
	return score;
}
Exemplo n.º 15
0
void ChessBoard::PromotePawn(Pawn* pawn, const ChessSquare* MoveTo, char PromotionPiece)
{
	ChessSquare pieceLoc;
	pawn->GetLocation(pieceLoc);
	
	ChessMove* newMove = NULL;
	if ((MoveTo->row) != (pawn->GetLocation().row ))
	{
		if (!GetPiece(MoveTo->row, MoveTo->col))
			ChessAssert();
			
		bWhiteMove ? _BlackPlayer->RemovePiece(GetPiece(MoveTo->row, MoveTo->col)) :
		_WhitePlayer->RemovePiece(GetPiece(MoveTo->row, MoveTo->col));
		
		//NEED TO CHANGE THIS
		newMove = new ChessMove(pawn, pieceLoc.row, pieceLoc.col,MoveTo->row, MoveTo->col, true, this);
		
#ifdef PRINT_MEMORY_MOVES
		printf("Created move at: %i\n", newMove);
		fflush(stdout);
#endif
		
		Board[MoveTo->row][MoveTo->col] = NULL;
		
	}
	else 
	{
		newMove = new ChessMove(pawn, pieceLoc.row, pieceLoc.col,MoveTo->row, MoveTo->col, false, this);
		
#ifdef PRINT_MEMORY_MOVES
		printf("Created move at: %i\n", newMove);
		fflush(stdout);
#endif
	}
	
	newMove->SetPromotion(true);
	
	Board[pieceLoc.row][pieceLoc.col] = NULL;
	
	if(pieceToText(pawn) != 'P')
		ChessAssert();
	
	ChessPiece* newPiece;
	Player* currPlayer;
	
	ChessSquare currLoc = pawn->GetLocation();
	
	if ( pawn->isWhite() )
	{
		if(pawn->GetLocation().col != 6)
			ChessAssert();
		currPlayer = _WhitePlayer;
	}
	else {
		if(pawn->GetLocation().col != 1)
			ChessAssert();
		currPlayer = _BlackPlayer;
	}
	
	//char* c = new char;
	
	//bool bValidChar;
	
	//bValidChar = false;
	//
	//printf("What piece would you like to turn the pawn into? Type N, R, B, or Q");
	
	//while (! (bValidChar) )
	{
		//std::cin >> c;
		
		switch(PromotionPiece)
		{
			case 'N':
				newPiece = new Knight(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				break;
			case 'n':
				newPiece = new Knight(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				break;
			case 'R':
				newPiece = new Rook(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				break;
			case 'r':
				newPiece = new Rook(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				break;
			case 'B':
				newPiece = new Bishop(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				break;
			case 'b':
				newPiece = new Bishop(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				break;
			case 'Q':
				newPiece = new Queen(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				break;
			case 'q':
				newPiece = new Queen(MoveTo->row, MoveTo->col, pawn->isWhite(), this);
				newPiece->SetIsInKingArray(false);
				//bValidChar = true;
				break;
			default:
				ChessAssert();
				printf("Please enter in N, R, B, or Q");
				break;
		}
	}
	
	newMove->makeMove();
	newMove->SetPromotionChar(PromotionPiece);
	
	if (bWhiteMove)
	{
		if (_WhitePlayer->IsInCheck() )
			_WhitePlayer->ClearCheckState();
	}
	else {
		if (_BlackPlayer->IsInCheck() )
			_BlackPlayer->ClearCheckState();
	}
	
	ResolveCheck(bWhiteMove);
	
	if (bWhiteMove)
	{
		if (_BlackPlayer->IsInCheck() )
			newMove->SetCheckMove(true);
		else {
			newMove->SetCheckMove(false);
		}
		
	}
	else {
		if (_WhitePlayer->IsInCheck() )
			newMove->SetCheckMove(true);
		else {
			newMove->SetCheckMove(false);
		}
	}
	
	currPlayer->PawnPromotion(pawn, newPiece);
	
	bWhiteMove = !bWhiteMove;
	bBlackMove = !bBlackMove;
	
	
#ifndef SUPPRESS_OUTPUT
	_ChessLogInstance->printLog();
	PrintBoard();
#endif
	ResolveLineOfSight(newPiece);
	//printf("Promotion Below: \n");
	//ChessBoard::GetInstance()->PrintBoard();
}
Exemplo n.º 16
0
void ChessBoard::UndoMove()
{
	Player* currPlayer;
	Player* opponentPlayer;
	bool bKingMoved = false;
	int col;
	if (bWhiteMove)
	{
		currPlayer = _BlackPlayer;
		opponentPlayer = _WhitePlayer;
		col = 7;
	}
	else {
		currPlayer = _WhitePlayer;
		opponentPlayer = _BlackPlayer;
		col = 0;
	}

	ChessMove* moveToUndo = _ChessLogInstance->getLastMove();
	if ( moveToUndo->isCastle() )
	{
		if (moveToUndo->isKingSideCastle())
		{
			ChessPiece* king = GetPiece(6, col); 
			if(pieceToText(king) != 'K')
				ChessAssert();
			ChessPiece* rook = GetPiece(5, col);
			if(pieceToText(rook) != 'R')
				ChessAssert();
			rook->SetRow(7);
			king->SetRow(4);
			
			Board[4][col] = king;
			Board[6][col] = NULL;
			Board[5][col] = NULL;
			Board[7][col] = rook;
			
			(dynamic_cast<King*>(king))->SetHasMoved(false);
			(dynamic_cast<King*>(king))->SetHasCastled(false);
			(dynamic_cast<Rook*>(rook))->SetHasMoved(false);
			ResolveLineOfSight(rook);
		}
		else {
			ChessPiece* king = GetPiece(2, col); 
			if(pieceToText(king) != 'K')
				ChessAssert();
			ChessPiece* rook = GetPiece(3, col);
			if(pieceToText(rook) != 'R')
				ChessAssert();
			rook->SetRow(0);
			king->SetRow(4);
			
			Board[4][col] = king;
			Board[3][col] = NULL;
			Board[2][col] = NULL;
			Board[0][col] = rook;
			(dynamic_cast<King*>(king))->SetHasMoved(false);
			(dynamic_cast<King*>(king))->SetHasCastled(false);
			(dynamic_cast<Rook*>(rook))->SetHasMoved(false);
			ResolveLineOfSight(rook);
		}
		bKingMoved = true;

	}
	//NEED TO CHECK FOR PAWN PROMOTION HERE
	else if ( moveToUndo->isPromotion() )
	{
		ChessPiece* newPromoPiece = GetPiece(moveToUndo->getRow(), moveToUndo->getCol());
		ChessSquare PromotionSquare = ChessSquare(moveToUndo->getRow(), moveToUndo->getCol());
		currPlayer->UndoPawnPromotion(moveToUndo->getStartingRow(), PromotionSquare, newPromoPiece);
		Board[moveToUndo->getRow()][moveToUndo->getCol()] = NULL;
		if (PromotionSquare.row != moveToUndo->getStartingRow())
		{
			ChessPiece* capturedPiece = opponentPlayer->FindCapturedPiece(moveToUndo->getRow(), moveToUndo->getCol(), moveToUndo->getCapturedPiece());
			if(!capturedPiece)
				ChessAssert();
			ChessSquare capturedPieceLoc = capturedPiece->GetLocation();
			if (Board[capturedPieceLoc.row][capturedPieceLoc.col] != NULL )
				ChessAssert();
			opponentPlayer->RebirthCapturedPiece(capturedPiece);
			AddPiece(capturedPiece);
		}
		//printf("Undid pawn promotion below: \n");
		//ChessBoard::GetInstance()->PrintBoard();
	}
	else if ( moveToUndo->IsEnPassant() )
	{
		ChessPiece* capturedPiece = opponentPlayer->FindCapturedPiece(moveToUndo->getRow(), moveToUndo->getStartingCol(), moveToUndo->getCapturedPiece());
		if(!capturedPiece)
			ChessAssert();
		ChessPiece* movedPiece = GetPiece(moveToUndo->getRow(), moveToUndo->getCol() );
		
		Board[moveToUndo->getRow()][moveToUndo->getCol()] = NULL;
		Board[moveToUndo->getRow()][moveToUndo->getStartingCol()] = capturedPiece;
		Board[moveToUndo->getStartingRow()][moveToUndo->getStartingCol()] = movedPiece;
		
		movedPiece->SetRow(moveToUndo->getStartingRow());
		movedPiece->SetCol(moveToUndo->getStartingCol());
		
		//AddPiece(capturedPiece);
		ResolveLineOfSight(capturedPiece);
		opponentPlayer->RebirthCapturedPiece(capturedPiece);
		ResolveLineOfSight(movedPiece);
	}
	else if ( moveToUndo->isCapture() )
	{
		ChessPiece* capturedPiece = moveToUndo->getCapturedPiece();
		if (!capturedPiece)
			ChessAssert();
		ChessPiece* movedPiece = GetPiece(moveToUndo->getRow(), moveToUndo->getCol() );
		
		Board[moveToUndo->getRow()][moveToUndo->getCol()] = capturedPiece;
		Board[moveToUndo->getStartingRow()][moveToUndo->getStartingCol()] = movedPiece;
		
		movedPiece->SetRow(moveToUndo->getStartingRow());
		movedPiece->SetCol(moveToUndo->getStartingCol());
		
		if (pieceToText(movedPiece) == 'K')
		{
			(dynamic_cast<King*>(movedPiece))->SetHasMoved(false);
			bKingMoved = true;
		}
		else if (pieceToText(movedPiece) == 'R')
			(dynamic_cast<Rook*>(movedPiece))->SetHasMoved(false);
		
		ResolveLineOfSight(capturedPiece);
		opponentPlayer->RebirthCapturedPiece(capturedPiece);
		ResolveLineOfSight(movedPiece);
	}
	else
	{
		ChessPiece* movedPiece = GetPiece(moveToUndo->getRow(), moveToUndo->getCol() );
		Board[moveToUndo->getRow()][moveToUndo->getCol()] = NULL;
		Board[moveToUndo->getStartingRow()][moveToUndo->getStartingCol()] = movedPiece;
		movedPiece->SetRow(moveToUndo->getStartingRow());
		movedPiece->SetCol(moveToUndo->getStartingCol());
		
		if (pieceToText(movedPiece) == 'K')
		{
			(dynamic_cast<King*>(movedPiece))->SetHasMoved(false);
			bKingMoved = true;
		}
		else if (pieceToText(movedPiece) == 'R')
			(dynamic_cast<Rook*>(movedPiece))->SetHasMoved(false);
		ResolveLineOfSight(movedPiece);
	}
	
	if (bKingMoved)
	{
		currPlayer->ClearCheckState();
		opponentPlayer->ClearAttackingKingPieces();
		
		ChessSquare kingLoc = currPlayer->GetKingLocation();
		for (int i = 0; i < opponentPlayer->GetNumPiecesLeft(); i++)
		{
			if (CheckForCheck(opponentPlayer->GetPiece(i)) )
			{
				currPlayer->EnterCheckState();
				//printf("Checking Piece: %c", pieceToText(currPlayer->GetAttackingKingPiece(i)));
				//fflush(stdout);
				opponentPlayer->AddToKingAttacking(opponentPlayer->GetPiece(i));
			}
			else if (opponentPlayer->GetPiece(i)->HasLineToKing(kingLoc))
				opponentPlayer->AddToKingAttacking(opponentPlayer->GetPiece(i));
		}
	}
	else 
	{
		for (int i = 0; i < opponentPlayer->GetNumAttackingKingPieces(); i++)
		{
			if (CheckForCheck(opponentPlayer->GetAttackingKingPiece(i)) )
			{
				currPlayer->EnterCheckState();
				//printf("Checking Piece: %c", pieceToText(opponentPlayer->GetAttackingKingPiece(i)));
				//fflush(stdout);
				break;
			}
			if (i == (opponentPlayer->GetNumAttackingKingPieces() - 1))
				currPlayer->ClearCheckState();
		}
	}

	
	for (int i = 0; i < currPlayer->GetNumAttackingKingPieces(); i++)
	{
		if (CheckForCheck(currPlayer->GetAttackingKingPiece(i)) )
		{
			opponentPlayer->EnterCheckState();
			//printf("Checking Piece: %c", pieceToText(currPlayer->GetAttackingKingPiece(i)));
			//fflush(stdout);
			break;
		}
		if (i == (currPlayer->GetNumAttackingKingPieces() - 1))
			opponentPlayer->ClearCheckState();
	}
	
	_ChessLogInstance->removeLastItemFromLog();
	
	bWhiteMove = !bWhiteMove;
	bBlackMove = !bBlackMove;
	//REMOVE BELOW WHEN WORKING
	ChessSquare currPlayerKing = currPlayer->GetKingLocation();
	/*for (int i = 0; i < opponentPlayer->GetNumPiecesLeft(); i++)
	{
		if (opponentPlayer->GetPiece(i)->HasLineToKing(currPlayerKing) )
		{
			if (!(opponentPlayer->GetPiece(i)->IsInKingArray()))
			{
				ChessPiece* forTestPiece = opponentPlayer->GetPiece(i);
				forTestPiece->HasLineToKing(currPlayerKing);
				ChessAssert();
			}
		}
		else {
			if (opponentPlayer->GetPiece(i)->IsInKingArray())
				ChessAssert();
		}

	}
	ChessSquare opponentPlayerKing = opponentPlayer->GetKingLocation();
	for (int i = 0; i < currPlayer->GetNumPiecesLeft(); i++)
	{
		if (currPlayer->GetPiece(i)->HasLineToKing(opponentPlayerKing) )
		{
			if (!(currPlayer->GetPiece(i)->IsInKingArray()))
				ChessAssert();
		}
		else {
			if (currPlayer->GetPiece(i)->IsInKingArray())
				ChessAssert();
		}
		
	}*/
}