#include "Chess.h" #include #include #include Chess::Chess(int gradeSize, int marginX, int marginY, float chessSize) { this->gradeSize = gradeSize; this->margin_x = marginX; this->margin_y = marginY; this->chessSize = chessSize; playerFlag = CHESS_BLACK; for (int i = 0; i < gradeSize; i++) { vectorrow; for(int j=0;jrow = row; pos->col = col; if (chessMap[pos->row][pos->col] == 0) { ret = true; } break; } //右上角判断 len = sqrt((leftTopPosX+chessSize-x) * (leftTopPosX + chessSize - x) + (y - leftTopPosY) * (x - leftTopPosY)); if (len < offset) { pos->row = row; pos->col = col + 1; if (chessMap[pos->row][pos->col] == 0) { ret = true; } break; } //左下角判断 len = sqrt((x - leftTopPosX) * (x - leftTopPosX) + (leftTopPosY+chessSize-y) * (leftTopPosY + chessSize - y)); if (len < offset) { pos->row = row + 1; pos->col = col; if (chessMap[pos->row][pos->col] == 0) { ret = true; } break; } //右下角判断 len = sqrt((leftTopPosX + chessSize - x) * (leftTopPosX + chessSize - x) + (leftTopPosY + chessSize - y) * (leftTopPosY + chessSize - y)); if (len < offset) { pos->row = row + 1; pos->col = col + 1; if (chessMap[pos->row][pos->col] == 0) { ret = true; } break; } } while (0); return ret; } void Chess::chessDown(ChessPos* pos, chess_kind_t kind) { int x = margin_x + chessSize * pos->col-0.5*chessSize; int y = margin_y + chessSize * pos->row-0.5*chessSize; if (kind == CHESS_WHITE) { putimage(x, y, &chessWhiteImg); chessMap[pos->row][pos->col] = -1; } else { putimage(x, y, &chessBlackImg); chessMap[pos->row][pos->col] = 1; } updateGameMap(pos); } int Chess::getGradeSize() { return gradeSize; } int Chess::getChessData(ChessPos* pos) { return chessMap[pos->row][pos->col]; } int Chess::getChessData(int row, int col) { return chessMap[row][col]; } bool Chess::checkOver() { if (checkWin()) { Sleep(1500); if (playerFlag == false) { //刚才走棋的是黑方(棋手方),棋手胜 loadimage(0, "res/胜利.png",765,768); } else { loadimage(0, "res/失败.png",765,768); } _getch();//暂停 return true; } return false; } void Chess::updateGameMap(ChessPos* pos) { lastPos = *pos; chessMap[pos->row][pos->col] = playerFlag ? CHESS_BLACK : CHESS_WHITE; playerFlag = !playerFlag; } bool Chess::checkWin() { //最近落子点的准确位置 int row = lastPos.row; int col = lastPos.col; //落子点的水平方向 for (int i = 0;i < 5; i++) { if (col - i >= 0 && col - i + 4 < gradeSize && chessMap[row][col - i] == chessMap[row][col - i + 1] && chessMap[row][col - i] == chessMap[row][col - i + 2] && chessMap[row][col - i] == chessMap[row][col - i + 3] && chessMap[row][col - i] == chessMap[row][col - i + 4]) { return true; } } //垂直方向 for (int i = 0; i < 5; i++) { if (row - i >= 0 && row - i + 4 < gradeSize && chessMap[row-i][col] == chessMap[row-i+1][col] && chessMap[row-i][col] == chessMap[row-i+2][col] && chessMap[row-i][col] == chessMap[row-i+3][col] && chessMap[row-i][col] == chessMap[row-i+4][col]) { return true; } } //"/"方向 for (int i = 0; i < 5; i++) { if (row+i=0&&col-i+4= 0 && row - i + 4 < gradeSize && col - i >= 0 && col - i + 4 < gradeSize && chessMap[row - i][col - i] == chessMap[row - i + 1][col - i + 1] && chessMap[row - i][col - i] == chessMap[row - i + 2][col - i + 2] && chessMap[row - i][col - i] == chessMap[row - i + 3][col - i + 3] && chessMap[row - i][col - i] == chessMap[row - i + 4][col - i + 4]) { return true; } } return false; }