All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
random_play.cc
Go to the documentation of this file.
1 /* random_play.cc
2  */
6 #include "osl/record/csa.h"
8 #include <boost/random/mersenne_twister.hpp>
9 // #include <boost/random/uniform_int.hpp>
10 #include <string>
11 #include <sys/time.h>
12 using namespace osl;
13 
18 void showState(const NumEffectState& state)
19 {
20  std::cout << state << std::endl;
21 #if 0
22  KanjiPrint printer(std::cout);
23  printer.print(state);
24 #endif
25 }
26 
30 Move selectMove(const NumEffectState& state, const MoveVector& moves)
31 {
32  static boost::mt11213b generator(random());
33  return moves[generator() % moves.size()];
34 #if 0
35  boost::uniform_int<boost::mt11213b> random(generator, 0, moves.size());
36  return moves[random()];
37 #endif
38 }
39 
43 bool isMated(const NumEffectState& state)
44 {
45  return state.inCheck(alt(state.turn()));
46 }
47 
48 int main()
49 {
50  srandom(time(0));
51 
52  NumEffectState state((SimpleState(HIRATE)));
53  std::string line;
54  while (true)
55  {
56  // 自分の手を指す
57  MoveVector moves;
58  LegalMoves::generate(state, moves);
59  if (moves.empty())
60  {
61  std::cerr << "make masita\n";
62  break;
63  }
64  const Move my_move = selectMove(state, moves);
65  assert(state.isValidMove(my_move));
66  state.makeMove(my_move);
67 
68  showState(state);
69  csaShow(std::cout, my_move);
70  std::cout << "\n";
71 
72  if (isMated(state))
73  {
74  std::cerr << "checkmate!";
75  break;
76  }
77 
78  // 相手の指手を読みこむ
79  if (! std::getline(std::cin, line))
80  break;
81 
82  const Move op_move=record::csa::strToMove(line, state);
83  if (! state.isValidMove(op_move))
84  break;
85 
86  state.makeMove(op_move);
87 
88  showState(state);
89  csaShow(std::cout, op_move);
90  std::cout << "\n";
91  }
92 }
93 
94 
95 
96 /* ------------------------------------------------------------------------- */
97 // ;;; Local Variables:
98 // ;;; mode:c++
99 // ;;; c-basic-offset:2
100 // ;;; End: