为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

胡汉三66 的学生作业:

Player.hpp #ifndef _PLAYER_HEAD_H #define _PLAYER_HEAD_H #include using namespace std; class Player{ // 抽象类 // 玩家类 public: Player(const string &name,const string &color){ // 构造函数 this->name = name; this->color = color; } string getName(void) const{ // 获取 玩家名字 return name; } string getColor(void) const{ // 获取 棋子颜色 return color; } virtual bool placeChess(int x, int y) = 0; // 纯虚函数 // 判断 落子位置 是否可以 保存棋子 private: string name; string color; }; #endif WhitePlayer.hpp #ifndef _WHITEPLAYER_HEAD_H #define _WHITEPLAYER_HEAD_H #include "Player.hpp" #include "ChessBoard.hpp" #include "WhiteChess.hpp" class WhitePlayer:public Player{ // 白棋玩家类 // 继承 玩家类 public: WhitePlayer(const string &name):Player(name,"white"){} // 构造函数 bool placeChess(int x,int y){ // 判断 落子位置 是否可以 保存棋子 ChessBoard *chessBoard = ChessBoard::getChessBoard(); // 通过类函数 间接创建 棋盘对象 bool ok = chessBoard->isVaildPostion(x,y); // 判断 落子位置 是否可以 保存棋子 if(ok){ chessBoard->placeChess(new WhiteChess(x,y)); // 如果可以 创建白棋对象 保存棋子 } return ok; } }; #endif BlackPlayer.hpp #ifndef _BLACKPLAYER_HEAD_H #define _BLACKPLAYER_HEAD_H #include "Player.hpp" #include "ChessBoard.hpp" #include "BlackChess.hpp" class BlackPlayer:public Player{ // 黑棋玩家类 // 继承 玩家类 public: BlackPlayer(const string &name):Player(name,"black"){} // 构造函数 bool placeChess(int x,int y){ // 判断 落子位置 是否可以 保存棋子 ChessBoard *chessBoard = ChessBoard::getChessBoard(); // 通过类函数 间接创建 棋盘对象 bool ok = chessBoard->isVaildPostion(x,y); // 判断 落子位置 是否可以 保存棋子 if(ok){ chessBoard->placeChess(new BlackChess(x,y)); // 如果可以 创建白棋对象 保存棋子 } return ok; } }; #endif main.cpp #include "BlackChess.hpp" // 黑棋 #include "WhiteChess.hpp" // 白棋 #include "ChessBoard.hpp" // 棋盘 #include "BlackPlayer.hpp" // 黑棋玩家 #include "WhitePlayer.hpp" // 白棋玩家 int main(int argc, const char *argv[]){ ChessBoard *chessBoard = ChessBoard::getChessBoard(); // 通过类函数 间接创建 棋盘对象 chessBoard->show(); // 打印棋盘 WhiteChess *whiteChess = new WhiteChess(5,7); // 创建 白棋对象 BlackChess *blackChess = new BlackChess(9,5); // 创建 黑棋对象 chessBoard->placeChess(whiteChess); // 棋盘 存放 白棋 chessBoard->placeChess(blackChess); // 棋盘 存放 黑棋 WhitePlayer *whitePlayer = new WhitePlayer("wdog"); // 创建 白棋玩家 whitePlayer->placeChess(9,5); // 白棋玩家 下棋 whitePlayer->placeChess(9,7); // 白棋玩家 下棋 BlackPlayer *blackPlayer = new BlackPlayer("bdog"); // 创建 黑棋玩家 blackPlayer->placeChess(5,7); // 黑棋玩家 下棋 blackPlayer->placeChess(13,5); // 黑棋玩家 下棋 return 0; } 【图片】

得分 100
学习任务

胡汉三66 的学生作业:

Chess.hpp #ifndef _CHESS_H_ #define _CHESS_H_ #include using namespace std; class Chess{ // 抽象类 public: Chess(const string &color,int x,int y):color(color),x(x),y(y){} // 构造函数 int getX(void) const{ return x; } // 返回 坐标点 x int getY(void) const{ return y; } // 返回 坐标点 y string getColor(void) const{ return color;} // 返回 棋子颜色 virtual void show(void) const = 0; // 纯虚函数 private: int x; // 坐标 x int y; // 坐标 y string color; // 棋子颜色 }; #endif BlackChess.hpp #ifndef _BLACKCHESS_H_ #define _BLACKCHESS_H_ #include #include "Chess.hpp" class BlackChess:public Chess { // 继承类 Chess 的公有成员 public: BlackChess(int x,int y):Chess("black",x,y){} // 构造函数 void show(void) const{ fprintf(stderr,"\033[%d;%dH\033[44;31m[☻ ]\033[0m",getY(),getX() - 1); // 棋子坐标 // 蓝底红字 棋子[☻ ] // 关闭所有属性 fprintf(stderr,"\033[%d;%dH\n",getY(),getX()); // 定义在棋子中心左侧 // 每个棋子宽度占3个符号 } }; #endif WhiteChess.hpp #ifndef _WHITECHESS_H_ #define _WHITECHESS_H_ #include #include "Chess.hpp" class WhiteChess:public Chess { // 继承类 Chess 的公有成员 public: WhiteChess(int x,int y):Chess("white",x,y){} // 构造函数 void show(void) const{ fprintf(stderr,"\033[%d;%dH\033[43;37m[☻ ]\033[0m",getY(),getX() - 1); // 棋子坐标 // 黄底白字 棋子 : [☻ ] // 关闭所有属性 fprintf(stderr,"\033[%d;%dH\n",getY(),getX()); // 定义在棋子中心左侧 // 每个棋子宽度占3个符号 } }; #endif main.cpp #include "BlackChess.hpp" #include "WhiteChess.hpp" int main(int argc, const char *argv[]){ WhiteChess w(5,6); BlackChess b(9,6); w.show(); b.show(); return 0; } 【图片】

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号