diff --git a/src/advanced.cpp b/src/advanced.cpp index 637f3f3..3075c59 100644 --- a/src/advanced.cpp +++ b/src/advanced.cpp @@ -6,12 +6,35 @@ #include "client.h" #include "server.h" +/** + * @brief The implementation of function CheckResult + * @details Only for debug. It checks the map_status and orginal map to see if + * the result of Gauss-Jordan Elimination is correct. + */ +void CheckResult() { +#define ERROR_IN_RESULT 0 + using namespace Client; + using namespace Server; + for (int i = 0; i < rows; i++) + for (int j = 0; j < columns; j++) { + if ('0' <= visible_map[i][j] && visible_map[i][j] <= '8') + assert(map_status[i][j] == 2); + if (visible_map[i][j] == '?') assert(map_status[i][j] != 2); + if (map_status[i][j] == 2) + assert('0' <= visible_map[i][j] && visible_map[i][j] <= '8'); + if (map_status[i][j] == 0) assert(visible_map[i][j] == '?'); + if (map_status[i][j] == 1) assert(origin_map[i][j] == '.'); + if (map_status[i][j] == -1) assert(origin_map[i][j] == 'X'); + } +#undef ERROR_IN_RESULT +} /** * @brief The implementation of function Execute * @details Use it only when trying advanced task. Do NOT modify it before * discussing with TA. */ void Execute(int row, int column) { + CheckResult(); std::string str; VisitBlock(row, column); if (game_state != 0) { diff --git a/src/autotest.py b/src/autotest.py new file mode 100755 index 0000000..2225285 --- /dev/null +++ b/src/autotest.py @@ -0,0 +1,139 @@ +#!/usr/bin/python3 +import sys +import os +import random +built_in_test_cases = [ +"""10 10 +........X. +.......... +.......... +......X... +.......... +.X.......X +.......... +X........X +.......... +X....XX... +0 0""", +"""10 10 +.....X...X +.........X +X..X...... +X.X....... +..X....... +.......... +.......... +.......... +.......... +X......... +0 0""", +"""10 10 +.XX....... +...X...XX. +.......... +...X...... +.X......X. +..X....X.. +XX....X... +..X...X... +..X.X.X.X. +....X..... +0 9""", +"""10 10 +.......X.. +X.....X... +X...X..... +...X...... +.......... +.X.X.X.... +..X.X..X.. +.X.X.....X +....X..... +.....X.... +4 9""", +"""20 20 +X...........X....... +..XX.............X.X +.X......X.........X. +....X.X...X...X..... +....X.X.........X... +X.XX...........X.X.. +...X.............XX. +...XX...X..X..X.X... +...X.X...X...X...... +.............XX.X... +.X............X..... +.X..X.........X....X +X.X.X..X...X..X..... +.X....X.X......X.... +...X.........X..X... +...X.X...X.......... +..X.XX.X......XXXX.. +.X.X...............X +XXX..X....X.XX..X... +.X...X.XX........X.X +0 10""" +] +mine_rate=0.15 +total_round=0 +win_round=0 +for i in range(0,10): + for data in built_in_test_cases: + fn=open("tmp/test.in","w") + fn.write(data) + fn.close() + os.system("build/src/client < tmp/test.in > tmp/test.out") + fn=open("tmp/test.out","r") + lines=fn.readlines() + fn.close() + print(lines) + total_round+=1 + if lines[0]=='YOU WIN!\n': + win_round+=1 + print("win rate: "+str(win_round/total_round),win_round,total_round) + +input("Press Enter to continue...") + +while True: + # randomly generate n,m in [2,30] + n=random.randint(2,30) + m=random.randint(2,30) + # print(n,m) + # randomly generate mine_rate in [0,1] + #mine_rate=random.random() + # generate a random map + map=[] + for i in range(n): + map.append([]) + for j in range(m): + if random.random() tmp/test.out") + # read the output + fn=open("tmp/test.out","r") + lines=fn.readlines() + fn.close() + # check the output + print(lines) + total_round+=1 + if lines[0]=='YOU WIN!\n': + win_round+=1 + print("win rate: "+str(win_round/total_round),win_round,total_round) \ No newline at end of file diff --git a/src/include/client.h b/src/include/client.h index 71d9c0c..666f3b9 100644 --- a/src/include/client.h +++ b/src/include/client.h @@ -290,6 +290,7 @@ void InterpretResult(std::vector > equations) { assert(vid >= 0); assert(vid < variaID_to_position.size()); std::pair pos = variaID_to_position[vid]; + if (map_status[pos.first][pos.second] != 0) continue; if (sol == 0) { map_status[pos.first][pos.second] = 1; no_mine_block_to_be_clicked.push(pos);