(Chess game)Function that returns possible moves of pawn
I'm writing a C++ chess game.I have an abstract class Piece and classes which inherit it.Class Piece has pure virtual method which returns all possible moves.But I have a problem with pawn position, because for each player pawn can move only forward, but in the board(which is matrix of Piece) forward appears different for each player.How can I implement possibleMoves() for pawns ? Thanks!
class Piece
{
public:
Piece(std::string, char verticalPosition, char horizontalPosition);
virtual std::pair<char,char>* possibleMoves() const = 0;
virtual ~Piece();
protected:
std::string name;
std::pair <char,char> initPosition;
std::pair <char,char> currPosition;
};
Pass the direction the pawn can move into its constructor as a parameter and store as a member variable. Use this when determining possible moves for the pawn.
As BobTFish said in comments, the pawn object should know it's color. As for the position, I think I'd pass it as an argument to possibleMoves
; you probably also have to pass the board, so that possibleMoves
can detect moves which are captures (and en passant, which gets more complicated, since it is only valid the first time the possibility arises).
上一篇: 国际象棋棋盘的ASCII码表示
下一篇: (国际象棋游戏)返回可能的棋子移动的功能