all-states2.cc
Go to the documentation of this file.
00001 #include "osl/state/numEffectState.h"
00002 #include "osl/hash/hashKey.h"
00003 
00004 #include <boost/program_options.hpp>
00005 #include <boost/format.hpp>
00006 #include "osl/record/kisen.h"
00007 #include "osl/record/csa.h"
00008 #include <osl/stl/hash_set.h>
00009 
00010 #include <iostream>
00011 #include <fstream>
00012 #include <sstream>
00013 
00014 struct hash
00015 {
00016   unsigned long operator() (const osl::state::SimpleState &state) const
00017   {
00018     return osl::hash::HashKey(state).signature();
00019   }
00020 };
00021 
00022 void find_all(const int num_ply, const int threshold,
00023               bool save, const std::vector<std::string> &files)
00024 {
00025   osl::stl::hash_set<osl::state::SimpleState, hash> states;
00026 
00027   for (size_t index = 0; index < files.size(); index++)
00028   {
00029     osl::record::KisenFile kisen(files[index]);
00030     for (size_t i = 0; i < kisen.size(); i++)
00031     {
00032       const osl::vector<osl::Move> moves = kisen.getMoves(i);
00033       osl::state::NumEffectState state(kisen.getInitialState());
00034 
00035       states.insert(state);
00036       for (size_t j = 0; j < moves.size() && j < static_cast<size_t>(num_ply);
00037            j++)
00038       {
00039         const osl::Square opKingSquare 
00040           = state.kingSquare(alt(state.turn()));
00041         if (state.hasEffectAt(state.turn(), opKingSquare))
00042         {
00043           break;
00044         }
00045         state.makeMove(moves[j]);
00046         states.insert(state);
00047       }
00048     }
00049   }
00050 
00051   int index = 0;
00052   for (osl::stl::hash_set<osl::state::SimpleState, hash>::const_iterator it =
00053          states.begin();
00054        it != states.end();
00055        ++it)
00056   {
00057     if (save)
00058     {
00059       std::ofstream output;
00060       output.open((boost::format("%05d.csa") % index++).str().c_str());
00061       output << *it;
00062       output.close();
00063     }
00064     else
00065     {
00066       std::cout << *it;
00067     }
00068   }
00069 }
00070 
00071 int main(int argc, char **argv)
00072 {
00073   int num_ply;
00074   int threshold;
00075   bool save_moves;
00076   boost::program_options::options_description command_line_options;
00077   command_line_options.add_options()
00078     ("num-ply",
00079      boost::program_options::value<int>(&num_ply)->default_value(10),
00080      "Show states after this number of plies are played")
00081     ("threshold",
00082      boost::program_options::value<int>(&threshold)->default_value(10),
00083      "Each state must appear this number of times to be shown")
00084     ("save",
00085      boost::program_options::value<bool>(&save_moves)->default_value(false),
00086      "Save moves leading to states to files in CSA format")
00087     ("input-file", boost::program_options::value< std::vector<std::string> >(),
00088      "input files in kisen format")
00089     ("help", "Show help message");
00090   boost::program_options::variables_map vm;
00091   boost::program_options::positional_options_description p;
00092   p.add("input-file", -1);
00093 
00094   try
00095   {
00096     boost::program_options::store(
00097       boost::program_options::command_line_parser(
00098         argc, argv).options(command_line_options).positional(p).run(), vm);
00099     boost::program_options::notify(vm);
00100     if (vm.count("help"))
00101     {
00102       std::cerr << "Usage: " << argv[0] << " [options] kisen-file"
00103                 << std::endl;
00104       std::cout << command_line_options << std::endl;
00105       return 0;
00106     }
00107   }
00108   catch (std::exception &e)
00109   {
00110     std::cerr << "error in parsing options" << std::endl
00111               << e.what() << std::endl;
00112     std::cerr << "Usage: " << argv[0] << " [options] kisen-file" << std::endl;
00113     std::cerr << command_line_options << std::endl;
00114     return 1;
00115   }
00116 
00117   const std::vector<std::string> files =
00118     vm["input-file"].as< std::vector<std::string> >();
00119   find_all(num_ply, threshold, save_moves, files);
00120 
00121   return 0;
00122 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines