a geicko-2 based round robin ranking system designed to test c++ battleship submissions
battleship.dunkirk.sh
1#include "battleship.h"
2#include "kasbs.h"
3#include "memory.h"
4#include <string>
5#include <cstdlib>
6
7using namespace std;
8
9// Edge AI - prioritizes edges and corners first
10void initMemoryEdge(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
26string smartMoveEdge(const ComputerMemory &memory) {
27 if (memory.mode == RANDOM) {
28 // First shoot corners
29 int corners[4][2] = {{0, 0}, {0, BOARDSIZE-1}, {BOARDSIZE-1, 0}, {BOARDSIZE-1, BOARDSIZE-1}};
30 for (int c = 0; c < 4; c++) {
31 int r = corners[c][0];
32 int col = corners[c][1];
33 if (memory.grid[r][col] == EMPTY_MARKER) {
34 char letter = static_cast<char>('A' + r);
35 return string(1, letter) + to_string(col + 1);
36 }
37 }
38
39 // Then shoot edges
40 for (int i = 0; i < BOARDSIZE; i++) {
41 // Top edge
42 if (memory.grid[0][i] == EMPTY_MARKER) {
43 return string(1, 'A') + to_string(i + 1);
44 }
45 // Bottom edge
46 if (memory.grid[BOARDSIZE-1][i] == EMPTY_MARKER) {
47 char letter = static_cast<char>('A' + BOARDSIZE - 1);
48 return string(1, letter) + to_string(i + 1);
49 }
50 // Left edge
51 if (memory.grid[i][0] == EMPTY_MARKER) {
52 char letter = static_cast<char>('A' + i);
53 return string(1, letter) + to_string(1);
54 }
55 // Right edge
56 if (memory.grid[i][BOARDSIZE-1] == EMPTY_MARKER) {
57 char letter = static_cast<char>('A' + i);
58 return string(1, letter) + to_string(BOARDSIZE);
59 }
60 }
61
62 // Then fill in the rest
63 for (int i = 0; i < BOARDSIZE; i++) {
64 for (int j = 0; j < BOARDSIZE; j++) {
65 if (memory.grid[i][j] == EMPTY_MARKER) {
66 char letter = static_cast<char>('A' + i);
67 return string(1, letter) + to_string(j + 1);
68 }
69 }
70 }
71 }
72
73 // Target mode
74 int directions[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
75 for (int d = 0; d < 4; d++) {
76 int newRow = memory.hitRow + directions[d][0];
77 int newCol = memory.hitCol + directions[d][1];
78
79 if (newRow >= 0 && newRow < BOARDSIZE && newCol >= 0 && newCol < BOARDSIZE &&
80 memory.grid[newRow][newCol] == EMPTY_MARKER) {
81 char letter = static_cast<char>('A' + newRow);
82 return string(1, letter) + to_string(newCol + 1);
83 }
84 }
85
86 return "A1";
87}
88
89void updateMemoryEdge(int row, int col, int result, ComputerMemory &memory) {
90 memory.lastResult = result;
91 char marker;
92 if (isAMiss(result)) {
93 marker = MISS_MARKER;
94 } else {
95 marker = HIT_MARKER;
96 }
97 memory.grid[row][col] = marker;
98
99 if (memory.mode == RANDOM && !isAMiss(result)) {
100 memory.mode = SEARCH;
101 memory.hitRow = row;
102 memory.hitCol = col;
103 } else if (isASunk(result)) {
104 memory.mode = RANDOM;
105 memory.hitRow = -1;
106 memory.hitCol = -1;
107 }
108}