Go to the documentation of this file.00001
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 bool osl::game_playing::
00022 UsiState::isSuccessorOf(const UsiState& parent)
00023 {
00024 return ! aborted && ! parent.aborted
00025 && initial_state == parent.initial_state
00026 && moves.size() == parent.moves.size()+1
00027 && std::equal(parent.moves.begin(), parent.moves.end(), moves.begin());
00028 }
00029
00030 const osl::NumEffectState osl::game_playing::
00031 UsiState::currentState() const
00032 {
00033 NumEffectState state(initial_state);
00034 BOOST_FOREACH(Move m, moves)
00035 state.makeMove(m);
00036 return state;
00037 }
00038
00039 void osl::game_playing::
00040 UsiState::parseUsi(const std::string& line)
00041 {
00042 assert(line.find("position") == 0);
00043 record::usi::parse(line.substr(8), initial_state, moves);
00044 }
00045
00046 void osl::game_playing::
00047 UsiState::openFile(std::string filename)
00048 {
00049 boost::algorithm::trim(filename);
00050 boost::algorithm::trim_left(filename);
00051 Record record;
00052 if (boost::algorithm::iends_with(filename, ".ki2"))
00053 {
00054 const Ki2File ki2(filename);
00055 record = ki2.getRecord();
00056 }
00057 else if (boost::algorithm::iends_with(filename, ".kif"))
00058 {
00059 const KakinokiFile kif(filename);
00060 record = kif.getRecord();
00061 }
00062 else
00063 {
00064 const CsaFile csa(filename.c_str());
00065 record = csa.getRecord();
00066 }
00067 initial_state = record.getInitialState();
00068 moves = record.getMoves();
00069 }
00070
00071 const std::string osl::game_playing::
00072 UsiState::usiString() const
00073 {
00074 std::string ret;
00075 ret.reserve(16+90+10+5*moves.size());
00076 ret = "position ";
00077 ret += record::usi::show(initial_state);
00078 ret += " moves";
00079 BOOST_FOREACH(Move move, moves) {
00080 ret += " ";
00081 ret += record::usi::show(move);
00082 }
00083 return ret;
00084 }
00085
00086 const std::string osl::game_playing::
00087 UsiState::usiBoard() const
00088 {
00089 std::string ret = "position ";
00090 ret += record::usi::show(currentState());
00091 return ret;
00092 }
00093
00094 void osl::game_playing::
00095 UsiState::parseIgnoreMoves(const std::string& line,
00096 MoveVector& ignore_moves) const
00097 {
00098 assert(line.find("ignore_moves") == 0);
00099 std::istringstream is(line);
00100 std::string word;
00101 is >> word;
00102 NumEffectState state(currentState());
00103 ignore_moves.clear();
00104 while (is >> word) {
00105 ignore_moves.push_back(record::usi::strToMove(word, state));
00106 }
00107 }
00108
00109
00110
00111
00112
00113