All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
showOpening.cc
Go to the documentation of this file.
3 #include "osl/record/psn.h"
5 #include "osl/oslConfig.h"
6 
7 #include <iostream>
8 
9 using namespace osl;
10 using namespace osl::game_playing;
11 using namespace osl::record;
12 using namespace osl::record::opening;
13 
14 void printStats(WinCountBook& book, int index)
15 {
16  std::cout << "Win: " << book.getWinCount(index)
17  << "\t"
18  << "Lose: " << book.getLoseCount(index)
19  << std::endl;
20 }
21 
23  NumEffectState* state)
24 {
25  std::cout << "<moves>" << std::endl;
26  vector<OBMove> moves = book.getMoves(tracer.stateIndex());
27  if (moves.size() == 0)
28  {
29  std::cout << "No more moves in the book" << std::endl;
30  }
31 
32  for (size_t i = 0; i < moves.size(); i++)
33  {
34  std::cout << "<move>" << std::endl;
35  std::cout << psn::show(moves[i].getMove()) << std::endl;
36  printStats(book, moves[i].getStateIndex());
37 
38  if (state != NULL)
39  {
40  NumEffectState newState(*state);
41  newState.makeMove(moves[i].getMove());
42  std::cout << "<board>" << std::endl;
43  std::cout << newState << std::endl;
44  std::cout << "</board>" << std::endl;
45  }
46  std::cout << "</move>" << std::endl;
47  }
48  std::cout << "</moves>" << std::endl;
49 }
50 
51 int main(int argc, char **argv)
52 {
53  std::string bookFilename = OslConfig::openingBook();
54  WinCountBook book(bookFilename.c_str());
55  WinCountTracer tracer(book);
56  NumEffectState state;
57 
58  char *programName = argv[0];
59  bool showNextMoves = false;
60  bool showBoards = false;
61  bool trace = false;
62  bool unknownOption = false;
63 
64  char c;
65  while ((c = getopt(argc, argv, "nst")) != EOF)
66  {
67  switch(c)
68  {
69  case 'n':
70  showNextMoves = true;
71  break;
72  case 's':
73  showBoards = true;
74  break;
75  case 't':
76  trace = true;
77  break;
78  default:
79  unknownOption = true;
80  }
81  }
82 
83  argc -= optind;
84  argv += optind;
85 
86  if (unknownOption)
87  {
88  std::cerr << "Usage: " << programName << " [-n] [-s] [-t]" << std::endl
89  << "[-n show next moves] "
90  << "[-s show boards] "
91  << "[-t show next moves for every move]"
92  << std::endl;
93  return 1;
94  }
95 
96  std::string line;
97 
98  // When in trace mode, show the candidates for the first move, too.
99  if (trace)
100  {
101  printNextMoves(book, tracer, showBoards ? &state : NULL);
102  }
103 
104  while (!std::getline(std::cin, line).eof())
105  {
106  Move move = psn::strToMove(line, state);
107  tracer.update(move);
108  state.makeMove(move);
109 
110  if (trace)
111  {
112  printNextMoves(book, tracer, showBoards ? &state : NULL);
113  }
114 
115  if (tracer.isOutOfBook())
116  {
117  std::cout << "Out of Book" << std::endl;
118  return 0;
119  }
120  }
121 
122  std::cout << "<total>" << std::endl;
123  printStats(book, tracer.stateIndex());
124  if (showBoards)
125  {
126  std::cout << "<board>" << std::endl;
127  std::cout << state << std::endl;
128  std::cout << "</board>" << std::endl;
129  }
130  std::cout << "</total>" << std::endl;
131 
132  if (showNextMoves && !trace)
133  {
134  printNextMoves(book, tracer, showBoards ? &state : NULL);
135  }
136 
137  return 0;
138 }