00001
00002
00003 #include "osl/game_playing/csaClient.h"
00004 #include "osl/game_playing/gnuShogiClient.h"
00005 #include "osl/game_playing/gameState.h"
00006 #include "osl/game_playing/csaLogger.h"
00007 #include "osl/game_playing/csaStopwatch.h"
00008 #include "osl/search/moveWithComment.h"
00009 #include "osl/record/csa.h"
00010 #include "osl/record/csaIOError.h"
00011 #include "osl/container/moveStack.h"
00012 #include "osl/sennichite.h"
00013 #include "osl/misc/ctime.h"
00014 #include <boost/foreach.hpp>
00015 #include <iostream>
00016
00017 osl::game_playing::
00018 CsaClient::CsaClient(ComputerPlayer *black, ComputerPlayer *white,
00019 CsaLogger *l, std::istream& is, std::ostream& os)
00020 : CuiClient(black, white, l, is, os),
00021 show_move_with_comment(false), silent(false), line(128,' ')
00022 {
00023 setComputerPlayer(WHITE, true);
00024 }
00025
00026 osl::game_playing::
00027 CsaClient::~CsaClient()
00028 {
00029 }
00030
00031 void osl::game_playing::
00032 CsaClient::readAndProcessCommand()
00033 {
00034 char ctime_buf[64];
00035 if (! silent) {
00036 std::cerr << "\nCsaClient start waiting ";
00037 const time_t now = time(0);
00038 std::cerr << ctime_r(&now, ctime_buf)
00039 << state->state()
00040 << "TIME[" << time_keeper.timeElapsed(BLACK)
00041 << ":" << time_keeper.timeElapsed(WHITE)
00042 << "] ";
00043 const MoveStack& history = state->moveHistory();
00044 const vector<int>& eval_history = state->evalStack();
00045 for (int i=1; i<=8 && history.hasLastMove(i); ++i) {
00046 std::cerr << "(" << history.size() - i + 1 << ")" << record::csa::show(history.lastMove(i));
00047 if (i-1 < (int)eval_history.size() && eval_history[eval_history.size()-i])
00048 std::cerr << "<" << eval_history[eval_history.size()-i] << ">";
00049 std::cerr << " ";
00050 }
00051 std::cerr << std::endl << std::endl;
00052 }
00053 CsaStopwatch timer;
00054 std::getline(is, line);
00055 if (! silent) {
00056 std::cerr << "\nCsaClient read " << line << " ";
00057 const time_t now = time(0);
00058 std::cerr << ctime_r(&now, ctime_buf)
00059 << std::endl;
00060 }
00061 const long op_think_time = timer.read();
00062 if (! is)
00063 {
00064 const char *message = "istream error (maybe closed)";
00065 std::cerr << message << std::cerr;
00066 logger->writeComment(message);
00067 throw EndGame();
00068 }
00069
00070 if (line == "%TORYO")
00071 {
00072 logger->resign(state->state().getTurn());
00073 throw EndGame();
00074 }
00075
00076
00077 try
00078 {
00079 const Move op_move=record::csa::strToMove(line, state->state());
00080 const GameState::MoveType illegal_move = state->isIllegal(op_move);
00081 if (illegal_move)
00082 {
00083 std::cerr << "illegal move: " << line << "\n";
00084 logger->inputError(line.c_str());
00085 if (illegal_move == GameState::PAWN_DROP_FOUL)
00086 logger->writeComment("pawn drop foul");
00087 else if (illegal_move == GameState::UNSAFE_KING)
00088 logger->writeComment("unsafe king");
00089 else if (illegal_move == GameState::OTHER_INVALID)
00090 logger->writeComment("other illegal move");
00091 throw EndGame();
00092 }
00093 const Sennichite result = pushMove(MoveWithComment(op_move), op_think_time);
00094 if (! result.isNormal())
00095 {
00096 os << "%SENNICHITE" << std::endl;
00097 logger->endByRepetition(result);
00098 throw EndGame();
00099 }
00100 if (! silent) {
00101 std::cerr << state->state()
00102 << "TIME[" << time_keeper.timeElapsed(BLACK)
00103 << ":" << time_keeper.timeElapsed(WHITE)
00104 << "] ";
00105 const MoveStack& history = state->moveHistory();
00106 const vector<int>& eval_history = state->evalStack();
00107 for (int i=1; i<=8 && history.hasLastMove(i); ++i) {
00108 std::cerr << "(" << history.size() - i + 1 << ")" << record::csa::show(history.lastMove(i));
00109 if (i-1 < (int)eval_history.size() && eval_history[eval_history.size()-i])
00110 std::cerr << "<" << eval_history[eval_history.size()-i] << "> ";
00111 std::cerr << " ";
00112 }
00113 std::cerr << std::endl << std::endl;
00114 }
00115 }
00116 catch (record::csa::CsaIOError&)
00117 {
00118 std::cerr << "bad input: " << line << "\n";
00119 logger->inputError(line.c_str());
00120 throw EndGame();
00121 }
00122 return;
00123 }
00124
00125 void osl::game_playing::
00126 CsaClient::setShowMoveWithComment(bool value)
00127 {
00128 show_move_with_comment = value;
00129 }
00130
00131 void osl::game_playing::
00132 CsaClient::processComputerMove(const MoveWithComment& selected,
00133 int my_think_time)
00134 {
00135 static std::string reserved="+7776FU";
00136 const Move best_move = selected.move;
00137 if ((! best_move.isNormal())
00138 || (state->isIllegal(best_move)))
00139 {
00140 if (best_move == Move::DeclareWin())
00141 {
00142 os << "%KACHI\n";
00143 logger->endByDeclaration(state->state().getTurn());
00144 }
00145 else
00146 {
00147 if (best_move.isNormal()) {
00148 std::cerr << "error: prefer resign to playing illegal move " << best_move << " code " << state->isIllegal(best_move) << "\n";
00149 logger->writeComment("error: prefer abort to playing illegal move");
00150 abort();
00151 }
00152 os << "%TORYO\n";
00153 logger->resign(state->state().getTurn());
00154 }
00155 throw EndGame();
00156 }
00157
00158 os << record::csa::show(best_move, reserved);
00159 if (show_move_with_comment && ! selected.moves.empty())
00160 {
00161 os << ",'* " << selected.value;
00162 BOOST_FOREACH(Move move, selected.moves)
00163 {
00164 os << " ";
00165 os << record::csa::show(move, reserved);
00166 }
00167 }
00168 os << std::endl << std::flush;
00169
00170 assert(isComputer(state->state().getTurn()));
00171
00172 const Sennichite result = pushMove(selected, my_think_time);
00173 if (! result.isNormal())
00174 {
00175 logger->endByRepetition(result);
00176 throw EndGame();
00177 }
00178 }
00179
00180
00181
00182
00183
00184