2

我正在编写一个国际象棋程序,我有一个名为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;
  }
}

我希望能够在不使用指针的情况下引用此类,以便不对电路板进行永久性更改。

4

3 回答 3

4

你需要为每个类提供一个虚拟的 Clone() 函数,对于 Bishop 来说,它看起来像:

Piece * Bishop :: Clone() {
     return new Bishop( * this );
}

现在从 OldBoard 克隆板,例如:

Board newboard;   // assume board is 8x8 matrix of Piece *

for ( int i = 0; i < 8; i++ ) {
    for ( int j = 0; j < 8; j++ ) {
       newboard[i][j] = OldBoard[i][j]->Clone();
    }
}
于 2011-05-04T08:13:06.837 回答
2

你需要一个抽象方法

 Piece *Piece::Duplicate()

然后在子类中实现它,即

 class Bishop {
   Bishop(const Bishop &b) { ... } // copy constructor
   Piece *Duplicate() { Bishop *n = new Bishop(*this); // calls copy constructor
     return n; }
 }
于 2011-05-04T08:12:00.960 回答
1

或者您可以为 Bishop 类编写一个复制构造函数,假设您的 currentBoard 是 Bishop 类的一个实例,该类又实现了一个深拷贝。

于 2011-05-04T08:19:39.120 回答