我遇到了问题。我有一个象棋游戏,我想让我的棋子在船上随机移动。我不知道为什么,但当我移动一块时,下一块移动,所以我写了一个移动(第一块)。在这种情况下,只有第一件不能移动。怎么解决这个问题?
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
这就是每个班级:
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);
}
答案 0 :(得分:0)
您移动fJoc->BlackRook1[1]
,但该值超出了数组BlackRook1
的有效范围。
该行
bkRook1* BlackRook1[1];
只定义一个单独的元素,即BlackRook1[0]
。
原因&#34;下一部分移动&#34;是纯粹的机会。 未定义的行为用于处理超出其范围的数组对象,实际上可能发生任何事情 - 包括&#34;似乎正常工作&#34;,或者,如在您的情况下,&#34;似乎有用,除了这个小事#34;。
我想它恰好适合你的情况,因为所有的对象初始化都是一个接一个,所以&#34;超越&#34;愉快地指向下一个创建的。 (除了最后一个之外,就是。随意尝试。)