All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
usiState.cc
Go to the documentation of this file.
1 /* usiState.cc
2  */
4 #include "osl/record/ki2.h"
5 #include "osl/record/kakinoki.h"
6 #include "osl/record/csaRecord.h"
7 #include "osl/record/usi.h"
8 #include <boost/algorithm/string/predicate.hpp>
9 #include <boost/algorithm/string/trim.hpp>
10 #include <boost/foreach.hpp>
12 UsiState::UsiState() : initial_state(HIRATE), aborted(false)
13 {
14 }
15 
18 {
19 }
20 
22 UsiState::reset(const SimpleState& i, const vector<Move>& m)
23 {
24  initial_state = i;
25  moves = m;
26  aborted = false;
27 }
28 
31 {
32  return ! aborted && ! parent.aborted
33  && initial_state == parent.initial_state
34  && moves.size() == parent.moves.size()+1
35  && std::equal(parent.moves.begin(), parent.moves.end(), moves.begin());
36 }
37 
38 const osl::NumEffectState osl::game_playing::
40 {
41  NumEffectState state(initial_state);
42  BOOST_FOREACH(Move m, moves)
43  state.makeMove(m);
44  return state;
45 }
46 
48 UsiState::parseUsi(const std::string& line)
49 {
50  assert(line.find("position") == 0);
51  record::usi::parse(line.substr(8), initial_state, moves);
52 }
53 
55 UsiState::openFile(std::string filename)
56 {
57  boost::algorithm::trim(filename);
58  boost::algorithm::trim_left(filename);
59  Record record;
60 #ifndef MINIMAL
61  if (boost::algorithm::iends_with(filename, ".ki2"))
62  {
63  const Ki2File ki2(filename);
64  record = ki2.getRecord();
65  }
66  else if (boost::algorithm::iends_with(filename, ".kif"))
67  {
68  const KakinokiFile kif(filename);
69  record = kif.getRecord();
70  }
71  else
72 #endif
73  {
74  const CsaFile csa(filename.c_str());
75  record = csa.getRecord();
76  }
77  initial_state = record.getInitialState();
78  moves = record.getMoves();
79 }
80 
81 const std::string osl::game_playing::
83 {
84  std::string ret;
85  ret.reserve(16+90+10+5*moves.size());
86  ret = "position ";
87  ret += record::usi::show(initial_state);
88  ret += " moves";
89  BOOST_FOREACH(Move move, moves) {
90  ret += " ";
91  ret += record::usi::show(move);
92  }
93  return ret;
94 }
95 
96 const std::string osl::game_playing::
98 {
99  std::string ret = "position ";
100  ret += record::usi::show(currentState());
101  return ret;
102 }
103 
105 UsiState::parseIgnoreMoves(const std::string& line,
106  MoveVector& ignore_moves) const
107 {
108  assert(line.find("ignore_moves") == 0);
109  std::istringstream is(line);
110  std::string word;
111  is >> word;
112  NumEffectState state(currentState());
113  ignore_moves.clear();
114  while (is >> word) {
115  ignore_moves.push_back(record::usi::strToMove(word, state));
116  }
117 }
118 
119 
120 // ;;; Local Variables:
121 // ;;; mode:c++
122 // ;;; c-basic-offset:2
123 // ;;; End: