C++ Builder - 在棋盘上移动棋子移动下一个棋子

C++ Builder - Moving a chess piece on board moves the next piece

我有问题。我有一个国际象棋游戏,我想让我的棋子在棋盘上随机移动。不知道为什么,当我移动一块时,下一块也会移动,所以我写了move(piece-1)。那样的话,只有第一块不能移动。如何解决?

formaJoc.cpp

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "formaJoc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define MAX_FIGURI 20
TfJoc *fJoc;
Test fnc;
Piece *pc[MAX_FIGURI];
//---------------------------------------------------------------------------
__fastcall TfJoc::TfJoc(TComponent* Owner)
        : TForm(Owner)
{

        blBoard[0] = new Board (0, 0, fJoc);

        BlackRook1[0]   =  new bkRook1  ( 40, 40, fJoc);
        BlackRook1[0]   -> LoadImg();
        BlackRook2[0]   =  new bkRook2  (292, 40, fJoc);
        BlackRook2[0]   -> LoadImg();
        BlackKnight1[0] =  new bkKnight1( 76, 40, fJoc);
        BlackKnight1[0] -> LoadImg();
        BlackKnight2[0] =  new bkKnight2(256, 40, fJoc);
        BlackKnight2[0] -> LoadImg();
        BlackBishop1[0] =  new bkBishop1(112, 40, fJoc);
        BlackBishop1[0] -> LoadImg();
        BlackBishop2[0] =  new bkBishop2(220, 40, fJoc);
        BlackBishop2[0] -> LoadImg();
        BlackQueen[0]   =  new bkQueen (148, 40, fJoc);
        BlackQueen[0]   -> LoadImg();
        BlackKing [0]   =  new bkKing  (184, 40, fJoc);
        BlackKing [0]   -> LoadImg();
        BlackPawns[0]   =  new bkPawn  ( 40, 76, fJoc);
        BlackPawns[0]   -> LoadImg();
        BlackPawns[1]   =  new bkPawn  ( 76, 76, fJoc);
        BlackPawns[1]   -> LoadImg();
        BlackPawns[2]   =  new bkPawn  (112, 76, fJoc);
        BlackPawns[2]   -> LoadImg();
        BlackPawns[3]   =  new bkPawn  (148, 76, fJoc);
        BlackPawns[3]   -> LoadImg();
        BlackPawns[4]   =  new bkPawn  (184, 76, fJoc);
        BlackPawns[4]   -> LoadImg();
        BlackPawns[5]   =  new bkPawn  (220, 76, fJoc);
        BlackPawns[5]   -> LoadImg();
        BlackPawns[6]   =  new bkPawn  (256, 76, fJoc);
        BlackPawns[6]   -> LoadImg();
        BlackPawns[7]   =  new bkPawn  (292, 76, fJoc);
        BlackPawns[7]   -> LoadImg();

        WhiteRooks[0]   =  new whRook  ( 40, 292, fJoc);
        WhiteRooks[0]   -> LoadImg();
        WhiteRooks[1]   =  new whRook  (292, 292, fJoc);
        WhiteRooks[1]   -> LoadImg();
        WhiteKnights[0] =  new whKnight( 76, 292, fJoc);
        WhiteKnights[0] -> LoadImg();
        WhiteKnights[1] =  new whKnight(256, 292, fJoc);
        WhiteKnights[1] -> LoadImg();
        WhiteBishops[0] =  new whBishop(112, 292, fJoc);
        WhiteBishops[0] -> LoadImg();
        WhiteBishops[1] =  new whBishop(220, 292, fJoc);
        WhiteBishops[1] -> LoadImg();
        WhiteQueen[0]   =  new whQueen (148, 292, fJoc);
        WhiteQueen[0]   -> LoadImg();
        WhiteKing [0]   =  new whKing  (184, 292, fJoc);
        WhiteKing [0]   -> LoadImg();
        WhitePawns[0]   =  new whPawn  ( 40, 256, fJoc);
        WhitePawns[0]   -> LoadImg();
        WhitePawns[1]   =  new whPawn  ( 76, 256, fJoc);
        WhitePawns[1]   -> LoadImg();
        WhitePawns[2]   =  new whPawn  (112, 256, fJoc);
        WhitePawns[2]   -> LoadImg();
        WhitePawns[3]   =  new whPawn  (148, 256, fJoc);
        WhitePawns[3]   -> LoadImg();
        WhitePawns[4]   =  new whPawn  (184, 256, fJoc);
        WhitePawns[4]   -> LoadImg();
        WhitePawns[5]   =  new whPawn  (220, 256, fJoc);
        WhitePawns[5]   -> LoadImg();
        WhitePawns[6]   =  new whPawn  (256, 256, fJoc);
        WhitePawns[6]   -> LoadImg();
        WhitePawns[7]   =  new whPawn  (292, 256, fJoc);
        WhitePawns[7]   -> LoadImg();

}

int id, row, col;
int &porecla=id;
int rowColToPixel[10] = {40, 76, 112, 148, 184, 220, 256, 292, 0, 331};

//---------------------------------------------------------------------------

void movePiece(int piece, int col, int row)
{
   int left = rowColToPixel[col];
   int top  = rowColToPixel[row];
      switch (piece)
      {
      case(1): {fJoc->BlackRook1[1]->imPiece->Left=left;fJoc->BlackRook1[1]->imPiece->Top=top;break;}
      case(2): {fJoc->BlackKnight1[1]->imPiece->Left=left;fJoc->BlackKnight1[1]->imPiece->Top=top;break;}
      case(3): {fJoc->BlackBishop1[1]->imPiece->Left=left;fJoc->BlackBishop1[1]->imPiece->Top=top;break;}
      case(4): {fJoc->BlackQueen[1]->imPiece->Left=left;fJoc->BlackQueen[1]->imPiece->Top=top;break;}
      case(5): {fJoc->BlackKing[1]->imPiece->Left=left;fJoc->BlackKing[1]->imPiece->Top=top;break;}
      case(6): {fJoc->BlackBishop2[1]->imPiece->Left=left;fJoc->BlackBishop2[1]->imPiece->Top=top;break;}
      case(7): {fJoc->BlackKnight2[1]->imPiece->Left=left;fJoc->BlackKnight2[1]->imPiece->Top=top;break;}
      case(8): {fJoc->BlackRook2[1]->imPiece->Left=left;fJoc->BlackRook2[1]->imPiece->Top=top;break;}
      case(9): {fJoc->BlackPawns[8]->imPiece->Left=left;fJoc->BlackPawns[8]->imPiece->Top=top;break;}
      }
}

//---------------------------------------------------------------------------

void __fastcall TfJoc::btnStartClick(TObject *Sender)
{
        exit(0);
}
//---------------------------------------------------------------------------

void __fastcall TfJoc::btnExitClick(TObject *Sender)
{
        exit(0);
}
//---------------------------------------------------------------------------

void __fastcall TfJoc::Image1Click(TObject *Sender)
{
        void LoadImg();        
}
//---------------------------------------------------------------------------
void __fastcall TfJoc::Button1Click(TObject *Sender)
{
        fnc.Test123("Muie!");
}
//---------------------------------------------------------------------------

void __fastcall TfJoc::bkRook1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 1;}
void __fastcall TfJoc::bkKnight1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 2;}
void __fastcall TfJoc::bkBishop1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 3;}
void __fastcall TfJoc::bkQueenMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 4;}
void __fastcall TfJoc::bkKingMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 5;}
void __fastcall TfJoc::bkBishop2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 6;}
void __fastcall TfJoc::bkKnight2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 7;}
void __fastcall TfJoc::bkRook2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 8;}
void __fastcall TfJoc::bkPawnMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 9;}

void __fastcall TfJoc::whRookMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 10;}
void __fastcall TfJoc::whKnightMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 11;}
void __fastcall TfJoc::whBishopMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 12;}
void __fastcall TfJoc::whQueenMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 13;}
void __fastcall TfJoc::whKingMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 14;}
void __fastcall TfJoc::whPawnMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y) {id = 15;}

void __fastcall TfJoc::imBoardMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        if (id != 99) {
                col = (X - 40) / 36;
                row = (Y - 40) / 36;
                movePiece(::id-1, col, row);
                id=99;
                //ShowMessage(id);
        }
}

formaJoc.h

//---------------------------------------------------------------------------

#ifndef formaJocH
#define formaJocH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <jpeg.hpp>
#include <Dialogs.hpp>
#include <windows.h>
#include "whBishop.h"
#include "bkBishop.h"
#include "whRook.h"
#include "bkRook.h"
#include "whKnight.h"
#include "bkKnight.h"
#include "whKing.h"
#include "bkKing.h"
#include "whQueen.h"
#include "bkQueen.h"
#include "whPawn.h"
#include "bkPawn.h"
#include "Unit1.h"
#include "Board.h"
//---------------------------------------------------------------------------
class TfJoc : public TForm
{
        friend class Piece;
__published:    // IDE-managed Components
        TButton *btnStart;
        TButton *btnExit;
        TButton *Button1;
        TImage *Image1;
        void __fastcall btnStartClick(TObject *Sender);
        void __fastcall btnExitClick(TObject *Sender);
        void __fastcall Image1Click(TObject *Sender);
        void __fastcall Button1Click(TObject *Sender);
private:
public:

        Board* blBoard[1];

        bkRook1*   BlackRook1[1];
        bkKnight1* BlackKnight1[1];
        bkBishop1* BlackBishop1[1];
        bkQueen*   BlackQueen[1];
        bkKing*    BlackKing[1];
        bkBishop2* BlackBishop2[1];
        bkKnight2* BlackKnight2[1];
        bkRook2*   BlackRook2[1];
        bkPawn*    BlackPawns[8];

        whRook*   WhiteRooks[2];
        whKnight* WhiteKnights[2];
        whBishop* WhiteBishops[2];
        whQueen*  WhiteQueen[1];
        whKing*   WhiteKing[1];
        whPawn*   WhitePawns[8];

        void __fastcall TfJoc::bkRook1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkKnight1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkBishop1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkQueenMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkKingMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkBishop2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkKnight2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkRook2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::bkPawnMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);

        void __fastcall TfJoc::whRookMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::whKnightMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::whBishopMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::whQueenMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::whKingMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall TfJoc::whPawnMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);

        void __fastcall imBoardMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y);

        __fastcall TfJoc(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfJoc *fJoc;
//---------------------------------------------------------------------------
#endif

像这样每个class:

bkBishop1::bkBishop1(unsigned int a, unsigned int b, TfJoc* fJoc):Piece(a, b, fJoc)
{
        nBishop1 = "Imagini/Nebun1.bmp";
        imPiece -> Left = a;
        imPiece -> Top = b;
        imPiece -> OnMouseDown = fJoc -> bkBishop1MouseDown;
}

void bkBishop1::LoadImg()
{
        imPiece->Picture->LoadFromFile(nBishop1);
}

bkBishop2::bkBishop2(unsigned int a, unsigned int b, TfJoc* fJoc):Piece(a, b, fJoc)
{
        nBishop2 = "Imagini/Nebun1.bmp";
        imPiece -> Left = a;
        imPiece -> Top = b;
        imPiece -> OnMouseDown = fJoc -> bkBishop2MouseDown;
}

void bkBishop2::LoadImg()
{
        imPiece->Picture->LoadFromFile(nBishop2);
}

您移动了 fJoc->BlackRook1[1],但该值超出了数组 BlackRook1 的有效范围。

bkRook1*   BlackRook1[1];

只定义了一个元素,即BlackRook1[0].

"the next piece moves" 的原因纯属偶然。 未定义的行为 寻址超出其范围的数组对象,实际上任何事情都可能发生 – 包括 "appears to work correctly",或者,如您的情况,"appears to work, apart from this minor thing"。

我想它恰好适用于你的情况,因为所有对象初始化都是一个接一个,所以 "beyond" 很高兴地指向下一个创建的对象。 (除最后一个外,其他都可以。请随意尝试。)