Exemple #1
0
/**
@param from The old position.
@param to The new position.
*/
void OnlinePlayer::opponentMoved(Position from, Position to)
{
	// Get the piece perhaps gets captured
	Piece* piece = getBoard()->getPieceAt(to.x, to.y);
	Piece* movedPiece = getBoard()->getPieceAt(from.x, from.y);
	if(piece != NULL)	{
		mGui->addCapture(piece->getColor(), piece->getType());
		getBoard()->removePiece(piece);
		pieceCapturedSound();
	}
	else	{
		// Castling?
		if(movedPiece->getType() == KING && abs(from.x - to.x) == 2)
			gSound->playEffect(CASTLE_SOUND);
		else
			pieceMovedSound();
	}

	movedPiece->setPos(to.x, to.y);
	movedPiece->moved();

	// No longer waiting for the opponent to do a move
	mWaitingOnMove = false;

	// Set the last move position
	setLastMove(from, to);

	// Checkmate?
	if(getBoard()->checkMate(getColor()))	{
		mGui->setStatus("Check mate!", RED, 100.0f);
		gGame->drawAll();
		mCheckMate = true;
		mGui->displayCheckMate(false);
		gDatabase->addLoss(getName());
	}
	else
		mGui->setStatus("Your turn!", GREEN, 300.0f);
}
Exemple #2
0
/**
@return The result of the action the player made. WRONG_COLOR, PIECE_SELECTED, PIECE_MOVED, INVALID_POSITION.
*/
ActionResult Player::performAction()
{
	// Translate mouse coordinate to sqaure coordinates
	Position pos = mBoard->toGridPos(gInput->mousePosition());
	ActionResult action;
	bool moved = false;

	// No piece is selected
	if(gInput->keyPressed(VK_LBUTTON) && mSelectedPiece == NULL)
	{
		Piece* piece = mBoard->getPieceAt(pos.x, pos.y);
		// A piece was pressed
		if(piece != NULL)
		{
			// The piece has another color than the players
			if(piece->getColor() != getColor())	{
				// Return wrong color msg
				action = ActionResult(WRONG_COLOR, pos);
				gSound->playEffect(ILLEGAL_SOUND);
			}
			else	{
				mSelectedPiece = piece;
				mBoard->setSelected(pos.x, pos.y);
				// Return piece selected msg
				action = ActionResult(PIECE_SELECTED, pos);
			}
		}
	}
	// A piece is already selected
	else if(gInput->keyPressed(VK_LBUTTON) && mSelectedPiece != NULL)
	{
		Piece* piece = mBoard->getPieceAt(pos.x, pos.y);
		Position oldPos = mSelectedPiece->getPos();
		// Was it a piece of the same color that was pressed? - Change the selected piece
		if(piece != NULL && piece->getColor() == getColor())
		{
			mSelectedPiece = piece;
			action = ActionResult(PIECE_SELECTED, pos);
		}
		// Didn't select a piece of the same color as self
		else
		{
			// The piece can move to the pressed location
			if(mBoard->validMove(mSelectedPiece, pos.x, pos.y))	
			{
				// A piece was captured
				if(piece != NULL)	{
					handleCapture(piece->getColor(), piece->getType());
					mBoard->removePiece(piece);
					pieceCapturedSound();
				}

				// Return piece moved msg
				action = ActionResult(PIECE_MOVED, pos, mSelectedPiece->getPos());

				// Update the selected piece's position
				mSelectedPiece->setPos(pos.x, pos.y);
				mSelectedPiece->moved();

				// Was it castling?
				if(mSelectedPiece->getType() == KING && abs(mSelectedPiece->getPos().x - oldPos.x) > 1)	{
					handleCastling(mSelectedPiece);
					gSound->playEffect(CASTLE_SOUND);
				}
				else
					pieceMovedSound();

				mSelectedPiece = NULL;
			}
			// Can't move to the pressed location
			else
			{
				// Return invalid position or checked msg
				if(mBoard->resultsInCheck(mSelectedPiece, pos.x, pos.y))
					action = ActionResult(GETS_CHECKED, mBoard->getPiece(KING, mSelectedPiece->getColor())->getPos());
				else	{
					if(pos.x >= 0 && pos.x <= 7 && pos.y >= 0 && pos.y <= 7)
						action = ActionResult(INVALID_POSITION, pos);
					else
						action = ActionResult(PIECE_SELECTED, Position(-10, -10));
				}
				gSound->playEffect(ILLEGAL_SOUND);
				mSelectedPiece = NULL;
			}
		}
	}

	// Return the action
	return action;
}