我的项目需要帮助。该项目是关于国际象棋游戏,我必须编写代码并有一个用户可以玩游戏的界面,但这是我们的老师给我们的东西,我们只需要编码。所以我的问题是,在我的board.cpp
代码和(我认为)我的所有数字中。
让我解释一下项目本身: 我有一个接口,它给我一个字符串,它包含有一个数字(或没有)的正方形和我们想要移动数字的正方形。
例如: 如果我想将 A2中的白色棋子移动到A3 ,我将收到的字符串是' A2A3' ,那么我必须返回char *包含一个数字(0 - 7)。所以在我运行程序之前我没有遇到任何问题,这意味着一切都很好但是当程序运行时我收到了下一条消息: check error here please
之后它指出了检查功能所在的功能(在board.cpp中),有几次我注意到了这一点:check this link please 看起来我的_wFigure(和_bFigure)没有获取intalization中的值...所以为了帮助你理解我的更多代码我粘贴在这里我的board.cpp代码:
#include "Board.h"
#include "Square.h"
#include "Figure.h"
#include "King.h"
#include "Knight.h"
#include "Bishop.h"
#include "Pawn.h"
#include "Queen.h"
#include "Rook.h"
#include <string>
char conventor_col(int num);
char conventor_row(int num);
Board::Board()
{
Figure* _bFigure = new Figure[16];
Figure* _wFigure = new Figure[16];
int i = 0;
int place = 8;
int row = 6;
int j;
_board = "rnbkqbnrpppppppp################################PPPPPPPPRBNKQNBR";
_boardSq = new Square[64];
_boardSq[0] = Square(8, 1, false, 'r', 0);
_boardSq[1] = Square(8, 2, true, 'n', 1);
_boardSq[2] = Square(8, 3, false, 'b', 2);
_boardSq[3] = Square(8, 4, true, 'k', 3);
_boardSq[4] = Square(8, 5, false, 'q', 4);
_boardSq[5] = Square(8, 6, true, 'b', 5);
_boardSq[6] = Square(8, 7, false, 'n', 6);
_boardSq[7] = Square(8, 8, true, 'r', 7);
for (i = 1; i <= 8; i++)
{
if (i % 2 == 1)
{
_boardSq[place] = Square(7, i, true, 'p', place);
place++;
}
else
{
_boardSq[place] =Square(7, i, false, 'p', place);
place++;
}
}
for (i = 0; i < 3; i++)
{
for (j = 1; j < 8; j++)
{
if (i % 2 == 1 && row % 2 == 0)
{
_boardSq[place] = Square(row, j, false, '#', place);
place++;
}
else if (i % 2 == 0 && row % 2 == 0)
{
_boardSq[place] = Square(row, j, true, '#', place);
place++;
}
else if (i % 2 == 0 && row % 2 == 1)
{
_boardSq[place] =Square(row, j, true, '#', place);
place++;
}
else if (i % 2 == 1 && row % 2 == 1)
{
_boardSq[place] =Square(row, j, false, '#', place);
place++;
}
}
row--;
}
for (i = 1; i <= 8; i++)
{
if (i % 2 == 1)
{
_boardSq[place] = Square(2, i, false, 'P', place);
place++;
}
else
{
_boardSq[place] = Square(2, i, true, 'P', place);
place++;
}
}
_boardSq[56] = Square(1, 1, true, 'R', 56);
_boardSq[57] = Square(1, 2, false, 'N', 57);
_boardSq[58] = Square(1, 3, true, 'B', 58);
_boardSq[59] = Square(1, 4, false, 'q', 59);
_boardSq[60] = Square(1, 5, true, 'K', 60);
_boardSq[61] = Square(1, 6, false, 'B', 61);
_boardSq[62] = Square(1, 7, true, 'N', 62);
_boardSq[63] = Square(1, 8, false, 'R', 63);
string place_3 = "a7";
string place_4 = "a2";
for (i = 8; i < 16; i++)
{
place_3[0] = place_3[0] + i - 8;
_bFigure[i]= Pawn(0, 'p', place_3, _boardSq[i]);
}
_bFigure[0] = Rook(0, 'r',"a8", _boardSq[0]);
_bFigure[7] = Rook(0, 'r',"h8", _boardSq[7]);
_bFigure[1] = Knight(0, 'n', "b8", _boardSq[1]);
_bFigure[6] = Knight(0, 'n', "g8", _boardSq[6]);
_bFigure[2] = Bishop(0, 'b', "c8", _boardSq[2]);
_bFigure[5] = Bishop(0, 'b', "f8", _boardSq[5]);
_bFigure[3] = Queen(0, 'q', "d8", _boardSq[3]);
_bFigure[4] = King(0, 'k', "e8", _boardSq[4]);
for (i = 48; i < 56; i++)
{
place_4 = place_4[0] + i - 48;
_wFigure[i-40] = Pawn(1, 'P', place_4, _boardSq[i]);
}
_wFigure[0] = Rook(1, 'R', "a1", _boardSq[0]);
_wFigure[7] = Rook(1, 'R', "h1", _boardSq[7]);
_wFigure[1] = Knight(1, 'N', "b1", _boardSq[1]);
_wFigure[6] = Knight(1, 'N', "g1", _boardSq[6]);
_wFigure[2] = Bishop(1, 'B', "c1", _boardSq[2]);
_wFigure[5] = Bishop(1, 'B', "f1", _boardSq[5]);
_wFigure[4] = Queen(1, 'Q', "e1", _boardSq[4]);
_wFigure[3] = King(1, 'K', "d1", _boardSq[3]);
}
//true - white
//false - black
Board::~Board()
{
delete[] _bFigure;
delete[] _wFigure;
}
string Board::board_now()
{
return _board;
}
Square* Board::board_now_sq()
{
return _boardSq;
}
void Board::change_board(string change)
{
int i = 0;
int place1 = 0;
int place2 = 0;
char temp;
for (i = 0; i < 64; i++)
{
if (conventor_row( _boardSq[i].get_row()) == change[1] && conventor_col( _boardSq[i].get_column()) == change[0])
{
place1 = _boardSq[i].get_place();
}
if (conventor_row( _boardSq[i].get_row()) == change[3] && conventor_col( _boardSq[i].get_column()) == change[2])
{
place2 = _boardSq[i].get_place();
}
}
temp = _board[place1];
_board[place1] = _board[place2];
_board[place2] = temp;
}
void Board::change_board_sq(string change)
{
int i = 0;
int place1;
int place2;
for (i = 0; i < 64; i++)
{
if (_boardSq[i].get_row() == change[1] && _boardSq[i].get_column() == change[0])
{
place1 = i;
}
if (_boardSq[i].get_row() == change[3] && _boardSq[i].get_column() == change[2])
{
place2 = i;
}
}
Square temp(_boardSq[place1]);
_boardSq[place1].set_taken(_boardSq[place2].get_taken());
_boardSq[place2].set_taken(temp.get_taken());
}
char conventor_row(int num)
{
switch (num)
{
case 1:
return '1';
break;
case 2:
return '2';
break;
case 3:
return '3';
break;
case 4:
return '4';
break;
case 5:
return '5';
break;
case 6:
return '6';
break;
case 7:
return '7';
break;
case 8:
return '8';
break;
}
}
char conventor_col(int num)
{
switch (num)
{
case 1:
return 'a';
break;
case 2:
return 'b';
break;
case 3:
return 'c';
break;
case 4:
return 'd';
break;
case 5:
return 'e';
break;
case 6:
return 'f';
break;
case 7:
return 'g';
break;
case 8:
return 'h';
break;
}
}
void Board::operator=(const Board& temp)
{
this->_board = temp._board;
this->_boardSq = temp._boardSq;
this->_wFigure = temp._wFigure;
this->_bFigure = temp._bFigure;
}
int Board::checker(string change,int Pcolor)
{
int i;
int checkF;
for (i = 0; i < 64; i++)
{
if (change[0] == conventor_col(_boardSq[i].get_column()) && change[1] == conventor_row(_boardSq[i].get_row())) //error 2
{
if (_boardSq[i].get_taken() == '#')
{
return '2';
}
}
if (change[2] == conventor_col(_boardSq[i].get_column()) && change[3] == conventor_row(_boardSq[i].get_row())) // error 3
{
if (Pcolor % 2 == 0) // white player
{
for (int j = 0; j < 16; j++)
{
if (_wFigure[j].get_place_sq().get_place() == _boardSq[i].get_place())
{
return '3';
}
}
}
else // black player
{
for (int j = 0; j < 16; j++)
{
if (_bFigure[j].get_place_sq().get_place() == _boardSq[i].get_place())
{
return '3';
}
}
}
}
}
if (Pcolor % 2 == 0) // error 5-7 +1
{
for (i = 0; i < 16; i++)
{
if ((_wFigure[i].get_place_str()[0] == change[0]) && (_wFigure[i].get_place_str()[1] == change[1]))
{
checkF= _wFigure[i].ligal_move(change, *this);
}
}
}
else
{
for (i = 0; i < 16; i++)
{
if ((_bFigure[i].get_place_str()[0] == change[0]) && (_bFigure[i].get_place_str()[1] == change[1]))
{
checkF = _bFigure[i].ligal_move(change, *this);
}
}
}
if (checkF != 0)
{
return checkF;
}
// error 4
Board temp;
temp = *this;
temp.change_board(change);
temp.change_board_sq(change);
if (Pcolor % 2 == 1)
{
for (i = 0; i < 16; i++)
{
if ((_wFigure[i].is_check(change,temp)==true && _wFigure[i].get_name()!='K'))
{
return '4';
}
}
}
else
{
for (i = 0; i < 16; i++)
{
if ((_bFigure[i].is_check(change, temp) == true && _bFigure[i].get_name() != 'k'))
{
return '4';
}
}
}
return '0';
}
他的标题(board.h):
#pragma once
#include <string>
#include "Square.h"
#include "Figure.h"
using namespace std;
class Figure;
class Board
{
public:
Board();
~Board();
string board_now();
Square* board_now_sq();
void change_board(string change);
void change_board_sq(string change);
void operator=(const Board& temp);
int checker(string change, int Pcolor);
private:
string _board;
Square* _boardSq;
Figure* _bFigure;
Figure* _wFigure ;
};
figure.cpp:
#include "Figure.h"
#include "Square.h"
#include <string>
using namespace std;
Figure::Figure()
{
}
int Figure::ligal_move(string move, Board B)
{
return 0;
}
Figure::Figure(int color, char name, string place_str, Square place_sq)
{
this->color = color;
this->name = name;
this->place_str=place_str;
this->set_place_sq(place_sq);
}
Figure::~Figure()
{
}
bool Figure::is_check(string move, Board B)
{
return true;
}
string Figure::get_place_str()
{
return place_str;
}
Square Figure::get_place_sq()
{
return place_sq;
}
void Figure::set_place_str(string place)
{
place_str = place;
}
void Figure::set_place_sq(Square place)
{
place_sq = place;
}
void Figure::set_name(char name_c)
{
name = name_c;
}
void Figure::set_color(int color_c)
{
color = color_c;
}
char Figure::get_name()
{
return name;
}
bool Figure::get_color()
{
return color;
}
the figure.h:
#pragma once
#include <string>
#include "Square.h"
#include "Board.h"
using namespace std;
class Board;
class Figure
{
public:
Figure();
Figure(int color, char name, string place_str, Square place_sq);
~Figure();
virtual int ligal_move(string move, Board B);
virtual string get_place_str();
virtual Square get_place_sq();
virtual void set_place_str(string place);
virtual void set_place_sq(Square place);
virtual void set_color(int color_c);
virtual void set_name(char name_c);
virtual bool get_color();
virtual char get_name();
virtual bool is_check(string move, Board B);
protected:
int color;
char name;
string place_str;
Square place_sq;
};
这里有一个数字(rook.cpp):
#include "Figure.h"
#include "Square.h"
#include "Rook.h"
#include "Board.h"
#include "Horizontal.h"
#include "Vertical.h"
#include <string>
using namespace std;
Rook::Rook(int color, char name, string place_str, Square place_sq) : Figure(color,name,place_str,place_sq)
{
}
Rook::~Rook()
{
}
int Rook::ligal_move(string move,Board B)
{
Horizontal Hmov(move, B);
Vertical Vmov(move, B);
if (move[0] == move[2] && move[1] == move[3])
{
return 7;
}
if (move[0]<'a' || move[0]>'h' || move[2]<'a' || move[2]>'h' || move[1]<'1' || move[1]>'8' || move[3]<'1' || move[3]>'8')
{
return 5;
}
if (Hmov.is_horizontal() == false && Vmov.is_Vertical() == false)
{
return 6;
}
else
{
if (Hmov.is_horizontal() == true && Hmov.is_middle1() != '#')
{
return 6;
}
else if (Vmov.is_Vertical() == true && Vmov.is_middle2() != '#')
{
return 6;
}
}
if (is_check(move,B) == true)
{
return 1;
}
return 0;
}
bool Rook::is_check(string move,Board B)
{
move[1] = move[3];
move[1] = 'a';
move[3] = 'h';
Horizontal Hmov(move, B);
move[0] = move[2];
move[1] = '8';
move[3] = '1';
Vertical Vmov(move, B);
if (color == 0)
{
if (Vmov.is_middle2() == 'k' || Hmov.is_middle1() == 'k')
{
return true;
}
else
{
return false;
}
}
else
{
if (Vmov.is_middle2() == 'K' || Hmov.is_middle1() == 'K')
{
return true;
}
else
{
return false;
}
}
}
和他的头文件(rook.h):
#pragma once
#include <string>
#include "Figure.h"
#include "Square.h"
#include "Board.h"
using namespace std;
class Rook : public Figure
{
public:
Rook(int color, char name, string place_str, Square place_sq);
~Rook();
int ligal_move(string move, Board B);
bool is_check(string move,Board B);
};
我真的不知道为什么,但他们都在board.cpp
的构建器板中获得了初始化的NULL值!
提前致谢!如果我得到了很好的解决方案,我将非常感激!
答案 0 :(得分:1)
构造函数的第一行:
Figure* _bFigure = new Figure[16];
Figure* _wFigure = new Figure[16];
实际上是创建局部变量,而不是初始化成员变量。也许你的意思是:
_bFigure = new Figure[16];
_wFigure = new Figure[16];
或者你可以完全避免使用动态数组 - 只需在标题中定义成员数组:
Figure _bFigure[16];
Figure _wFigure[16];