[ create a new paste ] login | about

Link: http://codepad.org/t72PRLDL    [ raw code | output | fork ]

C++, pasted on Feb 1:
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// class SudCell prototype --------------------------------------
class SudCell
{
private:
   bool completed;
public:
   static const int MIN_VAL = 1;
   static const int MAX_VAL = 9;
   static const int NUM_VALS = 9;
   static const int SQRT_NUM_VALS = 3;
   static const int NOT_FINAL_YET = -1;

   SudCell();
   SudCell(const SudCell &sud);
   string ToString();
   string ToStringReveal();
   int GetFinalValue();
   bool IsCompleted();
   bool ValuePresent(int val);
   int NumRemainingVals();
   void SetToBlank();
   bool ForcedSet(int val);
   bool Complete();
   bool RuleOut(int val);
   bool SoftSet(int val);
   bool Validate(int val);
private:
   bool digits[NUM_VALS];
};

// class Sudoku prototype --------------------------------------
class Sudoku
{
public:
   static const int MIN_VAL = SudCell::MIN_VAL;
   static const int MAX_VAL = SudCell::MAX_VAL;
   static const int NUM_VALS = SudCell::NUM_VALS;
   static const int SQRT_NUM_VALS = SudCell::SQRT_NUM_VALS;
   static const int NOT_FINAL_YET = SudCell::NOT_FINAL_YET;

   Sudoku();
   void Display(bool reveal);
   int GetFinalValue(int row, int col);
   bool IsCompleted(int row, int col);
   bool ValuePresent(int row, int col, int val);
   int NumRemainingVals(int row, int col);
   int NumRemainingValsTotal();
   bool AllSingletons();
   bool EmptyCellFound();
   void SetToBlank();
   bool ForcedSet(int row, int col, int val);
   bool Complete(int row, int col);
   bool RuleOut(int row, int col, int val);
   bool SoftSet(int row, int col, int val);
   bool Validate(int val);
   bool IndexValidate(int n);

   // B1 methods--------------
   void WireUpSectorArrays();
   void SectorDisplay(int sec_row, int sec_col);
   bool SectorValidate(int n);
   bool GetSector(int &sec_row, int &sec_col, int row, int col);
   bool GetSectorCoords(int &row_in_sec, int &col_in_sec, int row, int col);
   bool GetFullCoords(int sec_row, int sec_col, int row_in_sec, int col_in_sec, int &row, int &col);
   SudCell* sector[SQRT_NUM_VALS][SQRT_NUM_VALS]  [SQRT_NUM_VALS][SQRT_NUM_VALS];
private:
   SudCell suds[NUM_VALS][NUM_VALS];
   //SudCell* sector[SQRT_NUM_VALS][SQRT_NUM_VALS]  [SQRT_NUM_VALS][SQRT_NUM_VALS];
   //void WireUpSectorArrays();
};

// Main---------------------------------------
int main()
{
   Sudoku puzzle;
   puzzle.WireUpSectorArrays();
   puzzle.SectorDisplay(1,1);

/*
   // set some values in the matrix
   puzzle.ForcedSet(3, 4, 5);
   puzzle.ForcedSet(5, 7, 8);
   puzzle.ForcedSet(7, 2, 9);
   for (int k = -1; k < 12; k++)
      if ( !puzzle.ForcedSet(k, k, k) )
         cout << "Could not set " << k << endl;
   cout << endl;

   // see the matrix
   puzzle.Display(false);
   cout << endl;

   puzzle.WireUpSectorArrays();

   // see if we can display a couple legal and illegal sectors
   puzzle.SectorDisplay(1, 2);
   cout << endl;
   puzzle.SectorDisplay(3, 3);
   cout << endl;
   puzzle.SectorDisplay(2, 3);
   cout << endl;

   // should be invalid
   puzzle.SectorDisplay(0, 1);
   cout << endl;
   puzzle.SectorDisplay(2, 4);
   cout << endl;
*/
   /*
   SudCell sud1, sud2, sud3;

   if ( !sud1.ForcedSet(10) )
      cout << "Bad 10\n";

   if ( !sud2.ForcedSet(-1) )
      cout << "Bad -1\n";
   if ( !sud3.RuleOut(50) )
      cout << "Bad 50\n";

   cout << "A: " << sud1.ToString() << endl;
   cout << "B: " << sud2.ToString() << endl;
   cout << "C: " << sud3.ToString() << endl;
   cout << endl;

   cout << "D: " << sud1.ToStringReveal() << endl;
   cout << "E: " << sud2.ToStringReveal() << endl;
   cout << "F: " << sud3.ToStringReveal() << endl;
   cout << endl;

   sud1.ForcedSet(3);
   sud2.ForcedSet(9);
   sud3.RuleOut(0);
   sud3.RuleOut(2);
   sud3.RuleOut(4);
   sud3.RuleOut(6);
   sud3.RuleOut(8);

   cout << sud1.ToString() << endl;
   cout << sud2.ToString() << endl;
   cout << sud3.ToString() << endl;
   cout << endl;

   cout << sud1.ToStringReveal() << endl;
   cout << sud2.ToStringReveal() << endl;
   cout << sud3.ToStringReveal() << endl;
   cout << endl;

   sud1.Complete();
   sud2.Complete();
   sud3.Complete();

   cout << sud1.IsCompleted() << " " << sud2.IsCompleted() << " "
      << sud3.IsCompleted() << endl;
   cout << sud1.ValuePresent(1) << sud1.ValuePresent(2)
      << sud1.ValuePresent(3) << endl;
   cout << sud3.ValuePresent(1) << sud3.ValuePresent(2)
      << sud3.ValuePresent(3)<< endl;

   //Soduku check---------------------------------------
//int main()
//{
   Sudoku puzzle;

   // set some values in the matrix, attempting illegal
   puzzle.ForcedSet(3, 4, 5);
   puzzle.ForcedSet(7, 1, 3);
   puzzle.ForcedSet(9, 2, 6);
   for (int k = -1; k < 12; k++)
      if ( !puzzle.ForcedSet(k, k, k) )
         cout << "Could not set " << k << endl;

   cout << "\n ------------- first basic ------------- \n";
   puzzle.Display(false);

   cout << "\n ------------- then revealed ------------- \n";
   puzzle.Display(true);
   cout << endl;

   for (int row = Sudoku::MIN_VAL; row <= Sudoku::MAX_VAL ; row++)
      for (int col = Sudoku::MIN_VAL; col <= Sudoku::MAX_VAL ; col++)
      {
         // rule out 2 through 8 in all places except middle row and col
         if ( row == 5 || col == 5 )
            continue;
          for (int k = 2; k <= 8; k++)
            puzzle.RuleOut(row, col, k);
      }

   cout << "\n ---------- after massive rule-outs ---------- \n";
   puzzle.Display(true);
*/
   return 0;
}

// SUDCELL  -------------------------------------------------

SudCell::SudCell()
{
   completed = false;
   SetToBlank();
}

SudCell::SudCell(const SudCell &sud)
{
   this->completed = sud.completed;
   for (int x=0;x<9;x++)
      this->digits[x] = sud.digits[x];
}

string SudCell::ToString()
{
   string output;
   int boolcount = 0;
   int whichdigit = 0;
   for (int x=0;x<9;x++)
   {
      if (digits[x] == true)
      {
         whichdigit = x;
         boolcount++;
      }
   }
   if (boolcount > 1 || boolcount < 1)
      output = " - ";
   else
   {
      whichdigit++;
      ostringstream cnvrt;
      cnvrt << whichdigit;
      output = cnvrt.str();
      output = " " + output + " ";
   }

   return output;
}

string SudCell::ToStringReveal()
{
   string numstring, outstring;
   for (int x=0;x<9;x++)
   {
      if (digits[x] == true)
      {
         ostringstream cnvrt;
         cnvrt << x + 1;
         numstring += cnvrt.str();
      }
   }
   numstring = " " + numstring + " ";
   return numstring;
}

int SudCell::GetFinalValue()
{
   int boolcount = 0;
   int whichdigit = 0;
   for (int x=0;x<9;x++)
   {
      if (digits[x] == true)
      {
         whichdigit = digits[x];
         boolcount++;
      }
   }
   if (boolcount > 1 || boolcount == 0)
      return NOT_FINAL_YET;
   else
      return whichdigit;
}

bool SudCell::IsCompleted()
{
   int boolcount = 0;
   for (int x=0;x<9;x++)
   {
      if (digits[x] == true)
         boolcount++;
   }
   if (boolcount == 1)
      return true;
   else
      return false;
}

bool SudCell::ValuePresent(int val)
{
   if (digits[val-1] == true)
      return true;
   else
      return false;
}

int SudCell::NumRemainingVals()
{
   int boolcount = 0;
   for (int x=0;x<9;x++)
   {
      if (digits[x] == true)
         boolcount++;
   }
   return boolcount;
}

void SudCell::SetToBlank()
{
   for (int x=0;x<9;x++)
   {
      digits[x] = true;
   }
}

bool SudCell::ForcedSet(int val)
{
   if(val < MIN_VAL || val > MAX_VAL)
      return false;
   else
   {
   for (int x=0;x<9;x++)
   {
      digits[x] = false;
   }
   digits[val-1] = true;
   return true;
   }
}

bool SudCell::Complete()
{
   if (IsCompleted())
   {
      completed = true;
      return true;
   }
   else
      return false;
}
bool SudCell::RuleOut(int val)
{
if (val < MIN_VAL || val > MAX_VAL)
   return false;
else
{
   digits[val-1] = false;
   return true;
}
}
bool SudCell::SoftSet(int val)
{
if (val < MIN_VAL || val > MAX_VAL)
   return false;
else
{
   digits[val] = true;
   return true;
}
}

bool SudCell::Validate(int val)
{
   if (val > MAX_VAL || val < MIN_VAL)
      return false;
   else
      return true;
}

// SUDOKU  ------------------------------------------------

Sudoku::Sudoku()  //copy constructor??
{
}
void Sudoku::Display(bool reveal)
{
   string output;
   int levlinecount = 0, sepcount = 0, divider = 0, divideend = 0;
   if (reveal == false)
   {
      for (int x=0;x<MAX_VAL;x++)
      {
         for (int y=0;y<MAX_VAL;y++)
         {
            output = suds[x][y].ToString();
            cout << output;
            sepcount++;
            if (sepcount > 2)
            {
               sepcount = 0;
               if (levlinecount < 2)
               {
                  cout << "|";
                  levlinecount++;
               }
               else
               {
                  levlinecount = 0;
                  if (divider < 2)
                  {
                     cout << endl;
                     divider++;
                  }
                  else
                  {
                     if (divideend < 2)
                     {
                        cout << endl << "-----------------------------" << endl;
                        divider = 0;
                        divideend++;
                     }
                  }
               }
            }
         }
      }
   }
   else
   {
      for (int x=0;x<MAX_VAL;x++)
      {
         for (int y=0;y<MAX_VAL;y++)
         {
            output = suds[x][y].ToStringReveal();
            cout << output;
            sepcount++;
            if (sepcount > 2)
            {
               sepcount = 0;
               if (levlinecount < 2)
               {
                  cout << "|";
                  levlinecount++;
               }
               else
               {
                  levlinecount = 0;
                  if (divider < 2)
                  {
                     cout << endl;
                     divider++;
                  }
                  else
                  {
                     if (divideend < 2)
                     {
                        cout << endl << "-----------------------------" << endl;
                        divider = 0;
                        divideend++;
                     }
                  }
               }
            }
         }
      }
   }
}
int Sudoku::GetFinalValue(int row, int col)
{
   return suds[row-1][col-1].SudCell::GetFinalValue();
}
bool Sudoku::IsCompleted(int row, int col)
{
   return suds[row-1][col-1].SudCell::IsCompleted();
}
bool Sudoku::ValuePresent(int row, int col, int val)
{
   return suds[row-1][col-1].SudCell::ValuePresent(val);
}
int Sudoku::NumRemainingVals(int row, int col)
{
   return suds[row-1][col-1].SudCell::NumRemainingVals();
}
int Sudoku::NumRemainingValsTotal()
{
   int total = 0;
   for (int x=0;x<MAX_VAL;x++)
   {
      for (int y=0;y<MAX_VAL;y++)
      {
         total += suds[x][y].GetFinalValue();
      }
   }
   return total;
}
bool Sudoku::AllSingletons()
{
   bool answer = true;
   for (int x=0;x<MAX_VAL;x++)
   {
      for (int y=0;y<MAX_VAL;y++)
      {
         if (NOT_FINAL_YET == suds[x][y].GetFinalValue())
            answer = false;
      }
   }
   return answer;
}
bool Sudoku::EmptyCellFound()
{
   bool answer = false;
   for (int x=0;x<MAX_VAL;x++)
   {
      for (int y=0;y<MAX_VAL;y++)
      {
         if (0 == suds[x][y].NumRemainingVals())
            answer = true;
      }
   }
   return answer;
}
void Sudoku::SetToBlank()
{
   for (int x=0;x<MAX_VAL;x++)
   {
      for (int y=0;y<MAX_VAL;y++)
      {
         suds[x][y].SetToBlank();
      }
   }
}
bool Sudoku::ForcedSet(int row, int col, int val)
{
   return suds[row-1][col-1].SudCell::ForcedSet(val);
}
bool Sudoku::Complete(int row, int col)
{
   return suds[row-1][col-1].SudCell::Complete();
}
bool Sudoku::RuleOut(int row, int col, int val)
{
   return suds[row-1][col-1].SudCell::RuleOut(val);
}
bool Sudoku::SoftSet(int row, int col, int val)
{
   return suds[row-1][col-1].SudCell::SoftSet(val);
}
bool Sudoku::Validate(int val)
{
   if (val < MIN_VAL || val > MAX_VAL)
      return false;
   else
      return true;
}
bool Sudoku::IndexValidate(int n)
{
   if (n < 0 || n > NUM_VALS-1)
      return false;
   else
      return true;
}

// B1 methods----------------------------------------

void Sudoku::WireUpSectorArrays()
{
   int inc3 = 0, inc3acr = 0, inc3down = 0, inc3downjump = 0;
   for(int x=0;x<MAX_VAL;x++)
      for(int y=0;y<MAX_VAL;y++)
      {
         sector[inc3downjump][inc3acr][inc3down][inc3] = &suds[x][y];
         //cout << sector[inc3downjump][inc3acr][inc3down][inc3]->SudCell::ToString();
         inc3++;
         if(inc3 > 2)
         {
            inc3 = 0;
            inc3acr++;
            if (inc3acr > 2)
            {
               inc3acr = 0;
               inc3down ++;
               if(inc3down > 2)
               {
                  inc3down = 0;
                  inc3downjump++;
                  if(inc3downjump > 2)
                     inc3downjump = 0;
               }
            }
         }
      }
}

void Sudoku::SectorDisplay(int sec_row, int sec_col)
{
   int out = 0, stop = 0;
   for (int x=0;x<3;x++)
      for (int y=0;y<3;y++)
      {
         sector[sec_row][sec_col][x][y]->ToString();
         out++;
         if (out > 2)
         {
            cout << endl;
            out = 0;
            stop++;
            if (stop > 1)
               stop = 0;
         }
      }
}

bool Sudoku::SectorValidate(int n)
{
   if (n > 0 || n < SQRT_NUM_VALS)
      return false;
   else
      return true;
}

bool Sudoku::GetSector(int &sec_row, int &sec_col, int row, int col)
{
   if (row > 1 || row < 9)
      return false;
   else
   {
   if (row > 0 && row < 4)
      sec_row = 1;
   else if (row > 3 && row < 7)
      sec_row = 2;
   else if (row > 6 && row < 10)
      sec_row = 3;
   else
      return false;
   }

   if (row > 1 || row < 9)
      return false;
   else
   {
   if (col > 0 && col < 4)
      sec_col = 1;
   else if (col > 3 && col < 7)
      sec_col = 2;
   else if (col > 6 && col < 10)
      sec_col = 3;
   else
      return false;
   }
   return true;
}

bool Sudoku::GetSectorCoords(int &row_in_sec, int &col_in_sec, int row, int col)
{
   if (row > 1 || row < 9 || col > 1 || col < 9)
      return false;
   else
   {
      row_in_sec = (row % 3) + 1;
      col_in_sec = (col % 3) + 1;
      return true;
   }
}

bool Sudoku::GetFullCoords(int sec_row, int sec_col, int row_in_sec, int col_in_sec, int &row, int &col)
{
   if (sec_row < 1 || sec_row > 3 || sec_col < 1 || sec_row > 3)
      return false;
   else if (row_in_sec < 1 || row_in_sec > 3 || col_in_sec < 1 || col_in_sec > 3)
      return false;
   else
   {
      row = (sec_row * 3) + row_in_sec;
      col = (sec_col * 3) + col_in_sec;
      return true;
   }
}


Output:
1
2
3





Create a new paste based on this one


Comments: