/[projects]/SodukuSolver/test1/Sudoku.cpp
ViewVC logotype

Annotation of /SodukuSolver/test1/Sudoku.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 226 - (hide annotations) (download)
Mon Jun 15 11:06:35 2009 UTC (14 years, 11 months ago) by torben
File size: 3020 byte(s)


1 torben 226 #include "StdAfx.h"
2     #include ".\sudoku.h"
3    
4     #include <exception>
5    
6     //bruges til debug prints
7     #define LINE "+---+---+---+---+---+---+---+---+---+\r\n"
8    
9    
10     Sudoku::Sudoku(void)
11     {
12     clear();
13     }
14    
15     Sudoku::~Sudoku(void)
16     {
17     }
18    
19    
20     CString Sudoku::print_matrix()
21     {
22     CString out;
23    
24     int i,j,tal;
25     for(i=0; i<matrixSize; i++) {
26     out += LINE ;
27     for (j=0; j<matrixSize; j++) {
28     tal = matrix[j][i];
29     if (tal != 0) {
30     out.AppendFormat("| %d ", tal);
31     }
32     else
33     out += "| ";
34     }
35     out += "|\r\n";
36     }
37     out += LINE ;
38     return out;
39     }
40    
41     void Sudoku::clear()
42     {
43     for (int i=0; i<matrixSize; i++)
44     for (int j=0; j<matrixSize; j++) {
45     user_matrix[i][j] = false;
46     matrix[i][j] = 0;
47     }
48     }
49    
50     void Sudoku::clearCalculated(void)
51     {
52     for (int i=0; i<matrixSize; i++)
53     for (int j=0; j<matrixSize; j++) {
54     if (user_matrix[i][j] == false)
55     matrix[i][j] = 0;
56     }
57     }
58    
59    
60     bool Sudoku::checkrow(int x, int y, int tal)
61     {
62     for (int i=0; i<matrixSize; i++) {
63     if (matrix[x][i] == tal)
64     return true;
65     }
66     return false;
67     }
68    
69    
70     bool Sudoku::checkcol(int x, int y, int tal)
71     {
72     int i;
73     for (i=0; i<matrixSize; i++) {
74     if (matrix[i][y] == tal)
75     return true;
76     }
77     return false;
78     }
79    
80     bool Sudoku::checkbox(int x, int y, int tal)
81     {
82     int startx,starty;
83     startx = x - (x%3);
84     starty = y - (y%3);
85    
86     for (int row=startx; row<(startx+3); row++)
87     for (int col=starty; col<(starty+3); col++)
88     if (matrix[row][col] == tal)
89     return true;
90    
91     return false;
92     }
93    
94     bool Sudoku::solve(int x, int y)
95     {
96     int tal;
97     bool res;
98    
99     if (y >= matrixSize)
100     return true;
101    
102     if (x >= matrixSize) {
103     x=0;
104     y++;
105     }
106    
107     while (user_matrix[x][y] == true) {
108     x++;
109     if (x>=matrixSize) {
110     x=0;
111     y++;
112     }
113     }
114    
115     if (y>=matrixSize)
116     return true; //out of range
117    
118     for (tal=1; tal<=matrixSize; tal++) {
119     if ( checkrow(x,y,tal) || checkcol(x,y,tal) || checkbox(x,y,tal) )
120     continue;
121    
122     matrix[x][y] = tal;
123     res = solve(x+1,y);
124    
125     if (res == true)
126     return res;
127     }
128     matrix[x][y] = 0; //before stepping back, clear the current field
129     return false;
130     }
131    
132     bool Sudoku::solve()
133     {
134     clearCalculated();
135     initCheck();
136     return solve(0,0);
137     }
138    
139    
140     void Sudoku::setNumber(int x, int y, int number)
141     {
142     matrix[x][y] = number;
143     if (number != 0)
144     user_matrix[x][y] = true;
145     else
146     user_matrix[x][y] = false;
147    
148     }
149    
150     int Sudoku::getNumber(int x, int y)
151     {
152     return matrix[x][y];
153     }
154    
155     bool Sudoku::getUser(int x, int y)
156     {
157     return user_matrix[x][y];
158     }
159    
160    
161     // laver en check på om det bruger-indtastede problem kan løses eller om dataene har en intern konflikt
162     void Sudoku::initCheck(void)
163     {
164     int tal;
165     bool res;
166    
167     for (int i=0; i<matrixSize; i++) {
168     for (int j=0; j<matrixSize; j++) {
169     if (matrix[i][j] != 0) {
170     tal = matrix[i][j];
171     matrix[i][j] = 0;
172     res= (checkrow(i,j,tal) || checkcol(i,j,tal) || checkbox(i,j,tal));
173     matrix[i][j] = tal;
174     if (res)
175     throw std::exception("Invalid user matrix");
176     }
177     }
178     }
179     }
180    

  ViewVC Help
Powered by ViewVC 1.1.20