@@ -210,6 +210,7 @@ bool GetTreeImpl(const Ast::Tree& tree,
210210 std::vector<std::unique_ptr<Node>>* dst) {
211211 const auto * src = &tree;
212212 const Ast::Property* prop;
213+ const Ast::Property* stones;
213214
214215 // Extract all the nodes out of this tree.
215216 for (const auto & node : src->nodes ) {
@@ -219,6 +220,43 @@ bool GetTreeImpl(const Ast::Tree& tree,
219220 move.color = Color::kBlack ;
220221 } else if ((prop = node.FindProperty (" W" )) != nullptr ) {
221222 move.color = Color::kWhite ;
223+ } else if ((prop = node.FindProperty (" HA" )) != nullptr ) {
224+ int handicap;
225+ bool valid_handicap = absl::SimpleAtoi (prop->values [0 ], &handicap);
226+ if (!valid_handicap) {
227+ MG_LOG (ERROR) << " Invalid handicap property: " << prop->values [0 ];
228+ break ;
229+ }
230+
231+ if ((handicap <= 1 ) || (handicap > 9 )) {
232+ MG_LOG (ERROR) << " Invalid handicap value:" << handicap;
233+ return false ;
234+ }
235+ stones = node.FindProperty (" AB" );
236+ if (stones == nullptr ) {
237+ MG_LOG (ERROR) << " Handicap stones not specified." ;
238+ return false ;
239+ }
240+
241+ for (const auto & h_location : stones->values ) {
242+ Move m;
243+ m.color = Color::kBlack ;
244+ m.c = Coord::FromSgf (h_location, true );
245+ if (m.c == Coord::kInvalid ) {
246+ MG_LOG (ERROR) << " Can't parse node " << node.ToString () << " : \" "
247+ << h_location << " \" isn't a valid SGF coord for a handicap stone" ;
248+ return false ;
249+ }
250+ dst->push_back (absl::make_unique<Node>(m, " " ));
251+ dst = &(dst->back ()->children );
252+ if (h_location != stones->values .back ()) {
253+ m.color = Color::kWhite ;
254+ m.c = Coord::kPass ;
255+ dst->push_back (absl::make_unique<Node>(m, " " ));
256+ dst = &(dst->back ()->children );
257+ }
258+ }
259+ continue ;
222260 } else {
223261 continue ;
224262 }
0 commit comments