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

作业社区

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

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

ctwong 的学生作业:

Judge.hpp #ifndef _JUDGE_HEAD_H #define _JUDGE_HEAD_H #include "Player.hpp" #include "ChessBoard.hpp" class Judge { public: bool isWin(Player *player) { bool ok = false; string chessColour = player->getColour(); ok = isHorizontalWin(chessColour); if (ok) { return true; } ok = isVerticalWin(chessColour); if (ok) { return true; } ok = isUphillWin(chessColour); if (ok) { return true; } ok = isDownhillWin(chessColour); if (ok) { return true; } return ok; } bool isHorizontalWin(const string &chessColour) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); // right for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine, curColumn + i)) { count ++; } else { break; } } if (count >= 5) { return true; } // left for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine, curColumn - i)) { count ++; } else { break; } } return count - 1 >= 5 ? true : false; } bool isVerticalWin(const string &chessColour) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); // Up for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine - i, curColumn)) { count ++; } else { break; } } if (count >= 5) { return true; } // Down for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine + i, curColumn)) { count ++; } else { break; } } return count - 1 >= 5 ? true : false; } bool isUphillWin(const string &chessColour) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); // uphill->up for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine - i, curColumn + i)) { count ++; } else { break; } } if (count >= 5) { return true; } // uphill->Down for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine + i, curColumn - i)) { count ++; } else { break; } } return count - 1 >= 5 ? true : false; } bool isDownhillWin(const string &chessColour) { int count = 0; ChessBoard *chessBoard = ChessBoard::getChessBoard(); int curLine = chessBoard->getCurrentLine(); int curColumn = chessBoard->getCurrentColumn(); // downhill->Up for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine - i, curColumn - i)) { count ++; } else { break; } } if (count >= 5) { return true; } // downhill->Down for (int i = 0; i < 5; i ++) { if (chessBoard->isSameColourChess(chessColour, curLine + i, curColumn + i)) { count ++; } else { break; } } return count - 1 >= 5 ? true : false; } }; #endif main.cpp #include "BlackPlayer.hpp" #include "WhitePlayer.hpp" #include "KeyHandle.hpp" #include "Judge.hpp" int main(int argc, const char *argv[]) { ChessBoard *chessBoard = ChessBoard::getChessBoard(); chessBoard->show(); BlackPlayer blackPlayer("tiechui"); WhitePlayer whitePlayer("cuihua"); Player *players[] = {&blackPlayer, &whitePlayer}; #if 0 blackPlayer.placeChess(5, 1); whitePlayer.placeChess(5, 3); #endif KeyHandle keyHandle; Judge judge; bool win = false; while (!win) { for (Player *player:players) { keyHandle.waitPlayerPlaceChess(player); bool ok = judge.isWin(player); if (ok) { win = true; break; } } } return 0; } 【图片】

微信客服

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

帮助反馈 APP下载

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

公众号

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