a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: remove systemd properties incompatible with scope units

+1581 -5
+266
comito@0.0.0.0
··· 1 + #include "memory_functions_comito.h" 2 + 3 + using namespace std; 4 + 5 + void createMove(int row, int col, string &move); 6 + int moveCheck(int row, int col, const ComputerMemory &memory); 7 + void directionChecks(int &nextRow, int &nextCol, int dir, int offset, const ComputerMemory &memory); 8 + int 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 13 + void 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 31 + string 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 + 92 + void 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 :/ 199 + void 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 + 208 + int 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 + } 232 + int 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 + */
+266
comito@localhost
··· 1 + #include "memory_functions_comito.h" 2 + 3 + using namespace std; 4 + 5 + void createMove(int row, int col, string &move); 6 + int moveCheck(int row, int col, const ComputerMemory &memory); 7 + void directionChecks(int &nextRow, int &nextCol, int dir, int offset, const ComputerMemory &memory); 8 + int 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 13 + void 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 31 + string 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 + 92 + void 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 :/ 199 + void 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 + 208 + int 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 + } 232 + int 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 + */
+3 -5
internal/runner/runner.go
··· 32 32 defer cancel() 33 33 34 34 // Build systemd-run command with security properties 35 + // Note: PrivateNetwork, PrivateTmp, ProtectHome, ProtectSystem, NoNewPrivileges 36 + // are only available for service units, not scope units. 37 + // Scope units only support resource limits (Memory, CPU, Tasks) 35 38 systemdArgs := []string{ 36 39 "--scope", // Create transient scope unit 37 40 "--quiet", // Suppress systemd output ··· 39 42 "--property=MemoryMax=512M", // Max 512MB RAM 40 43 "--property=CPUQuota=200%", // Max 2 CPU cores worth 41 44 "--property=TasksMax=50", // Max 50 processes/threads 42 - "--property=PrivateNetwork=true", // Isolate network (no internet) 43 - "--property=PrivateTmp=true", // Private /tmp 44 - "--property=ProtectHome=true", // Make /home inaccessible 45 - "--property=ProtectSystem=strict", // Read-only /usr, /boot, /etc 46 - "--property=NoNewPrivileges=true", // Prevent privilege escalation 47 45 "--", 48 46 } 49 47 systemdArgs = append(systemdArgs, args...)
+266
remote-submissions/comito/memory_functions_comito.cpp
··· 1 + #include "memory_functions_comito.h" 2 + 3 + using namespace std; 4 + 5 + void createMove(int row, int col, string &move); 6 + int moveCheck(int row, int col, const ComputerMemory &memory); 7 + void directionChecks(int &nextRow, int &nextCol, int dir, int offset, const ComputerMemory &memory); 8 + int 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 13 + void 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 31 + string 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 + 92 + void 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 :/ 199 + void 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 + 208 + int 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 + } 232 + int 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 + */
+476
remote-submissions/klukas/memory_functions_klukas.cpp
··· 1 + #include "memory_functions_klukas.h" 2 + #include "battleship.h" 3 + #include "kasbs.h" 4 + #include "memory.h" 5 + #include <string> 6 + #include <vector> 7 + 8 + using namespace std; 9 + 10 + // extra helpers 11 + inline bool onBoard(int row, int col) { 12 + return row >= 0 && row < BOARDSIZE && col >= 0 && col < BOARDSIZE; 13 + } 14 + 15 + inline void nextDelta(int dir, int &deltaRow, int &deltaCol) { 16 + if (dir == NORTH) { deltaRow = -1; deltaCol = 0; } 17 + else if (dir == EAST) { deltaRow = 0; deltaCol = 1; } 18 + else if (dir == SOUTH) { deltaRow = 1; deltaCol = 0; } 19 + else if (dir == WEST) { deltaRow = 0; deltaCol = -1; } 20 + else if (dir == NONE) {deltaRow = 0; deltaCol = 0; } 21 + } 22 + 23 + inline int getOppositeDirection(int dir) { 24 + if (dir == NORTH) return SOUTH; 25 + if (dir == EAST) return WEST; 26 + if (dir == SOUTH) return NORTH; 27 + if (dir == WEST) return EAST; 28 + else return NONE; 29 + } 30 + 31 + inline bool canStepFrom(int row, int col, int dir) { 32 + int deltaRow, deltaCol; 33 + nextDelta(dir, deltaRow, deltaCol); 34 + int newRow = row + deltaRow; 35 + int newCol = col + deltaCol; 36 + return onBoard(newRow, newCol); 37 + } 38 + 39 + inline string formatMove(int row, int col) { 40 + char letter = static_cast<char>('A' + row); // 0 -> 'A', 1 -> 'B', ... 41 + return string(1, letter) + to_string(col + 1); 42 + } 43 + 44 + inline int nextValidDir(int row, int col, const ComputerMemory &memory, int currentDir) { 45 + int ordered[4] = { NORTH, EAST, SOUTH, WEST }; 46 + int curIdx = -1; 47 + for (int i = 0; i < 4; ++i) { 48 + if (ordered[i] == currentDir) { 49 + curIdx = i; break; 50 + } 51 + } 52 + 53 + for (int step = 1; step <= 4; ++step) { 54 + int idx = (curIdx + step) % 4; 55 + int d = ordered[idx]; 56 + int dr=0, dc=0; nextDelta(d, dr, dc); 57 + int newRow = row + dr; 58 + int newCol = col + dc; 59 + if (!onBoard(newRow, newCol)) continue; 60 + if (memory.grid[newRow][newCol] != EMPTY_MARKER) continue; 61 + return d; 62 + } 63 + 64 + return NONE; 65 + } 66 + 67 + // Calculate probability that a cell could contain a ship 68 + // Based on how many different ship placements include this cell 69 + inline int calculateCellProbability(int row, int col, const ComputerMemory &memory) { 70 + if (memory.grid[row][col] != EMPTY_MARKER) { 71 + return 0; // Already shot 72 + } 73 + 74 + int probability = 0; 75 + int shipSizes[] = {AC_SIZE, BS_SIZE, CR_SIZE, SB_SIZE, DS_SIZE}; 76 + 77 + // For each ship size, count how many valid placements include this cell 78 + for (int s = 0; s < 5; s++) { 79 + int shipSize = shipSizes[s]; 80 + 81 + // Check horizontal placements 82 + for (int startCol = col - shipSize + 1; startCol <= col; startCol++) { 83 + if (startCol < 0 || startCol + shipSize > BOARDSIZE) continue; 84 + 85 + bool valid = true; 86 + for (int c = startCol; c < startCol + shipSize; c++) { 87 + if (memory.grid[row][c] == MISS_MARKER || memory.grid[row][c] == SUNK_MARKER) { 88 + valid = false; 89 + break; 90 + } 91 + } 92 + if (valid) probability++; 93 + } 94 + 95 + // Check vertical placements 96 + for (int startRow = row - shipSize + 1; startRow <= row; startRow++) { 97 + if (startRow < 0 || startRow + shipSize > BOARDSIZE) continue; 98 + 99 + bool valid = true; 100 + for (int r = startRow; r < startRow + shipSize; r++) { 101 + if (memory.grid[r][col] == MISS_MARKER || memory.grid[r][col] == SUNK_MARKER) { 102 + valid = false; 103 + break; 104 + } 105 + } 106 + if (valid) probability++; 107 + } 108 + } 109 + 110 + return probability; 111 + } 112 + 113 + // We do the vector thing because we could otherwise run into an 114 + // infinite loop that is more and more likely the closer we get to cells being full 115 + inline string getSmartRandomMove(const ComputerMemory &memory) { 116 + vector<pair<int, int>> emptyCells; 117 + 118 + // First pass: checkerboard pattern (parity) - most efficient 119 + for (int i = 0; i < BOARDSIZE; i++) { 120 + for (int j = 0; j < BOARDSIZE; j++) { 121 + if (memory.grid[i][j] == EMPTY_MARKER && (i + j) % 2 == 0) { 122 + emptyCells.push_back({i, j}); 123 + } 124 + } 125 + } 126 + 127 + // Second pass: if no parity cells, use any empty cell 128 + if (emptyCells.empty()) { 129 + for (int i = 0; i < BOARDSIZE; i++) { 130 + for (int j = 0; j < BOARDSIZE; j++) { 131 + if (memory.grid[i][j] == EMPTY_MARKER) { 132 + emptyCells.push_back({i, j}); 133 + } 134 + } 135 + } 136 + } 137 + 138 + // If no empty cells, return empty string 139 + if (emptyCells.empty()) { 140 + return ""; 141 + } 142 + 143 + // Use probability density to weight the choice 144 + // Calculate probability for each candidate cell 145 + vector<int> probabilities; 146 + int maxProb = 0; 147 + 148 + for (const auto& cell : emptyCells) { 149 + int prob = calculateCellProbability(cell.first, cell.second, memory); 150 + probabilities.push_back(prob); 151 + if (prob > maxProb) maxProb = prob; 152 + } 153 + 154 + // If we have probability data, prefer high-probability cells 155 + if (maxProb > 0) { 156 + // Collect only the highest probability cells 157 + vector<pair<int, int>> highProbCells; 158 + for (size_t i = 0; i < emptyCells.size(); i++) { 159 + if (probabilities[i] == maxProb) { 160 + highProbCells.push_back(emptyCells[i]); 161 + } 162 + } 163 + 164 + if (!highProbCells.empty()) { 165 + int randomIndex = rand() % highProbCells.size(); 166 + return formatMove(highProbCells[randomIndex].first, highProbCells[randomIndex].second); 167 + } 168 + } 169 + 170 + // Fallback: pick random from all candidates 171 + int randomIndex = rand() % emptyCells.size(); 172 + return formatMove(emptyCells[randomIndex].first, emptyCells[randomIndex].second); 173 + } 174 + 175 + // initMemory initializes the memory; at the outset of the game the grid of 176 + // shots taken is empty, we've not hit any ships. Our player can only apply 177 + // a general, somewhat random firing strategy until we get a hit on some ship. 178 + void initMemoryKlukas(ComputerMemory &memory) { 179 + memory.mode = RANDOM; 180 + memory.hitRow = -1; 181 + memory.hitCol = -1; 182 + memory.hitShip = NONE; 183 + memory.fireDir = NONE; 184 + memory.fireDist = 1; 185 + memory.lastResult = NONE; 186 + 187 + for (int i = 0; i < BOARDSIZE; i++) { 188 + for (int j = 0; j < BOARDSIZE; j++) { 189 + memory.grid[i][j] = EMPTY_MARKER; 190 + } 191 + } 192 + } 193 + 194 + // Complete this function so it produces a "smart" move based on the information 195 + // which appears in the computer's memory 196 + string smartMoveKlukas(const ComputerMemory &memory) { 197 + if (memory.mode == RANDOM) { 198 + return getSmartRandomMove(memory); 199 + } 200 + 201 + int deltaRow = 0, deltaCol = 0; 202 + nextDelta(memory.fireDir, deltaRow, deltaCol); 203 + 204 + int row = memory.hitRow + deltaRow * memory.fireDist; 205 + int col = memory.hitCol + deltaCol * memory.fireDist; 206 + 207 + // Guard against off-board and reused shots 208 + if (!onBoard(row, col)) { 209 + debug("*** GUARD TRIPPED *** OFFBOARD: row=" + to_string(row) + " col=" + to_string(col) + 210 + " | hitRow=" + to_string(memory.hitRow) + " hitCol=" + to_string(memory.hitCol) + 211 + " | dir=" + to_string(memory.fireDir) + " dist=" + to_string(memory.fireDist) + 212 + " | mode=" + to_string(memory.mode) + " | dR=" + to_string(deltaRow) + " dC=" + to_string(deltaCol)); 213 + return getSmartRandomMove(memory); 214 + } 215 + if (memory.grid[row][col] != EMPTY_MARKER) { 216 + debug("*** GUARD TRIPPED *** ALREADY FIRED: " + formatMove(row, col) + " (marker='" + string(1, memory.grid[row][col]) + "')" + 217 + " | hitRow=" + to_string(memory.hitRow) + " hitCol=" + to_string(memory.hitCol) + 218 + " | dir=" + to_string(memory.fireDir) + " dist=" + to_string(memory.fireDist) + 219 + " | mode=" + to_string(memory.mode) + " lastResult=" + to_string(memory.lastResult) + 220 + " | dR=" + to_string(deltaRow) + " dC=" + to_string(deltaCol)); 221 + return getSmartRandomMove(memory); 222 + } 223 + 224 + return formatMove(row, col); 225 + } 226 + 227 + // Complete this function so it updates the computer's memory based on the 228 + // result of the last shot at location (row, col) 229 + void updateMemoryKlukas(int row, int col, int result, ComputerMemory &memory) { 230 + memory.lastResult = result; 231 + char marker; 232 + if (isAMiss(result)) marker = MISS_MARKER; 233 + else marker = HIT_MARKER; 234 + memory.grid[row][col] = marker; 235 + 236 + if (memory.mode == RANDOM) { 237 + if (!isAMiss(result)) { 238 + // Pick first available direction that hasn't been tried and is in bounds 239 + int firstDir = nextValidDir(row, col, memory, NONE); 240 + 241 + if (firstDir != NONE) { 242 + memory.mode = SEARCH; 243 + memory.hitRow = row; 244 + memory.hitCol = col; 245 + 246 + memory.fireDir = firstDir; 247 + memory.fireDist = 1; 248 + } else { 249 + // This would only be possible if its one of the last pieces to play 250 + // it sinks a ship which is very unlikely 251 + } 252 + } 253 + 254 + return; 255 + } 256 + 257 + if (memory.mode == SEARCH) { 258 + if (isAMiss(result)) { 259 + int nextDir = nextValidDir(memory.hitRow, memory.hitCol, memory, memory.fireDir); 260 + 261 + if (nextDir != NONE) { 262 + memory.fireDir = nextDir; 263 + memory.fireDist = 1; 264 + } else { 265 + memory.mode = RANDOM; 266 + memory.fireDir = NONE; 267 + memory.fireDist = 1; 268 + } 269 + 270 + return; 271 + } 272 + 273 + if (isASunk(result)) { 274 + memory.mode = RANDOM; 275 + memory.fireDir = NONE; 276 + memory.hitRow = -1; 277 + memory.hitCol = -1; 278 + memory.fireDist = 1; 279 + 280 + return; 281 + } 282 + 283 + if (!isAMiss(result)) { 284 + debug("SEARCH->DESTROY: Got 2nd hit at " + formatMove(row, col) + 285 + " | anchor=(" + to_string(memory.hitRow) + "," + to_string(memory.hitCol) + 286 + ") dir=" + to_string(memory.fireDir)); 287 + memory.mode = DESTROY; 288 + memory.fireDist = 2; // We want to check beyond the search 289 + 290 + int deltaRow = 0, deltaCol = 0; 291 + nextDelta(memory.fireDir, deltaRow, deltaCol); 292 + 293 + int nextRow = memory.hitRow + deltaRow * memory.fireDist; 294 + int nextCol = memory.hitCol + deltaCol * memory.fireDist; 295 + 296 + if (onBoard(nextRow, nextCol) && memory.grid[nextRow][nextCol] == MISS_MARKER) { 297 + int opposite = getOppositeDirection(memory.fireDir); 298 + 299 + // if we haven't checked this spot yet in search mode 300 + if (opposite > memory.fireDir) { 301 + memory.fireDist = 1; 302 + } 303 + 304 + memory.fireDir = opposite; 305 + 306 + // Skip over any hits in the opposite direction 307 + int oppDr = 0, oppDc = 0; 308 + nextDelta(opposite, oppDr, oppDc); 309 + int oppDist = memory.fireDist; 310 + int oppRow = memory.hitRow + oppDr * oppDist; 311 + int oppCol = memory.hitCol + oppDc * oppDist; 312 + 313 + while (onBoard(oppRow, oppCol) && memory.grid[oppRow][oppCol] == HIT_MARKER) { 314 + oppDist++; 315 + oppRow = memory.hitRow + oppDr * oppDist; 316 + oppCol = memory.hitCol + oppDc * oppDist; 317 + } 318 + 319 + // Validate the final position before committing to it 320 + if (opposite != NONE && onBoard(oppRow, oppCol) && memory.grid[oppRow][oppCol] == EMPTY_MARKER) { 321 + memory.fireDist = oppDist; 322 + } else { 323 + // Can't fire in opposite direction either, go back to random 324 + memory.mode = RANDOM; 325 + memory.fireDir = NONE; 326 + memory.fireDist = 1; 327 + } 328 + } else { 329 + // Continue in same direction - start at distance 2 (we just hit at distance 1) 330 + memory.fireDist = 2; 331 + 332 + // Skip over any additional hits we may have already made in this direction 333 + int checkRow = memory.hitRow + deltaRow * memory.fireDist; 334 + int checkCol = memory.hitCol + deltaCol * memory.fireDist; 335 + 336 + while (onBoard(checkRow, checkCol) && memory.grid[checkRow][checkCol] == HIT_MARKER) { 337 + memory.fireDist++; 338 + checkRow = memory.hitRow + deltaRow * memory.fireDist; 339 + checkCol = memory.hitCol + deltaCol * memory.fireDist; 340 + } 341 + 342 + // If we ended up offboard or at a non-empty cell, try opposite direction 343 + if (!onBoard(checkRow, checkCol) || memory.grid[checkRow][checkCol] != EMPTY_MARKER) { 344 + int opposite = getOppositeDirection(memory.fireDir); 345 + int oppDr = 0, oppDc = 0; 346 + nextDelta(opposite, oppDr, oppDc); 347 + 348 + // Find the first empty cell in the opposite direction 349 + int oppDist = 1; 350 + int oppRow = memory.hitRow + oppDr * oppDist; 351 + int oppCol = memory.hitCol + oppDc * oppDist; 352 + 353 + // Skip over any hits in the opposite direction 354 + while (onBoard(oppRow, oppCol) && memory.grid[oppRow][oppCol] == HIT_MARKER) { 355 + oppDist++; 356 + oppRow = memory.hitRow + oppDr * oppDist; 357 + oppCol = memory.hitCol + oppDc * oppDist; 358 + } 359 + 360 + if (opposite != NONE && onBoard(oppRow, oppCol) && memory.grid[oppRow][oppCol] == EMPTY_MARKER) { 361 + memory.fireDir = opposite; 362 + memory.fireDist = oppDist; 363 + } else { 364 + // Can't fire in either direction, go back to random 365 + memory.mode = RANDOM; 366 + memory.fireDir = NONE; 367 + memory.fireDist = 1; 368 + } 369 + } 370 + } 371 + 372 + return; 373 + } 374 + } 375 + 376 + if (memory.mode == DESTROY) { 377 + if (isASunk(result)) { 378 + memory.mode = RANDOM; 379 + memory.fireDir = NONE; 380 + memory.hitRow = -1; 381 + memory.hitCol = -1; 382 + memory.fireDist = 1; 383 + 384 + return; 385 + } 386 + 387 + if (!isAMiss(result)) { 388 + debug("DESTROY: Got hit at " + formatMove(row, col) + 389 + " | fireDist " + to_string(memory.fireDist) + "->" + to_string(memory.fireDist + 1)); 390 + memory.fireDist++; 391 + 392 + int deltaRow, deltaCol; 393 + nextDelta(memory.fireDir, deltaRow, deltaCol); 394 + int nextRow = memory.hitRow + deltaRow * memory.fireDist; 395 + int nextCol = memory.hitCol + deltaCol * memory.fireDist; 396 + 397 + // Skip over cells we've already hit 398 + while (onBoard(nextRow, nextCol) && 399 + memory.grid[nextRow][nextCol] == HIT_MARKER) { 400 + memory.fireDist++; 401 + nextRow = memory.hitRow + deltaRow * memory.fireDist; 402 + nextCol = memory.hitCol + deltaCol * memory.fireDist; 403 + } 404 + 405 + // After skipping, check if we have a valid position 406 + if (!onBoard(nextRow, nextCol) || 407 + memory.grid[nextRow][nextCol] != EMPTY_MARKER) { 408 + // Can't continue in this direction, try opposite 409 + int opposite = getOppositeDirection(memory.fireDir); 410 + int oppDr = 0, oppDc = 0; 411 + nextDelta(opposite, oppDr, oppDc); 412 + 413 + // Find the first empty cell in the opposite direction 414 + int oppDist = 1; 415 + int oppRow = memory.hitRow + oppDr * oppDist; 416 + int oppCol = memory.hitCol + oppDc * oppDist; 417 + 418 + // Skip over any hits in the opposite direction 419 + while (onBoard(oppRow, oppCol) && memory.grid[oppRow][oppCol] == HIT_MARKER) { 420 + oppDist++; 421 + oppRow = memory.hitRow + oppDr * oppDist; 422 + oppCol = memory.hitCol + oppDc * oppDist; 423 + } 424 + 425 + if (opposite != NONE && onBoard(oppRow, oppCol) && 426 + memory.grid[oppRow][oppCol] == EMPTY_MARKER 427 + ) { 428 + debug("DESTROY: Switching dir from " + to_string(memory.fireDir) + " to " + to_string(opposite) + 429 + " at dist=" + to_string(oppDist) + " to fire at " + formatMove(oppRow, oppCol)); 430 + memory.fireDir = opposite; 431 + memory.fireDist = oppDist; 432 + } else { 433 + memory.mode = RANDOM; 434 + memory.fireDir = NONE; 435 + memory.fireDist = 1; 436 + } 437 + } 438 + 439 + return; 440 + } 441 + 442 + if (isAMiss(result)) { 443 + int opposite = getOppositeDirection(memory.fireDir); 444 + int oppDr = 0, oppDc = 0; 445 + nextDelta(opposite, oppDr, oppDc); 446 + 447 + // Find the first empty cell in the opposite direction 448 + int oppDist = 1; 449 + int firstOppRow = memory.hitRow + oppDr * oppDist; 450 + int firstOppCol = memory.hitCol + oppDc * oppDist; 451 + 452 + // Skip over any hits in the opposite direction 453 + while (onBoard(firstOppRow, firstOppCol) && memory.grid[firstOppRow][firstOppCol] == HIT_MARKER) { 454 + oppDist++; 455 + firstOppRow = memory.hitRow + oppDr * oppDist; 456 + firstOppCol = memory.hitCol + oppDc * oppDist; 457 + } 458 + 459 + if (opposite != NONE && onBoard(firstOppRow, firstOppCol) && 460 + memory.grid[firstOppRow][firstOppCol] == EMPTY_MARKER 461 + ) { 462 + debug("DESTROY after MISS: Switching dir from " + to_string(memory.fireDir) + " to " + to_string(opposite) + 463 + " at dist=" + to_string(oppDist) + " to fire at " + formatMove(firstOppRow, firstOppCol)); 464 + memory.fireDir = opposite; 465 + memory.fireDist = oppDist; 466 + } else { 467 + memory.mode = RANDOM; 468 + memory.fireDir = NONE; 469 + memory.fireDist = 1; 470 + } 471 + 472 + return; 473 + } 474 + } 475 + } 476 +
+170
remote-submissions/premkum/memory_functions_premkum.cpp
··· 1 + #include "memory_functions_premkum.h" 2 + 3 + using namespace std; 4 + 5 + //these take the move of the smart computer 6 + int lastRow; 7 + int lastCol; 8 + 9 + // initMemory initializes the memory; at the outset of the game the grid of 10 + // shots taken is empty, we've not hit any ships, and our player can only apply 11 + // a general, somewhat random firing strategy until we get a hit on some ship 12 + void initMemoryPremkum(ComputerMemory &memory) { 13 + memory.mode = RANDOM; 14 + memory.hitRow = -1; 15 + memory.hitCol = -1; 16 + memory.hitShip = NONE; 17 + memory.fireDir = NONE; 18 + memory.fireDist = 1; 19 + memory.lastResult = NONE; 20 + 21 + for (int i = 0; i < BOARDSIZE; i++) { 22 + for (int j = 0; j < BOARDSIZE; j++) { 23 + memory.grid[i][j] = EMPTY_MARKER; 24 + } 25 + } 26 + } 27 + 28 + //function that returns the row of the alphabet based on the integer recieved 29 + string getRow(int row){ 30 + string returnRow = "A"; 31 + 32 + if(row <= 1){ 33 + returnRow = "A"; 34 + } 35 + if(row == 2){ 36 + returnRow = "B"; 37 + } 38 + if(row == 3){ 39 + returnRow = "C"; 40 + } 41 + if(row == 4){ 42 + returnRow = "D"; 43 + } 44 + if(row == 5){ 45 + returnRow = "E"; 46 + } 47 + if(row == 6){ 48 + returnRow = "F"; 49 + } 50 + if(row == 7){ 51 + returnRow = "G"; 52 + } 53 + if(row == 8){ 54 + returnRow = "H"; 55 + } 56 + if(row == 9){ 57 + returnRow = "I"; 58 + } 59 + if(row >= 10){ 60 + returnRow = "J"; 61 + } 62 + 63 + return returnRow; 64 + } 65 + 66 + // complete this function so it produces a "smart" move based on the information 67 + // which appears in the computer's memory 68 + string smartMovePremkum(const ComputerMemory &memory) { 69 + string move; 70 + 71 + if(memory.mode == RANDOM){ 72 + move = randomMove(); 73 + } 74 + 75 + if(memory.mode == SEARCH||memory.mode == DESTROY){ 76 + if(memory.fireDir == NONE || memory.fireDir == WEST){ 77 + move = getRow(memory.hitRow - 1) + to_string(memory.hitCol); 78 + } 79 + if(memory.fireDir == NORTH){ 80 + move = getRow(memory.hitRow) + to_string(memory.hitCol + 1); 81 + } 82 + if(memory.fireDir == EAST){ 83 + move = getRow(memory.hitRow + 1) + to_string(memory.hitCol); 84 + } 85 + if(memory.fireDir == SOUTH){ 86 + move = getRow(memory.hitRow) + to_string(memory.hitCol - 1); 87 + } 88 + } 89 + 90 + lastRow = int(move[0]) - 64; 91 + lastCol = stoi(move.substr(1, 2)); 92 + 93 + return move; 94 + } 95 + 96 + 97 + // complete this function so it updates the computer's memory based on the 98 + // result of the last shot at location (row, col) 99 + void updateMemoryPremkum(int row, int col, int result, ComputerMemory &memory) { 100 + 101 + if (memory.mode == RANDOM){ 102 + if(isAHit(result)) { 103 + memory.hitRow = lastRow; 104 + memory.hitCol = lastCol; 105 + memory.mode = SEARCH; 106 + memory.fireDir = 0; 107 + } 108 + } 109 + else if (memory.mode == SEARCH){ 110 + if(isAHit(result)){ 111 + memory.hitRow = lastRow; 112 + memory.hitCol = lastCol; 113 + if(isASunk(result)){ 114 + memory.mode = RANDOM; 115 + memory.fireDir = 0; 116 + } 117 + else{ 118 + memory.mode = DESTROY; 119 + } 120 + } 121 + else{ 122 + if(memory.fireDir <= 3){ 123 + memory.fireDir = memory.fireDir + 1; 124 + } 125 + else{ 126 + memory.fireDir = 1; 127 + } 128 + } 129 + } 130 + else { 131 + if(isASunk(result)){ 132 + memory.mode = RANDOM; 133 + memory.fireDir = 0; 134 + } 135 + else{ 136 + if(isAMiss(result)){ 137 + if(memory.fireDir == 1){ 138 + memory.fireDir = 2; 139 + } 140 + else if(memory.fireDir == 2){ 141 + memory.fireDir = 1; 142 + } 143 + else if(memory.fireDir == 3){ 144 + memory.fireDir = 4; 145 + } 146 + else if(memory.fireDir == 4){ 147 + memory.fireDir = 3; 148 + } 149 + 150 + } 151 + else{ 152 + if(memory.hitCol == 10 && memory.fireDir == 3){ 153 + memory.fireDir = 4; 154 + } 155 + if(memory.hitRow == 10 && memory.fireDir == 2){ 156 + memory.fireDir = 1; 157 + } 158 + if(memory.hitCol == 1 && memory.fireDir == 4){ 159 + memory.fireDir = 3; 160 + } 161 + if(memory.hitRow == 1 && memory.fireDir == 1){ 162 + memory.fireDir = 2; 163 + } 164 + } 165 + } 166 + } 167 + 168 + 169 + 170 + }
+134
remote-submissions/walther/memory_functions_walther.cpp
··· 1 + #include "memory_functions_walther.h" 2 + 3 + using namespace std; 4 + 5 + int shipCurrent; 6 + 7 + // initMemory initializes the memory; at the outset of the game the grid of 8 + // shots taken is empty, we've not hit any ships, and our player can only apply 9 + // a general, somewhat random firing strategy until we get a hit on some ship 10 + void initMemorywalther(ComputerMemory &memory) { 11 + memory.mode = RANDOM; 12 + memory.hitRow = -1; 13 + memory.hitCol = -1; 14 + memory.hitShip = NONE; 15 + memory.fireDir = NONE; 16 + memory.fireDist = 1; 17 + memory.lastResult = NONE; 18 + 19 + for (int i = 0; i < BOARDSIZE; i++) { 20 + for (int j = 0; j < BOARDSIZE; j++) { 21 + memory.grid[i][j] = EMPTY_MARKER; 22 + } 23 + } 24 + } 25 + 26 + // complete this function so it produces a "smart" move based on the information 27 + // which appears in the computer's memory 28 + string smartMovewalther(const ComputerMemory &memory) { 29 + string move; 30 + int tempRow = memory.hitRow; 31 + int tempCol = memory.hitCol; 32 + 33 + if (memory.mode == RANDOM) { 34 + move = randomMove(); 35 + } 36 + else if (memory.mode == SEARCH) { 37 + move = memory.grid[tempRow][tempCol]; 38 + } 39 + else if (memory.mode == DESTROY) { 40 + move = memory.grid[tempRow][tempCol]; 41 + } 42 + return move; 43 + } 44 + 45 + // complete this function so it updates the computer's memory based on the 46 + // result of the last shot at location (row, col) 47 + void updateMemorywalther(int row, int col, int result, ComputerMemory &memory) { 48 + if (isASunk(result)) { 49 + if (shipCurrent == isShip(result)) { 50 + memory.mode = RANDOM; 51 + memory.fireDir = NORTH; 52 + memory.fireDist = 1; 53 + } 54 + else { 55 + memory.fireDir += 1; 56 + } 57 + if (memory.mode == DESTROY) { 58 + if (memory.fireDir == NORTH) { 59 + memory.fireDir = SOUTH; 60 + } 61 + if (memory.fireDir == SOUTH) { 62 + memory.fireDir = NORTH; 63 + } 64 + if (memory.fireDir == EAST) { 65 + memory.fireDir = WEST; 66 + } 67 + if (memory.fireDir == WEST) { 68 + memory.fireDir = EAST; 69 + } 70 + } 71 + } 72 + else if (isAHit(result)) { 73 + if (memory.mode == RANDOM) { 74 + row = memory.hitRow; 75 + col = memory.hitCol; 76 + memory.fireDir = NORTH; 77 + shipCurrent = isShip(result); 78 + } 79 + else if (memory.mode == SEARCH) { 80 + if (shipCurrent == isShip(result)) { 81 + row = memory.hitRow; 82 + col = memory.hitCol; 83 + memory.fireDist += 1; 84 + memory.mode = DESTROY; 85 + } 86 + else { 87 + memory.fireDir += 1; 88 + } 89 + } 90 + else if (memory.mode == DESTROY) { 91 + if (shipCurrent == isShip(result)) { 92 + row = memory.hitRow; 93 + col = memory.hitCol; 94 + memory.fireDist += 1; 95 + } 96 + else { 97 + if (memory.fireDir == NORTH) { 98 + memory.fireDir = SOUTH; 99 + } 100 + if (memory.fireDir == SOUTH) { 101 + memory.fireDir = NORTH; 102 + } 103 + if (memory.fireDir == EAST) { 104 + memory.fireDir = WEST; 105 + } 106 + if (memory.fireDir == WEST) { 107 + memory.fireDir = EAST; 108 + } 109 + } 110 + } 111 + } 112 + else if (isAMiss(result)) { 113 + if (memory.mode == RANDOM) { 114 + memory.mode == RANDOM; 115 + } 116 + else if (memory.mode == SEARCH) { 117 + memory.fireDir += 1; 118 + } 119 + else if (memory.mode == DESTROY) { 120 + if (memory.fireDir == NORTH) { 121 + memory.fireDir = SOUTH; 122 + } 123 + if (memory.fireDir == SOUTH) { 124 + memory.fireDir = NORTH; 125 + } 126 + if (memory.fireDir == EAST) { 127 + memory.fireDir = WEST; 128 + } 129 + if (memory.fireDir == WEST) { 130 + memory.fireDir = EAST; 131 + } 132 + } 133 + } 134 + }