a geicko-2 based round robin ranking system designed to test c++ battleship submissions
battleship.dunkirk.sh
1// Snake AI - follows a snake/zigzag pattern across the board
2// Strategy: Systematic coverage with no gaps, spacing based on smallest ship
3
4#include "memory_functions_snake.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 int currentRow = 0;
22static int currentCol = 0;
23static bool movingRight = true;
24static const int spacing = 2;
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 initMemorySnake(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 targetStack.clear();
41 currentRow = 0;
42 currentCol = 0;
43 movingRight = true;
44}
45
46void updateMemorySnake(int row, int col, int result, ComputerMemory &memory) {
47 if (result == HIT || result == SUNK) {
48 memory.grid[row][col] = 'h';
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
69void getNextSnakePosition(int* row, int* col, const ComputerMemory &memory) {
70 while (true) {
71 // Off board, move to next row
72 if (currentCol < 0 || currentCol >= BOARDSIZE) {
73 currentRow += spacing;
74 movingRight = !movingRight;
75
76 if (movingRight) {
77 currentCol = 0;
78 } else {
79 currentCol = BOARDSIZE - 1;
80 }
81
82 // Covered board, start filling gaps
83 if (currentRow >= BOARDSIZE) {
84 for (int i = 0; i < BOARDSIZE; i++) {
85 for (int j = 0; j < BOARDSIZE; j++) {
86 if (memory.grid[i][j] == '?') {
87 *row = i;
88 *col = j;
89 return;
90 }
91 }
92 }
93 *row = rand() % BOARDSIZE;
94 *col = rand() % BOARDSIZE;
95 return;
96 }
97 continue;
98 }
99
100 // Current position valid
101 if (memory.grid[currentRow][currentCol] == '?') {
102 *row = currentRow;
103 *col = currentCol;
104
105 if (movingRight) {
106 currentCol += spacing;
107 } else {
108 currentCol -= spacing;
109 }
110 return;
111 }
112
113 // Already shot, move on
114 if (movingRight) {
115 currentCol += spacing;
116 } else {
117 currentCol -= spacing;
118 }
119 }
120}
121
122string smartMoveSnake(const ComputerMemory &memory) {
123 // Shoot targets from hits first
124 if (!targetStack.empty()) {
125 Cell target = targetStack.back();
126 targetStack.pop_back();
127 return formatMove(target.row, target.col);
128 }
129
130 // Follow snake pattern
131 int row, col;
132 getNextSnakePosition(&row, &col, memory);
133 return formatMove(row, col);
134}