File: query.cc

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (178 lines) | stat: -rw-r--r-- 4,987 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* query.cc
 */
#include "gpsshogi/recorddb/recordDb.h"
#include "gpsshogi/recorddb/facade.h"
#include "osl/state/numEffectState.h"
#include "osl/apply_move/applyMove.h"
#include "osl/record/kakinoki.h"
#include "osl/record/ki2.h"
#include "osl/record/ki2IOError.h"
#include "osl/record/csaRecord.h"
#include "osl/record/csaIOError.h"
#include "osl/record/kisen.h"
#include <boost/program_options.hpp>
#include <boost/scoped_ptr.hpp>
#include <iostream>

namespace po = boost::program_options;
using namespace osl;
using namespace gpsshogi;

std::unique_ptr<RecordDB> db;

void run(const std::string& filename, int move_number);
void run_kisen(const std::string& filename, int id, int move_number);
void query(const NumEffectState& state);

int verbose;
int main(int argc, char **argv)
{
  std::string database;
  std::vector<std::string> filenames;
  std::string kisen_filename;
  int kisen_id, move_number;
  po::options_description options;
  options.add_options()
    ("kisen,k", po::value<std::string>(&kisen_filename)->default_value(""),
     "specify filename of kisen")
    ("kisen-id,i", po::value<int>(&kisen_id)->default_value(0),
     "id of kisen file")
    ("move-number,m", po::value<int>(&move_number)->default_value(0),
     "move number, -1 for all")
    ("database,d", po::value<std::string>(&database)->default_value(""),
     "filename for database")
    ("verbose,v", po::value<int>(&verbose)->default_value(0),
     "set verbose level")
    ("help,h", "Show help message");
  po::options_description hidden("Hidden options");
  hidden.add_options()
    ("files", po::value<std::vector<std::string> >());
  po::options_description all_options;
  all_options.add(options).add(hidden);
  po::positional_options_description p;
  p.add("files", -1);

  po::variables_map vm;
  try
  {
    po::store(po::command_line_parser(argc, argv).
	      options(all_options).positional(p).run(), vm);
    po::notify(vm);
    if (vm.count("files"))
      filenames = vm["files"].as<std::vector<std::string> >();
  }
  catch (std::exception& e)
  {
    std::cerr << "error in parsing options" << std::endl
	      << e.what() << std::endl;
    std::cerr << options << std::endl;
    return 1;
  }
  if (vm.count("help"))
  {
    std::cerr << "Usage: " << argv[0] << " [options] files" << std::endl;
    std::cout << options << std::endl;
    return 1;
  }

  if (database != "")
  {
    db.reset(new RecordDB(database.c_str(), true));
  }
  if (filenames.empty() && kisen_filename == "") 
  {
    NumEffectState state;
    query(state);
  }
  else
  {
    for (size_t i=0; i<filenames.size(); ++i)
      run(filenames[i], move_number);
    if (kisen_filename != "")
      run_kisen(kisen_filename, kisen_id, move_number);
  }
}

void query(const NumEffectState& state)
{
  if (! db)
  {
    int win, loss, gps_win, gps_loss, bonanza_win, bonanza_loss;
    recorddb::query(state, win, loss, gps_win, gps_loss, bonanza_win, bonanza_loss);
    std::cout << win << " " << loss
	      << "  " << gps_win << " " << gps_loss
	      << "  " << bonanza_win << " " << bonanza_loss
	      << "\n";
    return;
  }
  
  std::string key = db->makeKey(state);
  SquareInfo info;
  db->get(key, info);
  if (verbose) 
    std::cerr << info.DebugString() << "\n";
  int win = info.win(), loss = info.loss(), gps_win = info.win_by_gps(), gps_loss = info.loss_by_gps();
  if (state.turn() != BLACK)
  {
    std::swap(win, loss);
    if (gps_win || gps_loss)
    {
      const int a = win - gps_loss, b = loss - gps_win;
      gps_win = a; gps_loss = b;
    }
  }
  std::cout << win << " " << loss // viewpoint of black player
	    << "  " << gps_win << " " << gps_loss << "\n"; 
}

void run(const std::string& filename, int move_number) 
{
  Record record;
  try 
  {
    if (filename.find(".kif") == filename.size()-4) 
    {
      KakinokiFile file(filename);
      record = file.getRecord();
    } 
    else if (filename.find(".ki2") == filename.size()-4) 
    {
      Ki2File file(filename);
      record = file.getRecord();
    }
    else if (filename.find(".csa") == filename.size()-4) 
    {
      CsaFile file(filename);
      record = file.getRecord();
    }
  }
  catch (std::exception& e)
  {
    std::cerr << filename << " " << e.what() << "\n";    
    return;
  }
  vector<Move> moves = record.getMoves();
  NumEffectState state = record.getInitialState();
  for (size_t i=0; i<moves.size(); ++i)  
  {  
    if ((int)i == move_number || move_number < 0)
      query(state);
    ApplyMoveOfTurn::doMove(state, moves[i]);
  }
  if ((int)moves.size() == move_number || move_number < 0)
    query(state);
}
void run_kisen(const std::string& filename, int kisen_id, int move_number)
{
  KisenFile kisen(filename);
  NumEffectState state = kisen.getInitialState();
  vector<Move> moves = kisen.getMoves(kisen_id);
  for (int i=0; i<move_number; ++i) 
    ApplyMoveOfTurn::doMove(state, moves[i]);
  query(state);
}

// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: