a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 118 lines 3.0 kB view raw
1// Player wrapper - isolates student AI code in its own process 2// Communicates with arena via stdin/stdout line-based protocol 3// 4// Protocol: 5// Arena -> Player: HELLO 1 6// Player -> Arena: HELLO OK 7// Arena -> Player: INIT 8// Player -> Arena: OK 9// Arena -> Player: GET_MOVE 10// Player -> Arena: MOVE <cell> (e.g., MOVE A5) 11// Arena -> Player: UPDATE <row> <col> <result> 12// Player -> Arena: OK 13// Arena -> Player: QUIT 14// Player -> Arena: OK 15 16#include "battleship.h" 17#include "memory.h" 18#include PLAYER_HEADER 19 20#include <iostream> 21#include <sstream> 22#include <string> 23 24#define CONCAT_INNER(a, b) a##b 25#define CONCAT(a, b) CONCAT_INNER(a, b) 26 27#define initMemoryFunc CONCAT(initMemory, PLAYER_SUFFIX) 28#define smartMoveFunc CONCAT(smartMove, PLAYER_SUFFIX) 29#define updateMemoryFunc CONCAT(updateMemory, PLAYER_SUFFIX) 30 31static void doInit(ComputerMemory &mem) { 32 initMemoryFunc(mem); 33} 34 35static std::string doSmartMove(const ComputerMemory &mem) { 36 return smartMoveFunc(mem); 37} 38 39static void doUpdate(int row, int col, int result, ComputerMemory &mem) { 40 updateMemoryFunc(row, col, result, mem); 41} 42 43int main() { 44 std::ios::sync_with_stdio(false); 45 std::cin.tie(nullptr); 46 47 ComputerMemory memory{}; 48 bool initialized = false; 49 50 std::string line; 51 52 auto sendLine = [](const std::string &s) { 53 std::cout << s << "\n"; 54 std::cout.flush(); 55 }; 56 57 // Handshake 58 if (!std::getline(std::cin, line)) { 59 return 0; 60 } 61 62 { 63 std::istringstream iss(line); 64 std::string cmd; 65 int version; 66 iss >> cmd >> version; 67 if (cmd != "HELLO" || version != 1) { 68 sendLine("ERROR bad_hello"); 69 return 1; 70 } 71 sendLine("HELLO OK"); 72 } 73 74 while (std::getline(std::cin, line)) { 75 if (line.empty()) continue; 76 77 std::istringstream iss(line); 78 std::string cmd; 79 iss >> cmd; 80 81 if (cmd == "INIT") { 82 memory = ComputerMemory{}; 83 doInit(memory); 84 initialized = true; 85 sendLine("OK"); 86 } else if (cmd == "GET_MOVE") { 87 if (!initialized) { 88 sendLine("ERROR not_initialized"); 89 continue; 90 } 91 std::string move = doSmartMove(memory); 92 if (move.empty()) { 93 sendLine("ERROR empty_move"); 94 } else { 95 sendLine("MOVE " + move); 96 } 97 } else if (cmd == "UPDATE") { 98 int row, col, result; 99 if (!(iss >> row >> col >> result)) { 100 sendLine("ERROR bad_update_args"); 101 continue; 102 } 103 if (!initialized) { 104 sendLine("ERROR not_initialized"); 105 continue; 106 } 107 doUpdate(row, col, result, memory); 108 sendLine("OK"); 109 } else if (cmd == "QUIT") { 110 sendLine("OK"); 111 break; 112 } else { 113 sendLine("ERROR unknown_command"); 114 } 115 } 116 117 return 0; 118}