-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minehunter.cc
1121 lines (1053 loc) · 32.3 KB
/
Minehunter.cc
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Minehunter.hh"
#include "WrapSlot.hh"
#include <strstream>
class Gtk_Timer
{
GTimer* timer;
public:
Gtk_Timer() { timer=g_timer_new(); };
~Gtk_Timer() { g_timer_destroy(timer); timer=NULL; };
inline void start() { g_timer_start(timer); };
inline void stop() { g_timer_stop(timer); };
inline gdouble secs() { return g_timer_elapsed(timer,NULL); };
inline gulong usecs() {
gulong us; g_timer_elapsed(timer,&us); return us; };
inline gdouble elapsed(gulong& us) {
return g_timer_elapsed(timer,&us); };
inline void reset() { g_timer_reset(timer); };
};
void Minehunter::queue_set(const ScanSet& S)
{
_assert(S.is_ok());
if (S.is_trivial())
return;
if (S.is_certain())
{
bool mineflag=(0!=S.max_mines() ? true : false);
set_index_type::const_iterator A(S.get_set().begin());
const set_index_type::const_iterator& end(S.get_set().end());
// Now add each element of S as a seperate ScanSet
// into the Moves list. Start with iterators on S
for (;A!=end;++A)
{
if ( !game_board_.is_marked(*A) )
{
ScanSet I;
// First put the info into I
I.add_index(*A);
if (mineflag)
{
I.max_mines()=1;
I.min_mines()=1;
}
// Then copy I into Moves
_assert(I.is_ok());
_assert(0<I.size());
_assert(I.is_certain());
moves_.insert(I); // ignore return value
}
}
return;
}
KnownType::iterator it;
it=known_.find(S);
if (known_.end()!=it) {
if (S.can_refine(*it)) {
ScanSet I(S.make_new_refined(*it));
known_.erase(it);
queue_set(I); // recursive call (only once)
}
}
else
{
pair<AddType::iterator, bool> p;
p=add_.insert(S);
if (!p.second)
{
AddType::iterator T;
T=p.first;
if (S.can_refine(*T))
{
ScanSet I(S.make_new_refined(*T));
add_.erase(T); // dump old version
p=add_.insert(I); // replace with new version
_assert(p.second);
}
}
}
return;
}
void Minehunter::setup(guint32 x_max,guint32 y_max,guint32 mines)
{
_assert(0<x_max);
_assert(0<y_max);
_assert(mines<x_max*y_max);
switch(state_)
{
case NEEDS_SETUP:
game_board_.reset(x_max,y_max,mines);
state_=NEEDS_SCANSET;
break;
default:
break; // DO NOTHING
}
}
/// Accepts and trims the newset
void Minehunter::add_scanset(ScanSet * newset)
{
_show_args1(*newset);
_nullChk(newset);
switch(state_)
{
case NEEDS_SCANSET:
state_=PROCESSING;
// fall through, no break
case PROCESSING:
{
ScanSet S=game_board_.trim(*newset);
game_board_.mark_measured(S);
queue_set(S);
delete newset;
newset=NULL;
}
break;
default:
break; // DO NOTHING
}
}
/// If new information, then queue_set it.
void Minehunter::mark_empty(guint32 loc)
{
_show_args1(loc);
_assert(loc<game_board_.locations());
switch(state_)
{
case NEEDS_SCANSET:
state_=PROCESSING;
// fall through
case PROCESSING:
if (!game_board_.is_marked(loc))
{
ScanSet S;
S.add_index(loc);
queue_set(S); // will route to moves_
}
break;
default:
break; // DO NOTHING
}
}
/// If new information, then queue_set it.
void Minehunter::mark_mine(guint32 loc)
{
_show_args1(loc);
_assert(loc<game_board_.locations());
switch(state_)
{
case NEEDS_SCANSET:
state_=PROCESSING;
// fall through
case PROCESSING:
if (!game_board_.is_marked(loc))
{
ScanSet S;
S.add_index(loc);
++S.min_mines();
++S.max_mines();
queue_set(S); // will route to moves_
}
break;
default:
break; // DO NOTHING
}
}
void Minehunter::game_over(GameEnd ending)
{
_show_args1((int)ending);
switch(state_)
{
case NEEDS_SCANSET: // Fall through
case PROCESSING: // Fall through
case NEEDS_GAMEOVER:
state_=NEEDS_SETUP;
known_.clear();
add_.clear();
moves_.clear();
break;
default:
break; // DO NOTHING
}
}
void Minehunter::process()
{
_show_args3(moves_.size(),add_.size(),known_.size());
_assert(PROCESSING==state_);
// Choose action
if (0<moves_.size())
{
move_it();
// Did we win?
if (game_board_.complete())
{
game_over(WON_GAME);
}
}
else if (0<add_.size())
{
add_it();
}
else
{
// Essentially the thread has nothing else to process
// and may need to guess
state_=NEEDS_SCANSET;
//if (0==pings_pending_)
//{
++pings_pending_;
_show_args1(pings_pending_);
sendPing.emit(true);
//}
// When the ping reply arrives at received_ping, the system
// will decide if it still needs to call guess_it.
// It is quite possible that new info arrives in the
}
}
void* Minehunter::main(void* signal_buffer)
{
_show_args1(signal_buffer);
_nullChk(signal_buffer);
// Experiment:
AbstractSignalBuffer *absb_ = static_cast<AbstractSignalBuffer*>(signal_buffer);
// Cannot use dynamic cast on void* so use intermediate absb_:
sb_ = dynamic_cast<SafeSignalBuffer*>(absb_);
sb_->reference(); // since it is a Sigc::Object
// Hopefully if void* is to random memory, it won't accidentally
// look like a SafeSignalBuffer.
while ((state_!=CANCELED)&&(!is_canceled))
{
switch(state_)
{
case NEEDS_SETUP:
_messsage("NEEDS_SETUP");
sb_->wait_for_change(0,0);
break;
case NEEDS_SCANSET:
// We are here if we were just setup
// or if we just sent a "might guess" ping
_messsage("NEEDS_SCANSET");
sb_->wait_for_change(0,0);
break;
case NEEDS_GAMEOVER:
_messsage("NEEDS_GAMEOVER");
sb_->wait_for_change(0,0);
break;
case PROCESSING:
_messsage("PROCESSING");
process();
break;
case CANCELED: // Can happen when another thread calls destroy()
_messsage("CANCELED");
break;
default:
_never_get_here;
}
if ((state_!=CANCELED)&&(!is_canceled))
{
sb_->callEvents.emit();
}
}
_messsage("Minehunter main loop has been CANCELED, exiting thread");
sb_->unreference(); // since it is a Sigc::Object
delete this;
return NULL;
}
////////////////////////////////////////////////////////////
// add_it is a very busy function
// Logic:
// if not is_ok or is_trivial or is_certain, abort.
// if same set as Known, try to refine, return
// loop over Known sets it intersects
// if added set is subset of Known
// if intersect is refined, put in Add
// if nontrivial difference, put in Add
// if Known set is subset of added
// if intersect is refined, put in Add
// if nontrivial difference, put in Add
// else
// put in Add
// put non trivial differences in Add
// after loop, put added into Known
void Minehunter::add_it()
{
// S will be item to add, I is auxillary set
ScanSet S=ScanSet(),I=ScanSet();
// It and It_next used to traverse Known
KnownType::iterator it,it_next;
// Used in loop over known
bool flag_next=false;
// pop off the set at the front of the add queue
_assert(0<add_.size());
S=*(add_.begin());
add_.erase(add_.begin());
_assert(S.is_ok()); // paranoia
_assert(!S.is_trivial()); // paranoia
_assert(!S.is_certain()); // paranoia
// DEBUG cerr << "add_it popped " << S << endl;
// We need to check for uniqueness
it=known_.find(S);
if (known_.end()!=it)
{
if (S.can_refine(*it))
{
I=S.make_new_refined(*it);
known_.erase(it);
queue_set(I);
}
else
{
; // Do nothing
}
return; // Stop processing
}
// Need to Compare S with all elements in Known
// And look for intersections
for (it=known_.begin();it!=known_.end();it=it_next)
{
_assert(0<(*it).size()); // Guard against old bug
// Get the set intersection
// Assume that S and (*it) must not have the same set of locations
I=S*(*it);
if (!I.is_trivial())
{
// Found a new set
// Check against S
if(S.size()==I.size())
{
// S is a subset of (*It)
if (I.can_refine(S))
{
// new info in intersection I=S*(*it)
queue_set(I);
// TODO : We could stop here and return....?
}
// get set difference
I=(*it)-S;
if (!I.is_trivial())
{
// Found a new set: the difference (*it)-S
queue_set(I);
}
}
else if (it->size()==I.size()) // Check against (*It)
{
// (*It) is a subset of S
if (I.can_refine(*it))
{
// new info in intersection I=S*(*it)
it_next=it;
++it_next;
flag_next=true; // We have handled setting it_next
known_.erase(it); // blow this away explicitly
queue_set(I);
}
// get set difference, without using it
I=S-I; // I hope this works :)
if (!I.is_trivial())
{
// Found a new set: the difference S-(S*(*it))
queue_set(I);
}
}
else
{
// Not a subset relationship
// Found a new Set: the intersection (*it)*S
queue_set(I);
// Get first difference
I=S-(*it);
if (!I.is_trivial())
{
// Found a new Set: the difference S-(*it)
queue_set(I);
}
// Get second difference
I=(*it)-S;
if (!I.is_trivial())
{
// Found a new Set: the difference (*it)-S
queue_set(I);
}
}
}
if (!flag_next) {
it_next=it; // Safe to reference *It still
++it_next; // get the next one...
} else {
flag_next=false;
}
}
// DEBUG cerr << "add_it adding the set to known" << endl;
known_.insert(S);
return;
}
void Minehunter::move_it()
{
_show_args0();
// S will be the location to move to
ScanSet S=ScanSet(),I=ScanSet();
// loc is location in S to move to
index_type loc;
// is_mine is whether S has a mine
bool is_mine;
// index points to location in a ScanSet
set_index_type::const_iterator index;
// a and a_next used to traverse Add, points to scanset
AddType::iterator a,a_next;
// k and k_next used to traverse Known, points to scanset
KnownType::iterator k,k_next;
// m and m_next used to traverse Moves, points to scanset
MovesType::iterator m,m_next;
// pop S from front of moves_
S=*(moves_.begin());
moves_.erase(moves_.begin());
_assert(S.is_ok()); // paranoia
_assert(1==S.size()); // paranoia
_assert(S.is_certain()); // paranoia
// DEBUG cerr << "move_it popped " << S << endl;
// Pull info from S
loc=*(S.get_set().begin());
is_mine=(S.max_mines()!=0);
// Loop through Add
// This loop adapted to editing a few elements in a set
for (a=add_.begin();a!=add_.end();a=a_next)
{
a_next=a;
++a_next;
// does a have loc in it?
index=a->get_set().find(loc);
if (index!=a->get_set().end())
{
// remove S from a
I=*a;
add_.erase(a); // remove before surgery on *a
I.remove_index(loc,is_mine);
queue_set(I);
// FIXME: Added paranoia, start loop over?
a_next=add_.begin();
}
}
// Loop through Known
for (k=known_.begin();k!=known_.end();k=k_next)
{
k_next=k;
++k_next;
_assert(0<k->size()); // Guard against old bug
// does k have loc in it?
index=(*k).get_set().find(loc);
if (index!=k->get_set().end())
{
// Need to put (*k)-S into Add
// get a copy of (*k)
I=(*k);
// erase k from Known, invalidating k
known_.erase(k);
// Remove the location and its mine
I.remove_index(loc,is_mine);
// put the replacement ScanSet into Add/Known
queue_set(I);
// FIXME: Added paranoia, reset loop to beginning of Known
k_next=known_.begin();
}
}
// Loop through Moves
for (m=moves_.begin();m!=moves_.end();m=m_next)
{
m_next=m;
++m_next;
index=m->get_set().find(loc);
if (index!=m->get_set().end())
{
moves_.erase(m); // remove and forget
// FIXME: Added paranoia, start loop over?
m_next=moves_.begin();
}
}
// Send correct signal
bool new_info;
if (!is_mine)
{
new_info=game_board_.mark_empty(loc);
_assert(new_info); // paranoia
markEmpty.emit(loc);
}
else
{
new_info=game_board_.mark_mine(loc);
_assert(new_info); // paranoia
markMine.emit(loc);
}
}
void Minehunter::receive_ping(bool new_ping)
{
_show_args2(new_ping,pings_pending_);
if (new_ping)
{
sendPing.emit(false);
}
else
{
--pings_pending_;
// See if there are any other pings we are waiting on...
if (0==pings_pending_)
switch(state_)
{
case NEEDS_SCANSET: // Fall through
case PROCESSING:
if ( (moves_.size()==0) && (add_.size()==0) )
{
guess_it();
}
break;
default:
// ignore ping return in this case
break;
}
}
}
void Minehunter::fake_guess(string& out_report)
{
_show_args0();
ostrstream report;
pair_prob_loc_t p_l;
report << "Having to fake the guess because there are ";
report << game_board_.measured() << endl;
report << "measured locations which is larger than 32." << endl;
if (0<game_board_.unmeasured())
{
p_l.first=0.4;
p_l.second=game_board_.locations();
prob_loc_->insert(p_l);
report << "Guessing unmeasured with fake 0.4 prob." << endl;
}
else
{
p_l.first=0.4;
p_l.second=*(known_.begin()->get_set().begin());
prob_loc_->insert(p_l);
report << "Guessing the first location I found (" << p_l.second;
report << ") with 0.4 prob." << endl;
}
report << ends;
out_report=report.str();
return;
}
void Minehunter::guess_it()
{
_show_args0();
// DEBUG cerr << known_.size() << " " << sb_->size();
string init_report,time_report, prob_report;
// This only exits if the game has been setup but there
// is no data yet.
if (game_board_.unmeasured()==game_board_.locations())
return;
clean_up();
//if (32<game_board_.measured())
//{
// fake_guess(prob_report);
// publishText.emit(prob_report);
// _show_args1(prob_report);
//}
//else
{
// Fill prob_loc_
if (initialize(init_report))
{
publishText.emit(init_report);
_show_args1(init_report);
if (!call_op(time_report))
{
publishText.emit("op was aborted");
delete prob_loc_;
prob_loc_=NULL;
return;
}
publishText(time_report);
_show_args1(time_report);
calc_prob(prob_report); // work with BIG numbers
publishText.emit(prob_report);
_show_args1(prob_report);
}
else
{
// There were no measured locations.
publishText.emit(init_report);
_show_args1(init_report);
}
}
// Check the guess signal for a receiver
if (!guess.empty())
{
guess.emit(prob_loc_); // Pass on data
prob_loc_=NULL; // Forget data
}
else
{
delete prob_loc_; // Destroy data
prob_loc_=NULL; // Clear pointer
}
}
bool Minehunter::call_op(string &out_report)
{
// Do timing analysis of guess op
Gtk_Timer timer;
ostrstream report;
timer.start();
op(); // work very hard
timer.stop();
if (!sb_->empty()) // Check for incomoing messages
{
return false; // abort
}
report << "Duration of op for " << (m_max_+1)
<< " measured locations was " << timer.secs() << " seconds." << endl;
report << ends;
out_report=report.str();
return true;
}
bool Minehunter::initialize(string& out_report)
{
_show_args0();
KnownType::const_iterator kit_(known_.begin()),k_end(known_.end());
guint32 k,loc,m;
set_index_type::const_iterator sit, send;
ostrstream report;
map<guint32,guint32>::const_iterator it_loc;
bool break_out=false;
m_=0;
min_mm_ = maxloc;
max_mm_ = 0;
report << "Initializing guessing algorithm, look for 'certain' ScanSets." << endl;
for(;kit_!=k_end;)
{
if (kit_->min_mines()==kit_->max_mines())
{
guint32 just_added = 0;
// DEBUG cerr << "found certain in known: " << *kit_;
k=k_max_;
++k_max_;
// formally request the additional room in the vectors
k_tot_.resize(k_max_);
k_mines_.resize(k_max_);
// record # of mines in the known_ scanset
k_tot_[k]=kit_->min_mines();
// Loop over locations in the known_ scanset
send=kit_->get_set().end();
for(sit=kit_->get_set().begin(); sit!=send;)
{
loc=(*sit);
it_loc=loc_m_.find(loc);
if (loc_m_.end()!=it_loc)
{
m=it_loc->second;
_assert(loc==m_loc_[m]); // paranoia
m_known_[m].push_front(k);
++sit; // increment loop
}
else
{
m=m_max_;
if (m<32) // Test for overflow
{
++m_max_;
++just_added; // In case we later exceed 32 locations
_assert(0==m_loc_[m]);
// assign index m to location loc
m_loc_[m]=loc;
loc_m_[loc]=m;
_assert(m==loc_m_[m_loc_[m]]);
m_known_[m].push_front(k);
++sit; // increment loop
}
else
{
// Undo the already executed part of sit/send loop
send=sit;
for(sit=kit_->get_set().begin(); sit!=send;++sit)
{
loc=(*sit);
m=loc_m_[loc];
m_known_[m].pop_front();
}
--k_max_;
m_max_ -= just_added;
kit_=k_end;
break_out=true;
}
}
}
}
if (!break_out)
++kit_; // increment loop
}
if (0==k_max_)
{
// All measured locations must have been mines! Oh dear...
guint32 num_locs=game_board_.unmeasured();
guint32 remaining=game_board_.mines()-game_board_.mined();
pair_prob_loc_t p_l;
p_l.first=remaining;
p_l.first/=num_locs;
p_l.second=game_board_.locations();
prob_loc_->insert( p_l );
report << "Only unmeasured locations, prob is" << p_l.first << endl;
report << ends;
out_report = report.str();
return false;
}
if ( break_out )
{
report << "Only the first " << m_max_
<< " measured locations out of " << game_board_.measured()
<< " were used." << endl;
}
--m_max_; // So that m_mined_[m_max_] is a valid entry
total_mines_=game_board_.mines()-game_board_.mined();
num_unmeasured_=game_board_.unmeasured();
report << "Found " << k_max_ << " 'certain' ScanSets" << endl;
/* Disable verbose reporting
for (k=0; k < k_max_; ++k)
{
report << "known #" << k << " has " << k_tot_[k] << " mines." << endl;
}
for (m=0; m<=m_max_; ++m)
{
report << "location " << m_loc_[m] << " in known #'s";
kend=m_known_[m].end();
for (k1=m_known_[m].begin(); k1 != kend; ++k1)
{
report << " " << *k1;
}
report << endl;
}
*/
report << "Total mines left to find: " << total_mines_ << endl;
report << "Measured locations: " << (m_max_+1) << endl;
report << "Unmeasured locations: " << num_unmeasured_ << endl;
report << ends;
out_report = report.str();
return true;
}
/// Build for pure speed. No local variables.
/// Can recusively call op(m+1) zero *or* one *or* two times.
/// Start with op(0) and it ends with op(m_max_) at the deepest.
void Minehunter::op() // operates on index this->m
{
// _show_args1(m_);
result_=0; // zero will mean we found a working layout
// Try to set m_mined_[m1]=1 and set result_
kend=m_known_[m_].end();
for (k1=m_known_[m_].begin(); k1 != kend; ++k1)
{
k_=(*k1);
if (k_mines_[k_] < k_tot_[k_])
{
++k_mines_[k_];
// This check tries to avoid later loop over all k_
if (k_mines_[k_] < k_tot_[k_])
{
// MOST COMMON PATH to set result_=-1
result_=-1; // too low..not enough mines yet
// but keep laying down mines
}
}
else
{
for(k2=m_known_[m_].begin(); k2 != k1; ++k2)
{
--k_mines_[*k2];
}
result_=+1; // too high..failed to set m_mined_[m]=1
break; // exits the for (k1) loop
}
}
if (+1!=result_)
{
m_mined_[m_]=1; // we succeeded to set m_mined_[m]=1
++measured_mines_; // total of m_mined_[]
if (0==result_)
{
// Check thoroughly
for(k_=0; k_<k_max_; ++k_)
{
if (k_mines_[k_] < k_tot_[k_])
{
result_=-1; // too low..not enough mines yet
break; // exits the for(k_<k_max_) loop
}
}
}
}
if (-1==result_)
{
// MOST COMMON PATH
if ((measured_mines_<total_mines_) && (m_<m_max_))
{
// MOST COMMON PATH
++m_;
op(); // **RECURSE** with m_mined_[m]=1
--m_;
// WARNING: the result_ variable is now undefined
// WARNING: the kend variable is now undefined
}
// set m_mined_[m]=0
kend=m_known_[m_].end();
for (k1=m_known_[m_].begin(); k1 != kend; ++k1)
{
--k_mines_[*k1];
}
m_mined_[m_]=0;
--measured_mines_;
}
else if (0==result_)
{
// check for incoming messages or "death"
// if they exist, abort guessing operation
if (!sb_->empty() || is_canceled)
{
m_max_=0; // no more recursion will be done, exit all
}
else
{
// record the working layout
++mm_ways_[measured_mines_];
if (measured_mines_ > max_mm_)
{
max_mm_=measured_mines_;
}
if (measured_mines_ < min_mm_)
{
min_mm_=measured_mines_;
}
for (m_temp=0; m_temp<=m_; ++m_temp) // loop can stop at m_temp==m_
{
mm_m_ways_[measured_mines_][m_temp] += m_mined_[m_temp];
}
// set m_mined_[m]=0
kend=m_known_[m_].end();
for (k1=m_known_[m_].begin(); k1 != kend; ++k1)
{
--k_mines_[*k1];
}
m_mined_[m_]=0;
--measured_mines_;
}
}
// Always **RECURSE** with m_mined_[m]=0
if (m_<m_max_)
{
++m_;
op();
--m_;
}
}
/// This erases all but prob_loc_
void Minehunter::clean_up()
{
_show_args0();
guint32 m,mm; // unused: loc,k
measured_mines_=0;
total_mines_=0;
num_unmeasured_=0;
m_max_=0;
for (m=0; m<maxloc; ++m)
{
m_known_[m].clear();
m_mined_[m]=0;
m_loc_[m]=0;
for (mm=0; mm<maxloc; ++mm)
{
mm_ways_[m]=0;
mm_m_ways_[mm][m]=0;
}
}
loc_m_.clear();
// for (loc=0; loc<1024; ++loc) // XXX
// {
// loc_m_[loc]=0;
// }
k_max_=0;
k_mines_.clear();
k_tot_.clear();
// for (k=0; k<1024; ++k)
// {
// k_mines_[k]=0;
// k_tot_[k]=0;
// }
if (NULL==prob_loc_)
{
prob_loc_=new MapProbLoc_t();
}
else
{
delete prob_loc_;
prob_loc_=NULL;
prob_loc_=new MapProbLoc_t();
}
}
// This function is in a separate file since it needs
// the CLN, a Class Library for Numbers by Bruno Haible
// Found via the www.fsf.org list of GNU GPL software
// Does infinite precision arithmetic
// From cln.h:
#define WANT_OBFUSCATING_OPERATORS
// Basic types and definitions.
#include "cl_types.h"
#include "cl_object.h"
// Abstract number classes.
#include "cl_number.h"
#include "cl_number_io.h"
#include "cl_complex_class.h"
#include "cl_real_class.h"
#include "cl_rational_class.h"
// Integers.
#include "cl_integer_class.h"
#include "cl_integer.h"
#include "cl_integer_io.h"
// Rational numbers.
#include "cl_rational.h"
#include "cl_rational_io.h"
// Also need an array container:
#include <cl_SV_integer.h>
///////////////////////////////////////////////////////////////
/// Algorithm simply copied from my GreatGuess.java class
/// Returns a string reporting details
void Minehunter::calc_prob(string& out_report)
{
_show_args0();
unsigned long mm,m; // measured mines mm loop and location index m loop
cl_I temp, temp2;
cl_SV_I unmeasured_comb(max_mm_-min_mm_+1); // min_mm_ to max_mm_ inclusive
cl_SV_I m_total_ways(m_max_+1); // 0...(m_max_) inclusive, numerator
cl_I big_total_comb(0); // denominator
cl_I total_unmeasured(0); // unmeasured numerator
cl_I big_total_unmeasured(0); // unmeasured denominator
cl_RA ratio; // Exact fraction
pair_prob_loc_t p_l; // double prob. and guint32 location
ostrstream report; // Make reporting string with sstream...fun!
// Fill m_total_ways
if (0==num_unmeasured_)
{
for (mm=min_mm_; mm<=max_mm_; ++mm)
{
big_total_comb += mm_ways_[mm];
for (m=0; m<=m_max_; ++m)
{
m_total_ways[m] += mm_m_ways_[mm][m];
}
}
}
else
{
// Also calculations for unmeasured locations
for (mm=min_mm_; mm<=max_mm_; ++mm)