
作业社区
探索学习新天地,共享知识资源!
水蛭_312 的学生作业:
#ifndef _JUDGE_H #define _JUDGE_H #include "Player.hpp" #include "ChessBoard.hpp" class Judge { public: bool isWin(Player* player) { bool ok = false; string chessColor = player->getColor(); ok = isHorizontalWin(chessColor); if (ok) { return true; } ok = isVerticalWin(chessColor); if (ok) { return true; } ok = isUphillWin(chessColor); if (ok) { return true; } ok = isDownhillWin(chessColor); if (ok) { return true; } return ok; } bool isHorizontalWin(const string& chessColor) { ChessBoard* chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 0; // 判定 // down 判定 相同的棋子 for (int i = 0;i < 5;i++) { if(chessBoard->isSameColorChess(chessColor,line + i,column)){ count++; } else { break; } } if (count >=5) { return true; } // up for (int i = 0;i < 5;i++) { if (chessBoard->isSameColorChess(chessColor, line - i, column)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } bool isVerticalWin(const string& chessColor) { ChessBoard* chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 0; // 判定 // right 判定 相同的棋子 for (int i = 0;i < 5;i++) { if (chessBoard->isSameColorChess(chessColor, line, column + i)) { count++; } else { break; } } if (count >= 5) { return true; } // left for (int i = 0;i < 5;i++) { if (chessBoard->isSameColorChess(chessColor, line, column - i)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } bool isUphillWin(const string& chessColor) { ChessBoard* chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 0; // 判定 // uphill right 判定 相同的棋子 for (int i = 0;i < 5;i++) { if (chessBoard->isSameColorChess(chessColor, line-i, column + i)) { count++; } else { break; } } if (count >= 5) { return true; } // uphill left for (int i = 0;i < 5;i++) { if (chessBoard->isSameColorChess(chessColor, line + i, column - i)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } bool isDownhillWin(const string& chessColor) { ChessBoard* chessBoard = ChessBoard::getChessBoard(); int line = chessBoard->getCurrentLine(); int column = chessBoard->getCurrentColumn(); int count = 0; // 判定 // Downhill right 判定 相同的棋子 for (int i = 0;i < 5;i++) { if (chessBoard->isSameColorChess(chessColor, line + i, column + i)) { count++; } else { break; } } if (count >= 5) { return true; } // Downhill left for (int i = 0;i < 5;i++) { if (chessBoard->isSameColorChess(chessColor, line - i, column - i)) { count++; } else { break; } } return count - 1 >= 5 ? true : false; } }; #endif




