Advertisement
permatrash

Sudoku solver puzzle.cpp

Jul 14th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.97 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include <fstream>
  4. #include<cstdlib>
  5. #include"cell.h"
  6. #include"puzzle.h"
  7. using namespace std;
  8.  
  9. //isSolved=======================================================================================================================================
  10. int Puzzle::isSolved(){
  11.     for(int i=0; i<9; i++){
  12.         for(int j=0; j<9; j++){
  13.             if(this->checkConflicts(i, j) == 1) return 0;
  14.         }
  15.     }
  16.  
  17.     return 1;
  18. }
  19.  
  20. //Test Readout=====================================================================================================================================
  21. void Puzzle::testRead(){
  22.     for(int i=0; i<9; i++){
  23.         for(int j=0; j<9; j++){
  24.             cout << "Set up Cell [" << i << "][" << j << "]";
  25.             cout << " with value: " << this->board[i][j].getValue();
  26.             cout << " and candidates: ";
  27.             for(int k=1; k <=9; k++){
  28.                 cout <<this->board[i][j].getCandidate(k);
  29.             }
  30.             cout << endl;
  31.         }
  32.     }
  33.  
  34.     for(int i=0; i<9; i++){
  35.         for(int j=0;j<9;j++){
  36.             cout << this->board[i][j].getValue();
  37.         }
  38.     }
  39.     cout << endl;
  40. }
  41.  
  42.  
  43. //Eliminate Candidates function====================================================================================================================================================================
  44.  
  45. void Puzzle::eliminateCandidates(){
  46.  
  47.     for(int i=0; i<9; i++){
  48.         for(int j=0; j<9; j++){
  49.  
  50.             //if the cell value is zero, find conflicting cells and eliminate them from candidates array of current cell
  51.             if(this->board[i][j].getValue() == 0){
  52.  
  53.                 //Row elimination
  54.                 for (int k=0; k<9; k++){
  55.                         //if another cell in the row has a definite value, eliminate that as a candidate from the current cell.
  56.                         if(this->board[k][j].getValue() != 0) this->board[i][j].setCandidate(this->board[k][j].getValue(), 0);
  57.                 }
  58.  
  59.                 //Column elimination
  60.                 for (int k=0; k<9; k++){
  61.                         //if another cell in the column has a definite value, eliminate that as a candidate from the current cell
  62.                         if(this->board[i][k].getValue() != 0) this->board[i][j].setCandidate(this->board[i][k].getValue(), 0);
  63.                 }
  64.  
  65.                 //Block elimination
  66.  
  67.                 // *00
  68.                 // 000
  69.                 // 000
  70.                 if((j == 0 || j == 3 || j == 6) && (i == 0 || i == 3 || i == 6))
  71.                     {
  72.                     if(this->board[i][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+1].getValue(),0);
  73.                     if(this->board[i][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+2].getValue(),0);
  74.                     if(this->board[i+1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j].getValue(),0);
  75.                     if(this->board[i+2][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j].getValue(),0);
  76.                     if(this->board[i+1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j+1].getValue(),0);
  77.                     if(this->board[i+1][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j+2].getValue(),0);
  78.                     if(this->board[i+2][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j+1].getValue(),0);
  79.                     if(this->board[i+2][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j+2].getValue(),0);
  80.                   }
  81.  
  82.                 //  000
  83.                 //  000
  84.                 //  *00
  85.                 if((j == 0 || j == 3 || j == 6) && (i == 2 || i == 5 || i == 8))
  86.                   {
  87.                      if(this->board[i-1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j].getValue(),0);
  88.                      if(this->board[i-2][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j].getValue(),0);
  89.                      if(this->board[i][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+1].getValue(),0);
  90.                      if(this->board[i][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+2].getValue(),0);
  91.                      if(this->board[i-1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j+1].getValue(),0);
  92.                      if(this->board[i-1][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j+2].getValue(),0);
  93.                      if(this->board[i-2][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j+1].getValue(),0);
  94.                      if(this->board[i-2][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j+2].getValue(),0);
  95.                   }
  96.  
  97.                 // 000
  98.                 // *00
  99.                 // 000
  100.                 if((j == 0 || j == 3 || j == 6) && (i == 1 || i == 4 || i == 7))
  101.                   {
  102.                      if(this->board[i-1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j].getValue(),0);
  103.                      if(this->board[i-1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j+1].getValue(),0);
  104.                      if(this->board[i-1][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j+2].getValue(),0);
  105.                      if(this->board[i][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+1].getValue(),0);
  106.                      if(this->board[i][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+2].getValue(),0);
  107.                      if(this->board[i+1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j].getValue(),0);
  108.                      if(this->board[i+1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j+1].getValue(),0);
  109.                      if(this->board[i+1][j+2].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j+2].getValue(),0);
  110.                   }
  111.  
  112.                 // 0*0
  113.                 // 000
  114.                 // 000
  115.                 if((j == 1 || j == 4 || j == 7) && (i == 0 || i == 3 || i == 6))
  116.                   {
  117.                     if(this->board[i][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-1].getValue(),0);
  118.                     if(this->board[i][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+1].getValue(),0);
  119.                     if(this->board[i+1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j+1].getValue(),0);
  120.                     if(this->board[i+1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j-1].getValue(),0);
  121.                     if(this->board[i+1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j].getValue(),0);
  122.                     if(this->board[i+2][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j-1].getValue(),0);
  123.                     if(this->board[i+2][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j].getValue(),0);
  124.                     if(this->board[i+2][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j+1].getValue(),0);
  125.                   }
  126.  
  127.                 // 000
  128.                 // 0*0
  129.                 // 000
  130.                 if((j == 1 || j == 4 || j == 7) && (i == 1 || i == 4 || i == 7))
  131.                   {
  132.                      if(this->board[i-1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j].getValue(),0);
  133.                      if(this->board[i-1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j-1].getValue(),0);
  134.                      if(this->board[i-1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j+1].getValue(),0);
  135.                      if(this->board[i][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+1].getValue(),0);
  136.                      if(this->board[i][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-1].getValue(),0);
  137.                      if(this->board[i+1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j+1].getValue(),0);
  138.                      if(this->board[i+1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j].getValue(),0);
  139.                      if(this->board[i+1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j-1].getValue(),0);
  140.                   }
  141.  
  142.  
  143.                 // 000
  144.                 // 000
  145.                 // 0*0
  146.                 if((j == 1 || j == 4 || j == 7) && (i == 2 || i == 5 || i == 8))
  147.                   {
  148.                      if(this->board[i][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-1].getValue(),0);
  149.                      if(this->board[i][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j+1].getValue(),0);
  150.                      if(this->board[i-1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j].getValue(),0);
  151.                      if(this->board[i-1][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j+1].getValue(),0);
  152.                      if(this->board[i-1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j-1].getValue(),0);
  153.                      if(this->board[i-2][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j].getValue(),0);
  154.                      if(this->board[i-2][j+1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j+1].getValue(),0);
  155.                      if(this->board[i-2][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j-1].getValue(),0);
  156.                   }
  157.  
  158.                 // 00*
  159.                 // 000
  160.                 // 000
  161.                 if((j == 2 || j == 5 || j == 8) && (i == 0 || i == 3 || i == 6))
  162.                   {
  163.                      if(this->board[i][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-1].getValue(),0);
  164.                      if(this->board[i][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-2].getValue(),0);
  165.                      if(this->board[i+1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j].getValue(),0);
  166.                      if(this->board[i+1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j-1].getValue(),0);
  167.                      if(this->board[i+1][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j-2].getValue(),0);
  168.                      if(this->board[i+2][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j].getValue(),0);
  169.                      if(this->board[i+2][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j-1].getValue(),0);
  170.                      if(this->board[i+2][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i+2][j-2].getValue(),0);
  171.                   }
  172.  
  173.                 // 000
  174.                 // 00*
  175.                 // 000
  176.                 if((j == 2 || j == 5 || j == 8) && (i == 1 || i == 4 || i == 7))
  177.                   {
  178.                      if(this->board[i-1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j].getValue(),0);
  179.                      if(this->board[i-1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j-1].getValue(),0);
  180.                      if(this->board[i-1][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j-2].getValue(),0);
  181.                      if(this->board[i][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-1].getValue(),0);
  182.                      if(this->board[i][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-2].getValue(),0);
  183.                      if(this->board[i+1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j].getValue(),0);
  184.                      if(this->board[i+1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j-1].getValue(),0);
  185.                      if(this->board[i+1][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i+1][j-2].getValue(),0);
  186.                   }
  187.  
  188.                 // 000
  189.                 // 000
  190.                 // 00*
  191.                 if((j == 2 || j == 5 || j == 8) && (i == 2 || i == 5 || i == 8))
  192.                   {
  193.                      if(this->board[i][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-1].getValue(),0);
  194.                      if(this->board[i][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i][j-2].getValue(),0);
  195.                      if(this->board[i-1][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j].getValue(),0);
  196.                      if(this->board[i-1][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j-1].getValue(),0);
  197.                      if(this->board[i-1][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i-1][j-2].getValue(),0);
  198.                      if(this->board[i-2][j].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j].getValue(),0);
  199.                      if(this->board[i-2][j-1].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j-1].getValue(),0);
  200.                      if(this->board[i-2][j-2].getValue() != 0) this->board[i][j].setCandidate(this->board[i-2][j-2].getValue(),0);
  201.                   }
  202.  
  203.             } //if this->board[i][j] == 0
  204.         }//for j
  205.     }//for i
  206. }
  207. //===================================================================================================================================================================================================
  208.  
  209.  
  210.  
  211. //Find Single Candidate===============================================================================================================================================================================
  212. int Puzzle::findSingleCandidate(){
  213.  
  214.     int candCount = 0;
  215.     int singleCand = 0;
  216.     int i,j,k;
  217.     i=0;
  218.     j=0;
  219.     k=1;
  220.  
  221.     for(i=0; i<9; i++){
  222.         for(j=0; j<9; j++){
  223.             if(this->board[i][j].getValue() == 0){
  224.  
  225.                 candCount = 0;
  226.  
  227.                 //count number of possible candidates
  228.                 for(k = 1; k<=9; k++){
  229.                     if(this->board[i][j].getCandidate(k) == 1){
  230.                         candCount++;
  231.                         singleCand = k;
  232.                     }
  233.                 }
  234.  
  235.                 //if there is only one candidate, break.
  236.                 if(candCount == 1){
  237.                     this->board[i][j].setValue(singleCand);
  238.                     this->board[i][j].setCandidate(singleCand, 0);
  239.                     return 1;
  240.                 }
  241.  
  242.             }// if this->board[i][j].getValue() == 0
  243.         }//for j
  244.     }//for i
  245.     return 0;
  246. }
  247.  
  248. //=====================================================================================================================================================================================================
  249.  
  250.  
  251. //Check conflicts======================================================================================================================================================================================
  252. int Puzzle::checkConflicts(int i, int j){
  253.     //Returns 1 if there is a conflict with the current value stored in this->board[i][j]. Returns 0 otherwise.
  254.  
  255.     int k;
  256.  
  257.         //Row compare
  258.         for (k=0; k<9; k++){
  259.                 //skip self comparison
  260.                 if(k==i) continue;
  261.                 //if another cell in the row has a definite value, eliminate that as a candidate from the current cell.
  262.                  else if(this->board[k][j].getValue()  == this->board[i][j].getValue()) return 1;
  263.         }
  264.  
  265.         //Column compare
  266.         for (k=0; k<9; k++){
  267.                 //skip self comparison
  268.                 if(k==j) continue;
  269.                 //if another cell in the column has a definite value, eliminate that as a candidate from the current cell.
  270.                 else if(this->board[i][k].getValue()  == this->board[i][j].getValue()) return 1;
  271.         }
  272.  
  273.         //Block compare
  274.         // *00
  275.         // 000
  276.         // 000
  277.         if((j == 0 || j == 3 || j == 6) && (i == 0 || i == 3 || i == 6))
  278.             {
  279.             if(this->board[i][j+1].getValue() == this->board[i][j].getValue()) return 1;
  280.             if(this->board[i][j+2].getValue() == this->board[i][j].getValue()) return 1;
  281.             if(this->board[i+1][j].getValue() == this->board[i][j].getValue()) return 1;
  282.             if(this->board[i+2][j].getValue() == this->board[i][j].getValue()) return 1;
  283.             if(this->board[i+1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  284.             if(this->board[i+1][j+2].getValue() == this->board[i][j].getValue()) return 1;
  285.             if(this->board[i+2][j+1].getValue() == this->board[i][j].getValue()) return 1;
  286.             if(this->board[i+2][j+2].getValue() == this->board[i][j].getValue()) return 1;
  287.           }
  288.  
  289.         //  000
  290.         //  000
  291.         //  *00
  292.         if((j == 0 || j == 3 || j == 6) && (i == 2 || i == 5 || i == 8))
  293.           {
  294.              if(this->board[i-1][j].getValue() == this->board[i][j].getValue()) return 1;
  295.              if(this->board[i-2][j].getValue() == this->board[i][j].getValue()) return 1;
  296.              if(this->board[i][j+1].getValue() == this->board[i][j].getValue()) return 1;
  297.              if(this->board[i][j+2].getValue() == this->board[i][j].getValue()) return 1;
  298.              if(this->board[i-1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  299.              if(this->board[i-1][j+2].getValue() == this->board[i][j].getValue()) return 1;
  300.              if(this->board[i-2][j+1].getValue() == this->board[i][j].getValue()) return 1;
  301.              if(this->board[i-2][j+2].getValue() == this->board[i][j].getValue()) return 1;
  302.           }
  303.  
  304.         // 000
  305.         // *00
  306.         // 000
  307.         if((j == 0 || j == 3 || j == 6) && (i == 1 || i == 4 || i == 7))
  308.           {
  309.              if(this->board[i-1][j].getValue() == this->board[i][j].getValue()) return 1;
  310.              if(this->board[i-1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  311.              if(this->board[i-1][j+2].getValue() == this->board[i][j].getValue()) return 1;
  312.              if(this->board[i][j+1].getValue() == this->board[i][j].getValue()) return 1;
  313.              if(this->board[i][j+2].getValue() == this->board[i][j].getValue()) return 1;
  314.              if(this->board[i+1][j].getValue() == this->board[i][j].getValue()) return 1;
  315.              if(this->board[i+1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  316.              if(this->board[i+1][j+2].getValue() == this->board[i][j].getValue()) return 1;
  317.           }
  318.  
  319.         // 0*0
  320.         // 000
  321.         // 000
  322.         if((j == 1 || j == 4 || j == 7) && (i == 0 || i == 3 || i == 6))
  323.           {
  324.             if(this->board[i][j-1].getValue() == this->board[i][j].getValue()) return 1;
  325.             if(this->board[i][j+1].getValue() == this->board[i][j].getValue()) return 1;
  326.             if(this->board[i+1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  327.             if(this->board[i+1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  328.             if(this->board[i+1][j].getValue() == this->board[i][j].getValue()) return 1;
  329.             if(this->board[i+2][j-1].getValue() == this->board[i][j].getValue()) return 1;
  330.             if(this->board[i+2][j].getValue() == this->board[i][j].getValue()) return 1;
  331.             if(this->board[i+2][j+1].getValue() == this->board[i][j].getValue()) return 1;
  332.           }
  333.  
  334.         // 000
  335.         // 0*0
  336.         // 000
  337.         if((j == 1 || j == 4 || j == 7) && (i == 1 || i == 4 || i == 7))
  338.           {
  339.              if(this->board[i-1][j].getValue() == this->board[i][j].getValue()) return 1;
  340.              if(this->board[i-1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  341.              if(this->board[i-1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  342.              if(this->board[i][j+1].getValue() == this->board[i][j].getValue()) return 1;
  343.              if(this->board[i][j-1].getValue() == this->board[i][j].getValue()) return 1;
  344.              if(this->board[i+1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  345.              if(this->board[i+1][j].getValue() == this->board[i][j].getValue()) return 1;
  346.              if(this->board[i+1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  347.           }
  348.  
  349.  
  350.         // 000
  351.         // 000
  352.         // 0*0
  353.         if((j == 1 || j == 4 || j == 7) && (i == 2 || i == 5 || i == 8))
  354.           {
  355.              if(this->board[i][j-1].getValue() == this->board[i][j].getValue()) return 1;
  356.              if(this->board[i][j+1].getValue() == this->board[i][j].getValue()) return 1;
  357.              if(this->board[i-1][j].getValue() == this->board[i][j].getValue()) return 1;
  358.              if(this->board[i-1][j+1].getValue() == this->board[i][j].getValue()) return 1;
  359.              if(this->board[i-1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  360.              if(this->board[i-2][j].getValue() == this->board[i][j].getValue()) return 1;
  361.              if(this->board[i-2][j+1].getValue() == this->board[i][j].getValue()) return 1;
  362.              if(this->board[i-2][j-1].getValue() == this->board[i][j].getValue()) return 1;
  363.           }
  364.  
  365.         // 00*
  366.         // 000
  367.         // 000
  368.         if((j == 2 || j == 5 || j == 8) && (i == 0 || i == 3 || i == 6))
  369.           {
  370.              if(this->board[i][j-1].getValue() == this->board[i][j].getValue()) return 1;
  371.              if(this->board[i][j-2].getValue() == this->board[i][j].getValue()) return 1;
  372.              if(this->board[i+1][j].getValue() == this->board[i][j].getValue()) return 1;
  373.              if(this->board[i+1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  374.              if(this->board[i+1][j-2].getValue() == this->board[i][j].getValue()) return 1;
  375.              if(this->board[i+2][j].getValue() == this->board[i][j].getValue()) return 1;
  376.              if(this->board[i+2][j-1].getValue() == this->board[i][j].getValue()) return 1;
  377.              if(this->board[i+2][j-2].getValue() == this->board[i][j].getValue()) return 1;
  378.           }
  379.  
  380.         // 000
  381.         // 00*
  382.         // 000
  383.         if((j == 2 || j == 5 || j == 8) && (i == 1 || i == 4 || i == 7))
  384.           {
  385.              if(this->board[i-1][j].getValue() == this->board[i][j].getValue()) return 1;
  386.              if(this->board[i-1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  387.              if(this->board[i-1][j-2].getValue() == this->board[i][j].getValue()) return 1;
  388.              if(this->board[i][j-1].getValue() == this->board[i][j].getValue()) return 1;
  389.              if(this->board[i][j-2].getValue() == this->board[i][j].getValue()) return 1;
  390.              if(this->board[i+1][j].getValue() == this->board[i][j].getValue()) return 1;
  391.              if(this->board[i+1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  392.              if(this->board[i+1][j-2].getValue() == this->board[i][j].getValue()) return 1;
  393.           }
  394.  
  395.         // 000
  396.         // 000
  397.         // 00*
  398.         if((j == 2 || j == 5 || j == 8) && (i == 2 || i == 5 || i == 8))
  399.           {
  400.              if(this->board[i][j-1].getValue() == this->board[i][j].getValue())  return 1;
  401.              if(this->board[i][j-2].getValue() == this->board[i][j].getValue()) return 1;
  402.              if(this->board[i-1][j].getValue() == this->board[i][j].getValue()) return 1;
  403.              if(this->board[i-1][j-1].getValue() == this->board[i][j].getValue()) return 1;
  404.              if(this->board[i-1][j-2].getValue() == this->board[i][j].getValue()) return 1;
  405.              if(this->board[i-2][j].getValue() == this->board[i][j].getValue()) return 1;
  406.              if(this->board[i-2][j-1].getValue() == this->board[i][j].getValue()) return 1;
  407.              if(this->board[i-2][j-2].getValue() == this->board[i][j].getValue()) return 1;
  408.           }
  409.  
  410.     //No conflict detected, return 0;
  411.     return 0;
  412. }
  413.  
  414. //===================================================================================================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement