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

作业社区

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

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

weixin_慕哥3021856 的学生作业:

玩家类、白棋手类、黑棋手类 // player.hpp #ifndef _PLAYER_HEAD_H #define _PLAYER_HEAD_H #include using namespace std; class Player { public: Player(const string &name, const string &pieceColor): name(name), pieceColor(pieceColor){} string getName(void) const { return name; } string getPieceColor(void) const { return pieceColor; } virtual bool placePiece(int x, int y) = 0; private: string name; string pieceColor; }; #endif // whitePlayer.hpp #ifndef _WHITEPIECEPLAYER_H #define _WHITEPIECEPLAYER_H #include "player.hpp" #include "board.hpp" #include "whitePiece.hpp" class WhitePiecePlayer: public Player { public: WhitePiecePlayer(const string &name): Player(name, "white"){} bool placePiece(int x, int y) { Board *board = Board::getBoardObj(); bool placeOK = board->isValidPosition(x, y); if (placeOK) { board->placePiece(new WhitePiece(x, y)); } return placeOK; } }; #endif // blackPlayer.hpp #ifndef _BLACKPIECEPLAYER_H #define _BLACKPIECEPLAYER_H #include "player.hpp" #include "board.hpp" #include "blackPiece.hpp" class BlackPiecePlayer: public Player { public: BlackPiecePlayer(const string &name): Player(name, "black"){} bool placePiece(int x, int y) { Board *board = Board::getBoardObj(); bool placeOK = board->isValidPosition(x, y); if (placeOK) { board->placePiece(new BlackPiece(x, y)); } return placeOK; } }; #endif 棋子基类、白棋类、黑棋类 // piece.hpp #ifndef __PIECE_HEAD_H_ #define __PIECE_HEAD_H_ #include using namespace std; class Piece { public: Piece(const string &color, int x, int y): x(x), y(y), color(color){} int getX(void) const { return x; } int getY(void) const { return y; } string getColor(void) const { return color; } virtual void showPiece(void) const = 0; virtual ~Piece(){} protected: int x; int y; string color; }; #endif // whitePiece.hpp #ifndef __WHITEPIECE_HEAD_H #define __WHITEPIECE_HEAD_H #include #include "piece.hpp" using namespace std; class WhitePiece:public Piece { public: WhitePiece(int x, int y):Piece("white", x, y){} void showPiece(void) const { fprintf(stderr, "\033[%d;%dH\033[40;37m⚪\033[0m", y, x - 1); fprintf(stderr, "\033[%d;%dH\n", y, x); } }; #endif // blackPiece.hpp #ifndef __BLACKPIECE_HEAD_H #define __BLACKPIECE_HEAD_H #include #include "piece.hpp" using namespace std; class BlackPiece:public Piece { public: BlackPiece(int x, int y):Piece("black", x, y){} void showPiece(void) const { fprintf(stderr, "\033[%d;%dH\033[30m⚫\033[0m", y, x - 1); fprintf(stderr, "\033[%d;%dH\n", y, x); } }; #endif 棋盘类 #ifndef _BOARD_HEAD_H #define _BOARD_HEAD_H #include "piece.hpp" using namespace std; #define MIN_X 1 #define MAX_X 57 #define MIN_Y 1 #define MAX_Y 29 #define MAX_LINE 15 #define MAX_COLUMN 15 #define SPACE_X 4 #define SPACE_Y 2 class Board { public: static Board *getBoardObj() { if (!board) board = new Board; return board; } ~Board() { fprintf(stdout, "\033[%d;%dH", MAX_Y+2, MIN_X); for (int i = 0; i < MAX_LINE; i++) { for (int j = 0; j < MAX_COLUMN; j++) { if (this->boardPiece[i][j]) delete boardPiece[i][j]; } } } void show() const { const char *filename = "./ChessBoard.txt"; char buf[1024]; int i = 1; FILE *fp = fopen(filename, "r"); if (!fp) { perror("Failed to open ChessBoard.txt!"); return; } // fprintf(stdout, "\033[%d;%dH", MIN_Y, MIN_X); while (fgets(buf, sizeof(buf), fp)) { fprintf(stdout, "\033[%d;%dH", 1+i, 2); fputs(buf, stdout); i++; } fclose(fp); } void placePiece(Piece *piece) { int line = piece->getY() / SPACE_Y; int column = piece->getX() / SPACE_X; this->boardPiece[line][column] = piece; piece->showPiece(); return; } bool isValidPosition(int x, int y) { if (x < MIN_X || y < MIN_Y) return false; if (x > MAX_X || y > MAX_Y) return false; int line = y / SPACE_Y; int column = x / SPACE_X; return boardPiece[line][column] ? false: true; } class GC { public: ~GC() { if (board) { delete board; } } }; private: Board() { for (int i = 0; i < MAX_LINE; i++) { for (int j = 0; j < MAX_COLUMN; j++) { this->boardPiece[i][j] = nullptr; } } } Piece *boardPiece[MAX_LINE][MAX_COLUMN]; static Board *board; }; Board *Board::board = nullptr; Board::GC gc; #endif main函数 #include "blackPiecePlayer.hpp" #include "whitePiecePlayer.hpp" int main(int argc, const char *argv[]) { Board *board = Board::getBoardObj(); board->show(); BlackPiecePlayer *laowang = new BlackPiecePlayer("laowang"); WhitePiecePlayer *laoli = new WhitePiecePlayer("laoli"); laowang->placePiece(6, 2); laoli->placePiece(10, 2); return 0; } 【图片】

得分 100
讨论题

别摸我的键盘 的学生作业:

seqstack.h // // Created by Administrator on 2024-12-29. // #ifndef QRS_STUDY_SEQSTACK_H #define QRS_STUDY_SEQSTACK_H #include #include #include #define MAX 12 typedef char data_t; typedef struct { data_t stack[MAX]; int top; } seqstack_t; extern seqstack_t * create_empty_seqstack(); extern int is_empty_seqstack(seqstack_t * s); extern int is_full_seqstack(seqstack_t * s); extern void push_seqstack(seqstack_t * s,data_t data); extern data_t pop_seqstack(seqstack_t * s); extern data_t get_top_data(seqstack_t * s); #endif //QRS_STUDY_SEQSTACK_H seqstack.c // // Created by Administrator on 2024-12-29. // #include "seqstack.h" /** * 创建空的顺序栈 * @return */ seqstack_t *create_empty_seqstack() { seqstack_t *s = (seqstack_t *) malloc(sizeof(seqstack_t)); if (NULL == s) { printf("seqstack fail"); return NULL; } memset(s, 0, sizeof(seqstack_t)); s->top = -1; return s; } /* * 顺序栈是否为空 */ int is_empty_seqstack(seqstack_t *s) { return s->top == -1 ? 1 : 0; } /** * 顺序栈是否满了 * @param s * @return */ int is_full_seqstack(seqstack_t *s) { return s->top == MAX - 1 ? 1 : 0; } /** * 压栈 * @param s * @param data */ void push_seqstack(seqstack_t *s, data_t data) { s->stack[++s->top] = data; } /** * 弹栈 * @param s * @return */ data_t pop_seqstack(seqstack_t *s) { return s->stack[s->top--]; } /** * 获取栈顶元素 * @param s * @return */ data_t get_top_data(seqstack_t *s) { return s->stack[s->top]; } main.c // // Created by Administrator on 2024-12-29. // #include "seqstack.h" int main() { setbuf(stdout, NULL); data_t c[] = {'a', 'n', 'i', 'h', 'c', ' ', 'e', 'v', 'o', 'l', ' ', 'I'}; seqstack_t *s = create_empty_seqstack(); int len = sizeof(c); for (int i = 0; i < len; i++) { if (!is_full_seqstack(s)) { data_t a = c[i]; push_seqstack(s, a); } } printf("stack top element is: %c \n", get_top_data(s)); printf("stack elements is: \n"); while (!is_empty_seqstack(s)) { data_t data = pop_seqstack(s); printf("%c ", data); } printf("\n"); } 执行结果 stack top element is: I stack elements is: I l o v e c h i n a

微信客服

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

帮助反馈 APP下载

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

公众号

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