00001
00002
00003 #include "osl/record/usi.h"
00004 #include "osl/record/psn.h"
00005 #include "osl/state/simpleState.h"
00006 #include "osl/apply_move/applyMove.h"
00007 #include <iostream>
00008 #include <sstream>
00009 #include <cctype>
00010
00011 const std::string osl::record::usi::
00012 show(Move m)
00013 {
00014 return psn::show(m);
00015 }
00016
00017 const osl::Move osl::record::usi::
00018 strToMove(const std::string& str, const SimpleState& s)
00019 {
00020 return psn::strToMove(str, s);
00021 }
00022
00023 osl::PtypeO osl::record::usi::
00024 charToPtypeO(char c)
00025 {
00026 const Ptype ptype = psn::charToPtype(toupper(c));
00027 const Player pl = isupper(c) ? BLACK : WHITE;
00028 return newPtypeO(pl, ptype);
00029 }
00030
00031 void osl::record::usi::parseBoard(const std::string& word, SimpleState& state)
00032 {
00033 state.init();
00034 int x=9, y=1;
00035 for (size_t i=0; i<word.size(); ++i) {
00036 const char c = word[i];
00037 if (isalpha(c)) {
00038 const PtypeO ptypeo = charToPtypeO(c);
00039 state.setPiece(getOwner(ptypeo), Position(x,y), getPtype(ptypeo));
00040 --x;
00041 } else if (c == '/') {
00042 assert(x == 0);
00043 x = 9;
00044 ++y;
00045 } else if (isdigit(c)) {
00046 const int n = c - '0';
00047 x -= n;
00048 assert(x >= 0);
00049 } else {
00050 std::cerr << "usi: unknown input " << c << "\n";
00051 }
00052 }
00053 }
00054
00055 void osl::record::usi::parse(const std::string& line, SimpleState& state)
00056 {
00057 std::istringstream is(line);
00058 std::string word;
00059 is >> word;
00060 if (word == "startpos")
00061 state.init(HIRATE);
00062 else {
00063 assert(word == "sfen");
00064 is >> word;
00065 parseBoard(word, state);
00066 is >> word;
00067 assert(word == "b" || word == "w");
00068 state.setTurn((word == "b") ? BLACK : WHITE);
00069 is >> word;
00070 if (word != "-") {
00071 int prefix = 1;
00072 for (size_t i=0; i<word.size(); ++i) {
00073 if (isalpha(word[i])) {
00074 PtypeO ptypeo = charToPtypeO(word[i]);
00075 for (int j=0; j<prefix; ++j)
00076 state.setPiece(getOwner(ptypeo), Position::STAND(), getPtype(ptypeo));
00077 prefix = 1;
00078 }
00079 else {
00080 assert(isdigit(word[i]));
00081 prefix = word[i] - '0';
00082 }
00083 }
00084 }
00085 int move_number;
00086 is >> move_number;
00087 assert(is);
00088 }
00089 if (! (is >> word))
00090 return;
00091 assert(word == "moves");
00092 while (is >> word) {
00093 Move m = strToMove(word, state);
00094 ApplyMoveOfTurn::doMove(state, m);
00095 }
00096 }
00097
00098
00099
00100
00101
00102