All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
gnuShogiClient.cc
Go to the documentation of this file.
1 /* gnuShogiClient.cc
2  */
8 #include "osl/record/psn.h"
9 #include "osl/sennichite.h"
10 #include <iostream>
11 #include <sstream>
12 
15  CsaLogger *l,
16  std::istream& is, std::ostream& os)
17  : CuiClient(black, white, l, is, os)
18 {
19 }
20 
23 {
24 }
25 
28 {
30  std::string line;
31  std::getline(is, line);
32  const long op_think_time = timer.read();
33  if (! is) {
34  logger->inputError("(stream not available)");
35  throw EndGame();
36  }
37 
38  if (line == "quit" || line == "exit")
39  throw GnuShogiQuit();
40 
41  if (line == "undo")
42  {
43  logger->popMove();
44  popMove();
45  return false;
46  }
47  if (line == "force")
48  {
49  setComputerPlayer(BLACK, false);
50  setComputerPlayer(WHITE, false);
51  logger->breakGame();
52  return false;
53  }
54  if (line == "black") {
55  setComputerPlayer(BLACK, true);
56  setComputerPlayer(WHITE, false);
57  return true;
58  }
59  if (line == "white") {
60  setComputerPlayer(BLACK, false);
61  setComputerPlayer(WHITE, true);
62  return true;
63  }
64  if (line == "go") {
65  const Player turn = state->state().turn();
66  if (! isComputer(turn)) {
67  setComputerPlayer(turn, true);
68  setComputerPlayer(alt(turn), false);
69  }
70  return false;
71  }
72  if (line == "new" || line == "beep" || line == "random")
73  return false; // XXX
74  if (line.find("time") == 0 || line.find("otime") == 0) {
75  if (line.find("time") == 0) {
76  std::istringstream ss(line);
77  std::string dummy;
78  int time;
79  ss >> dummy >> time;
80  setTimeLeft(time/100, time/100);
81  }
82  std::cerr << line << "\n";
83  return false;
84  }
85  if (line.find("help") == 0) {
86  std::cerr << " 2g7f move a piece from 2g to 2f\n"
87  " P*2d drop a pawn to 2d\n"
88  " undo undo the last move\n"
89  " force human plays both colors\n"
90  " black/white set computer's color\n"
91  " exit/quit exit program\n";
92  return true;
93  }
94 
95  if (line.size() < 4)
96  goto ignore;
97  if (isdigit(line[0]) || line[1] == '*') // FIXME
98  {
99  const Move op_move=record::psn::strToMove(line, state->state());
100 
101  if (state->isIllegal(op_move))
102  {
103  os << "Illegal move\n";
104  logger->inputError(line.c_str());
105  return true;
106  }
107  const Sennichite result = pushMove(MoveWithComment(op_move), op_think_time);
108  if (! result.isNormal())
109  {
110  if (result == Sennichite::BLACK_LOSE())
111  os << "White mates!\n";
112  else if (result == Sennichite::WHITE_LOSE())
113  os << "Black mates!\n";
114  else
115  os << "Black mates!\n"; // does xshogi know draw?
116  setComputerPlayer(BLACK, false);
117  setComputerPlayer(WHITE, false);
118  logger->endByRepetition(result);
119  throw EndGame();
120  }
121  const int chess_moves = state->chessMoves();
122  os << chess_moves << ". " << record::psn::show(op_move)
123  << " " << (time_keeper.timeLeft(op_move.player()) - op_think_time)*100
124  << std::endl << std::flush;
125  return false;
126  }
127 ignore:
128  std::cerr << line << "\n";
129  std::string comment = "ignored line " + line;
130  logger->writeComment(comment.c_str());
131  return false;
132 }
133 
135 GnuShogiClient::processComputerMove(const MoveWithComment& selected,
136  int my_think_time)
137 {
138  const Move best_move = selected.move;
139  const Player turn = state->state().turn();
140  if ((! best_move.isNormal())
141  || (state->isIllegal(best_move)))
142  {
143  os << ((alt(turn) == BLACK) ? "Black" : "White")
144  << " mates!\n";
145  logger->resign(turn);
146  setComputerPlayer(BLACK, false);
147  setComputerPlayer(WHITE, false);
148  throw EndGame();
149  }
150 
151  const int chess_moves = state->chessMoves();
152  os << chess_moves << ". ... " << record::psn::show(best_move)
153  << " " << (time_keeper.timeLeft(turn) - my_think_time)*100
154  << std::endl << std::flush;
155 
156  const Sennichite result = pushMove(selected, my_think_time);
157  if (! result.isNormal())
158  {
159  if (result == Sennichite::BLACK_LOSE())
160  os << "White mates!\n";
161  else if (result == Sennichite::WHITE_LOSE())
162  os << "Black mates!\n";
163  else
164  os << "Black mates!\n"; // does xshogi know draw?
165  setComputerPlayer(BLACK, false);
166  setComputerPlayer(WHITE, false);
167  logger->endByRepetition(result);
168  throw EndGame();
169  }
170 }
171 
172 /* ------------------------------------------------------------------------- */
173 // ;;; Local Variables:
174 // ;;; mode:c++
175 // ;;; c-basic-offset:2
176 // ;;; End: