Help with fun Sudoku Program!

Hello all, i am currently working on a cool little program that takes a sudoku problem and outputs the answer. It's been fun, but i seem to have hit a snag on two of the functions.

Here is the main program:

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
#include <iostream>
#include <fstream>
#include "intset.cpp"
#define DEBUG

using namespace std;

typedef SetOfSmallInts Puzzle [9] [9];
typedef SetOfSmallInts* PuzzleSection [9];
enum SolutionStatus {solved, unsolvable, working};

void copyPuzzle(Puzzle pp, Puzzle p)
{
  int i, j;
  for(i = 0; i < 9; i++)
  {
	for(j = 0; j < 9; j++)
	{
	  pp[i][j] = p[i][j];
	}
  }
}

void getRow(PuzzleSection R, Puzzle p, int i)
{
  int j;
  for(j = 0; j < 9; j++)
  {
	R[j] = &(p[i][j]);
  }
}

void getColumn(PuzzleSection R, Puzzle p, int j)
{
  int i;
  for(i = 0; i < 9; i++)
  {
	R[i] = &(p[i][j]);
  }
}

void getSquare(PuzzleSection R, Puzzle p, int k)
{
  int i;
  for(i = 0; i < 9; i++)
  {
	R[i] = &(p[k - k%3 + i/3][3*(k%3) + i%3]);
  }
}


void readPuzzle(Puzzle p)
{
	 char c;
	 SetOfSmallInts set = rangeSet(1,9);
	 for(int i =0; i<9; i++)
	 {
			 for(int j=0; j < 9; j++)
			 {
					 cin >> c;
					 if(c == '-') p[i][j]=set;
					 else p[i][j] = singletonSet(c-'0');
			 }
	 }
}

void printPuzzle(Puzzle p)
{
   int k;
   for(int i=0; i < 9; i++)
   {
	  for(int j=0; j < 9; j++)
	  {
		   k= onlyMember(p[i][j]);
		   if (j == 3 || j == 6) cout << " ";
		   if (k == 0) cout << "-";
		   else cout << k;
	   }
	   if (i==2 || i == 5) cout << "\n";
	   cout <<"\n";
   }
}

bool tacticOneOnSection(PuzzleSection sec)
{
 SetOfSmallInts tempSet[9];
 Puzzle section;
   
   for(int i = 0; i < 9; i++)
   {
    for(int j = 0; j < 9; j++)
    {
           if(member(0,sec[i][j]))
           {
            sec[i][j] = rangeSet(1,9);
           }
    }
   }

   for(int i = 0; i < 9; i++)
   {
    for(int j = 0; j < 9; j++)
    {
            if(size(sec[i][j]) > 1)
            {
             getRow(sec, section, i);
			 for(int z = 0; z < 9; z++)
			 {
                     tempSet[z] = *section[z];
             }
			 for(int k = 0; k<9; k++)
			 {
                     if(size(tempSet[k]) == 1)
                     sec[i][j] = setDifference(sec[i][j], tempSet[k]);				
             }
            }

             if(size(sec[i][j]) > 1)
	         {
      	      getColumn(sec , section, j);
		      for(int z = 0; z < 9; z++)
		      {
			          tempSet[z] = *section[z];
              }
		      for(int k = 0; k<9; k++)
		      {
			          if(size(tempSet[k]) == 1)
			          sec[i][j] = setDifference(sec[i][j], tempSet[k]);
              }
             }

	         if(size(sec[i][j]) > 1)
	         {
		      getSquare(sec, section, i; 
              for(int z = 0; z < 9; z++)
              {
		              tempSet[z] = *section[z];
              } 
	          for(int k = 0; k<9; k++)
	          {
		              if(size(tempSet[k]) == 1)
		              sec[i][j] = setDifference(sec[i][j], tempSet[k]);
              }
             }
        }
       }
}

bool tacticOne(Puzzle p)
{	  
       bool result = false;
	   PuzzleSection scanSec;
	   for(int i = 0; i < 9; i++)
	   {
			   getRow(scanSec,p,i);
			   if(tacticOneOnSection(scanSec)) result = true;
	   }
	   for(int i = 0; i < 9; i++)
	   {
			   getColumn(scanSec,p,i);
			   if(tacticOneOnSection(scanSec)) result = true;
	   }
	   for(int i = 0; i < 9; i++)
	   {
			   getSquare(scanSec,p,i);
			   if(tacticOneOnSection(scanSec)) result = true;
	   }
	   return result;
}

bool allSingleton(Puzzle p)
{
	 bool final = false;
	 SetOfSmallInts sett;
	 for( int i = 0; i < 9; i++)
	 {
		  for( int j = 0; i < 9; j++)
		  {
			   sett=p[i][j];
			   if(isSingleton(sett)) final = true;
			   else return false;
		  }
	 return final;
	 }
}

bool nonSingleton(Puzzle p)
{
	 bool final = false;
	 SetOfSmallInts sett;
	 for( int i = 0; i < 9; i++)
	 {
		  for( int j = 0; i < 9; j++)
		  {
			   sett=p[i][j];
			   if(isSingleton(sett)!=1) final = true;
			   else return false;
		  }
	 return final;
	 }
}

/*SolutionStatus solver(Puzzle p)
{
	 while()
  {
    allSingleton(p);
    if(allSingleton==true)return solved;
    nonSingleton(p);
    if(nonSingleton==true)return unsolvable;
    tacticOne(p);
    if(tacticOne==false)return false;
    } 
}
*/

void showPuzzle(Puzzle p)
{
   SetOfSmallInts s;
   int smallInt = 0;
   for(int i = 0; i < 9; i++)
   {
	  for(int j = 0; j < 9; j++)
	   {
		 s=p[i][j];
		 for(int k=0; k < 9; k++)
		  {
			smallInt=smallest(s);
			if(smallInt==0 && k!=0)
			{
			   cout << " ";
			}
			else cout << smallInt;
			s = remove(smallInt, s);
			if (k==9) cout << " ";
		  }
	   }
	  cout << '\n';
	 }
}

int main()
{
	Puzzle p;
	readPuzzle(p);
	printPuzzle(p);
    showPuzzle(p);   	
    system("pause");   
}


and here are its two other files:

intset.h

http://www.cs.ecu.edu/~karl/3300/fall13/assignments/sudoku/intset.h

intset.cpp

http://www.cs.ecu.edu/~karl/3300/fall13/assignments/sudoku/intset.cpp

Now the problem i am having deals only with two of the functions in the main program, tacticOne(p) and tacticOneOnSection(sec), pretty much everything else has been smooth sailing and works fine.

Tactic One is the idea of finding a singleton set in the sudoku, basically a number that we already know, and then eliminating that number from the possible numbers on the row, column, and in the 3x3 square it lies in.

for example the first "1" in this example is a singleton set. We want to eliminate "1" from the possible answers in the first row, first column, and it's own 3x3 square(which it shares with "7" and "3"):

1-- 489 --6
73- --- -4-
--- --1 295

--7 12- 6--
5-- 7-3 --8
--6 -95 7--

914 6-- ---
-2- --- -37
8-- 512 --4

tacticOneOnSection's job is to do tactic 1 on a given section, sec. It should return true if it made a change to the section and false if it did not make any change.

tacticOne's job is to do tactic 1 on every row, column and square of the puzzle. It must use tacticOneOnSection for each row, column or square. It should return true if it made a change to any of the sets.

Kind of complicated, but its been fun until i hit this snag. Any help you guys and gals can provide would be greatly appreciated.
Last edited on
Any help someone can offer would be greatly appreciated, since this is due in a day or two.
You should, perhaps, pay attention to the warnings generated by your compiler, or if there are none, turn your warning levels up.

allSingleton doesn't always return a value.
nonSingleton doesn't always return a value.
tacticOneOnSection never returns a value.

Those are 3 sources of undefined behavior.

[edit: Well, I take that back, allSingleton and nonSingleton do always return values, although the outer loop in each one is pointless, since you always return on the first iteration.]
Last edited on
I have been testing around with the return values but can't seem to figure out the right combination. Do you have any ideas? Right now tacticOneOnSection is of the highest priority. Once i figure out what to do the rest should be easier.

Any help you can offer would be most appreciated.
Topic archived. No new replies allowed.