a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 3.1 kB view raw
1// Parity AI - uses checkerboard with hunt/target mode 2// Strategy: Efficient coverage with smart follow-up on hits 3 4#include "memory_functions_parity.h" 5#include "battleship.h" 6#include "kasbs.h" 7#include "memory.h" 8#include <string> 9#include <vector> 10#include <cstdlib> 11#include <ctime> 12 13using namespace std; 14 15struct Cell { 16 int row; 17 int col; 18}; 19 20static vector<Cell> targetStack; 21static bool huntMode = true; 22static int currentRow = 0; 23static int currentCol = 0; 24 25inline string formatMove(int row, int col) { 26 char letter = static_cast<char>('A' + row); 27 return string(1, letter) + to_string(col + 1); 28} 29 30void initMemoryParity(ComputerMemory &memory) { 31 srand(time(NULL)); 32 33 for (int i = 0; i < BOARDSIZE; i++) { 34 for (int j = 0; j < BOARDSIZE; j++) { 35 memory.grid[i][j] = '?'; 36 } 37 } 38 39 targetStack.clear(); 40 huntMode = true; 41 currentRow = 0; 42 currentCol = 0; 43} 44 45void updateMemoryParity(int row, int col, int result, ComputerMemory &memory) { 46 if (result == HIT || result == SUNK) { 47 memory.grid[row][col] = 'h'; 48 huntMode = false; 49 50 // Add adjacent cells to target stack 51 int directions[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}}; 52 for (int i = 0; i < 4; i++) { 53 int newRow = row + directions[i][0]; 54 int newCol = col + directions[i][1]; 55 56 if (newRow >= 0 && newRow < BOARDSIZE && 57 newCol >= 0 && newCol < BOARDSIZE && 58 memory.grid[newRow][newCol] == '?') { 59 60 Cell cell = {newRow, newCol}; 61 targetStack.push_back(cell); 62 } 63 } 64 } else { 65 memory.grid[row][col] = 'm'; 66 } 67 68 if (targetStack.empty()) { 69 huntMode = true; 70 } 71} 72 73string smartMoveParity(const ComputerMemory &memory) { 74 // Target mode - shoot from stack 75 if (!huntMode && !targetStack.empty()) { 76 Cell target = targetStack.back(); 77 targetStack.pop_back(); 78 return formatMove(target.row, target.col); 79 } 80 81 // Hunt mode - checkerboard pattern 82 while (true) { 83 if (currentRow >= BOARDSIZE) { 84 // Try any unknown cell 85 for (int i = 0; i < BOARDSIZE; i++) { 86 for (int j = 0; j < BOARDSIZE; j++) { 87 if (memory.grid[i][j] == '?') { 88 return formatMove(i, j); 89 } 90 } 91 } 92 return formatMove(rand() % BOARDSIZE, rand() % BOARDSIZE); 93 } 94 95 // Check parity and unknown 96 if ((currentRow + currentCol) % 2 == 0 && memory.grid[currentRow][currentCol] == '?') { 97 int row = currentRow; 98 int col = currentCol; 99 100 currentCol++; 101 if (currentCol >= BOARDSIZE) { 102 currentCol = 0; 103 currentRow++; 104 } 105 return formatMove(row, col); 106 } 107 108 currentCol++; 109 if (currentCol >= BOARDSIZE) { 110 currentCol = 0; 111 currentRow++; 112 } 113 } 114}