c++ 菜鸟级 运行错误 段错误 Segmentation fault

2025-01-05 17:17:03
推荐回答(3个)
回答1:

Board的成员变量需要初始化, 再就是CheckerPiece的构造函数在Board类进行构造的时候好像是涉及到嵌套类的构造问题吧, 把CheckerPiece建立一个无参构造函数和一个初始化函数, 这样可以进行初始化

还有就是内部的一些矩阵元素赋值的时候

#include 

#include 

#include 

#include 

using namespace std;

// 定义了类Checkerpiece(用来检查块, 相当于裁判?)

class Checkerpiece

{

    

    char color; // to distinct different players

    int x;

    int y ;  //:  the coordinate of the piece .

    bool selected; // : whether the piece is selected to be moved 

    bool isKing; //whether the piece reach the end of the board

    //bool hasEnemy; // : whether the piece has a enemy piece around it

public:

    //Board livespace;

    Checkerpiece(){};

    Checkerpiece(char color,int x,int y);   // Constructor

    void InitCheckerpiece(char color,int x,int y);

    int getX();

    int getY();

    char getColor();

    bool ishimking();

    void select();

    void unselect();

    bool crown();

    //bool movable();

    //bool move(int nextX,int nextY,Board& checkerboard);

    //bool simplymove(int nextX,int nextY,Board& checkerboard);

    //bool jump(int nextX,int nextY,Board& checkerboard);

    //bool isenemy(Checkerpiece cp);

    

};

// CheckerPiece类的成员函数的实现

Checkerpiece::Checkerpiece(char color0,int x0,int y0)

{

    color = color0;

    x = x0;

    y = y0;

    

}

void Checkerpiece::InitCheckerpiece(char color0,int x0,int y0)

{

    color = color0;

    x = x0;

    y = y0;

}

int Checkerpiece::getX()

{

    return x;

}

int Checkerpiece::getY()

{

    return y;

}

char Checkerpiece::getColor()

{

    return color;

}

bool Checkerpiece::ishimking()

{

    return isKing;

}

void Checkerpiece::select()

{

    selected = true;

}

void Checkerpiece::unselect()

{

    selected = false;

}

bool Checkerpiece::crown()

{

    return isKing = true;

}

// 定义类Player(玩家信息)

class Player

{

    //bool red_turn;

    char color;

    int noOfPieces;

    int noOf_movable;

    //Checkerpiece *pieces[12];

public:

    

    Player(char color);

    bool lose();

    char playercolor();

};

// 成员函数的实现

Player::Player(char color0)

{

    color = color0;

    noOfPieces = 12;

    noOf_movable = 12;

    

}

bool Player::lose()

{

    return noOfPieces*noOf_movable ==0; // 没有棋子或所有棋子不可动则表示输了

}

char Player::playercolor()

{

    return color;

}

// 定义了Board类(棋盘)

class Board

{

    Checkerpiece  matrix[8][8];

public:

    Board();

    void disPlayMatrix();

    char getmatrix(int x,int y);

    bool getKingship(int x,int y);

    bool setKingship(int x,int y);

    void setPosition(int crrentX,int crrentY,int nextX,int nextY);

    void capture(int x,int y);

    bool move(Player p);

    int  readX();

    int  readY();

    bool simplymove(int currentX,int currentY,int nextX,int nextY);

    bool jump(int currentX,int currentY,int nextX,int nextY);

};

// 初始化Board的成员

Board::Board()

{

    for(int i =0;i<8;i++)

        for(int j =0;j<8;j++)

            matrix [i][j].InitCheckerpiece(' ',i,j); 

        for(int k =0;k<8;k+=2)

        {

            matrix [0][k]=Checkerpiece('r',0,k); 

            matrix [1][k+1]=Checkerpiece('r',1,k); 

            matrix [2][k]=Checkerpiece('r',2,k); 

            matrix [5][k+1]=Checkerpiece('b',5,k); 

            matrix [6][k]=Checkerpiece('b',6,k); 

            matrix [7][k+1]=Checkerpiece('b',7,k); 

        }

        

}

void Board::disPlayMatrix()

{

    //Checkerpiece *matrix4print [8][8];

    cout<<"  a b c d e f g h"<

    for(int i =0;i<8;i++)

    {

        cout<

        for(int j =0;j<8;j++)

        {

            //*matrix4print [i][j]=*matrix[i][j];

            //cout<< *matrix4print[i][j].getColor()<<'|';

            Checkerpiece cp =matrix [i][j];

            cout<< cp.getColor()<<'|';

        }

        cout<

    }

}

char Board::getmatrix(int x,int y)

{

    Checkerpiece cp = matrix [x][y];

    return cp.getColor();

}

bool Board::getKingship(int x,int y)

{

    Checkerpiece cp = matrix [x][y];

    return cp.ishimking();

}

bool Board::setKingship(int x,int y)

{

    Checkerpiece cp = matrix [x][y];

    return cp.crown();

}

void Board::setPosition(int currentX,int currentY,int nextX,int nextY)

{

    //Checkerpiece oldP = 

    char color = getmatrix(currentX,currentY);

    matrix[currentX][currentY].InitCheckerpiece(' ',currentX,currentY );

    matrix[nextX][nextY].InitCheckerpiece(color,currentX,currentY );

}

void Board::capture(int x,int y)

{

    matrix[x][y].InitCheckerpiece(' ',x,y);

}

bool Board::simplymove(int currentX,int currentY,int nextX,int nextY)

{

    bool Kingship = getKingship(currentX,currentY);

    char pcolor = getmatrix(currentX,currentY);

    if((Kingship==false)&&((nextXcurrentY&&pcolor =='b')))

        cout<<"Not king! You are not allowed to move backfarward!"<

    else

    {

        setPosition(currentX,currentY,nextX,nextY);

        

        if((nextX ==0&&pcolor =='b')||(nextX ==7&&pcolor=='r'))

            setKingship(nextX,nextY);

    }

    return(getmatrix(currentX,currentY)!=pcolor);

}

bool Board::jump(int currentX,int currentY,int nextX,int nextY)

{

    bool Kingship = getKingship(currentX,currentY);

    char pcolor = getmatrix(currentX,currentY);

    int midX = (currentX+nextY)/2;

    int midY = (currentY+nextY)/2;

    if((Kingship==false)&&((nextXcurrentY&&pcolor =='b')))

        cout<<"Not king! You are not allowed to move backfarward!"<

    else

    {

        setPosition(currentX,currentY,nextX,nextY);

        capture(midX,midY);

        

        if((nextX ==0&&pcolor =='b')||(nextX ==7&&pcolor=='r'))

            setKingship(nextX,nextY);

    }

    return(getmatrix(currentX,currentY)!=pcolor);

}

int Board::readX()

{

    int row;

    cout<<"Enter a number from 1-8 to choose row  :";

    cin>>row;

    return row-1;

}

int Board::readY()

{

    char letter;

    int column;

    cout<<"Enter a letter from a-h to choose column: ";

    cin>>letter;

    switch (letter)

    {

    case 'a':   column = 0;

        break;

    case 'b':   column = 1;

        break;

    case 'c':   column = 2;

        break;

    case 'd':   column = 3;

        break;

    case 'e':   column = 4;

        break;

    case 'f':   column = 5;

        break;

    case 'g':   column = 6;

        break;

    case 'h':   column = 7;

        break;

    default :   column = 8;

    }

    return column;

}

bool Board::move(Player p)

{

    disPlayMatrix();

    char playerid = p.playercolor();

    cout<

    int currentX = readX();

    int currentY = readY();

    if(getmatrix(currentX,currentY)!=playerid)

        cout<<"No expected piece at the position you entered!"<

    else

    {

        cout<<"Enter the position you'd like to move to: "<

        int nextX =readX();

        int nextY =readY();

        if(nextY==8||nextX<0||nextX>7)

        {

            cout<<"Don't be daughty, you can't move out of the board,dead or alive."<

        }

        else if(getmatrix(nextX,nextY) !=' ')

            cout<<"The position is occupid!"<

        else

        { 

            if(abs(currentX-nextX)==1&&abs(currentY-nextY)==1)

            {

                return simplymove(currentX,currentY,nextX,nextY);

            }

            else if(abs(currentX-nextX)==2&&abs(currentY-nextY)==2)

            {

                return jump(currentX,currentY,nextX,nextY);

            }

            else

            {

                cout<<"Not a valid move!"<

            }

        }

    }

    return false;

}

int main()

{

    Board checkergame;

    Player red('r');

    Player black('b');

    checkergame.move(red);

    bool red_turn = true;

    bool turntaking;

    while(!red.lose()&&!black.lose())

    {

        if(red_turn==true)

        {

            turntaking = checkergame.move(red);

            red_turn = red_turn&&turntaking;

        }

        else

        {

            turntaking = checkergame.move(black);

            red_turn = turntaking;

        }

    }

   

    return 0;

}

回答2:

Board::Board()里对matrix的不正确初始化导致写内存错误。
*matrix[i][j] = Checkerpiece(...);
这行代码构造了一个Checkerpiece对象,并将matrix[i][j]指针指向的对象赋值为刚构造出来的对象(matrix是Checkerpiece指针的二维数组)。但matrix[i][j]是个未初始化的Checkerpiece指针,没有指向任何实际的Checkerpiece对象(可以认为内容是垃圾,可能指向地址空间的任何地方),当它指向的地址刚好不可写时,上述赋值操作就导致写内存错误。
可以简单地改成这样:
matrix[i][j] = new Checkerpiece(...);
这是把new出来的Checkerpiece对象的指针复制到matrix[i][j]。最后别忘了在析构函数里delete所有元素。

回答3:

直接定义成数组成员就行了,不要用指针了,反正你也是用到8*8的大小,在游戏中赋值什么的,直接操作就好。

Checkerpiece matrix[8][8];

1,
把类似:*matrix [i][j] = Checkerpiece(' ',i,j);
改成matrix [i][j] = Checkerpiece(' ',i,j);
2,类型的函数体内代码
char Board::getmatrix(int x,int y){
Checkerpiece cp = *matrix [x][y];
return cp.getColor();
}
改成
char Board::getmatrix(int x,int y){
return matrix [x][y].getColor();
}

//------------------------------------------------------------------------------------------------------
#include
#include
#include
#include
using namespace std;
class Checkerpiece{

char color; // to distinct different players
int x;
int y ; //: the coordinate of the piece .
bool selected; // : whether the piece is selected to be moved
bool isKing; //whether the piece reach the end of the board
//bool hasEnemy; // : whether the piece has a enemy piece around it
public:
//Board livespace;
Checkerpiece():color(' '),x(0),y(0),selected(false),isKing(false){};
Checkerpiece(char color,int x,int y);
int getX();
int getY();
char getColor();
bool ishimking();
void select();
void unselect();
bool crown();
//bool movable();
//bool move(int nextX,int nextY,Board& checkerboard);
//bool simplymove(int nextX,int nextY,Board& checkerboard);
//bool jump(int nextX,int nextY,Board& checkerboard);
//bool isenemy(Checkerpiece cp);

};

Checkerpiece::Checkerpiece(char color0,int x0,int y0){
color = color0;
x = x0;
y = y0;

}
int Checkerpiece::getX(){
return x;
}
int Checkerpiece::getY(){
return y;
}
char Checkerpiece::getColor(){
return color;
}
bool Checkerpiece::ishimking(){
return isKing;
}
void Checkerpiece::select(){
selected = true;
}
void Checkerpiece::unselect(){
selected = false;
}
bool Checkerpiece::crown(){
return isKing = true;
}

class Player{
//bool red_turn;
char color;
int noOfPieces;
int noOf_movable;
//Checkerpiece *pieces[12];
public:

Player(char color);
bool lose();
char playercolor();
};
Player::Player(char color0){
color = color0;
noOfPieces = 12;
noOf_movable = 12;

}
bool Player::lose(){
return noOfPieces*noOf_movable ==0;
}
char Player::playercolor(){
return color;
}

class Board{
Checkerpiece matrix[8][8];
public:
Board();
void disPlayMatrix();
char getmatrix(int x,int y);
bool getKingship(int x,int y);
bool setKingship(int x,int y);
void setPosition(int crrentX,int crrentY,int nextX,int nextY);
void capture(int x,int y);
bool move(Player p);
int readX();
int readY();
bool simplymove(int currentX,int currentY,int nextX,int nextY);
bool jump(int currentX,int currentY,int nextX,int nextY);
};
Board::Board(){
for(int i =0;i<8;i++)
for(int j =0;j<8;j++)
matrix [i][j] = Checkerpiece(' ',i,j);
for(int k =0;k<8;k+=2){
matrix [0][k]=Checkerpiece('r',0,k);
matrix [1][k+1]=Checkerpiece('r',1,k);
matrix [2][k]=Checkerpiece('r',2,k);
matrix [5][k+1]=Checkerpiece('b',5,k);
matrix [6][k]=Checkerpiece('b',6,k);
matrix [7][k+1]=Checkerpiece('b',7,k);
}

}

void Board::disPlayMatrix(){
//Checkerpiece *matrix4print [8][8];
cout<<" a b c d e f g h"< for(int i =0;i<8;i++){
cout< for(int j =0;j<8;j++){
//*matrix4print [i][j]=*matrix[i][j];
//cout<< *matrix4print[i][j].getColor()<<'|';
Checkerpiece cp =matrix [i][j];
cout<< cp.getColor()<<'|';
}
cout< }
}
char Board::getmatrix(int x,int y){
Checkerpiece cp = matrix [x][y];
return cp.getColor();
}
bool Board::getKingship(int x,int y){
Checkerpiece cp = matrix [x][y];
return cp.ishimking();
}
bool Board::setKingship(int x,int y){
Checkerpiece cp = matrix [x][y];
return cp.crown();
}
void Board::setPosition(int currentX,int currentY,int nextX,int nextY){
//Checkerpiece oldP =
char color = getmatrix(currentX,currentY);
matrix[currentX][currentY] = Checkerpiece(' ',currentX,currentY );
matrix[nextX][nextY] = Checkerpiece(color,currentX,currentY );
}
void Board::capture(int x,int y){
matrix[x][y] = Checkerpiece(' ',x,y);
}
bool Board::simplymove(int currentX,int currentY,int nextX,int nextY){
bool Kingship = getKingship(currentX,currentY);
char pcolor = getmatrix(currentX,currentY);
if((Kingship==false)&&((nextXcurrentY&&pcolor =='b')))
cout<<"Not king! You are not allowed to move backfarward!"< else{
setPosition(currentX,currentY,nextX,nextY);

if((nextX ==0&&pcolor =='b')||(nextX ==7&&pcolor=='r'))
setKingship(nextX,nextY);
}
return(getmatrix(currentX,currentY)!=pcolor);
}
bool Board::jump(int currentX,int currentY,int nextX,int nextY){
bool Kingship = getKingship(currentX,currentY);
char pcolor = getmatrix(currentX,currentY);
int midX = (currentX+nextY)/2;
int midY = (currentY+nextY)/2;
if((Kingship==false)&&((nextXcurrentY&&pcolor =='b')))
cout<<"Not king! You are not allowed to move backfarward!"< else{
setPosition(currentX,currentY,nextX,nextY);
capture(midX,midY);

if((nextX ==0&&pcolor =='b')||(nextX ==7&&pcolor=='r'))
setKingship(nextX,nextY);
}
return(getmatrix(currentX,currentY)!=pcolor);
}
int Board::readX(){
int row;
cout<<"Enter a number from 1-8 to choose row :";
cin>>row;
return row-1;
}
int Board::readY(){
char letter;
int column;
cout<<"Enter a letter from a-h to choose column: ";
cin>>letter;
switch (letter){
case 'a': column = 0;
break;
case 'b': column = 1;
break;
case 'c': column = 2;
break;
case 'd': column = 3;
break;
case 'e': column = 4;
break;
case 'f': column = 5;
break;
case 'g': column = 6;
break;
case 'h': column = 7;
break;
default : column = 8;
}
return column;
}
bool Board::move(Player p){
disPlayMatrix();
char playerid = p.playercolor();
cout< int currentX = readX();
int currentY = readY();
if(getmatrix(currentX,currentY)!=playerid)
cout<<"No expected piece at the position you entered!"< else{
cout<<"Enter the position you'd like to move to: "< int nextX =readX();
int nextY =readY();
if(nextY==8||nextX<0||nextX>7){
cout<<"Don't be daughty, you can't move out of the board,dead or alive."< }
else if(getmatrix(nextX,nextY) !=' ')
cout<<"The position is occupid!"< else{
if(abs(currentX-nextX)==1&&abs(currentY-nextY)==1){
return simplymove(currentX,currentY,nextX,nextY);
}
else if(abs(currentX-nextX)==2&&abs(currentY-nextY)==2){
return jump(currentX,currentY,nextX,nextY);
}
else{
cout<<"Not a valid move!"< }
}
}
return false;
}
int main(){
Board checkergame;
Player red('r');
Player black('b');
checkergame.move(red);
bool red_turn = true;
bool turntaking;
while(!red.lose()&&!black.lose()){
if(red_turn==true){
turntaking = checkergame.move(red);
red_turn = red_turn&&turntaking;
}
else{
turntaking = checkergame.move(black);
red_turn = turntaking;
}
return 0;
}
}