no extra info provided

Committer:
jh152
Date:
Sat Apr 24 14:08:06 2021 +0000
Revision:
0:dd04a694fbdd
no extra info provided

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jh152 0:dd04a694fbdd 1 #include"SquareConqueror.h"
jh152 0:dd04a694fbdd 2 //struct MY_Flag getFlags;
jh152 0:dd04a694fbdd 3
jh152 0:dd04a694fbdd 4 ///////////////////////////////// Class: Game_State ////////////////////////////////////////////
jh152 0:dd04a694fbdd 5 Game_State::Game_State(int dist_raw[], int ide_raw[], int rgb_raw[], struct MY_flags* getFlags) {
jh152 0:dd04a694fbdd 6 // The constructor
jh152 0:dd04a694fbdd 7 /*Dist_raw[4] = dist_raw[];
jh152 0:dd04a694fbdd 8 Ide_raw[4] = ide_raw[];
jh152 0:dd04a694fbdd 9 RGB_raw[4] = rgb_raw[];*/
jh152 0:dd04a694fbdd 10
jh152 0:dd04a694fbdd 11 this->dist_raw = dist_raw;
jh152 0:dd04a694fbdd 12 this->ide_raw = ide_raw;
jh152 0:dd04a694fbdd 13 this->rgb_raw = rgb_raw;
jh152 0:dd04a694fbdd 14
jh152 0:dd04a694fbdd 15 //getFlags = new MY_flags{ NOT_OPEN,NOT_OPEN,'k',false};
jh152 0:dd04a694fbdd 16 this->getFlags = getFlags;
jh152 0:dd04a694fbdd 17 }
jh152 0:dd04a694fbdd 18
jh152 0:dd04a694fbdd 19 void Game_State::identityOperaor() {
jh152 0:dd04a694fbdd 20 // Deal with data from photoelectric sensors, and give hard level and also judge whether administrator.
jh152 0:dd04a694fbdd 21 /* int ide_raw[4];
jh152 0:dd04a694fbdd 22 for (int count = 0; count < sizeof(ide_raw[4]); count++) {
jh152 0:dd04a694fbdd 23 ide_raw[count] = int(ide_raw[count] < 0.1);
jh152 0:dd04a694fbdd 24 }*/
jh152 0:dd04a694fbdd 25 getFlags->hard_level = ((ide_raw[3] * 8 + ide_raw[2] * 4 + ide_raw[1] * 2 + ide_raw[0]) % 4);
jh152 0:dd04a694fbdd 26 if (getFlags->hard_level == 0) {
jh152 0:dd04a694fbdd 27 if (ide_raw[3] == 0 && ide_raw[2] == 0 && ide_raw[1] == 0 && ide_raw[0] == 0) {
jh152 0:dd04a694fbdd 28 getFlags->is_administrator = false;
jh152 0:dd04a694fbdd 29 }
jh152 0:dd04a694fbdd 30 else {
jh152 0:dd04a694fbdd 31 getFlags->is_administrator = true;
jh152 0:dd04a694fbdd 32 getFlags->hard_level = 4;
jh152 0:dd04a694fbdd 33 }
jh152 0:dd04a694fbdd 34 }
jh152 0:dd04a694fbdd 35 else {
jh152 0:dd04a694fbdd 36 getFlags->is_administrator = false;
jh152 0:dd04a694fbdd 37 }
jh152 0:dd04a694fbdd 38 }
jh152 0:dd04a694fbdd 39
jh152 0:dd04a694fbdd 40 void Game_State::distOperator() {
jh152 0:dd04a694fbdd 41 // update the distnce matrex
jh152 0:dd04a694fbdd 42 int dist_sum = 0;
jh152 0:dd04a694fbdd 43 for (int count = 0; count < sizeof(dist_raw); count++) {
jh152 0:dd04a694fbdd 44 dist_sum += dist_raw[count];
jh152 0:dd04a694fbdd 45 }
jh152 0:dd04a694fbdd 46 /*Dist_raw[DIST_READING_NUM] = (Dist_raw[DIST_READING_NUM] + 1) % DIST_READING_NUM;
jh152 0:dd04a694fbdd 47 dist_sum = -Dist_raw[Dist_raw[DIST_READING_NUM]] + Dist_pre;
jh152 0:dd04a694fbdd 48 Dist_raw[Dist_raw[DIST_READING_NUM]] = Dist_pre;*/
jh152 0:dd04a694fbdd 49
jh152 0:dd04a694fbdd 50 // judge whether too far
jh152 0:dd04a694fbdd 51 if (dist_sum >= (MAX_DIST * DIST_READING_NUM)) {
jh152 0:dd04a694fbdd 52 if (getFlags->separation == NOT_OPEN)
jh152 0:dd04a694fbdd 53 getFlags->separation = NOT_OPEN;
jh152 0:dd04a694fbdd 54 else {
jh152 0:dd04a694fbdd 55 getFlags->separation = TOO_FAR;
jh152 0:dd04a694fbdd 56 }
jh152 0:dd04a694fbdd 57 }
jh152 0:dd04a694fbdd 58 else {
jh152 0:dd04a694fbdd 59 getFlags->separation = LEGAL;
jh152 0:dd04a694fbdd 60 }
jh152 0:dd04a694fbdd 61 }
jh152 0:dd04a694fbdd 62 void Game_State::bprOperator() {
jh152 0:dd04a694fbdd 63 // Deal with data from color sensor, and give distance information
jh152 0:dd04a694fbdd 64 int r = rgb_raw[1] * 255 / rgb_raw[0];
jh152 0:dd04a694fbdd 65 int g = rgb_raw[2] * 255 / rgb_raw[0];
jh152 0:dd04a694fbdd 66 int b = rgb_raw[3] * 255 / rgb_raw[0];
jh152 0:dd04a694fbdd 67 if (r > RGB_DARK && g > RGB_DARK && b > RGB_DARK) {
jh152 0:dd04a694fbdd 68 if (r > g && r > b)
jh152 0:dd04a694fbdd 69 {
jh152 0:dd04a694fbdd 70 getFlags->bpr_mark = 'r';
jh152 0:dd04a694fbdd 71
jh152 0:dd04a694fbdd 72 }
jh152 0:dd04a694fbdd 73 if (g > r && g > b)
jh152 0:dd04a694fbdd 74 {
jh152 0:dd04a694fbdd 75 getFlags->bpr_mark = 'g';
jh152 0:dd04a694fbdd 76 }
jh152 0:dd04a694fbdd 77 if (b > r && b > g)
jh152 0:dd04a694fbdd 78 {
jh152 0:dd04a694fbdd 79 getFlags->bpr_mark = 'b';
jh152 0:dd04a694fbdd 80 }
jh152 0:dd04a694fbdd 81 }
jh152 0:dd04a694fbdd 82 else {
jh152 0:dd04a694fbdd 83 getFlags->bpr_mark = 'k';
jh152 0:dd04a694fbdd 84 }
jh152 0:dd04a694fbdd 85 }
jh152 0:dd04a694fbdd 86
jh152 0:dd04a694fbdd 87 bool Game_State::whetherContinue() {
jh152 0:dd04a694fbdd 88 bool gameGo = false;
jh152 0:dd04a694fbdd 89 if ((getFlags->hard_level != NOT_OPEN) && (getFlags->bpr_mark == 'g') && (getFlags->separation != NOT_OPEN)) {
jh152 0:dd04a694fbdd 90 gameGo = true;
jh152 0:dd04a694fbdd 91 }
jh152 0:dd04a694fbdd 92
jh152 0:dd04a694fbdd 93 return gameGo;
jh152 0:dd04a694fbdd 94 }
jh152 0:dd04a694fbdd 95
jh152 0:dd04a694fbdd 96
jh152 0:dd04a694fbdd 97 ///////////////////////////////// Class: SoltionsFinder ////////////////////////////////////////////
jh152 0:dd04a694fbdd 98 SoltionsFinder::SoltionsFinder(int i, int j, int m, int n, int k)
jh152 0:dd04a694fbdd 99 {
jh152 0:dd04a694fbdd 100 c1 = m;
jh152 0:dd04a694fbdd 101 c2 = n;
jh152 0:dd04a694fbdd 102 u1 = i;
jh152 0:dd04a694fbdd 103 u2 = j;
jh152 0:dd04a694fbdd 104 m_situation = c1 * 1000 + c2 * 100 + u1 * 10 + u2 - 1;
jh152 0:dd04a694fbdd 105 hard_level = k;
jh152 0:dd04a694fbdd 106 //m_judger = 1;
jh152 0:dd04a694fbdd 107 memset(m_pool, 0.0, sizeof(m_pool));
jh152 0:dd04a694fbdd 108 }
jh152 0:dd04a694fbdd 109
jh152 0:dd04a694fbdd 110 SoltionsFinder::~SoltionsFinder()
jh152 0:dd04a694fbdd 111 {
jh152 0:dd04a694fbdd 112 }
jh152 0:dd04a694fbdd 113
jh152 0:dd04a694fbdd 114 //float cost[10000][5];
jh152 0:dd04a694fbdd 115 void SoltionsFinder::SolPools(void)
jh152 0:dd04a694fbdd 116 {
jh152 0:dd04a694fbdd 117 int num;
jh152 0:dd04a694fbdd 118 float m_case1, m_case2, m_case3, m_case4;
jh152 0:dd04a694fbdd 119 ifstream infile; //输入流
jh152 0:dd04a694fbdd 120 ofstream outfile; //输出流
jh152 0:dd04a694fbdd 121
jh152 0:dd04a694fbdd 122 int m_count = 0;
jh152 0:dd04a694fbdd 123
jh152 0:dd04a694fbdd 124 infile.open("SolMap.txt", ios::in);
jh152 0:dd04a694fbdd 125 if (!infile.is_open())
jh152 0:dd04a694fbdd 126 cout << "Open file failure" << endl;
jh152 0:dd04a694fbdd 127 while (!infile.eof()) // 若未到文件结束一直循环
jh152 0:dd04a694fbdd 128 {
jh152 0:dd04a694fbdd 129 infile >> num >> m_case1 >> m_case2 >> m_case3 >> m_case4;
jh152 0:dd04a694fbdd 130 m_pool[m_count][0] = num;
jh152 0:dd04a694fbdd 131 m_pool[m_count][1] = m_case1;
jh152 0:dd04a694fbdd 132 m_pool[m_count][2] = m_case2;
jh152 0:dd04a694fbdd 133 m_pool[m_count][3] = m_case3;
jh152 0:dd04a694fbdd 134 m_pool[m_count][4] = m_case4;
jh152 0:dd04a694fbdd 135 ++m_count;
jh152 0:dd04a694fbdd 136
jh152 0:dd04a694fbdd 137 }
jh152 0:dd04a694fbdd 138 infile.close(); //关闭文件
jh152 0:dd04a694fbdd 139
jh152 0:dd04a694fbdd 140 }
jh152 0:dd04a694fbdd 141 int SoltionsFinder::GetSolution() {
jh152 0:dd04a694fbdd 142 ///////////////////////////////////// sort /////////////////////////////////////
jh152 0:dd04a694fbdd 143 int orderList[4] = { 1, 2, 3, 4 };
jh152 0:dd04a694fbdd 144 int orderTemp;
jh152 0:dd04a694fbdd 145 int m_result;
jh152 0:dd04a694fbdd 146 /* int pre_result;*/
jh152 0:dd04a694fbdd 147 //float tempMax = m_pool[m_situation][orderList[0]];
jh152 0:dd04a694fbdd 148
jh152 0:dd04a694fbdd 149 if (m_pool[m_situation][orderList[0]] < m_pool[m_situation][orderList[1]]) {
jh152 0:dd04a694fbdd 150 //tempMax = m_pool[m_situation][orderList[1]];
jh152 0:dd04a694fbdd 151 orderTemp = orderList[0];
jh152 0:dd04a694fbdd 152 orderList[0] = orderList[1];
jh152 0:dd04a694fbdd 153 orderList[1] = orderTemp;
jh152 0:dd04a694fbdd 154 }
jh152 0:dd04a694fbdd 155
jh152 0:dd04a694fbdd 156 if (m_pool[m_situation][orderList[0]] < m_pool[m_situation][orderList[2]]) {
jh152 0:dd04a694fbdd 157 orderTemp = orderList[0];
jh152 0:dd04a694fbdd 158 orderList[0] = orderList[2];
jh152 0:dd04a694fbdd 159 orderList[2] = orderList[1];
jh152 0:dd04a694fbdd 160 orderList[1] = orderTemp;
jh152 0:dd04a694fbdd 161 }
jh152 0:dd04a694fbdd 162 else if (m_pool[m_situation][orderList[0]] > m_pool[m_situation][orderList[2]] && m_pool[m_situation][orderList[1]] < m_pool[m_situation][orderList[2]]) {
jh152 0:dd04a694fbdd 163 orderTemp = orderList[1];
jh152 0:dd04a694fbdd 164 orderList[1] = orderList[2];
jh152 0:dd04a694fbdd 165 orderList[2] = orderTemp;
jh152 0:dd04a694fbdd 166 }
jh152 0:dd04a694fbdd 167
jh152 0:dd04a694fbdd 168 if (m_pool[m_situation][orderList[0]] < m_pool[m_situation][orderList[3]]) {
jh152 0:dd04a694fbdd 169 orderTemp = orderList[0];
jh152 0:dd04a694fbdd 170 orderList[0] = orderList[3];
jh152 0:dd04a694fbdd 171 orderList[3] = orderList[2];
jh152 0:dd04a694fbdd 172 orderList[2] = orderList[1];
jh152 0:dd04a694fbdd 173 orderList[1] = orderTemp;
jh152 0:dd04a694fbdd 174 }
jh152 0:dd04a694fbdd 175 else if (m_pool[m_situation][orderList[0]] > m_pool[m_situation][orderList[3]] && m_pool[m_situation][orderList[1]] < m_pool[m_situation][orderList[3]])
jh152 0:dd04a694fbdd 176 {
jh152 0:dd04a694fbdd 177 orderTemp = orderList[1];
jh152 0:dd04a694fbdd 178 orderList[1] = orderList[3];
jh152 0:dd04a694fbdd 179 orderList[3] = orderList[2];
jh152 0:dd04a694fbdd 180 orderList[2] = orderTemp;
jh152 0:dd04a694fbdd 181 }
jh152 0:dd04a694fbdd 182 else if (m_pool[m_situation][orderList[1]] > m_pool[m_situation][orderList[3]] && m_pool[m_situation][orderList[2]] < m_pool[m_situation][orderList[3]]) {
jh152 0:dd04a694fbdd 183 orderTemp = orderList[2];
jh152 0:dd04a694fbdd 184 orderList[2] = orderList[3];
jh152 0:dd04a694fbdd 185 orderList[3] = orderTemp;
jh152 0:dd04a694fbdd 186 }
jh152 0:dd04a694fbdd 187 // End sorting, now orderList[0] gives the code of the best solution
jh152 0:dd04a694fbdd 188 do {
jh152 0:dd04a694fbdd 189
jh152 0:dd04a694fbdd 190 switch (hard_level) {
jh152 0:dd04a694fbdd 191 case 1:
jh152 0:dd04a694fbdd 192 m_result = orderList[hard_level - 1];
jh152 0:dd04a694fbdd 193
jh152 0:dd04a694fbdd 194 break;
jh152 0:dd04a694fbdd 195 case 2:
jh152 0:dd04a694fbdd 196 srand(time(NULL));
jh152 0:dd04a694fbdd 197 if ((rand() % 3 < 2) || (m_pool[m_situation][orderList[1]] == -1)) {
jh152 0:dd04a694fbdd 198 m_result = orderList[hard_level - 2];
jh152 0:dd04a694fbdd 199 }
jh152 0:dd04a694fbdd 200 else {
jh152 0:dd04a694fbdd 201 m_result = orderList[hard_level - 1];
jh152 0:dd04a694fbdd 202 }
jh152 0:dd04a694fbdd 203
jh152 0:dd04a694fbdd 204 break;
jh152 0:dd04a694fbdd 205 case 3:
jh152 0:dd04a694fbdd 206 srand(time(NULL));
jh152 0:dd04a694fbdd 207 if ((rand() % 3 == 1) || (m_pool[m_situation][orderList[1]] == -1)) {
jh152 0:dd04a694fbdd 208 m_result = orderList[hard_level - 3];
jh152 0:dd04a694fbdd 209 }
jh152 0:dd04a694fbdd 210 else if ((rand() % 3 == 0) || (m_pool[m_situation][orderList[2]] == -1)) {
jh152 0:dd04a694fbdd 211 m_result = orderList[hard_level - 2];
jh152 0:dd04a694fbdd 212 }
jh152 0:dd04a694fbdd 213 else {
jh152 0:dd04a694fbdd 214 m_result = orderList[hard_level - 1];
jh152 0:dd04a694fbdd 215 }
jh152 0:dd04a694fbdd 216 break;
jh152 0:dd04a694fbdd 217 default:
jh152 0:dd04a694fbdd 218 break;
jh152 0:dd04a694fbdd 219 }
jh152 0:dd04a694fbdd 220 } while (m_pool[m_situation][m_result] == -1);
jh152 0:dd04a694fbdd 221 m_result += 3;
jh152 0:dd04a694fbdd 222 return m_result;
jh152 0:dd04a694fbdd 223 }