This file is indexed.

/usr/include/osl/ntesuki/ntesukiSimulationSearcherDisproof.tcc is in libosl-dev 0.4.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* ntesukiSimulationDisproof.tcc
 */
#include "osl/ntesuki/ntesukiSimulationSearcher.h"
#include "osl/state/hashEffectState.h"

#include "osl/ntesuki/ntesukiRecord.h"
#include "osl/ntesuki/ntesukiExceptions.h"
#include "osl/container/moveVector.h"
#include "osl/move_classifier/moveAdaptor.h"
#include "osl/apply_move/applyMoveWithPath.h"

#ifndef NDEBUG
#define RETURN \
  TRY_DFPN;\
  ntesuki_assert(result.isFinal());\
  CATCH_DFPN; \
  return
#else
#define RETURN return
#endif

using namespace osl;
using namespace osl::ntesuki;

template <class Searcher, Player P>
class
NtesukiSimulationSearcher::
AttackHelperDisproof
{
  Searcher* searcher;
  NtesukiRecord* record;
  const NtesukiRecord* record_orig;
  unsigned int pass_left;
  const Move last_move;

public:
  AttackHelperDisproof(Searcher* searcher,
		       NtesukiRecord* record,
		       const NtesukiRecord* record_orig,
		       unsigned int pass_left,
		       const Move last_move)
    : searcher(searcher),
      record(record), record_orig(record_orig),
      pass_left(pass_left),
      last_move(last_move)
  {}

  void operator()(Square p)
  {
    (*searcher).template defenseForDisproof<PlayerTraits<P>::opponent>
      (record, record_orig, pass_left, last_move);
  }
};
      
template <class Searcher, Player P>
class NtesukiSimulationSearcher::
DefenseHelperDisproof
{
  Searcher* searcher;
  NtesukiRecord* record;
  const NtesukiRecord* record_orig;
  unsigned int pass_left;
  const Move last_move;
public:
  DefenseHelperDisproof(Searcher* searcher,
			NtesukiRecord* record,
			const NtesukiRecord* record_orig,
			unsigned int pass_left,
			const Move last_move)
    : searcher(searcher),
      record(record), record_orig(record_orig),
      pass_left(pass_left),
      last_move(last_move)
  {}

	
  void operator()(Square p)
  {
    (*searcher).template attackForDisproof<PlayerTraits<P>::opponent>
      (record, record_orig, pass_left, last_move);
  }
};

/*======================================================================
 * Disproof
 *======================================================================
 */

template <Player P>
void
NtesukiSimulationSearcher::
attackForDisproof(NtesukiRecord* record,
		  const NtesukiRecord* record_orig,
		  const unsigned int pass_left,
		  const Move last_move)
{
  const Player attacker = P;
  ++node_count;
  ntesuki_assert(P == state.turn());
  
  ntesuki_assert(record);
  ntesuki_assert(record->getBestMove<attacker>(pass_left).isInvalid());
  ntesuki_assert(record_orig);

  const bool invalid_defense = state.inCheck(PlayerTraits<P>::opponent);
  if (invalid_defense)
  {
    result = ProofDisproof::Checkmate();
    RETURN;
  }

  ntesuki_assert (!record->isVisited());
  NtesukiRecord::VisitLock visitLock(record);

  if (record->setUpNode<P>())
  {
    result = record->getValueWithPath<attacker>(pass_left, path);
    if (result.isFinal())
    {
      /* result by fixed depth searcher */
      RETURN;
    }
  }

  /* n が少ないときの結果を確定 */
  if (pass_left > 0)
  {
    result = record->getValueWithPath<attacker>(pass_left - 1, path);
    if (!result.isFinal())
    {
      NtesukiRecord::UnVisitLock unVisitLock(record);
      attackForDisproof<P>(record, record_orig, pass_left - 1, last_move);
    }

    if (!result.isCheckmateFail())
    {
      RETURN;
    }
    ntesuki_assert(result.isCheckmateFail());
  }

  /* 攻め手の生成
   */
  NtesukiMoveList moves;
  record->generateMoves<P>(moves, pass_left, false);

  /* 攻める手の実行
   */
  bool has_loop = false;
  bool disproof_failed = false;
  for (NtesukiMoveList::iterator move_it = moves.begin();
       move_it != moves.end(); move_it++)
  {
    NtesukiMove& move = *move_it;

    if (move.isPass()) continue;
    if (move.isCheckmateFail<attacker>(pass_left)) continue;
    if (!move.isCheck() && 0 == pass_left) continue;

    /* DANGEROUS  this time pawn checkmate might be avoidable */
    if (move.isNoPromote()) continue; 

    NtesukiRecord *record_child = table.allocateWithMove(record, move);
    if (record_child == 0)
    {
      result = ProofDisproof::Checkmate();
      RETURN;
    }
    ntesuki_assert(record_child);
    if(record_child->isVisited())
    {
      //move.setCheckmateFail<attacker>(pass_left);
      has_loop = true;
      continue;
    }

    PathEncoding path_child(path, move.getMove());
    result = record_child->getValueWithPath<attacker>(pass_left, path_child);

    if(result.isUnknown())
    {
      const NtesukiRecord* record_child_orig = table.findWithMoveConst(record_orig, move);
      if (!record_child_orig ||
	  !record_child_orig->getValue<attacker>(pass_left).isCheckmateFail())
      {
	/* a move that used not to be a check became a check, or
	 * might have been a loop
	 */
	result = ProofDisproof::Checkmate();
	RETURN;
      }
      
      AttackHelperDisproof<NtesukiSimulationSearcher, P> helper(this,
								record_child,
								record_child_orig,
								pass_left,
								move.getMove());
      TRY_DFPN;
      ApplyMoveWithPath<P>::doUndoMove(state, path, move.getMove(), helper);
      CATCH_DFPN;
      if (record->getValueWithPath<attacker>(pass_left, path).isFinal())
      {
	result = record->getValueWithPath<attacker>(pass_left, path);
	RETURN;
      }
    }

    if (result.isCheckmateSuccess())
    {
      disproof_failed = true;
      continue;
    }
    else if (result == ProofDisproof::LoopDetection())
    {
      has_loop = true;
    }
    
    ntesuki_assert(result.isCheckmateFail());
    move.setCheckmateFail<attacker>(pass_left);
  }

  if (disproof_failed)
  {
    result = ProofDisproof::Checkmate();
    RETURN;
  }


  if (has_loop)
  {
    record->setLoopWithPath<attacker>(pass_left, path);
    result = ProofDisproof::LoopDetection();
    TRY_DFPN;
    record->setResult<attacker>(pass_left, NtesukiResult(1, 1),
				NtesukiMove::INVALID(), false);
    CATCH_DFPN;
  }
  else
  {
    result = ProofDisproof::NoCheckmate();
    TRY_DFPN;
    record->setResult<attacker>(pass_left, result,
				NtesukiMove::INVALID(), true);
    CATCH_DFPN;
  }
  RETURN;
}

template <Player P>
void
NtesukiSimulationSearcher::
defenseForDisproof(NtesukiRecord* record,
		   const NtesukiRecord* record_orig,
		   const unsigned int pass_left,
		   const Move last_move)
{
  const Player attacker = PlayerTraits<P>::opponent;
  ++node_count;
  ntesuki_assert(P == state.turn());

  ntesuki_assert(state.inCheck(P) || (pass_left > 0));

  ntesuki_assert(record);
  ntesuki_assert(record->getBestMove<attacker>(pass_left).isInvalid());
  ntesuki_assert(record_orig);
  ntesuki_assert(!record->isVisited());
  ntesuki_assert(record_orig->getValue<attacker>(pass_left).
		 isCheckmateFail());
  NtesukiRecord::VisitLock visitLock(record);

  if (record->setUpNode<P>())
  {
    result = record->getValueWithPath<attacker>(pass_left, path);
    if (result.isFinal())
    {
      /* result by fixed depth searcher */
      RETURN;
    }
  }

#ifndef NDEBUG
  /* 攻撃側に王手がかかっていないか調べる */
  ntesuki_assert(!state.inCheck(attacker));
#endif

  /* 以前の bestMove を実行
   */
  const NtesukiMove best_move =
    record_orig->getBestMove<attacker>(pass_left);
  if (best_move.isInvalid())
  {
    /* is by fixed depth searcher */
    result = ProofDisproof::Checkmate();
    RETURN;
  }
  const NtesukiRecord *record_child_orig = table.findWithMoveConst(record_orig, best_move);
  if (!record_child_orig ||
      !record_child_orig->getValue<attacker>(pass_left).isCheckmateFail())
  {
    /* fixed depth searcher 等での disproof */
    result = ProofDisproof::Checkmate();
    RETURN;
  }
  ntesuki_assert(record_child_orig);

  if (!best_move.isPass() &&
      !state.template isAlmostValidMove<false>(best_move.getMove()))
  {
    result = ProofDisproof::Checkmate();
    RETURN;
  }
  
  NtesukiRecord *record_child = table.allocateWithMove(record, best_move);
  if (record_child == 0)
  {
    result = ProofDisproof::Checkmate();
    RETURN;
  }
  ntesuki_assert(record_child);
  
  int pass_left_child = pass_left;
  if (best_move.isPass()) --pass_left_child;

  if (record_child->isVisited())
  {
    TRY_DFPN;
    result = ProofDisproof::LoopDetection();
    record->setLoopWithPath<attacker>(pass_left, path);
    record->setResult<attacker>(pass_left, NtesukiResult(1, 1),
				NtesukiMove::INVALID(), false);
    CATCH_DFPN;
    RETURN;
  }
  else if (!record_child_orig->getValue<attacker>(pass_left_child).isCheckmateFail())
  {
    result = ProofDisproof::Checkmate();
    RETURN;
  }

  const PathEncoding path_child(path, best_move.getMove());
  result = record_child->getValueWithPath<attacker>(pass_left_child, path_child);

  if (result.isUnknown())
  {
    DefenseHelperDisproof<NtesukiSimulationSearcher, P> helper(this,
							       record_child,
							       record_child_orig,
							       pass_left_child,
							       best_move.getMove());
    TRY_DFPN;
    ApplyMoveWithPath<P>::doUndoMoveOrPass(state, path, best_move.getMove(), helper);
    CATCH_DFPN;
    if (record->getValueWithPath<attacker>(pass_left, path).isFinal())
    {
      result = record->getValueWithPath<attacker>(pass_left, path);
      RETURN;
    }
  }

  if (result == ProofDisproof::LoopDetection())
  {
    TRY_DFPN;
    record->setLoopWithPath<attacker>(pass_left, path);
    record->setResult<attacker>(pass_left, NtesukiResult(1, 1),
				NtesukiMove::INVALID(), false);
    CATCH_DFPN;
    RETURN;
  }
  else if (result.isCheckmateFail())
  {
    NtesukiMove move(best_move.getMove());
    TRY_DFPN;
    move.setCheckmateFail<attacker>(pass_left);
    record->setResult<attacker>(pass_left, result, move, true);
    CATCH_DFPN;
    RETURN;
  }
  else
  {
    /* best_move is invalid : some rare casese, including
     * - the original state was invalid(attacker was under check)
     */
    //    ntesuki_assert(record_orig->getValue<attacker>(pass_left)
    //		   == ProofDisproof::AttackBack());
    result = ProofDisproof::Checkmate();
    RETURN;
  }
}

/* Start simulation to disproof, P as Attacker.
 * @return true, if nocheckmate is proven
 */
template <Player P>
bool
NtesukiSimulationSearcher::
startFromAttackDisproof(NtesukiRecord *record,
			const NtesukiRecord *record_orig,
			const unsigned int pass_left,
			const Move last_move)
{
  ++disproof_count;
  const Player attacker = P;
  ntesuki_assert(record_orig);
  if (!record_orig->getValue<attacker>(pass_left).isCheckmateFail())
    return false;

  TRY_DFPN;
  attackForDisproof<P>(record, record_orig, pass_left, last_move);
  CATCH_DFPN;
  if (result.isCheckmateFail())
  {
    ++disproof_success_count;
    return true;
  }
  return false;
}

/* Start simulation to disproof, P as Defender.
 * @return true, if nocheckmate is proven
 */
template <Player P>
bool
NtesukiSimulationSearcher::
startFromDefenseDisproof(NtesukiRecord *record,
			 const NtesukiRecord *record_orig,
			 const unsigned int pass_left,
			 const Move last_move)
{
  ntesuki_assert (P == state.turn());
  ++disproof_count;
  const Player attacker = PlayerTraits<P>::opponent;
  ntesuki_assert(record_orig);
  if (!record_orig->getValue<attacker>(pass_left).isCheckmateFail())
    return false;

  TRY_DFPN;
  defenseForDisproof<P>(record, record_orig, pass_left, last_move);
  CATCH_DFPN;
  if (result.isCheckmateFail())
  {
    ++disproof_success_count;
    return true;
  }
  return false;
}

#undef RETURN

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