a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
1#include "memory_functions_comito.h" 2 3using namespace std; 4 5void createMove(int row, int col, string &move); 6int moveCheck(int row, int col, const ComputerMemory &memory); 7void directionChecks(int &nextRow, int &nextCol, int dir, int offset, const ComputerMemory &memory); 8int DirFlip(int currDir); 9 10// initMemory initializes the memory; at the outset of the game the grid of 11// shots taken is empty, we've not hit any ships, and our player can only apply 12// a general, somewhat random firing strategy until we get a hit on some ship 13void initMemorycomito(ComputerMemory &memory) { 14 memory.mode = RANDOM; 15 memory.hitRow = -1; 16 memory.hitCol = -1; 17 memory.hitShip = NONE; 18 memory.fireDir = NONE; 19 memory.fireDist = 1; 20 memory.lastResult = NONE; 21 22 for (int i = 0; i < BOARDSIZE; i++) { 23 for (int j = 0; j < BOARDSIZE; j++) { 24 memory.grid[i][j] = EMPTY_MARKER; 25 } 26 } 27} 28 29// complete this function so it produces a "smart" move based on the information 30// which appears in the computer's memory 31string smartMovecomito(const ComputerMemory &memory) { 32 string move; 33 int checkResult = -1; 34 int nextRow = -1; 35 int nextCol = -1; 36 if (memory.mode == SEARCH) 37 { 38 int i = 0; 39 while (i <= 4 && checkResult != 1) 40 { 41 42 switch (memory.fireDir) 43 { 44 case 1: 45 nextRow = memory.hitRow - 1; 46 nextCol = memory.hitCol; 47 break; 48 case 2: 49 nextRow = memory.hitRow + 1; 50 nextCol = memory.hitCol; 51 break; 52 case 3: 53 nextCol = memory.hitCol - 1; 54 nextRow = memory.hitRow; 55 break; 56 case 4: 57 nextCol = memory.hitCol + 1; 58 nextRow = memory.hitRow; 59 break; 60 } 61 i++; 62 checkResult = moveCheck(nextRow, nextCol, memory); 63 } 64 } 65 if (memory.mode == DESTROY) 66 { 67 switch (memory.fireDir) 68 { 69 case 1: 70 nextRow = memory.hitRow - memory.fireDist; 71 nextCol = memory.hitCol; 72 break; 73 case 2: 74 nextRow = memory.hitRow + memory.fireDist; 75 nextCol = memory.hitCol; 76 break; 77 case 3: 78 nextCol = memory.hitCol - memory.fireDist; 79 nextRow = memory.hitRow; 80 break; 81 case 4: 82 nextCol = memory.hitCol + memory.fireDist; 83 nextRow = memory.hitRow; 84 break; 85 } 86 } 87 createMove(nextRow, nextCol, move); 88 debug(move); 89 return move; 90} 91 92void updateMemorycomito(int row, int col, int result, ComputerMemory &memory) { 93 94 int moveRes = result / 10; 95 int hitShipId = result % 10; 96 97 if (memory.mode == RANDOM) //if random 98 { 99 if (result == 0) //if missed 100 { 101 memory.fireDir = 0; 102 } 103 else //if hit 104 { 105 memory.hitShip = isShip(result); 106 memory.hitRow = row; 107 memory.hitCol = col; 108 memory.mode = SEARCH; 109 memory.fireDir++; 110 } 111 } 112 else if (memory.mode == SEARCH) //if search 113 { 114 if (result == 0) //if missed 115 { 116 memory.fireDir++; 117 if (memory.fireDir > 4) 118 { 119 memory.mode = RANDOM; 120 memory.fireDist = 1; 121 } 122 } 123 else 124 { 125 memory.mode = DESTROY; 126 memory.fireDist++; 127 } 128 }else //if destroy 129 { 130 int i = BOARDSIZE; 131 int nextRow = -1; 132 int nextCol = -1; 133 int checkResult = -1; 134 while (checkResult != 1 && i > 0) 135 { 136 switch (memory.fireDir) 137 { 138 case 1: 139 nextRow = memory.hitRow - memory.fireDist; 140 nextCol = memory.hitCol; 141 break; 142 case 2: 143 nextRow = memory.hitRow + memory.fireDist; 144 nextCol = memory.hitCol; 145 break; 146 case 3: 147 nextCol = memory.hitCol - memory.fireDist; 148 nextRow = memory.hitRow; 149 break; 150 case 4: 151 nextCol = memory.hitCol + memory.fireDist; 152 nextRow = memory.hitRow; 153 break; 154 } 155 checkResult = moveCheck(nextRow, nextCol, memory); 156 157 switch (checkResult) 158 { 159 case 0: 160 memory.fireDir = DirFlip(memory.fireDir); 161 memory.fireDist = 1; 162 break; 163 case 1: 164 //check if should fire here and if yes fire 165 if (moveRes != 0) 166 { 167 168 } 169 break; 170 case 2: 171 memory.fireDist += 1; 172 break; 173 case 3: 174 memory.fireDir = DirFlip(memory.fireDir); 175 memory.fireDist = 1; 176 break; 177 } 178 i--; 179 } 180 if (i < 1) 181 { 182 memory.mode = RANDOM; 183 } 184 } 185 //update memory grid 186 if (result == 0) 187 { 188 memory.grid[row][col] = MISS_MARKER; 189 } 190 if (result == 1) 191 { 192 memory.grid[row][col] = HIT_MARKER; 193 } 194} 195 196//I added this function to call within the move so that 197//I can input the row number and just get the letter out 198//I didn't know how best to do this so I did whatever this is :/ 199void createMove(int row, int col, string &move) 200{ 201 char letter = 'A'; 202 letter += row; 203 move.push_back(letter); 204 string number = to_string(col + 1); 205 move.append(number); 206} 207 208int moveCheck(int row, int col, const ComputerMemory &memory) 209{ 210 int success = 0; 211 bool isMovePlayedYet = false; 212 213 if (row < 0 || row >= BOARDSIZE || col < 0 || col >= BOARDSIZE) 214 { 215 success = false; 216 } 217 else 218 { 219 if(memory.grid[row][col] == EMPTY_MARKER) 220 { 221 success = 1; 222 }else if (memory.grid[row][col] == HIT_MARKER) 223 { 224 success = 2; 225 }else 226 { 227 success = 3; 228 } 229 } 230 return success; 231} 232int DirFlip(int currDir) 233{ 234 if (currDir == 1) 235 { 236 return 2; 237 } 238 if (currDir == 3) 239 { 240 return 4; 241 } 242 return currDir; 243} 244/* attempted function graveyard 245{ 246 switch (dir) 247 { 248 case 1: 249 nextRow = memory.hitRow - offset; 250 nextCol = memory.hitCol; 251 break; 252 case 2: 253 nextRow = memory.hitRow + offset; 254 nextCol = memory.hitCol; 255 break; 256 case 3: 257 nextCol = memory.hitCol - offset; 258 nextRow = memory.hitRow; 259 break; 260 case 4: 261 nextCol = memory.hitCol + offset; 262 nextRow = memory.hitRow; 263 break; 264 } 265} 266*/