我正试图修复这个bug一小时。据说“板”元素有多种定义。这是代码:
file piece_base.h
#ifndef PIECE_BASE_H
#define PIECE_BASE_H
#include <string>
class piece_base
{
public:
piece_base(std::string);
virtual ~piece_base();
void position(int,int);
void kill (piece_base &target) const;
void getKill();
protected:
std::string name;
bool alive;
int m_x;
int m_y;
private:
};
piece_base *board[8][8];
#endif // piece_base_H
file piece_base.cpp
#include "../include/piece_base.h"
piece_base::piece_base(std::string x)
{
piece_base::name = x;
piece_base::m_x = 0;
piece_base::m_y = 0;
}
piece_base::~piece_base()
{
//dtor
}
void piece_base::position(int x, int y)
{
piece_base::m_x = x;
piece_base::m_y = y;
}
void piece_base::getKill()
{
alive = false;
}
void piece_base::kill(piece_base &target) const
{
target.getKill();
}
file piece_pawn.h
#ifndef PIECE_PAWN_H
#define PIECE_PAWN_H
#include "piece_base.h"
#include <string>
class piece_pawn : public piece_base
{
public:
piece_pawn();
virtual ~piece_pawn();
void movePiece(int, int);
protected:
bool movementRules(int, int);
bool xOkay(int);
bool yOkay(int);
private:
};
#endif // PIECE_PAWN_H
file piece_pawn.cpp
#include "piece_pawn.h"
piece_pawn::piece_pawn() : piece_base("Pawn")
{
}
piece_pawn::~piece_pawn()
{
//dtor
}
bool piece_pawn::movementRules(int x, int y)
{
bool z;
if (xOkay(x) && yOkay(y) == 1)
z = true;
else if (xOkay(x) && yOkay(y) == 2)
{
// piece_pawn::kill()
}
return z;
}
bool piece_pawn::xOkay(int x)
{
int z;
if (x == piece_pawn::m_x + 1)
z = true;
return z;
}
bool piece_pawn::yOkay(int y)
{
if (y == piece_pawn::m_y)
return 1;
else if (y == piece_pawn::m_y + 1 || y == piece_pawn::m_y - 1)
return 2;
else
return 0;
}
void piece_pawn::movePiece(int x, int y)
{
if (piece_pawn::movementRules(x, y))
{
board[piece_pawn::m_x][piece_pawn::m_y] = NULL;
piece_pawn::m_x = x;
piece_pawn::m_y = y;
board[piece_pawn::m_x][piece_pawn::m_y] = this;
}
}
最后:P文件main.cpp
#include "../include/piece_pawn.h"
#include <iostream>
using namespace std;
int main()
{
piece_pawn pawn1;
//int boardConverted;
pawn1.movePiece(1,0);
for (int x = 0; x < 8; x = x + 1)
{
for (int y = 0; y < 8; y = y + 1)
{
cout << board[x][y] << "|";
}
cout << endl;
}
return 0;
}
我真的不知道我的问题是否过于复杂,但我希望有人会回答:P