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 <boost/scoped_ptr.hpp>
00007 #include "osl/record/kisen.h"
00008 #include "osl/record/csa.h"
00009 #include "osl/record/csaRecord.h"
00010 #include "osl/stl/hash_set.h"
00011
00012 #include <iostream>
00013 #include <fstream>
00014
00015 struct hash
00016 {
00017 unsigned long operator() (const osl::state::SimpleState &state) const
00018 {
00019 return osl::hash::HashKey(state).signature();
00020 }
00021 };
00022
00023 class StatePredicate
00024 {
00025 public:
00026 StatePredicate(const std::vector<std::string> &filenames) { }
00027 virtual ~StatePredicate() { }
00028 virtual bool match (const osl::state::NumEffectState &state) const
00029 {
00030 return false;
00031 }
00032 virtual bool isLoaded() const { return false; }
00033 };
00034
00035 class CsaPredicate : public StatePredicate
00036 {
00037 public:
00038 CsaPredicate(const std::vector<std::string> &filenames)
00039 : StatePredicate(filenames)
00040 {
00041 for (size_t i = 0; i < filenames.size(); ++i)
00042 {
00043 osl::record::csa::CsaFile file(filenames[i]);
00044 states.insert(file.getInitialState());
00045 }
00046 }
00047 ~CsaPredicate() { }
00048 bool match (const osl::state::NumEffectState &state) const
00049 {
00050 return states.find(state) != states.end();
00051 }
00052 bool isLoaded() const
00053 {
00054 return !states.empty();
00055 }
00056 private:
00057 osl::stl::hash_set<osl::state::SimpleState, hash> states;
00058 };
00059
00060 class PieceStandPredicate : public StatePredicate
00061 {
00062 private:
00063 bool match(const osl::state::NumEffectState &state, osl::Player player) const
00064 {
00065 return state.countPiecesOnStand<osl::ROOK>(player) == 1 &&
00066 state.countPiecesOnStand<osl::BISHOP>(player) == 1 &&
00067 state.countPiecesOnStand<osl::GOLD>(player) == 0 &&
00068 state.countPiecesOnStand<osl::SILVER>(player) == 1 &&
00069 state.countPiecesOnStand<osl::KNIGHT>(player) == 3 &&
00070 state.countPiecesOnStand<osl::LANCE>(player) == 3;
00071 }
00072 public:
00073 PieceStandPredicate(const std::vector<std::string> &filenames)
00074 : StatePredicate(filenames) { }
00075 bool match (const osl::state::NumEffectState &state) const
00076 {
00077 return match(state, osl::BLACK) || match(state, osl::WHITE);
00078 }
00079 bool isLoaded() const { return true; }
00080 };
00081
00082 int main(int argc, char **argv)
00083 {
00084 std::string kisen_filename, predicate_name;
00085 boost::program_options::options_description command_line_options;
00086 command_line_options.add_options()
00087 ("kisen",
00088 boost::program_options::value<std::string>(&kisen_filename)->
00089 default_value(""),
00090 "Kisen filename to search")
00091 ("predicate",
00092 boost::program_options::value<std::string>(&predicate_name)->
00093 default_value("csa"),
00094 "Predicate to use. Valid options are csa and stand")
00095 ("input-file", boost::program_options::value< std::vector<std::string> >(),
00096 "input files in kisen format")
00097 ("help", "Show help message");
00098 boost::program_options::variables_map vm;
00099 boost::program_options::positional_options_description p;
00100 p.add("input-file", -1);
00101
00102 try
00103 {
00104 boost::program_options::store(
00105 boost::program_options::command_line_parser(
00106 argc, argv).options(command_line_options).positional(p).run(), vm);
00107 boost::program_options::notify(vm);
00108 if (vm.count("help"))
00109 {
00110 std::cerr << "Usage: " << argv[0] << " [options] CSA_FILES"
00111 << std::endl;
00112 std::cout << command_line_options << std::endl;
00113 return 0;
00114 }
00115 }
00116 catch (std::exception &e)
00117 {
00118 std::cerr << "error in parsing options" << std::endl
00119 << e.what() << std::endl;
00120 std::cerr << "Usage: " << argv[0] << " [options] CSA_FILES" << std::endl;
00121 std::cerr << command_line_options << std::endl;
00122 return 1;
00123 }
00124
00125 std::vector<std::string> files;
00126
00127 if (vm.count("input-file"))
00128 files = vm["input-file"].as< std::vector<std::string> >();
00129
00130 boost::scoped_ptr<StatePredicate> predicate;
00131 if (predicate_name == "csa")
00132 {
00133 predicate.reset(new CsaPredicate(files));
00134 }
00135 else if (predicate_name == "stand")
00136 {
00137 predicate.reset(new PieceStandPredicate(files));
00138 }
00139 else
00140 {
00141 std::cerr << "Unknown predicate " << predicate_name;
00142 return 1;
00143 }
00144
00145 if (!predicate->isLoaded())
00146 {
00147 std::cerr << "No target" << std::endl;
00148 }
00149 osl::record::KisenFile kisen(kisen_filename);
00150 for (size_t i = 0; i < kisen.size(); i++)
00151 {
00152 const osl::vector<osl::Move> moves = kisen.getMoves(i);
00153 osl::state::NumEffectState state = kisen.getInitialState();
00154 for (size_t j = 0; j < moves.size(); j++)
00155 {
00156 const osl::Square opKingSquare
00157 = state.kingSquare(alt(state.turn()));
00158 if (state.hasEffectAt(state.turn(), opKingSquare))
00159 {
00160 break;
00161 }
00162 state.makeMove(moves[j]);
00163 if (predicate->match(state))
00164 {
00165 std::cout << i << " " << j << std::endl << state;
00166 }
00167 }
00168 }
00169
00170 return 0;
00171 }