All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
find-states.cc
Go to the documentation of this file.
2 #include "osl/hash/hashKey.h"
3 
4 #include <boost/program_options.hpp>
5 #include <boost/format.hpp>
6 #include <boost/scoped_ptr.hpp>
7 #include "osl/record/kisen.h"
8 #include "osl/record/csa.h"
9 #include "osl/record/csaRecord.h"
10 #include "osl/stl/hash_set.h"
11 
12 #include <iostream>
13 #include <fstream>
14 
15 struct hash
16 {
17  unsigned long operator() (const osl::state::SimpleState &state) const
18  {
19  return osl::hash::HashKey(state).signature();
20  }
21 };
22 
24 {
25 public:
26  StatePredicate(const std::vector<std::string> &filenames) { }
27  virtual ~StatePredicate() { }
28  virtual bool match (const osl::state::NumEffectState &state) const
29  {
30  return false;
31  }
32  virtual bool isLoaded() const { return false; }
33 };
34 
36 {
37 public:
38  CsaPredicate(const std::vector<std::string> &filenames)
39  : StatePredicate(filenames)
40  {
41  for (size_t i = 0; i < filenames.size(); ++i)
42  {
43  osl::record::csa::CsaFile file(filenames[i]);
44  states.insert(file.getInitialState());
45  }
46  }
48  bool match (const osl::state::NumEffectState &state) const
49  {
50  return states.find(state) != states.end();
51  }
52  bool isLoaded() const
53  {
54  return !states.empty();
55  }
56 private:
58 };
59 
61 {
62 private:
63  bool match(const osl::state::NumEffectState &state, osl::Player player) const
64  {
65  return state.countPiecesOnStand<osl::ROOK>(player) == 1 &&
66  state.countPiecesOnStand<osl::BISHOP>(player) == 1 &&
67  state.countPiecesOnStand<osl::GOLD>(player) == 0 &&
68  state.countPiecesOnStand<osl::SILVER>(player) == 1 &&
69  state.countPiecesOnStand<osl::KNIGHT>(player) == 3 &&
70  state.countPiecesOnStand<osl::LANCE>(player) == 3;
71  }
72 public:
73  PieceStandPredicate(const std::vector<std::string> &filenames)
74  : StatePredicate(filenames) { }
75  bool match (const osl::state::NumEffectState &state) const
76  {
77  return match(state, osl::BLACK) || match(state, osl::WHITE);
78  }
79  bool isLoaded() const { return true; }
80 };
81 
82 int main(int argc, char **argv)
83 {
84  std::string kisen_filename, predicate_name;
85  boost::program_options::options_description command_line_options;
86  command_line_options.add_options()
87  ("kisen",
88  boost::program_options::value<std::string>(&kisen_filename)->
89  default_value(""),
90  "Kisen filename to search")
91  ("predicate",
92  boost::program_options::value<std::string>(&predicate_name)->
93  default_value("csa"),
94  "Predicate to use. Valid options are csa and stand")
95  ("input-file", boost::program_options::value< std::vector<std::string> >(),
96  "input files in kisen format")
97  ("help", "Show help message");
98  boost::program_options::variables_map vm;
99  boost::program_options::positional_options_description p;
100  p.add("input-file", -1);
101 
102  try
103  {
105  boost::program_options::command_line_parser(
106  argc, argv).options(command_line_options).positional(p).run(), vm);
107  boost::program_options::notify(vm);
108  if (vm.count("help"))
109  {
110  std::cerr << "Usage: " << argv[0] << " [options] CSA_FILES"
111  << std::endl;
112  std::cout << command_line_options << std::endl;
113  return 0;
114  }
115  }
116  catch (std::exception &e)
117  {
118  std::cerr << "error in parsing options" << std::endl
119  << e.what() << std::endl;
120  std::cerr << "Usage: " << argv[0] << " [options] CSA_FILES" << std::endl;
121  std::cerr << command_line_options << std::endl;
122  return 1;
123  }
124 
125  std::vector<std::string> files;
126 
127  if (vm.count("input-file"))
128  files = vm["input-file"].as< std::vector<std::string> >();
129 
130  boost::scoped_ptr<StatePredicate> predicate;
131  if (predicate_name == "csa")
132  {
133  predicate.reset(new CsaPredicate(files));
134  }
135  else if (predicate_name == "stand")
136  {
137  predicate.reset(new PieceStandPredicate(files));
138  }
139  else
140  {
141  std::cerr << "Unknown predicate " << predicate_name;
142  return 1;
143  }
144 
145  if (!predicate->isLoaded())
146  {
147  std::cerr << "No target" << std::endl;
148  }
149  osl::record::KisenFile kisen(kisen_filename);
150  for (size_t i = 0; i < kisen.size(); i++)
151  {
152  const osl::vector<osl::Move> moves = kisen.getMoves(i);
154  for (size_t j = 0; j < moves.size(); j++)
155  {
156  const osl::Square opKingSquare
157  = state.kingSquare(alt(state.turn()));
158  if (state.hasEffectAt(state.turn(), opKingSquare))
159  {
160  break;
161  }
162  state.makeMove(moves[j]);
163  if (predicate->match(state))
164  {
165  std::cout << i << " " << j << std::endl << state;
166  }
167  }
168  }
169 
170  return 0;
171 }