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