usiState.cc
Go to the documentation of this file.
00001 /* usiState.cc
00002  */
00003 #include "osl/game_playing/usiState.h"
00004 #include "osl/record/ki2.h"
00005 #include "osl/record/kakinoki.h"
00006 #include "osl/record/csaRecord.h"
00007 #include "osl/record/usi.h"
00008 #include <boost/algorithm/string/predicate.hpp>
00009 #include <boost/algorithm/string/trim.hpp>
00010 #include <boost/foreach.hpp>
00011 osl::game_playing::
00012 UsiState::UsiState() : initial_state(HIRATE), aborted(false) 
00013 {
00014 }
00015 
00016 osl::game_playing::
00017 UsiState::~UsiState()
00018 {
00019 }
00020 
00021 void osl::game_playing::
00022 UsiState::reset(const SimpleState& i, const vector<Move>& m)
00023 {
00024   initial_state = i;
00025   moves = m;
00026   aborted = false;
00027 }
00028 
00029 bool osl::game_playing::
00030 UsiState::isSuccessorOf(const UsiState& parent)
00031 {
00032   return ! aborted && ! parent.aborted
00033     && initial_state == parent.initial_state
00034     && moves.size() == parent.moves.size()+1
00035     && std::equal(parent.moves.begin(), parent.moves.end(), moves.begin());
00036 }
00037 
00038 const osl::NumEffectState osl::game_playing::
00039 UsiState::currentState() const
00040 {
00041   NumEffectState state(initial_state);
00042   BOOST_FOREACH(Move m, moves)
00043     state.makeMove(m);
00044   return state;
00045 }
00046 
00047 void osl::game_playing::
00048 UsiState::parseUsi(const std::string& line)
00049 {
00050   assert(line.find("position") == 0);
00051   record::usi::parse(line.substr(8), initial_state, moves);
00052 }
00053 
00054 void osl::game_playing::
00055 UsiState::openFile(std::string filename)
00056 {
00057   boost::algorithm::trim(filename);
00058   boost::algorithm::trim_left(filename);
00059   Record record;
00060 #ifndef MINIMAL
00061   if (boost::algorithm::iends_with(filename, ".ki2"))
00062   {
00063     const Ki2File ki2(filename);
00064     record = ki2.getRecord();
00065   }
00066   else if (boost::algorithm::iends_with(filename, ".kif"))
00067   {
00068     const KakinokiFile kif(filename);
00069     record = kif.getRecord();
00070   }
00071   else
00072 #endif
00073   {
00074     const CsaFile csa(filename.c_str());
00075     record = csa.getRecord();
00076   }
00077   initial_state = record.getInitialState();
00078   moves = record.getMoves();
00079 }
00080 
00081 const std::string osl::game_playing::
00082 UsiState::usiString() const
00083 {
00084   std::string ret;
00085   ret.reserve(16+90+10+5*moves.size());
00086   ret = "position ";
00087   ret += record::usi::show(initial_state);
00088   ret += " moves";
00089   BOOST_FOREACH(Move move, moves) {
00090     ret += " ";
00091     ret += record::usi::show(move);
00092   }
00093   return ret;
00094 }
00095 
00096 const std::string osl::game_playing::
00097 UsiState::usiBoard() const
00098 {
00099   std::string ret = "position ";
00100   ret += record::usi::show(currentState());
00101   return ret;
00102 }
00103 
00104 void osl::game_playing::
00105 UsiState::parseIgnoreMoves(const std::string& line,
00106                            MoveVector& ignore_moves) const
00107 {
00108   assert(line.find("ignore_moves") == 0);
00109   std::istringstream is(line);
00110   std::string word;
00111   is >> word;
00112   NumEffectState state(currentState());
00113   ignore_moves.clear();
00114   while (is >> word) {
00115     ignore_moves.push_back(record::usi::strToMove(word, state));
00116   }
00117 }
00118 
00119 
00120 // ;;; Local Variables:
00121 // ;;; mode:c++
00122 // ;;; c-basic-offset:2
00123 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines