showOpening.cc
Go to the documentation of this file.
00001 #include "osl/game_playing/winCountTracer.h"
00002 #include "osl/record/opening/openingBook.h"
00003 #include "osl/record/psn.h"
00004 #include "osl/state/numEffectState.h"
00005 #include "osl/oslConfig.h"
00006 
00007 #include <iostream>
00008 
00009 using namespace osl;
00010 using namespace osl::game_playing;
00011 using namespace osl::record;
00012 using namespace osl::record::opening;
00013 
00014 void printStats(WinCountBook& book, int index)
00015 {
00016   std::cout << "Win: " << book.getWinCount(index)
00017             << "\t"
00018             << "Lose: " << book.getLoseCount(index)
00019             << std::endl;
00020 }
00021 
00022 void printNextMoves(WinCountBook& book, WinCountTracer& tracer,
00023                     NumEffectState* state)
00024 {
00025   std::cout << "<moves>" << std::endl;
00026   vector<OBMove> moves = book.getMoves(tracer.stateIndex());
00027   if (moves.size() == 0)
00028     {
00029       std::cout << "No more moves in the book" << std::endl;
00030     }
00031 
00032   for (size_t i = 0; i < moves.size(); i++)
00033     {
00034       std::cout << "<move>" << std::endl;
00035       std::cout << psn::show(moves[i].getMove()) << std::endl;
00036       printStats(book, moves[i].getStateIndex());
00037 
00038       if (state != NULL)
00039         {
00040           NumEffectState newState(*state);
00041           newState.makeMove(moves[i].getMove());
00042           std::cout << "<board>" << std::endl;
00043           std::cout << newState << std::endl;
00044           std::cout << "</board>" << std::endl;
00045         }
00046       std::cout << "</move>" << std::endl;
00047     }
00048   std::cout << "</moves>" << std::endl;
00049 }
00050 
00051 int main(int argc, char **argv)
00052 {
00053   std::string bookFilename = OslConfig::openingBook();
00054   WinCountBook book(bookFilename.c_str());
00055   WinCountTracer tracer(book);
00056   NumEffectState state;
00057 
00058   char *programName = argv[0];
00059   bool showNextMoves = false;
00060   bool showBoards = false;
00061   bool trace = false;
00062   bool unknownOption = false;
00063 
00064   char c;
00065   while ((c = getopt(argc, argv, "nst")) != EOF)
00066     {
00067       switch(c)
00068         {
00069         case 'n':
00070           showNextMoves = true;
00071           break;
00072         case 's':
00073           showBoards = true;
00074           break;
00075         case 't':
00076           trace = true;
00077           break;
00078         default:
00079           unknownOption = true;
00080         }
00081     }
00082 
00083   argc -= optind;
00084   argv += optind;
00085 
00086   if (unknownOption)
00087     {
00088       std::cerr << "Usage: " << programName << " [-n] [-s] [-t]" << std::endl
00089                 << "[-n show next moves] "
00090                 << "[-s show boards] "
00091                 << "[-t show next moves for every move]"
00092                 << std::endl;
00093       return 1;
00094     }
00095 
00096   std::string line;
00097 
00098   // When in trace mode, show the candidates for the first move, too.
00099   if (trace)
00100     {
00101       printNextMoves(book, tracer, showBoards ? &state : NULL);
00102     }
00103 
00104   while (!std::getline(std::cin, line).eof())
00105     {
00106       Move move = psn::strToMove(line, state);
00107       tracer.update(move);
00108       state.makeMove(move);
00109 
00110       if (trace)
00111         {
00112           printNextMoves(book, tracer, showBoards ? &state : NULL);
00113         }
00114 
00115       if (tracer.isOutOfBook())
00116         {
00117           std::cout << "Out of Book" << std::endl;
00118           return 0;
00119         }
00120     }
00121 
00122   std::cout << "<total>" << std::endl;
00123   printStats(book, tracer.stateIndex());
00124   if (showBoards)
00125     {
00126       std::cout << "<board>" << std::endl;
00127       std::cout << state << std::endl;
00128       std::cout << "</board>" << std::endl;
00129     }
00130   std::cout << "</total>" << std::endl;
00131 
00132   if (showNextMoves && !trace)
00133     {
00134       printNextMoves(book, tracer, showBoards ? &state : NULL);
00135     }
00136 
00137   return 0;
00138 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines