我正在编写一个国际象棋程序,我有一个名为Pieces
. 我通过使用指针在我的主类中使用这个抽象类,如下所示:Pieces * newset = currentboard[][];
如果我在这块棋盘上移动,实际上已经移动了。我想分析板的当前状态,因此创建板的副本。我怎么做?
下面给出了我的班级样本Piece
和我正在做的事情。
class Piece
{
public:
Piece(char cColor) : mcColor(cColor) {}
~Piece() {}
virtual char GetPiece() = 0;
virtual int GetValue() = 0;
virtual int GetPieceActionValue() = 0;
char GetColor() {
return mcColor;
}
bool IsLegalMove(int CurrentRow, int CurrentCol, int DestRow, int DestCol, Piece* NewBoard[8][8]) {
Piece* qpDest = NewBoard[DestRow][DestCol];
if ((qpDest == 0) || (mcColor != qpDest->GetColor())) {
return LegalSquares(CurrentRow, CurrentCol, DestRow, DestCol, NewBoard);
}
return false;
}
private:
virtual bool LegalSquares(int CurrentRow, int CurrentCol, int DestRow, int DestCol, Piece* NewBoard[8][8]) = 0;
char mcColor;
};
这是派生类的示例:
class Bishop
{
public:
Bishop(char cColor) : Piece(cColor) {}
~Bishop() {}
private:
virtual char GetPiece() {
return 'B';
}
virtual int GetValue() {
return 325;
}
virtual int GetPieceActionValue() {
return 3;
}
bool LegalSquares(int CurrentRow, int CurrentCol, int DestRow, int DestCol, Piece* NewBoard[8][8]) {
if ((DestCol - CurrentCol == DestRow - CurrentRow) || (DestCol - CurrentCol == CurrentRow - DestRow)) {
// Make sure that all invervening squares are empty
int iRowOffset = (DestRow - CurrentRow > 0) ? 1 : -1;
int iColOffset = (DestCol - CurrentCol > 0) ? 1 : -1;
int iCheckRow;
int iCheckCol;
for (iCheckRow = CurrentRow + iRowOffset, iCheckCol = CurrentCol + iColOffset;
iCheckRow != DestRow;
iCheckRow = iCheckRow + iRowOffset, iCheckCol = iCheckCol + iColOffset)
{
if (NewBoard[iCheckRow][iCheckCol] != 0) {
return false;
}
}
return true;
}
return false;
}
};
使用类进行移动:
if (qpCurrPiece->IsLegalMove(StartRow, StartCol, EndRow, EndCol, NewBoard)) {
// Make the move
Piece* qpTemp = NewBoard[EndRow][EndCol];
NewBoard[EndRow][EndCol] = NewBoard[StartRow][StartCol];
NewBoard[StartRow][StartCol] = 0;
// Make sure that the current player is not in check
if (!GameBoard.IsInCheck(mcPlayerTurn)) {
delete qpTemp;
bValidMove = true;
} else {
// Undo the last move
NewBoard[StartRow][StartCol] = NewBoard[EndRow][EndCol];
NewBoard[EndRow][EndCol] = qpTemp;
}
}
我希望能够在不使用指针的情况下引用此类,以便不对电路板进行永久性更改。