栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++象棋

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++象棋

#include 
#include 
#include 
using namespace std;//存储结构:chess类是基类,派生类是各种棋子,在chessboard类中用chess的指针调用各个棋子
class chessboard;
class chess{
private:
int id;//等级
public:
chess(int i):id(i){}
int get(){return id;}
virtual bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy)=0;
virtual ~chess(){};//虚析构
};
class chessboard{
private:
chess *c[10][9];
char chessword[15][4]={"帅","相","炮","士","車","馬","兵","","卒","马","车","仕","砲","象","将"};//名字
public:
chessboard(){memset(c, NULL, sizeof(c));};//把指针初始化为零指针
void init();
chess* get(int x,int y){return c[x][y];}
int getid(int x,int y){ if(c[x][y]!=NULL) return c[x][y]->get();return 0;}
void show();
void play();
bool move(int startx,int starty,int aimx,int aimy);
~chessboard();//析构函数
static bool end;//判断结束
static int player;
};
bool chessboard::end=true;
int chessboard::player=-1;
bool chessboard::move(int startx,int starty,int aimx,int aimy){
if(startx>=0&&startx<10&&starty>=0&&starty<9//初步判断传入的点是否符合规则
   &&aimx>=0&&aimx<10&&aimy>=0&&aimy<9
   &&getid(startx,starty)&&getid(startx,starty)*player>0
   &&c[startx][starty]->judge_move(*this,startx,starty,aimx,aimy)){
    if(c[aimx][aimy]!=NULL) delete c[aimx][aimy];//吃子
    c[aimx][aimy]=c[startx][starty];
    c[startx][starty]=NULL;
player*=-1;
return true;
}
cout<<"走法错误,不符合规则"<0)||(startx/5==1&&sid<0)) return true;
        return false;
    }
    return false;
}
};
class general : public chess{//帅(将)的实现
public:
general(int c) : chess((c==0?-7:7)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
    int tempx=aimx-startx,tempy=aimy-starty;
    int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
    if(sid*aid<=0&&tempy*tempy+tempx*tempx==1&&aimx%7>=0&&aimx%7<=2&&aimy>=3&&aimy<=5)
        return true;
    return false;
}
~general(){chessboard::end=false;}
};
class elephant : public chess{//象(相)的实现
public:
elephant(int c) : chess((c==0?-6:6)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
    int tempx=aimx-startx,tempy=aimy-starty;
    int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
    if(sid*aid<=0&&tempy*tempy+tempx*tempx==8&&startx/5==aimx/5&&!cb.get(startx+tempx/2,starty+tempy/2))
        return true;
    return false;
}
};
class cannon : public chess{//炮的实现
public:
cannon(int c) : chess((c==0?-5:5)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
    int tempx=aimx-startx,tempy=aimy-starty;
    int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
    if(sid*aid<=0&&!(tempx&&tempy)&&(tempx+tempy)){
        int tot=0;
        if(tempx!=0){
            int sign=tempx>0?1:-1;
            for(int i=1;i0?1:-1;
            for(int i=1;i=0&&aimx%7<=2&&aimy>=3&&aimy<=5)
        return true;
    return false;
}
};
class rook : public chess{//车的实现
public:
rook(int c) : chess((c==0?-3:3)){}
bool judge_move(chessboard &cb,int startx,int starty,int aimx,int aimy){
    int tempx=aimx-startx,tempy=aimy-starty;
    int sid=cb.getid(startx, starty),aid=cb.getid(aimx, aimy);
    if(sid*aid<=0&&!(tempx&&tempy)&&(tempx+tempy)){
        if(tempx!=0){
            int sign=tempx>0?1:-1;
            for(int i=1;i0?1:-1;
            for(int i=1;iget()+7];
        else if((i==1&&j==4)||(i==8&&j==4))
            cout<<"米";
        else
            cout<<"十";
    }
    cout<init();
this->show();
do{
   int startx,starty,aimx,aimy;
    int sid,aid;
    do{
        sid=aid=0;
        cout<<"请输入起始棋子位置与目标位置的坐标:"<>startx>>starty>>aimx>>aimy;
    }while(!this->move(startx,starty,aimx,aimy));
    this->show();
}while(chessboard::end);
cout<<"结束,赢家是Player"<<(chessboard::player==1?1:2)< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/296389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号