版本比较
【P5380】鸭棋
正文
增加 39 行删除 39 行
11 行未改动1213141516161718192080 行未改动10110210310410510510610710810939 行未改动1491501511521531531541551561571581591601611621631631641651661661671681681691701711724 行未改动17717817918018118118218318418510 行未改动1961971981992002002012022032042 行未改动2072082092102112112122132142156 行未改动2222232242252262262272282292302312322332342342352362362372382392402 行未改动24324424524624724724824925025114 行未改动26626726826927027027127227327419 行未改动2942952962972982982993003013023033043053063073073083093093103113123132 行未改动3163173183193203203213223233242 行未改动32732832933033133133233333433585 行未改动42142242342442542542642742842921 行未改动45145245345445545545645745845917 行未改动4774784794804814814824834844844854864864874884894904 行未改动49549649749849949950050150250310 行未改动5145155165175185185195205215222 行未改动5255265275285295295305315325336 行未改动5405415425435445445455465475485495505515525525535545545555565575582 行未改动56156256356456556556656756856914 行未改动58458558658758858858959059159219 行未改动6126136146156166166176186196206216226236246256256266276276286296306312 行未改动6346356366376386386396406416422 行未改动64564664764864964965065165265372 行未改动7267277287297307307317327337342 行未改动7377387397407417417427437447459 行未改动11 行未改动class NewClass{ //NewClass 是类名public: //表示任何对象都可以使用下面的部分(类似的,还有 private 和 protected,但是我们这道题不使用) NewClass(int _myValue=0){ //构造函数——在你创造这个类的时候会调用 myValue=_myValue; } } int myValue; //若干个成员变量 void print(){ //若干个成员函数 cout<<myValue<<endl; }80 行未改动 id=_id;camp=_camp;name=_name;x=_x;y=_y; } virtual bool canGo(int tx,int ty,Board &nowBoard){return 0;} //虚函数,判断当前棋子是否能够从 (x,y) 走到 (tx,ty)。由于该父类不表示任何棋子,所以直接返回 0。由于我们需要获取棋盘中其它棋子的状态,所以要把当前的整个棋盘作为参数传入函数中 virtual bool canKill(Board &nowBoard){return 0;} //虚函数,判断当前棋子是否将对面的军 ~Chess(){} ~Chess(){}};```
这时问题出现了,`Board` 调用了 `Chess` 类,`Chess` 又调用了 `Board` 类,这怎么办呢?39 行未改动public: Car(int _camp=0,int _x=0,int _y=0){ init(1,_camp,"car",_x,_y); } bool canGo(int tx,int ty,Board &nowBoard){ bool canGo(int tx,int ty,Board &nowBoard){ if (x==tx && y==ty) return false; if (nowBoard.chess[tx][ty]->camp==camp) return false; if (x!=tx && y==ty){ for (int i=min(x,tx)+1;i<=max(x,tx)-1;++i){ if (nowBoard.chess[i][y]->id!=-1) return false; } }else if (x==tx && y!=ty){ for (int i=min(y,ty)+1;i<=max(y,ty)-1;++i){ if (nowBoard.chess[x][i]->id!=-1) return false; } } }else return false; return true; } } bool canKill(Board &nowBoard){ int tx,ty; int tx,ty; tx=x+1;ty=y; while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) tx++; if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true; tx=x-1;ty=y;4 行未改动 if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true; tx=x;ty=y-1; while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) ty--; if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true; return false; return false; } ~Car(){}};class Horse:public Chess{ //马10 行未改动 if (tx==x+sx && ty==y+sy*2 && nowBoard.chess[x][y+sy]->id==-1) return true; } } return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;sx+=2){ for (int sy=-1;sy<=1;sy+=2){ if (xRange(x+sx*2) && yRange(y+sy) && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*2][y+sy]->id==0 && nowBoard.chess[x+sx*2][y+sy]->camp!=camp) return true;2 行未改动 } return false; } ~Horse(){}}; };class Elephant:public Chess{ //象public: Elephant(int _camp=0,int _x=0,int _y=0){ init(3,_camp,"elephant",_x,_y);6 行未改动 if (tx==x+sx*2 && ty==y+sy*2 && nowBoard.chess[x+sx][y+sy]->id==-1) return true; } } return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;sx+=2){ for (int sy=-1;sy<=1;sy+=2){ if (xRange(x+sx*2) && yRange(y+sy*2) && nowBoard.chess[x+sx][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*2]->id==0 && nowBoard.chess[x+sx*2][y+sy*2]->camp!=camp) return true; } } return false; } } ~Elephant(){}}; };class Guard:public Chess{ //士public: Guard(int _camp=0,int _x=0,int _y=0){ init(4,_camp,"guard",_x,_y);2 行未改动 if (x==tx && y==ty) return false; if (nowBoard.chess[tx][ty]->camp==camp) return false; if (abs(tx-x)==1 && abs(ty-y)==1) return true; else return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;++sx){ for (int sy=-1;sy<=1;++sy){ if (abs(sx)==1 && abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){14 行未改动 if (x==tx && y==ty) return false; if (nowBoard.chess[tx][ty]->camp==camp) return false; if (abs(tx-x)+abs(ty-y)==1) return true; else return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;++sx){ for (int sy=-1;sy<=1;++sy){ if (abs(sx)+abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){19 行未改动 if (tx==x+sx*2 && ty==y+sy*3 && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1) return true; } } return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;sx+=2){ for (int sy=-1;sy<=1;sy+=2){ if (xRange(x+sx*3) && yRange(y+sy*2) && nowBoard.chess[x+sx*2][y+sy]->id==-1 && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*3][y+sy*2]->id==0 && nowBoard.chess[x+sx*3][y+sy*2]->camp!=camp) return true; if (xRange(x+sx*2) && yRange(y+sy*3) && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*3]->id==0 && nowBoard.chess[x+sx*2][y+sy*3]->camp!=camp) return true; } } return false; } } ~Duck(){}}; };class Soldier:public Chess{ //兵public: Soldier(int _camp=0,int _x=0,int _y=0){ init(6,_camp,"soldier",_x,_y);2 行未改动 if (x==tx && y==ty) return false; if (nowBoard.chess[tx][ty]->camp==camp) return false; if (abs(x-tx)<=1 && abs(y-ty)<=1) return true; else return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;++sx){ for (int sy=-1;sy<=1;++sy){ if (abs(sx)<=1 && abs(sy)<=1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){2 行未改动 } } return false; } ~Soldier(){} ~Soldier(){}};```
这种写法较若干 if 的优点很明显:可更新性好 & 可读性好。我可以往棋盘中加入更多种类的棋子,而只需要多写一个子类,并不需要在除了该子类外的任何地方做任何修改。85 行未改动 x=x*10+ch-48; ch=getchar(); } return x*f; } }};using IO::read;bool xRange(int x){ return 0<=x && x<=9;21 行未改动 id=_id;camp=_camp;name=_name;x=_x;y=_y; } virtual bool canGo(int tx,int ty,Board &nowBoard){return 0;} virtual bool canKill(Board &nowBoard){return 0;} ~Chess(){} ~Chess(){}};class Empty:public Chess{public: Empty(int _camp=0,int _x=0,int _y=0){17 行未改动 } }else if (x==tx && y!=ty){ for (int i=min(y,ty)+1;i<=max(y,ty)-1;++i){ if (nowBoard.chess[x][i]->id!=-1) return false; } } }else return false; return true; } } bool canKill(Board &nowBoard){ int tx,ty; int tx,ty; tx=x+1;ty=y; while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) tx++; if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true; tx=x-1;ty=y;4 行未改动 if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true; tx=x;ty=y-1; while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) ty--; if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true; return false; return false; } ~Car(){}};class Horse:public Chess{10 行未改动 if (tx==x+sx && ty==y+sy*2 && nowBoard.chess[x][y+sy]->id==-1) return true; } } return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;sx+=2){ for (int sy=-1;sy<=1;sy+=2){ if (xRange(x+sx*2) && yRange(y+sy) && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*2][y+sy]->id==0 && nowBoard.chess[x+sx*2][y+sy]->camp!=camp) return true;2 行未改动 } return false; } ~Horse(){}}; };class Elephant:public Chess{public: Elephant(int _camp=0,int _x=0,int _y=0){ init(3,_camp,"elephant",_x,_y);6 行未改动 if (tx==x+sx*2 && ty==y+sy*2 && nowBoard.chess[x+sx][y+sy]->id==-1) return true; } } return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;sx+=2){ for (int sy=-1;sy<=1;sy+=2){ if (xRange(x+sx*2) && yRange(y+sy*2) && nowBoard.chess[x+sx][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*2]->id==0 && nowBoard.chess[x+sx*2][y+sy*2]->camp!=camp) return true; } } return false; } } ~Elephant(){}}; };class Guard:public Chess{public: Guard(int _camp=0,int _x=0,int _y=0){ init(4,_camp,"guard",_x,_y);2 行未改动 if (x==tx && y==ty) return false; if (nowBoard.chess[tx][ty]->camp==camp) return false; if (abs(tx-x)==1 && abs(ty-y)==1) return true; else return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;++sx){ for (int sy=-1;sy<=1;++sy){ if (abs(sx)==1 && abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){14 行未改动 if (x==tx && y==ty) return false; if (nowBoard.chess[tx][ty]->camp==camp) return false; if (abs(tx-x)+abs(ty-y)==1) return true; else return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;++sx){ for (int sy=-1;sy<=1;++sy){ if (abs(sx)+abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){19 行未改动 if (tx==x+sx*2 && ty==y+sy*3 && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1) return true; } } return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;sx+=2){ for (int sy=-1;sy<=1;sy+=2){ if (xRange(x+sx*3) && yRange(y+sy*2) && nowBoard.chess[x+sx*2][y+sy]->id==-1 && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*3][y+sy*2]->id==0 && nowBoard.chess[x+sx*3][y+sy*2]->camp!=camp) return true; if (xRange(x+sx*2) && yRange(y+sy*3) && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*3]->id==0 && nowBoard.chess[x+sx*2][y+sy*3]->camp!=camp) return true; } } return false; } } ~Duck(){}}; };class Soldier:public Chess{public: Soldier(int _camp=0,int _x=0,int _y=0){ init(6,_camp,"soldier",_x,_y);2 行未改动 if (x==tx && y==ty) return false; if (nowBoard.chess[tx][ty]->camp==camp) return false; if (abs(x-tx)<=1 && abs(y-ty)<=1) return true; else return false; } } bool canKill(Board &nowBoard){ for (int sx=-1;sx<=1;++sx){ for (int sy=-1;sy<=1;++sy){ if (abs(sx)<=1 && abs(sy)<=1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){2 行未改动 } } return false; } ~Soldier(){} ~Soldier(){}};void Board::init(){ for (int i=0;i<10;++i){ for (int j=0;j<9;++j){72 行未改动 case 4:printf("[士]");break; case 5:printf("[鸭]");break; case 6:printf("[兵]");break; default:printf(" "); } } }else{ switch (chess[i][j]->id){ case 0:printf("(王)");break; case 1:printf("(车)");break;2 行未改动 case 4:printf("(士)");break; case 5:printf("(鸭)");break; case 6:printf("(兵)");break; default:printf(" "); } } } } printf("\n"); }9 行未改动