## Posted By

Desoxena on 02/19/11

# Sudoku solver (4x4)

/ Published in: C++

This is a Sudoku solver that I made; it isn't so great yet, you do have to supply quite a bit of information, but it is still a work in progress.

1. //As you can tell, I am a 'for' loop junkie...
2.
3. /*
4.   Puzzle
5. [?][4]|[?][2]
6. [3][2]|[4][?]
7. -------------
8. [2][?]|[?][4]
9. [?][1]|[?][3]
10. 7 ?'s
11. 9 knowns
12. */
13.
14. /*
15. [1][4]|[3][2]
16. [3][2]|[4][1]
17. -------------
18. [2][3]|[1][4]
19. [4][1]|[2][3]
20.
21. */
22. #include <iostream>
23. #include <cstdlib>
24. #include <string>
25. #include <algorithm>
26.
27. using namespace std;
28.
29. void sudokuSolve()
30. {
31. string myRow[4];
32. string numbers[] = {"1", "2", "3", "4"};
33. char numbers1[] = {'1', '2', '3', '4'};
34. int a, b, c, d, e, f, x, z, p, y, myLoop, theLoop;
35. string sudoku[] = {"?4?2", "324?", "2??4", "?1?3"};
36. string myBox[4];
37. string command;
38. for (a = 0; a < 2; a++) {
39. cout << "[" << sudoku[a].at(0) << "|" << sudoku[a].at(1) << "]|[" << sudoku[a].at(2) << "|" << sudoku[a].at(3) << "]" << endl;
40. }
41. cout << "-----------" << endl;
42. for (a = 2; a < 4; a++) {
43. cout << "[" << sudoku[a].at(0) << "|" << sudoku[a].at(1) << "]|[" << sudoku[a].at(2) << "|" << sudoku[a].at(3) << "]" << endl;
44. }
45. cin >> command;
46. if (command == "solve")
47. {
48. system("cls");
49. for (theLoop = 0; theLoop < 4; theLoop++) {
50. for (myLoop = 0; myLoop < 4; myLoop++) {
51. for (a = 0; a < 4; a++) {
52. for (b = 0; b < 4; b++) {
53. if (sudoku[a].at(b) == numbers1[myLoop])
54. {
55. for (c = 0; c < 4; c++) {
56. if (sudoku[c].at(b) == '?')
57. {
58. sudoku[c].replace(b, 1, "#");
59. }
60. }
61. for (c = 0; c < 4; c++) {
62. if (sudoku[a].at(c) == '?')
63. {
64. sudoku[a].replace(c, 1, "#");
65. }
66. }
67. for (x = 0; x < 4; x=x+2) {
68. for (z = 0; z < 4; z=z+2) {
69. for (c = x; c < x+1; c++) {
70. for (d = z; d < z+1; d++) {
71. if (sudoku[c].at(d) == '?')
72. {
73. sudoku[c].replace(d, 1, "X");
74. for (e = 0; e < 4; e++) {
75. if (sudoku[c].at(e) != '?')
76. {
77. sudoku[c].replace(d, 1, numbers[myLoop]);
78. }
79. else if(sudoku[c].at(e) == '?')
80. {
81. sudoku[c].replace(e, 1, "#");
82. sudoku[c].replace(d, 1, "#");
83. }
84. }
85. }
86. }
87. }
88. }
89. }
90. }
91. }
92. }
93. for (p = 0; p < 4; p++) {
94. for (y = 0; y < 4; y++) {
95. if (sudoku[p].at(y) == '#')
96. {
97. sudoku[p].replace(y, 1, "?");
98. }
99. }
100. }
101. for (int a1 = 0; a1 < 4; a1++) {
102. if (sudoku[0].at(a1) == '?' &&
103. sudoku[1].at(a1) != '?' &&
104. sudoku[2].at(a1) != '?' &&
105. sudoku[3].at(a1) != '?' &&
106. sudoku[0].at(a1) != sudoku[1].at(a1) &&
107. sudoku[0].at(a1) != sudoku[2].at(a1) &&
108. sudoku[0].at(a1) != sudoku[3].at(a1) &&
109. numbers1[myLoop] != sudoku[1].at(a1) &&
110. numbers1[myLoop] != sudoku[2].at(a1) &&
111. numbers1[myLoop] != sudoku[3].at(a1))
112. {
113. sudoku[0].replace(a1, 1, numbers[myLoop]);
114. }
115. else if (sudoku[0].at(a1) != '?' &&
116. sudoku[1].at(a1) == '?' &&
117. sudoku[2].at(a1) != '?' &&
118. sudoku[3].at(a1) != '?' &&
119. sudoku[1].at(a1) != sudoku[0].at(a1) &&
120. sudoku[1].at(a1) != sudoku[2].at(a1) &&
121. sudoku[1].at(a1) != sudoku[3].at(a1) &&
122. numbers1[myLoop] != sudoku[0].at(a1) &&
123. numbers1[myLoop] != sudoku[2].at(a1) &&
124. numbers1[myLoop] != sudoku[3].at(a1))
125. {
126. sudoku[1].replace(a1, 1, numbers[myLoop]);
127. }
128. else if (sudoku[0].at(a1) != '?' &&
129. sudoku[1].at(a1) != '?' &&
130. sudoku[2].at(a1) == '?' &&
131. sudoku[3].at(a1) != '?' &&
132. sudoku[2].at(a1) != sudoku[0].at(a1) &&
133. sudoku[2].at(a1) != sudoku[1].at(a1) &&
134. sudoku[2].at(a1) != sudoku[3].at(a1) &&
135. numbers1[myLoop] != sudoku[0].at(a1) &&
136. numbers1[myLoop] != sudoku[1].at(a1) &&
137. numbers1[myLoop] != sudoku[3].at(a1))
138. {
139. sudoku[2].replace(a1, 1, numbers[myLoop]);
140. }
141. if (sudoku[0].at(a1) != '?' &&
142. sudoku[1].at(a1) != '?' &&
143. sudoku[2].at(a1) != '?' &&
144. sudoku[3].at(a1) == '?' &&
145. sudoku[3].at(a1) != sudoku[0].at(a1) &&
146. sudoku[3].at(a1) != sudoku[1].at(a1) &&
147. sudoku[3].at(a1) != sudoku[2].at(a1) &&
148. numbers1[myLoop] != sudoku[0].at(a1) &&
149. numbers1[myLoop] != sudoku[1].at(a1) &&
150. numbers1[myLoop] != sudoku[2].at(a1))
151. {
152. sudoku[3].replace(a1, 1, numbers[myLoop]);
153. }
154. }
155. }
156. }
157. for (a = 0; a < 2; a++) {
158. cout << "[" << sudoku[a].at(0) << "|" << sudoku[a].at(1) << "]|[" << sudoku[a].at(2) << "|" << sudoku[a].at(3) << "]" << endl;
159. }
160. cout << "-----------" << endl;
161. for (a = 2; a < 4; a++) {
162. cout << "[" << sudoku[a].at(0) << "|" << sudoku[a].at(1) << "]|[" << sudoku[a].at(2) << "|" << sudoku[a].at(3) << "]" << endl;
163. }
164. }
165. }
166. int main()
167. {
168.
169. sudokuSolve();
170. system("pause");
171. }