a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 2.9 kB view raw
1// Spiral AI - shoots in an inward spiral pattern 2// Strategy: Methodical coverage starting from edges, spiraling toward center 3 4#include "memory_functions_spiral.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 14struct SpiralState { 15 int nextRow; 16 int nextCol; 17 int spiralDepth; 18 int currentDirection; // 0=RIGHT, 1=DOWN, 2=LEFT, 3=UP 19 int stepsInCurrentDirection; 20 int stepsToTake; 21 bool initialized; 22}; 23 24static SpiralState state; 25 26inline string formatMove(int row, int col) { 27 char letter = static_cast<char>('A' + row); 28 return string(1, letter) + to_string(col + 1); 29} 30 31void initMemorySpiral(ComputerMemory &memory) { 32 srand(time(NULL)); 33 34 for (int i = 0; i < BOARDSIZE; i++) { 35 for (int j = 0; j < BOARDSIZE; j++) { 36 memory.grid[i][j] = '?'; 37 } 38 } 39 40 state.nextRow = 0; 41 state.nextCol = 0; 42 state.currentDirection = 0; // RIGHT 43 state.spiralDepth = 0; 44 state.stepsInCurrentDirection = 0; 45 state.stepsToTake = BOARDSIZE - 1; 46 state.initialized = true; 47} 48 49void updateMemorySpiral(int row, int col, int result, ComputerMemory &memory) { 50 if (result == HIT || result == SUNK) { 51 memory.grid[row][col] = 'h'; 52 } else { 53 memory.grid[row][col] = 'm'; 54 } 55} 56 57string smartMoveSpiral(const ComputerMemory &memory) { 58 if (!state.initialized) { 59 return formatMove(rand() % BOARDSIZE, rand() % BOARDSIZE); 60 } 61 62 int row = state.nextRow; 63 int col = state.nextCol; 64 65 // Advance to next position in spiral 66 state.stepsInCurrentDirection++; 67 68 // Move in current direction 69 switch (state.currentDirection) { 70 case 0: // RIGHT 71 state.nextCol++; 72 break; 73 case 1: // DOWN 74 state.nextRow++; 75 break; 76 case 2: // LEFT 77 state.nextCol--; 78 break; 79 case 3: // UP 80 state.nextRow--; 81 break; 82 } 83 84 // Check if we need to turn 85 if (state.stepsInCurrentDirection >= state.stepsToTake) { 86 state.stepsInCurrentDirection = 0; 87 state.currentDirection = (state.currentDirection + 1) % 4; 88 89 // After turning twice, reduce steps 90 if (state.currentDirection == 0 || state.currentDirection == 2) { 91 state.stepsToTake--; 92 state.spiralDepth++; 93 } 94 } 95 96 // If spiraled to center, use fallback 97 if (state.spiralDepth >= BOARDSIZE / 2) { 98 for (int i = 0; i < BOARDSIZE; i++) { 99 for (int j = 0; j < BOARDSIZE; j++) { 100 if (memory.grid[i][j] == '?') { 101 return formatMove(i, j); 102 } 103 } 104 } 105 return formatMove(rand() % BOARDSIZE, rand() % BOARDSIZE); 106 } 107 108 return formatMove(row, col); 109}