All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
all-states2.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 "osl/record/kisen.h"
7 #include "osl/record/csa.h"
8 #include <osl/stl/hash_set.h>
9 
10 #include <iostream>
11 #include <fstream>
12 #include <sstream>
13 
14 struct hash
15 {
16  unsigned long operator() (const osl::state::SimpleState &state) const
17  {
18  return osl::hash::HashKey(state).signature();
19  }
20 };
21 
22 void find_all(const int num_ply, const int threshold,
23  bool save, const std::vector<std::string> &files)
24 {
26 
27  for (size_t index = 0; index < files.size(); index++)
28  {
29  osl::record::KisenFile kisen(files[index]);
30  for (size_t i = 0; i < kisen.size(); i++)
31  {
32  const osl::vector<osl::Move> moves = kisen.getMoves(i);
34 
35  states.insert(state);
36  for (size_t j = 0; j < moves.size() && j < static_cast<size_t>(num_ply);
37  j++)
38  {
39  const osl::Square opKingSquare
40  = state.kingSquare(alt(state.turn()));
41  if (state.hasEffectAt(state.turn(), opKingSquare))
42  {
43  break;
44  }
45  state.makeMove(moves[j]);
46  states.insert(state);
47  }
48  }
49  }
50 
51  int index = 0;
53  states.begin();
54  it != states.end();
55  ++it)
56  {
57  if (save)
58  {
59  std::ofstream output;
60  output.open((boost::format("%05d.csa") % index++).str().c_str());
61  output << *it;
62  output.close();
63  }
64  else
65  {
66  std::cout << *it;
67  }
68  }
69 }
70 
71 int main(int argc, char **argv)
72 {
73  int num_ply;
74  int threshold;
75  bool save_moves;
76  boost::program_options::options_description command_line_options;
77  command_line_options.add_options()
78  ("num-ply",
79  boost::program_options::value<int>(&num_ply)->default_value(10),
80  "Show states after this number of plies are played")
81  ("threshold",
82  boost::program_options::value<int>(&threshold)->default_value(10),
83  "Each state must appear this number of times to be shown")
84  ("save",
85  boost::program_options::value<bool>(&save_moves)->default_value(false),
86  "Save moves leading to states to files in CSA format")
87  ("input-file", boost::program_options::value< std::vector<std::string> >(),
88  "input files in kisen format")
89  ("help", "Show help message");
90  boost::program_options::variables_map vm;
91  boost::program_options::positional_options_description p;
92  p.add("input-file", -1);
93 
94  try
95  {
97  boost::program_options::command_line_parser(
98  argc, argv).options(command_line_options).positional(p).run(), vm);
99  boost::program_options::notify(vm);
100  if (vm.count("help"))
101  {
102  std::cerr << "Usage: " << argv[0] << " [options] kisen-file"
103  << std::endl;
104  std::cout << command_line_options << std::endl;
105  return 0;
106  }
107  }
108  catch (std::exception &e)
109  {
110  std::cerr << "error in parsing options" << std::endl
111  << e.what() << std::endl;
112  std::cerr << "Usage: " << argv[0] << " [options] kisen-file" << std::endl;
113  std::cerr << command_line_options << std::endl;
114  return 1;
115  }
116 
117  const std::vector<std::string> files =
118  vm["input-file"].as< std::vector<std::string> >();
119  find_all(num_ply, threshold, save_moves, files);
120 
121  return 0;
122 }