a geicko-2 based round robin ranking system designed to test c++ battleship submissions
battleship.dunkirk.sh
1// Probability AI - calculates ship placement likelihood for each cell
2// Strategy: Smart probability-based targeting with density analysis
3
4#include "memory_functions_probability.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 shipSizes[] = {5, 4, 3, 3, 2};
15static int numShips = 5;
16
17inline string formatMove(int row, int col) {
18 char letter = static_cast<char>('A' + row);
19 return string(1, letter) + to_string(col + 1);
20}
21
22void initMemoryProbability(ComputerMemory &memory) {
23 srand(time(NULL));
24
25 for (int i = 0; i < BOARDSIZE; i++) {
26 for (int j = 0; j < BOARDSIZE; j++) {
27 memory.grid[i][j] = '?';
28 }
29 }
30}
31
32void updateMemoryProbability(int row, int col, int result, ComputerMemory &memory) {
33 if (result == HIT || result == SUNK) {
34 memory.grid[row][col] = 'h';
35 } else {
36 memory.grid[row][col] = 'm';
37 }
38}
39
40int calculateProbability(int row, int col, const ComputerMemory &memory) {
41 if (memory.grid[row][col] != '?') {
42 return 0;
43 }
44
45 int probability = 0;
46
47 // For each ship size
48 for (int ship = 0; ship < numShips; ship++) {
49 int size = shipSizes[ship];
50
51 // Check horizontal placements
52 for (int startCol = col - size + 1; startCol <= col; startCol++) {
53 if (startCol < 0 || startCol + size > BOARDSIZE) continue;
54
55 bool valid = true;
56 for (int c = startCol; c < startCol + size; c++) {
57 if (memory.grid[row][c] == 'm' || memory.grid[row][c] == 's') {
58 valid = false;
59 break;
60 }
61 }
62 if (valid) probability++;
63 }
64
65 // Check vertical placements
66 for (int startRow = row - size + 1; startRow <= row; startRow++) {
67 if (startRow < 0 || startRow + size > BOARDSIZE) continue;
68
69 bool valid = true;
70 for (int r = startRow; r < startRow + size; r++) {
71 if (memory.grid[r][col] == 'm' || memory.grid[r][col] == 's') {
72 valid = false;
73 break;
74 }
75 }
76 if (valid) probability++;
77 }
78 }
79
80 // Bonus for cells adjacent to hits
81 int directions[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
82 for (int i = 0; i < 4; i++) {
83 int newRow = row + directions[i][0];
84 int newCol = col + directions[i][1];
85
86 if (newRow >= 0 && newRow < BOARDSIZE &&
87 newCol >= 0 && newCol < BOARDSIZE &&
88 memory.grid[newRow][newCol] == 'h') {
89 probability += 50;
90 }
91 }
92
93 return probability;
94}
95
96string smartMoveProbability(const ComputerMemory &memory) {
97 int maxProb = -1;
98 int bestRow = 0, bestCol = 0;
99
100 // Calculate probability for each cell
101 for (int i = 0; i < BOARDSIZE; i++) {
102 for (int j = 0; j < BOARDSIZE; j++) {
103 int prob = calculateProbability(i, j, memory);
104 if (prob > maxProb) {
105 maxProb = prob;
106 bestRow = i;
107 bestCol = j;
108 }
109 }
110 }
111
112 // Fallback to random if needed
113 if (maxProb <= 0) {
114 for (int i = 0; i < BOARDSIZE; i++) {
115 for (int j = 0; j < BOARDSIZE; j++) {
116 if (memory.grid[i][j] == '?') {
117 return formatMove(i, j);
118 }
119 }
120 }
121 bestRow = rand() % BOARDSIZE;
122 bestCol = rand() % BOARDSIZE;
123 }
124
125 return formatMove(bestRow, bestCol);
126}