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/game_playing/moveWithComment.h"
00009 #include "osl/record/csa.h"
00010 #include "osl/record/csaIOError.h"
00011 #include "osl/sennichite.h"
00012
00013 #include <iostream>
00014
00015 osl::game_playing::
00016 CsaClient::CsaClient(ComputerPlayer *black, ComputerPlayer *white,
00017 CsaLogger *l, std::istream& is, std::ostream& os)
00018 : CuiClient(black, white, l, is, os),
00019 show_move_with_comment(false), silent(false), line(128,' ')
00020 {
00021 setComputerPlayer(WHITE, true);
00022 }
00023
00024 osl::game_playing::
00025 CsaClient::~CsaClient()
00026 {
00027 }
00028
00029 void osl::game_playing::
00030 CsaClient::readAndProcessCommand()
00031 {
00032 char ctime_buf[64];
00033 if (! silent) {
00034 std::cerr << "\nCsaClient start waiting ";
00035 const time_t now = time(0);
00036 std::cerr << ctime_r(&now, ctime_buf)
00037 << state->state() << std::endl;
00038 }
00039 CsaStopwatch timer;
00040 std::getline(is, line);
00041 if (! silent) {
00042 std::cerr << "\nCsaClient read " << line << " ";
00043 const time_t now = time(0);
00044 std::cerr << ctime_r(&now, ctime_buf) << std::endl;
00045 }
00046 const long op_think_time = timer.read();
00047 if (! is)
00048 {
00049 const char *message = "istream error (maybe closed)";
00050 std::cerr << message << std::cerr;
00051 logger->writeComment(message);
00052 throw EndGame();
00053 }
00054
00055 if (line == "%TORYO")
00056 {
00057 logger->resign(state->state().getTurn());
00058 throw EndGame();
00059 }
00060
00061
00062 try
00063 {
00064 const Move op_move=record::csa::strToMove(line, state->state());
00065 const GameState::MoveType illegal_move = state->isIllegal(op_move);
00066 if (illegal_move)
00067 {
00068 std::cerr << "illegal move: " << line << "\n";
00069 logger->inputError(line.c_str());
00070 if (illegal_move == GameState::PAWN_DROP_FOUL)
00071 logger->writeComment("pawn drop foul");
00072 else if (illegal_move == GameState::UNSAFE_KING)
00073 logger->writeComment("unsafe king");
00074 else if (illegal_move == GameState::OTHER_INVALID)
00075 logger->writeComment("other illegal move");
00076 throw EndGame();
00077 }
00078 const Sennichite result = pushMove(MoveWithComment(op_move), op_think_time);
00079 if (! result.isNormal())
00080 {
00081 os << "%SENNICHITE" << std::endl;
00082 logger->endByRepetition(result);
00083 throw EndGame();
00084 }
00085 }
00086 catch (record::csa::CsaIOError&)
00087 {
00088 std::cerr << "bad input: " << line << "\n";
00089 logger->inputError(line.c_str());
00090 throw EndGame();
00091 }
00092 return;
00093 }
00094
00095 void osl::game_playing::
00096 CsaClient::setShowMoveWithComment(bool value)
00097 {
00098 show_move_with_comment = value;
00099 }
00100
00101 void osl::game_playing::
00102 CsaClient::processComputerMove(const MoveWithComment& selected,
00103 int my_think_time)
00104 {
00105 static std::string reserved="+7776FU";
00106 const Move best_move = selected.move;
00107 if ((! best_move.isNormal())
00108 || (state->isIllegal(best_move)))
00109 {
00110 if (best_move == Move::DeclareWin())
00111 {
00112 os << "%KACHI\n";
00113 logger->endByDeclaration(state->state().getTurn());
00114 }
00115 else
00116 {
00117 os << "%TORYO\n";
00118 logger->resign(state->state().getTurn());
00119 }
00120 throw EndGame();
00121 }
00122
00123 os << record::csa::show(best_move, reserved);
00124 if (show_move_with_comment)
00125 {
00126 os << ",'* " << selected.value;
00127 for (vector<Move>::const_iterator p=selected.moves.begin();
00128 p!=selected.moves.end(); ++p)
00129 {
00130 os << " ";
00131 os << record::csa::show(*p, reserved);
00132 }
00133 }
00134 os << std::endl << std::flush;
00135
00136 assert(isComputer(state->state().getTurn()));
00137
00138 const Sennichite result = pushMove(selected, my_think_time);
00139 if (! result.isNormal())
00140 {
00141 logger->endByRepetition(result);
00142 throw EndGame();
00143 }
00144 }
00145
00146
00147
00148
00149
00150