All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
all-states.cc
Go to the documentation of this file.
2 #include "osl/hash/hashKey.h"
3 
4 #include <boost/program_options.hpp>
5 #include "osl/record/kisen.h"
6 #include "osl/record/csa.h"
7 #include "osl/stl/hash_map.h"
8 
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 
13 struct hash
14 {
15  unsigned long operator() (const osl::state::SimpleState &state) const
16  {
17  return osl::hash::HashKey(state).signature();
18  }
19 };
20 
21 struct equalKey
22 {
24  {
25  return s1 == s2;
26  }
27 };
28 
29 struct State
30 {
31  State() : count(0)
32  {
33  }
34  int count;
36 };
37 
38 void find_all(const int num_ply, const int threshold,
39  bool save, const std::vector<std::string> &files)
40 {
42 
43  for (size_t index = 0; index < files.size(); index++)
44  {
45  osl::record::KisenFile kisen(files[index]);
46  for (size_t i = 0; i < kisen.size(); i++)
47  {
48  const osl::vector<osl::Move> moves = kisen.getMoves(i);
50 
51  size_t j = 0;
52  for (; j < moves.size() && (int)j < num_ply; j++)
53  {
54  const osl::Square opKingSquare
55  = state.kingSquare(alt(state.turn()));
56  if (state.hasEffectAt(state.turn(), opKingSquare))
57  {
58  break;
59  }
60  state.makeMove(moves[j]);
61  }
62  if ((int)j == num_ply)
63  {
65  if (it != states.end())
66  {
67  (it->second.count)++;
68  }
69  else
70  {
71  State s;
72  s.count = 1;
73  for (int k = 0; k < num_ply; k++)
74  {
75  s.moves.push_back(moves[k]);
76  }
77  states[state] = s;
78  }
79  }
80  }
81  }
82 
83  int index = 1;
85  it != states.end();
86  ++it)
87  {
88  if (it->second.count >= threshold)
89  {
90  std::cout << index << " (" << it->second.count << ")" << std::endl;
91  std::cout << it->first;
92  std::ofstream output;
93  if (save)
94  {
95  std::ostringstream oss(std::ostringstream::out);
96  oss << index << ".csa";
97  const std::string &filename = oss.str();
98  output.open(filename.c_str());
99  output << "PI" << std::endl
100  << "+" << std::endl;
101  }
102  const osl::stl::vector<osl::Move> &moves = it->second.moves;
103  for (size_t i = 0; i < moves.size(); i++)
104  {
105  std::cout << osl::record::csa::show(moves[i]) << " ";
106  if (save)
107  {
108  output << osl::record::csa::show(moves[i]) << std::endl;
109  }
110  }
111  if (save)
112  {
113  output.close();
114  }
115  std::cout << std::endl;
116  index++;
117  }
118  }
119 }
120 
121 int main(int argc, char **argv)
122 {
123  int num_ply;
124  int threshold;
125  bool save_moves;
126  boost::program_options::options_description command_line_options;
127  command_line_options.add_options()
128  ("num-ply",
129  boost::program_options::value<int>(&num_ply)->default_value(10),
130  "Show states after this number of plies are played")
131  ("threshold",
132  boost::program_options::value<int>(&threshold)->default_value(10),
133  "Each state must appear this number of times to be shown")
134  ("save",
135  boost::program_options::value<bool>(&save_moves)->default_value(false),
136  "Save moves leading to states to files in CSA format")
137  ("input-file", boost::program_options::value< std::vector<std::string> >(),
138  "input files in kisen format")
139  ("help", "Show help message");
140  boost::program_options::variables_map vm;
141  boost::program_options::positional_options_description p;
142  p.add("input-file", -1);
143 
144  try
145  {
147  boost::program_options::command_line_parser(
148  argc, argv).options(command_line_options).positional(p).run(), vm);
149  boost::program_options::notify(vm);
150  if (vm.count("help"))
151  {
152  std::cerr << "Usage: " << argv[0] << " [options] kisen-file"
153  << std::endl;
154  std::cout << command_line_options << std::endl;
155  return 0;
156  }
157  }
158  catch (std::exception &e)
159  {
160  std::cerr << "error in parsing options" << std::endl
161  << e.what() << std::endl;
162  std::cerr << "Usage: " << argv[0] << " [options] kisen-file" << std::endl;
163  std::cerr << command_line_options << std::endl;
164  return 1;
165  }
166 
167  const std::vector<std::string> files =
168  vm["input-file"].as< std::vector<std::string> >();
169  find_all(num_ply, threshold, save_moves, files);
170 
171  return 0;
172 }