a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
1// Cluster AI - targets high-density regions where ships are likely clustered 2// Strategy: Focus fire on promising areas before moving to next cluster 3 4#include "memory_functions_cluster.h" 5#include "battleship.h" 6#include "kasbs.h" 7#include "memory.h" 8#include <string> 9#include <cstdlib> 10#include <ctime> 11 12using namespace std; 13 14static int densityMap[BOARDSIZE][BOARDSIZE]; 15static int clusterCenterRow = -1; 16static int clusterCenterCol = -1; 17static int shotsInCurrentCluster = 0; 18static const int maxShotsPerCluster = 12; 19 20inline string formatMove(int row, int col) { 21 char letter = static_cast<char>('A' + row); 22 return string(1, letter) + to_string(col + 1); 23} 24 25void initMemoryCluster(ComputerMemory &memory) { 26 srand(time(NULL)); 27 28 for (int i = 0; i < BOARDSIZE; i++) { 29 for (int j = 0; j < BOARDSIZE; j++) { 30 memory.grid[i][j] = '?'; 31 densityMap[i][j] = 0; 32 } 33 } 34 35 clusterCenterRow = -1; 36 clusterCenterCol = -1; 37 shotsInCurrentCluster = 0; 38} 39 40void updateMemoryCluster(int row, int col, int result, ComputerMemory &memory) { 41 if (result == HIT || result == SUNK) { 42 memory.grid[row][col] = 'h'; 43 44 // Increase density around hits 45 for (int i = row - 2; i <= row + 2; i++) { 46 for (int j = col - 2; j <= col + 2; j++) { 47 if (i >= 0 && i < BOARDSIZE && j >= 0 && j < BOARDSIZE) { 48 densityMap[i][j] += 10; 49 } 50 } 51 } 52 } else { 53 memory.grid[row][col] = 'm'; 54 55 // Decrease density around misses 56 for (int i = row - 1; i <= row + 1; i++) { 57 for (int j = col - 1; j <= col + 1; j++) { 58 if (i >= 0 && i < BOARDSIZE && j >= 0 && j < BOARDSIZE) { 59 densityMap[i][j] -= 2; 60 if (densityMap[i][j] < 0) densityMap[i][j] = 0; 61 } 62 } 63 } 64 } 65} 66 67void findBestCluster(int* centerRow, int* centerCol, const ComputerMemory &memory) { 68 int maxDensity = -1; 69 *centerRow = -1; 70 *centerCol = -1; 71 72 for (int i = 1; i < BOARDSIZE - 1; i++) { 73 for (int j = 1; j < BOARDSIZE - 1; j++) { 74 if (memory.grid[i][j] != '?') continue; 75 76 // Calculate cluster density (3x3 area) 77 int clusterDensity = 0; 78 for (int di = -1; di <= 1; di++) { 79 for (int dj = -1; dj <= 1; dj++) { 80 clusterDensity += densityMap[i + di][j + dj]; 81 } 82 } 83 84 if (clusterDensity > maxDensity) { 85 maxDensity = clusterDensity; 86 *centerRow = i; 87 *centerCol = j; 88 } 89 } 90 } 91} 92 93string smartMoveCluster(const ComputerMemory &memory) { 94 // Find new cluster if needed 95 if (clusterCenterRow == -1 || shotsInCurrentCluster >= maxShotsPerCluster) { 96 findBestCluster(&clusterCenterRow, &clusterCenterCol, memory); 97 shotsInCurrentCluster = 0; 98 99 // No cluster found, pick any unknown cell 100 if (clusterCenterRow == -1) { 101 for (int i = 0; i < BOARDSIZE; i++) { 102 for (int j = 0; j < BOARDSIZE; j++) { 103 if (memory.grid[i][j] == '?') { 104 return formatMove(i, j); 105 } 106 } 107 } 108 return formatMove(rand() % BOARDSIZE, rand() % BOARDSIZE); 109 } 110 } 111 112 // Shoot within current cluster 113 int attempts = 0; 114 while (attempts < 100) { 115 int offsetRow = (rand() % 5) - 2; 116 int offsetCol = (rand() % 5) - 2; 117 118 int targetRow = clusterCenterRow + offsetRow; 119 int targetCol = clusterCenterCol + offsetCol; 120 121 if (targetRow >= 0 && targetRow < BOARDSIZE && 122 targetCol >= 0 && targetCol < BOARDSIZE && 123 memory.grid[targetRow][targetCol] == '?') { 124 125 shotsInCurrentCluster++; 126 return formatMove(targetRow, targetCol); 127 } 128 attempts++; 129 } 130 131 // Fallback 132 shotsInCurrentCluster++; 133 return formatMove(rand() % BOARDSIZE, rand() % BOARDSIZE); 134}