Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
piece_points_bpo.cpp
Go to the documentation of this file.
1
3
4#include <algorithm>
5#include <fstream>
6#include <iostream>
8#include <unordered_map>
10
11using namespace std;
12using namespace piecepoints;
13using namespace gameboard;
14// using nloh_json = nlohmann::json;
15
17 : black_base_{}
18 , red_base_offsets_{}
19 , black_position_{}
20 , red_position_offsets_{} {};
21
23 BasePointsSMap_t black_base_input,
24 BasePointsSMap_t red_base_offsets_input,
25 TeamPointsSMap_t black_position_input,
26 TeamPointsSMap_t red_position_offsets_input
27)
28 : black_base_{black_base_input}
29 , red_base_offsets_{red_base_offsets_input}
30 , black_position_{black_position_input}
31 , red_position_offsets_{red_position_offsets_input} {}
32
33BPOPointsSKeys::BPOPointsSKeys(const string &json_file_path)
34 : black_base_{}
35 , red_base_offsets_{}
36 , black_position_{}
37 , red_position_offsets_{} {
38 json_utility_->Import(*this, json_file_path);
39}
40
41void BPOPointsSKeys::ToFile(string output_path) {
42 json_utility_->Export(*this, output_path);
43}
44
46 GamePointsSMap_t s_map{};
47
48 for (auto piece : black_base_) {
49 s_map["black"][piece.first] = utility_functs::array_plus_const(
50 black_position_[piece.first],
51 black_base_[piece.first]
52 );
53 }
54
55 for (auto piece : red_base_offsets_) {
56 auto red_base = black_base_[piece.first] + red_base_offsets_[piece.first];
57 auto red_position = utility_functs::two_array_sum(
58 black_position_[piece.first],
59 red_position_offsets_[piece.first]
60 );
61 s_map["red"][piece.first] = utility_functs::array_plus_const(red_position, red_base);
62 }
63
64 return s_map;
65}
66
68 unordered_map<string, PieceType> key_substitutions = {
69 {"null", PieceType::kNnn},
70 {"general", PieceType::kGen},
71 {"advisor", PieceType::kAdv},
72 {"elephant", PieceType::kEle},
73 {"chariot", PieceType::kCha},
74 {"horse", PieceType::kHor},
75 {"cannon", PieceType::kCan},
76 {"soldier", PieceType::kSol}
77 };
78
79 auto ekey_black_base = utility_functs::replace_keys(black_base_, key_substitutions);
80 auto ekey_red_base_offsets =
82 auto ekey_black_position =
84 auto ekey_red_position_offsets =
86
87 return BPOPointsEKeys(
88 ekey_black_base,
89 ekey_red_base_offsets,
90 ekey_black_position,
91 ekey_red_position_offsets
92 );
93}
94
96 auto bpo_points_ekeys = ToBPOPointsEKeys();
97 return bpo_points_ekeys.ToGamePointsArray();
98}
99
101 TeamBasePoints_t black_base_input,
102 TeamBasePoints_t red_base_offsets_input,
103 TeamPointsEMap_t black_position_input,
104 TeamPointsEMap_t red_position_offsets_input
105)
106 : black_base_{black_base_input}
107 , red_base_offsets_{red_base_offsets_input}
108 , black_position_{black_position_input}
109 , red_position_offsets_{red_position_offsets_input} {}
110
112 unordered_map<string, PieceType> key_substitutions = {
113 {"null", PieceType::kNnn},
114 {"general", PieceType::kGen},
115 {"advisor", PieceType::kAdv},
116 {"elephant", PieceType::kEle},
117 {"chariot", PieceType::kCha},
118 {"horse", PieceType::kHor},
119 {"cannon", PieceType::kCan},
120 {"soldier", PieceType::kSol}
121 };
122
124 utility_functs::replace_keys(external_spec.black_base_, key_substitutions);
125
127 utility_functs::replace_keys(external_spec.red_base_offsets_, key_substitutions);
128
130 utility_functs::replace_keys(external_spec.black_position_, key_substitutions);
131
133 external_spec.red_position_offsets_,
134 key_substitutions
135 );
136}
137
139 TeamPointsArray_t black_net_points{};
140 for (auto piece : black_base_) {
141
142 black_net_points[piece.first] = utility_functs::array_plus_const(
143 black_position_[piece.first],
144 black_base_[piece.first]
145 );
146 }
147 return black_net_points;
148}
149
151 TeamPointsArray_t red_net_points{};
152 for (auto piece : red_base_offsets_) {
153 auto base_points = black_base_[piece.first] + red_base_offsets_[piece.first];
154
155 auto unflipped_position_points = utility_functs::two_array_sum(
156 black_position_[piece.first],
157 red_position_offsets_[piece.first]
158 );
159
160 auto flipped_position_points =
161 utility_functs::vertical_flip_array(unflipped_position_points);
162
163 red_net_points[piece.first] =
164 utility_functs::array_plus_const(flipped_position_points, base_points);
165 }
166 return red_net_points;
167}
168
170 GamePointsArray_t game_points_array{};
171 game_points_array[GetZColorIndexOf(PieceColor::kBlk)] = BlackNetPoints();
172 game_points_array[GetZColorIndexOf(PieceColor::kRed)] = RedNetPoints();
173 return game_points_array;
174}
Definitions of classes used for storing piece points in Base Points Offset form.
Piece Points spec in "Base Points Offset" form with PieceType enum keys in member unordered_map objec...
TeamPointsArray_t BlackNetPoints()
GamePointsArray_t ToGamePointsArray()
TeamPointsArray_t RedNetPoints()
BPOPointsEKeys(TeamBasePoints_t black_base_input, TeamBasePoints_t red_base_offsets_input, TeamPointsEMap_t black_position_input, TeamPointsEMap_t red_position_offsets_input)
Piece Points spec in "Base Points Offset" form with string keys in member unordered_map objects for e...
GamePointsSMap_t ToGamePointsSmap()
unique_ptr< JsonUtility< jsonio::NlohmannJsonUtility > > json_utility_
GamePointsArray_t ToGamePointsArray()
void ToFile(string output_path)
Tracking piece positions and determining legal moves.
size_t GetZColorIndexOf(PieceColor color)
Definition: game_piece.hpp:77
Providing position-dependent values of pieces to objects in moveselection namespace.
unordered_map< string, Points_t > BasePointsSMap_t
array< TeamPointsArray_t, 2 > GamePointsArray_t
unordered_map< string, PiecePointsArray_t > TeamPointsSMap_t
unordered_map< gameboard::PieceType, Points_t > TeamBasePoints_t
unordered_map< gameboard::PieceType, PiecePointsArray_t > TeamPointsEMap_t
array< PiecePointsArray_t, gameboard::kNumPieceTypeVals > TeamPointsArray_t
unordered_map< string, TeamPointsSMap_t > GamePointsSMap_t
unordered_map< ToKey, Value > replace_keys(unordered_map< FromKey, Value > orig_map, std ::unordered_map< FromKey, ToKey > key_substitutions)
two_d_array_t array_plus_const(two_d_array_t array, array_element_t offset)
two_d_array_t two_array_sum(two_d_array_t a, two_d_array_t b)
two_d_array_t vertical_flip_array(two_d_array_t orig_array)
Defiition of miscellaneous free functions (and implementation of those that are templates).