00001 #include "osl/record/kisen.h"
00002 #include "osl/record/csaIOError.h"
00003 #include "osl/pieceStand.h"
00004 #include "osl/misc/iconvConvert.h"
00005 #include <boost/filesystem/convenience.hpp>
00006 #include <boost/foreach.hpp>
00007 #include <iostream>
00008
00009 namespace osl
00010 {
00011 namespace record
00012 {
00013 Square KisenUtils::convertSquare( int pos ){
00014 assert(1<=pos && pos<=0x51);
00015 int y=((pos-1)/9)+1, x=((pos-1)%9)+1;
00016 return Square(x,y);
00017 }
00018 int KisenUtils::convertSquare(Square pos)
00019 {
00020 return ((pos.y() - 1) * 9 + 1) + pos.x() - 1;
00021 }
00022
00023 Move KisenUtils::convertMove(SimpleState const& state,int c0,int c1){
00024 Move move;
00025
00026 if(1<=c1 && c1<=0x51){
00027 Square from=convertSquare(c1),to;
00028 Piece fromPiece=state.pieceOnBoard(from);
00029 if (! fromPiece.isPiece())
00030 throw CsaIOError("Square error");
00031 assert(fromPiece.isPiece());
00032 assert(fromPiece.owner()==state.turn() ||
00033 (std::cerr << c1 << "," << from << "," << fromPiece << std::endl,0)
00034 );
00035 bool isPromote=false;
00036 if(1<=c0 && c0<=0x51){
00037 to=convertSquare(c0);
00038 }
00039 else if(0x65<=c0 && c0<=0xb5){
00040 to=convertSquare(c0-0x64);
00041 isPromote=true;
00042 }
00043 else{
00044 throw CsaIOError("c0 range error");
00045 }
00046 Piece toPiece=state.pieceAt(to);
00047 if (! toPiece.isEmpty() && toPiece.owner()!=alt(state.turn()))
00048 throw CsaIOError("inconsintent move (to)");
00049 Ptype ptype=fromPiece.ptype();
00050 if(isPromote)ptype=promote(ptype);
00051 const Ptype captured = toPiece.ptype();
00052 if (captured == KING)
00053 return Move::INVALID();
00054 move=Move(from,to,ptype,captured,isPromote,state.turn());
00055 }
00056 else{
00057 assert(0x65<=c1);
00058 assert(1<=c0&&c0<=0x51);
00059 Square to=convertSquare(c0);
00060 Ptype ptype=PTYPE_EMPTY;
00061 int piece_on_stand = c1;
00062 const Ptype ptypes[]={ROOK,BISHOP,GOLD,SILVER,KNIGHT,LANCE,PAWN};
00063 for(size_t i=0;i<sizeof(ptypes)/sizeof(Ptype);i++){
00064 int count=state.countPiecesOnStand(state.turn(),ptypes[i]);
00065 if(count>0){
00066 if(piece_on_stand>0x64){
00067 piece_on_stand-=count;
00068 if(piece_on_stand<=0x64) ptype=ptypes[i];
00069 }
00070 }
00071 }
00072 assert(ptype!=PTYPE_EMPTY ||
00073 (std::cerr << state << to << " " << c1
00074 << " " << piece_on_stand << std::endl, false));
00075 move=Move(to,ptype,state.turn());
00076 }
00077 if (! state.isValidMove(move,true)) {
00078 std::cerr << "warning: bad move in kisen\n" << state << move << "\n";
00079 return Move();
00080 }
00081 assert(state.isValidMove(move,true) ||
00082 (std::cerr << state << move << std::endl, false));
00083 return move;
00084 }
00085
00086
00087 KisenFile::KisenFile(const std::string& fileName)
00088 :ifs(fileName.c_str()),initialState(HIRATE), fileName(fileName)
00089 {
00090 if (! ifs)
00091 throw CsaIOError("KisenFile not found");
00092 ifs.seekg(0,std::ios::end);
00093 assert((ifs.tellg() % 512)==0);
00094 numberOfGames=ifs.tellg()/512;
00095 }
00096
00097 const vector<Move> KisenFile::getMoves(size_t index)
00098 {
00099 assert(index<size());
00100 vector<Move> moves;
00101
00102 ifs.seekg(index*512,std::ios::beg);
00103 CArray<unsigned char, 512> cbuf;
00104 ifs.read(reinterpret_cast<char *>(&cbuf[0]),512);
00105 NumEffectState state;
00106
00107 Player turn=BLACK;
00108 for(size_t turnCount=0;
00109 (turnCount*2 < cbuf.size())
00110 && cbuf[turnCount*2]!=0 && cbuf[turnCount*2+1]!=0;
00111 turnCount++, turn=alt(turn)){
00112 if(turnCount==KisenFile::maxMoves || cbuf[ turnCount *2 ] == 0 || cbuf[ turnCount * 2 + 1 ] == 0 ){ break; }
00113 int c0=cbuf[turnCount*2], c1=cbuf[turnCount*2+1];
00114 if (moves.empty() && c0 == 0xff && c1 == 0xff)
00115 break;
00116 const Move move=KisenUtils::convertMove(state,c0,c1);
00117 if (move.isInvalid())
00118 break;
00119 moves.push_back(move);
00120 state.makeMove(move);
00121 assert(state.isConsistent( true ) );
00122 }
00123 return moves;
00124 }
00125 #ifndef MINIMAL
00126 const std::string KisenFile::ipxFileName(const std::string& filename)
00127 {
00128 namespace bf = boost::filesystem;
00129 const bf::path ipxfilename = bf::change_extension(bf::path(filename), ".ipx");
00130 return ipxfilename.file_string();
00131 }
00132
00133 KisenIpxFile::KisenIpxFile(const std::string& fileName)
00134 :ifs(fileName.c_str()), file_name(fileName)
00135 {
00136 if (! ifs)
00137 throw CsaIOError("KisenIpxFile not found");
00138 ifs.seekg(0,std::ios::end);
00139 assert((ifs.tellg() % 256)==0);
00140 numberOfGames=ifs.tellg()/256;
00141 }
00142 const std::string KisenIpxFile::getPlayer(size_t index,Player pl)
00143 {
00144 assert(index<size());
00145 vector<Move> moves;
00146 ifs.seekg(index*256,std::ios::beg);
00147 CArray<unsigned char, 256> cbuf;
00148 ifs.read(reinterpret_cast<char *>(&cbuf[0]),256);
00149 int startIndex=0;
00150 if(pl==WHITE)startIndex=14;
00151 CArray<char,15> buf;
00152 buf[14]='\0';
00153 strncpy(&buf[0],reinterpret_cast<char *>(&cbuf[startIndex]),14);
00154 return std::string(&buf[0]);
00155 }
00156 unsigned int KisenIpxFile::getRating(size_t index,Player pl)
00157 {
00158 assert(index<size());
00159 vector<Move> moves;
00160 ifs.seekg(index*256,std::ios::beg);
00161 CArray<unsigned char, 256> cbuf;
00162 ifs.read(reinterpret_cast<char *>(&cbuf[0]),256);
00163 int startIndex=0324;
00164 if(pl==WHITE)startIndex=0326;
00165 return cbuf[startIndex]+256*cbuf[startIndex+1];
00166 }
00167 unsigned int KisenIpxFile::getResult(size_t index)
00168 {
00169 assert(index<size());
00170 ifs.seekg(index*256,std::ios::beg);
00171 CArray<unsigned char, 256> cbuf;
00172 ifs.read(reinterpret_cast<char *>(&cbuf[0]),256);
00173 return cbuf[64+48+6];
00174 }
00175 const std::string KisenIpxFile::getTitle(size_t index,Player pl)
00176 {
00177 assert(index<size());
00178 vector<Move> moves;
00179 ifs.seekg(index*256,std::ios::beg);
00180 CArray<unsigned char, 256> cbuf;
00181 ifs.read(reinterpret_cast<char *>(&cbuf[0]),256);
00182 int startIndex=28;
00183 if(pl==WHITE)startIndex+=8;
00184 CArray<char,9> buf;
00185 buf[8]='\0';
00186 strncpy(&buf[0],reinterpret_cast<const char*>(&cbuf[startIndex]),8);
00187 return std::string(&buf[0]);
00188 }
00189 boost::gregorian::date KisenIpxFile::getStartDate(size_t index)
00190 {
00191 assert(index<size());
00192 ifs.seekg(index*256,std::ios::beg);
00193 CArray<unsigned char, 256> cbuf;
00194 ifs.read(reinterpret_cast<char *>(&cbuf[0]),256);
00195 const int startIndex=84;
00196 const unsigned int year = cbuf[startIndex] + 256*cbuf[startIndex+1];
00197 const unsigned int month = cbuf[startIndex+2];
00198 const unsigned int day = cbuf[startIndex+3];
00199 try {
00200 const boost::gregorian::date d = boost::gregorian::date(year, month, day);
00201 return d;
00202 } catch (std::out_of_range& e) {
00203 std::cerr << e.what() << ": ["
00204 << index << "] " << year << "-" << month << "-" << day << "\n";
00205 return boost::gregorian::date(boost::gregorian::not_a_date_time);
00206 }
00207 }
00208
00209 KisenPlusFile::KisenPlusFile(const std::string& fileName)
00210 :ifs(fileName.c_str()),initialState(HIRATE)
00211 {
00212 if (! ifs)
00213 throw CsaIOError("KisenPlusFile not found");
00214 ifs.seekg(0,std::ios::end);
00215 assert((ifs.tellg() % 2048)==0);
00216 numberOfGames=ifs.tellg()/2048;
00217 }
00218
00219 const vector<Move> KisenPlusFile::getMoves(size_t index)
00220 {
00221 vector<Move> moves;
00222 vector<int> times;
00223 getMoves(index, moves, times);
00224 return moves;
00225 }
00226
00227 void KisenPlusFile::getMoves(size_t index,
00228 vector<Move>& moves, vector<int>& times)
00229 {
00230 assert(index<size());
00231
00232 ifs.seekg(index*2048,std::ios::beg);
00233 CArray<unsigned char, 2048> cbuf;
00234 ifs.read(reinterpret_cast<char *>(&cbuf[0]),2048);
00235 NumEffectState state;
00236 for (size_t i = 0;
00237 i < 2048 && cbuf[i]!=0 && cbuf[i+1]!=0;
00238 i += 8)
00239 {
00240 int c0 = cbuf[i];
00241 int c1 = cbuf[i + 1];
00242 bool is_promote = false;
00243 Move move;
00244
00245 if (c0 > 100)
00246 {
00247 is_promote = true;
00248 c0 = 256 - c0;
00249 }
00250
00251 Square to(c0 % 10, c0 / 10);
00252
00253 if (c1 < 10)
00254 {
00255
00256 move = Move(to,
00257 PieceStand::order[c1 - 1],
00258 state.turn());
00259 }
00260 else
00261 {
00262 Square from(c1 % 10, c1 / 10);
00263 Ptype type = state.pieceAt(from).ptype();
00264 if (is_promote)
00265 type = promote(type);
00266 move = Move(from, to,
00267 type, state.pieceAt(to).ptype(),
00268 is_promote, state.turn());
00269 }
00270 moves.push_back(move);
00271 times.push_back(cbuf[i + 7] * 60 + cbuf[i + 6]);
00272 state.makeMove(move);
00273 assert(state.isConsistent( true ) );
00274 }
00275 }
00276 #endif
00277 }
00278 }
00279
00280 osl::record::
00281 KisenFile::~KisenFile()
00282 {
00283 }
00284 #ifndef MINIMAL
00285 osl::record::
00286 KisenIpxFile::~KisenIpxFile()
00287 {
00288 }
00289
00290 void osl::record::
00291 OKisenStream::save(const SimpleState& src, const vector<Move> &moves)
00292 {
00293 if (!(src == SimpleState(HIRATE)))
00294 {
00295 std::cerr << "Can not save non-HIRATE record" << std::endl;
00296 return;
00297 }
00298 NumEffectState state;
00299 const int max_length = std::min(256, static_cast<int>(moves.size()));
00300 for (int i = 0; i < max_length; ++i)
00301 {
00302 const Move move = moves[i];
00303 if (!move.isDrop())
00304 {
00305 int from = KisenUtils::convertSquare(move.from());
00306 int to = KisenUtils::convertSquare(move.to());
00307 if (move.isPromotion())
00308 {
00309 to += 100;
00310 }
00311 os << static_cast<char>(to) << static_cast<char>(from);
00312 }
00313 else
00314 {
00315 int to = KisenUtils::convertSquare(move.to());
00316 int count = 1;
00317 BOOST_FOREACH(Ptype ptype, PieceStand::order)
00318 {
00319 if (ptype == move.ptype())
00320 {
00321 break;
00322 }
00323 count += state.countPiecesOnStand(move.player(), ptype);
00324 }
00325 count += 100;
00326 os << static_cast<char>(to) << static_cast<char>(count);
00327 }
00328 state.makeMove(moves[i]);
00329 }
00330 for (int i = max_length; i < 256; ++i)
00331 {
00332 os << '\0' << '\0';
00333 }
00334 }
00335
00336 void osl::record::
00337 OKisenStream::save(Record *record)
00338 {
00339 vector<Move> moves;
00340 vector<int> time;
00341 record->getMoves(moves, time);
00342 SimpleState state = record->getInitialState();
00343 save(state, moves);
00344 }
00345
00346 void osl::record::
00347 KisenIpxWriter::writeString(const std::string &name, size_t length)
00348 {
00349 for (size_t i = 0; i < length; ++i)
00350 {
00351 if (i < name.length())
00352 {
00353 os << name[i];
00354 }
00355 else
00356 {
00357 os << '\0';
00358 }
00359 }
00360 }
00361
00362 void osl::record::
00363 KisenIpxWriter::writeRating(int rating)
00364 {
00365 int high = rating / 256;
00366 int low = rating % 256;
00367 os << static_cast<char>(low) << static_cast<char>(high);
00368 }
00369
00370 void osl::record::
00371 KisenIpxWriter::writeStartDate(int year, int month, int day, int hour, int min)
00372 {
00373 const int high_year = year / 256;
00374 const int low_year = year % 256;
00375 os << static_cast<char>(low_year)
00376 << static_cast<char>(high_year)
00377 << static_cast<char>(month)
00378 << static_cast<char>(day)
00379 << static_cast<char>(hour)
00380 << static_cast<char>(min);
00381 }
00382
00383 void osl::record::
00384 KisenIpxWriter::save(const Record &record,
00385 int black_rating, int white_rating,
00386 const std::string &black_title,
00387 const std::string &white_title)
00388 {
00389
00390
00391 #ifndef _WIN32
00392 writeString(misc::IconvConvert::convert("EUC-JP", "SJIS", record.getPlayer(BLACK)), 14);
00393 writeString(misc::IconvConvert::convert("EUC-JP", "SJIS", record.getPlayer(WHITE)), 14);
00394 #endif
00395 writeString(black_title, 8);
00396 writeString(white_title, 8);
00397 for (int i = 44; i < 84; ++i)
00398 {
00399 os << '\0';
00400 }
00401 const boost::gregorian::date start_date = record.getDate();
00402 if (!start_date.is_special()) {
00403
00404 writeStartDate(start_date.year(), start_date.month(), start_date.day(), 9, 0);
00405 } else {
00406 for (int i = 84; i < 90; ++i)
00407 {
00408 os << '\0';
00409 }
00410 }
00411 for (int i = 90; i < 118; ++i)
00412 {
00413 os << '\0';
00414 }
00415 vector<Move> moves;
00416 vector<int> time;
00417 record.getMoves(moves, time);
00418
00419 if (moves.size() <= 256)
00420 {
00421 if (moves.size() % 2 == 0)
00422 os << static_cast<char>(KisenIpxFile::WHITE_WIN);
00423 else
00424 os << static_cast<char>(KisenIpxFile::BLACK_WIN);
00425 }
00426 else
00427 {
00428 if (moves.size() % 2 == 0)
00429 os << static_cast<char>(KisenIpxFile::WHITE_WIN_256);
00430 else
00431 os << static_cast<char>(KisenIpxFile::BLACK_WIN_256);
00432 }
00433 for (int i = 119; i < 212; ++i)
00434 {
00435 os << '\0';
00436 }
00437 writeRating(black_rating);
00438 writeRating(white_rating);
00439 for (int i = 216; i < 256; ++i)
00440 {
00441 os << '\0';
00442 }
00443 }
00444 #endif
00445
00446
00447
00448