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