Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
player_spec_builder.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
8#include <optional>
9#include <string>
10#include <unordered_set>
12#include <variant>
13
14namespace game {
15using PlayerInputType = std::unordered_map<
16 std::string,
17 std::variant<std::string, size_t, int64_t, DepthType>>;
18
26
27 const std::unordered_set<std::string> allowed_keys_ = {
28 "color",
29 "evaluator_type",
30 "key_size_bits",
31 "num_zobrist_calculators",
32 "minimax_search_depth"
33 };
34
35 // Helper function to extract and set values from the input map
36 template <typename T, typename F>
38 const PlayerInputType &input_map,
39 const std::string &key,
40 F setter
41 ) {
42 if (input_map.count(key) > 0) {
43 try {
44 setter(std::get<T>(input_map.at(key)));
45 } catch (const std::bad_variant_access &e) {
46 throw std::runtime_error("Invalid type for key: " + key);
47 }
48 }
49 }
50
51public:
58
60 // Validate the presence of the required "color" key
61 if (player_input.count("color") == 0) {
62 throw std::runtime_error("Input validation failed: 'color' key is required.");
63 }
64
65 // Validate all keys are allowed
66 for (const auto &[key, _] : player_input) {
67 if (allowed_keys_.count(key) == 0) {
68 throw std::runtime_error(
69 "Input validation failed: Unrecognized key '" + key + "'."
70 );
71 }
72 }
73
74 // Use helper function to handle each attribute
75 TrySetAttribute<std::string>(player_input, "color", [this](auto color_str) {
76 auto color = input_translator_.GetPieceColorFromString(color_str);
77 color_ = color;
78 });
79
80 TrySetAttribute<std::string>(
81 player_input,
82 "evaluator_type",
83 [this](auto evaluator_type_str) {
86 }
87 );
88
89 TrySetAttribute<size_t>(player_input, "key_size_bits", [this](auto key_size_bits) {
90 zobrist_key_size_bits_ = key_size_bits;
91 // zobrist_key_type_ = input_translator_.GetZobristKeyTypeFromInt(key_size_bits);
92 });
93
94 TrySetAttribute<size_t>(
95 player_input,
96 "num_zobrist_calculators",
97 [this](auto num_calculators) {
98 zobrist_calculator_count_ = num_calculators;
99 // zobrist_calculator_count_ =
100 // input_translator_.GetZobristCalculatorCountFromInt(num_calculators);
101 }
102 );
103
104 TrySetAttribute<DepthType>(player_input, "minimax_search_depth", [this](auto depth) {
105 minimax_search_depth_ = depth;
106 });
107
108 return *this;
109 }
110
112 return PlayerSpec{
113 color_,
118 };
119 }
120};
121
122} // namespace game
Constants, typedefs, and simple structs used by gameboard::GameBoard.
gameboard::PieceColor GetPieceColorFromString(std::string input_color_name)
EvaluatorType GetEvaluatorTypeFromString(std::string input_evaluator_name)
void TrySetAttribute(const PlayerInputType &input_map, const std::string &key, F setter)
PlayerInputTranslator input_translator_
const std::unordered_set< std::string > allowed_keys_
gameboard::PieceColor color_
PlayerSpecBuilder & SetMultipleAttributes(const PlayerInputType &player_input)
Defines GamePiece and supporting constants and free functions.
uint16_t DepthType
Definition: game.hpp:19
std::unordered_map< std::string, std::variant< std::string, size_t, int64_t, DepthType > > PlayerInputType
Tracking piece positions and determining legal moves.