
作业社区
探索学习新天地,共享知识资源!
大禹123 的学生作业:
#ifndef __JUDGE_HEAD_H__ #define __JUDGE_HEAD_H__ #include #include "Player.hpp" #include "ChessBoard.hpp" class Judge { public: bool isWin(const Player *player) { bool ok; ChessBorad *chessBoard = ChessBorad::getChessBoard(); int currentLine = chessBoard->getCurrentLine(); int currentColumn = chessBoard->getCurrentColumn(); /*horizontal*/ ok = isHorizontalWin(player->getColor()); if (ok) { return (true); }; /* vertical */ ok = isVerticalWin(player->getColor()); if (ok) { return (true); } /* uphill */ ok = isUphillWin(player->getColor()); if (ok) { return (true); } /* downhill */ ok = isDownhillWin(player->getColor()); if (ok) { return (true); } return (false); } bool isHorizontalWin(const string &color) { int count = 0; ChessBorad *chessBoard = ChessBorad::getChessBoard(); int currentLine = chessBoard->getCurrentLine(); int currentColumn = chessBoard->getCurrentColumn(); // 向右 行不变,列增加 for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine, currentColumn + i); if (!ok) { break; } count++; /* code */ } if (count >= 5) return true; // 向左 行不变,列减少 for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine, currentColumn - i); if (!ok) { break; } count++; /* code */ } if ((count - 1) >= 5) return true; return false; } // 垂直方向 bool isVerticalWin(const string &color) { int count = 0; ChessBorad *chessBoard = ChessBorad::getChessBoard(); int currentLine = chessBoard->getCurrentLine(); int currentColumn = chessBoard->getCurrentColumn(); // 向下 列不变,行增加 for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine + i, currentColumn); if (!ok) { break; } count++; /* code */ } if (count >= 5) return true; // 向上 列不变,行减少 for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine - i, currentColumn); if (!ok) { break; } count++; /* code */ } if ((count - 1) >= 5) return true; return false; } bool isUphillWin(const string &color) { int count = 0; ChessBorad *chessBoard = ChessBorad::getChessBoard(); int currentLine = chessBoard->getCurrentLine(); int currentColumn = chessBoard->getCurrentColumn(); // 爬坡 行减少,列增加 for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine - i, currentColumn+i); if (!ok) { break; } count++; /* code */ } if (count >= 5) return true; for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine + i, currentColumn - i); if (!ok) { break; } count++; /* code */ } if ((count - 1) >= 5) return true; return false; } bool isDownhillWin(const string &color) { int count = 0; ChessBorad *chessBoard = ChessBorad::getChessBoard(); int currentLine = chessBoard->getCurrentLine(); int currentColumn = chessBoard->getCurrentColumn(); // 爬坡 行减少,列增加 for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine - i, currentColumn - i); if (!ok) { break; } count++; /* code */ } if (count >= 5) return true; for (int i = 0; i < 5; i++) { bool ok = chessBoard->isSameColorChess(color, currentLine + i, currentColumn + i); if (!ok) { break; } count++; /* code */ } if ((count - 1) >= 5) return true; return false; } }; #endif





大禹123 的学生作业:
ChessBoard.hpp #ifndef __CHESS_BOARD_HEAD_H__ #define __CHESS_BOARD_HEAD_H__ #include #include #include #include #include "Chess.hpp" #define MIN_X 1 #define MIN_Y 1 #define MAX_X 57 #define MAX_Y 29 #define MAX_COLUMN 15 #define MAX_LINE 15 #define X_SKIP 4 #define Y_SKIP 2 using namespace std; class ChessBorad { public: static ChessBorad *getChessBoard(void) { if (!chessboard) { pthread_mutex_lock(&lock); if (!chessboard) { chessboard = new ChessBorad; } pthread_mutex_unlock(&lock); } return chessboard; } bool isValidPisition(int x, int y) { if (x < MIN_X || y < MIN_Y) return false; if (x > MAX_X || y > MAX_Y) return false; int line = x / X_SKIP; int column = y / Y_SKIP; return this->chess[line][column] ? false : true; } void show(void) { fprintf(stderr, "\033[%d;%dH", MIN_Y, MIN_X); char buffer[1024] = {0}; int rbytes = 0, wbytes = 0; FILE *fp = fopen("./ChessBoard.txt", "r"); if (NULL == fp) { fprintf(stderr, "[ERROR] fail to open ChessBoard.txt:%s\n", strerror(errno)); return; } while (fgets(buffer, sizeof(buffer), fp)) { fputs(buffer, stdout); } fclose(fp); } ~ChessBorad() { fprintf(stderr, "\033[%d;%dH", MAX_Y + 1, MIN_X); // cout getY() / Y_SKIP; this->chess[line][column] = chess; chess->show(); } private: ChessBorad() { Chess *chess[MAX_LINE][MAX_COLUMN]; for (int i = 0; i < MAX_LINE; i++) { for (int j = 0; j < MAX_COLUMN; j++) { chess[i][j] = nullptr; } } }; static ChessBorad *chessboard; static pthread_mutex_t lock; static Gc gc; const Chess *chess[MAX_LINE][MAX_COLUMN]; }; ChessBorad *ChessBorad::chessboard = nullptr; pthread_mutex_t ChessBorad::lock = PTHREAD_MUTEX_INITIALIZER; ChessBorad::Gc gc; // 重点在这里,定义了一个静态区对象,程序结束的时候就销毁了 #endif Chess.hpp #ifndef __CHESS_HEAD_H__ #define __CHESS_HEAD_H__ #include using namespace std; class Chess { public: Chess(int x, int y, string color) : x(x), y(y), color(color) {} virtual ~Chess() { // cout




