00001
00002
00003 #include "osl/game_playing/gnuShogiClient.h"
00004 #include "osl/game_playing/gameState.h"
00005 #include "osl/game_playing/csaLogger.h"
00006 #include "osl/game_playing/csaStopwatch.h"
00007 #include "osl/game_playing/moveWithComment.h"
00008 #include "osl/record/psn.h"
00009 #include "osl/sennichite.h"
00010 #include <iostream>
00011 #include <sstream>
00012
00013 osl::game_playing::
00014 GnuShogiClient::GnuShogiClient(ComputerPlayer *black, ComputerPlayer *white,
00015 CsaLogger *l,
00016 std::istream& is, std::ostream& os)
00017 : CuiClient(black, white, l, is, os)
00018 {
00019 }
00020
00021 osl::game_playing::
00022 GnuShogiClient::~GnuShogiClient()
00023 {
00024 }
00025
00026 void osl::game_playing::
00027 GnuShogiClient::readAndProcessCommand()
00028 {
00029 CsaStopwatch timer;
00030 std::string line;
00031 std::getline(is, line);
00032 const long op_think_time = timer.read();
00033 if (! is) {
00034 logger->inputError("(stream not available)");
00035 throw EndGame();
00036 }
00037
00038 if (line == "undo")
00039 {
00040 logger->popMove();
00041 popMove();
00042 return;
00043 }
00044 if (line == "force")
00045 {
00046 setComputerPlayer(BLACK, false);
00047 setComputerPlayer(WHITE, false);
00048 logger->breakGame();
00049 return;
00050 }
00051 if (line == "black") {
00052 setComputerPlayer(BLACK, true);
00053 setComputerPlayer(WHITE, false);
00054 return;
00055 }
00056 if (line == "white") {
00057 setComputerPlayer(BLACK, false);
00058 setComputerPlayer(WHITE, true);
00059 return;
00060 }
00061 if (line == "go" && ! isComputer(BLACK)) {
00062 const Player turn = state->state().getTurn();
00063 setComputerPlayer(turn, true);
00064 setComputerPlayer(alt(turn), false);
00065 return;
00066 }
00067 if (line == "new" || line == "beep" || line == "random")
00068 return;
00069 if (line.find("time") == 0 || line.find("otime") == 0) {
00070 if (line.find("time") == 0) {
00071 std::istringstream ss(line);
00072 std::string dummy;
00073 int time;
00074 ss >> dummy >> time;
00075 setTimeLeft(time/100, time/100);
00076 }
00077 std::cerr << line << "\n";
00078 return;
00079 }
00080
00081 if (line.size() < 4)
00082 goto ignore;
00083 if (isdigit(line[0]) || line[1] == '*')
00084 {
00085 const Move op_move=record::psn::strToMove(line, state->state());
00086
00087 if (state->isIllegal(op_move))
00088 {
00089 os << "Illegal move\n";
00090 logger->inputError(line.c_str());
00091 return;
00092 }
00093 const Sennichite result = pushMove(MoveWithComment(op_move), op_think_time);
00094 if (! result.isNormal())
00095 {
00096 if (result == Sennichite::BLACK_LOSE())
00097 os << "White mates!\n";
00098 else if (result == Sennichite::WHITE_LOSE())
00099 os << "Black mates!\n";
00100 else
00101 os << "Black mates!\n";
00102 setComputerPlayer(BLACK, false);
00103 setComputerPlayer(WHITE, false);
00104 logger->endByRepetition(result);
00105 throw EndGame();
00106 }
00107 return;
00108 }
00109 ignore:
00110 std::cerr << line << "\n";
00111 std::string comment = "ignored line " + line;
00112 logger->writeComment(comment.c_str());
00113 }
00114
00115 void osl::game_playing::
00116 GnuShogiClient::processComputerMove(const MoveWithComment& selected,
00117 int my_think_time)
00118 {
00119 const Move best_move = selected.move;
00120 const Player turn = state->state().getTurn();
00121 if ((! best_move.isNormal())
00122 || (state->isIllegal(best_move)))
00123 {
00124 os << ((alt(turn) == BLACK) ? "Black" : "White")
00125 << " mates!\n";
00126 logger->resign(turn);
00127 setComputerPlayer(BLACK, false);
00128 setComputerPlayer(WHITE, false);
00129 throw EndGame();
00130 }
00131
00132 const int ply = state->ply();
00133 os << ply << ". ... " << record::psn::show(best_move)
00134 << " " << (time_keeper.timeLeft(turn) - my_think_time)*100
00135 << std::endl << std::flush;
00136
00137 const Sennichite result = pushMove(selected, my_think_time);
00138 if (! result.isNormal())
00139 {
00140 if (result == Sennichite::BLACK_LOSE())
00141 os << "White mates!\n";
00142 else if (result == Sennichite::WHITE_LOSE())
00143 os << "Black mates!\n";
00144 else
00145 os << "Black mates!\n";
00146 setComputerPlayer(BLACK, false);
00147 setComputerPlayer(WHITE, false);
00148 logger->endByRepetition(result);
00149 throw EndGame();
00150 }
00151 }
00152
00153
00154
00155
00156
00157