From 76fd33a5c26ecbc00302d29d241d5c6be4e20c39 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Thu, 28 Sep 2023 10:05:30 +0800 Subject: [PATCH] Exceed baseline 1 --- src/advanced.cpp | 4 +++ src/include/client.h | 81 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/advanced.cpp b/src/advanced.cpp index 3075c59..97e5c22 100644 --- a/src/advanced.cpp +++ b/src/advanced.cpp @@ -28,6 +28,7 @@ void CheckResult() { } #undef ERROR_IN_RESULT } +bool clicked[100][100]; /** * @brief The implementation of function Execute * @details Use it only when trying advanced task. Do NOT modify it before @@ -35,6 +36,9 @@ void CheckResult() { */ void Execute(int row, int column) { CheckResult(); + assert(!clicked[row][column]); + clicked[row][column] = true; + // above are some checks std::string str; VisitBlock(row, column); if (game_state != 0) { diff --git a/src/include/client.h b/src/include/client.h index 666f3b9..7033d52 100644 --- a/src/include/client.h +++ b/src/include/client.h @@ -356,6 +356,84 @@ std::pair TotalRandomGuess() { } return std::make_pair(row, column); } +/** + * @brief The definition of function SimpleGuess() + * + * @details This function is designed to make a guess when there is no definite + * none-mine block to be clicked using simple algorithm. + */ +std::pair SimpleGuess() { + using namespace Client; + // std::cout << "SimpleGuess" << std::endl; + std::vector probability[max_size][max_size]; + double default_probability = 0.18; + int total_known = 0, total_known_with_mine = 0; + for (int i = 0; i < rows; i++) + for (int j = 0; j < columns; j++) + if (map_status[i][j] != 0) { + total_known++; + if (map_status[i][j] == -1) total_known_with_mine++; + } + if (total_known > 8) + default_probability = (double)(total_known_with_mine) / (total_known); + // if((double)(total_known)/(rows*columns)<0.3) return TotalRandomGuess(); + for (int i = 0; i < rows; i++) + for (int j = 0; j < columns; j++) + if (map_status[i][j] == 2) { + int nearby_mines = game_map[i][j] - '0', + nearby_unkown = + 0; // nearby_mines is the number of mines in currently unknown + // blocks that are adjacent to the block (i,j) + const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, + dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; + for (int k = 0; k < 8; k++) { + int x = i + dx[k], y = j + dy[k]; + if (x >= 0 && x < rows && y >= 0 && y < columns) { + if (map_status[x][y] == 0) + nearby_unkown++; + else if (map_status[x][y] == -1) + nearby_mines--; + } + } + if(nearby_unkown==0) continue; + for (int k = 0; k < 8; k++) { + int x = i + dx[k], y = j + dy[k]; + if (x >= 0 && x < rows && y >= 0 && y < columns) { + if (map_status[x][y] == 0) + probability[x][y].push_back((double)(nearby_mines) / + (nearby_unkown)); + } + } + } + std::pair best_guess = TotalRandomGuess(); + for(int i=0;i0) + { + current_prob=0; + for(int k=0;k0) + { + this_prob=0; + for(int k=0;k TotalRandomGuess() { std::pair MakeBestGuess() { using namespace Client; // just make a total random guess before a better algorithm is designed - return TotalRandomGuess(); + // return TotalRandomGuess(); + return SimpleGuess(); return std::make_pair(0, 0); } /**