Xinqi Bao's Git

All done
[sudoku.git] / sudokuDlg.cpp
1 // sudokuDlg.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "sudoku.h"
6 #include "sudokuDlg.h"
7
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
13
14 int i_1,j_1;
15 int i=1,j=1,i_i,j_j;
16 int i_txt=0;
17 double a[10][10];
18 double b[10][10];
19 double c[10][10];
20 int t,n,k=0;
21 int times=0;
22
23 /////////////////////////////////////////////////////////////////////////////
24 // CAboutDlg dialog used for App About
25
26 class CAboutDlg : public CDialog
27 {
28 public:
29 CAboutDlg();
30
31 // Dialog Data
32 //{{AFX_DATA(CAboutDlg)
33 enum { IDD = IDD_ABOUTBOX };
34 //}}AFX_DATA
35
36 // ClassWizard generated virtual function overrides
37 //{{AFX_VIRTUAL(CAboutDlg)
38 protected:
39 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
40 //}}AFX_VIRTUAL
41
42 // Implementation
43 protected:
44 //{{AFX_MSG(CAboutDlg)
45 //}}AFX_MSG
46 DECLARE_MESSAGE_MAP()
47 };
48
49 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
50 {
51 //{{AFX_DATA_INIT(CAboutDlg)
52 //}}AFX_DATA_INIT
53 }
54
55 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
56 {
57 CDialog::DoDataExchange(pDX);
58 //{{AFX_DATA_MAP(CAboutDlg)
59 //}}AFX_DATA_MAP
60 }
61
62 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
63 //{{AFX_MSG_MAP(CAboutDlg)
64 // No message handlers
65 //}}AFX_MSG_MAP
66 END_MESSAGE_MAP()
67
68 /////////////////////////////////////////////////////////////////////////////
69 // CSudokuDlg dialog
70
71 CSudokuDlg::CSudokuDlg(CWnd* pParent /*=NULL*/)
72 : CDialog(CSudokuDlg::IDD, pParent)
73 {
74 //{{AFX_DATA_INIT(CSudokuDlg)
75 // NOTE: the ClassWizard will add member initialization here
76 //}}AFX_DATA_INIT
77 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
78 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
79 }
80
81 void CSudokuDlg::DoDataExchange(CDataExchange* pDX)
82 {
83 CDialog::DoDataExchange(pDX);
84 //{{AFX_DATA_MAP(CSudokuDlg)
85 // NOTE: the ClassWizard will add DDX and DDV calls here
86 //}}AFX_DATA_MAP
87 }
88
89 BEGIN_MESSAGE_MAP(CSudokuDlg, CDialog)
90 //{{AFX_MSG_MAP(CSudokuDlg)
91 ON_WM_SYSCOMMAND()
92 ON_WM_PAINT()
93 ON_WM_QUERYDRAGICON()
94 ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
95 ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
96 ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
97 //}}AFX_MSG_MAP
98 END_MESSAGE_MAP()
99
100 /////////////////////////////////////////////////////////////////////////////
101 // CSudokuDlg message handlers
102
103 BOOL CSudokuDlg::OnInitDialog()
104 {
105 CDialog::OnInitDialog();
106
107 // Add "About..." menu item to system menu.
108
109 // IDM_ABOUTBOX must be in the system command range.
110 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
111 ASSERT(IDM_ABOUTBOX < 0xF000);
112
113 CMenu* pSysMenu = GetSystemMenu(FALSE);
114 if (pSysMenu != NULL)
115 {
116 CString strAboutMenu;
117 strAboutMenu.LoadString(IDS_ABOUTBOX);
118 if (!strAboutMenu.IsEmpty())
119 {
120 pSysMenu->AppendMenu(MF_SEPARATOR);
121 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
122 }
123 }
124
125 // Set the icon for this dialog. The framework does this automatically
126 // when the application's main window is not a dialog
127 SetIcon(m_hIcon, TRUE); // Set big icon
128 SetIcon(m_hIcon, FALSE); // Set small icon
129
130 // TODO: Add extra initialization here
131
132 return TRUE; // return TRUE unless you set the focus to a control
133 }
134
135 void CSudokuDlg::OnSysCommand(UINT nID, LPARAM lParam)
136 {
137 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
138 {
139 CAboutDlg dlgAbout;
140 dlgAbout.DoModal();
141 }
142 else
143 {
144 CDialog::OnSysCommand(nID, lParam);
145 }
146 }
147
148 // If you add a minimize button to your dialog, you will need the code below
149 // to draw the icon. For MFC applications using the document/view model,
150 // this is automatically done for you by the framework.
151
152 void CSudokuDlg::OnPaint()
153 {
154 if (IsIconic())
155 {
156 CPaintDC dc(this); // device context for painting
157
158 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
159
160 // Center icon in client rectangle
161 int cxIcon = GetSystemMetrics(SM_CXICON);
162 int cyIcon = GetSystemMetrics(SM_CYICON);
163 CRect rect;
164 GetClientRect(&rect);
165 int x = (rect.Width() - cxIcon + 1) / 2;
166 int y = (rect.Height() - cyIcon + 1) / 2;
167
168 // Draw the icon
169 dc.DrawIcon(x, y, m_hIcon);
170 }
171 else
172 {
173 CDialog::OnPaint();
174 }
175
176
177 CClientDC dc(this);
178
179 for(i_1=0;i_1<10;i_1++)
180 {
181 dc.MoveTo(20,20+40*i_1);
182 dc.LineTo(380,20+40*i_1);
183 if(i_1%3==0)
184 {
185 dc.MoveTo(20,20+40*i_1+1);
186 dc.LineTo(380,20+40*i_1+1);
187 }
188 }
189 for(j_1=0;j_1<10;j_1++)
190 {
191 dc.MoveTo(20+40*j_1,20);
192 dc.LineTo(20+40*j_1,380);
193 if(j_1%3==0)
194 {
195 dc.MoveTo(20+40*j_1+1,20);
196 dc.LineTo(20+40*j_1+1,380);
197 }
198 }
199
200 if(i<1)
201 TextOut(dc,460,150,"ÎÞ½â",4);
202 else
203 {
204 CString cs;
205 for(i_1=1;i_1<10;i_1++)
206 {
207 for(j_1=1;j_1<10;j_1++)
208 {
209 if(a[i_1][j_1]!=0)
210 {
211 cs.Format("%.0f",a[i_1][j_1]);
212 TextOut(dc,40*j_1-10,40*i_1-10,cs,cs.GetLength());
213 }
214 }
215 }
216 }
217
218 }
219
220 // The system calls this to obtain the cursor to display while the user drags
221 // the minimized window.
222 HCURSOR CSudokuDlg::OnQueryDragIcon()
223 {
224 return (HCURSOR) m_hIcon;
225 }
226
227 void CSudokuDlg::OnButton1()
228 {
229 // TODO: Add your control notification handler code here
230
231 CFileDialog fileDlg(TRUE);
232 fileDlg.m_ofn.lpstrTitle = "OpenFile";
233 fileDlg.m_ofn.lpstrFilter = "Txt(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
234
235 CClientDC paDC(this);
236 CString strLine;
237
238 i_txt=1;
239 if(IDOK == fileDlg.DoModal())
240 {
241 CStdioFile file(fileDlg.GetFileName(),CFile::modeRead);
242 while(file.ReadString(strLine))
243 {
244 //AfxMessageBox(strLine);
245
246 float st=atof(strLine);
247 a[(i_txt-1)/9+1][(i_txt-1)%9+1]=st;
248 i_txt++;
249 if(i_txt==820) break;
250 }
251
252 file.Close();
253 Invalidate();
254 }
255
256 for(i_1=1;i_1<10;i_1++)
257 {
258 for(j_1=1;j_1<10;j_1++)
259 {
260 b[i_1][j_1]=a[i_1][j_1];
261 }
262 }
263 for(i_1=1;i_1<10;i_1++)
264 {
265 for(j_1=1;j_1<10;j_1++)
266 {
267 c[i_1][j_1]=a[i_1][j_1];
268 }
269 }
270 i=1;j=1;i_i=1;j_j=1;
271 }
272
273 void CSudokuDlg::OnButton2()
274 {
275 // TODO: Add your control notification handler code here
276
277 if( times!=0 && c[i][j]==0 ) b[i][j]--;
278 times=0;
279
280 i=i_i;
281 j=j_j;
282 for(i;i<10;i++)
283 {
284 // if(j==9) j=1;
285 for(j;j<10;j++)
286 {
287 if(c[i][j]==0)
288 {
289 for(t=b[i][j]+1;t<10;t++)
290 {
291 b[i][j]=t;
292 k=0;
293 for(n=0;n<9;n++)
294 {
295 if(k) break;
296 if( b[(i-1)/3*3+1+n/3][(j-1)/3*3+1+n%3]==b[i][j] && (i-1)/3*3+1+n/3!=i && (j-1)/3*3+1+n%3!=j )
297 k=1;
298 }
299 for(n=1;n<10;n++)
300 {
301 if(k) break;
302 if( b[i][n]==b[i][j] && n!=j )
303 k=1;
304 }
305 for(n=1;n<10;n++)
306 {
307 if(k) break;
308 if( b[n][j]==b[i][j] && n!=i )
309 k=1;
310 }
311 if(!k) break;
312 }
313 if( b[i][j]==9 && k!=0 )
314 {
315 b[i][j]=0;
316
317 if(j==1) {i--;j=9;}
318 else j--;
319
320 while(c[i][j]!=0)
321 {
322 if(j==1) {i--;j=9;}
323 else j--;
324 }
325
326 if(j==1) {i--;j=9;}
327 else j--;
328 }
329 }
330 times++;
331 if(times==100000)
332 {
333 if( b[i][j]==9 && k!=0 )
334 {
335 if(j==9) {i++;j=1;}
336 else j++;
337 }
338 break;
339 }
340 }
341 if(times==100000) break;
342 j=1;
343 }
344 i_i=i;
345 j_j=j;
346
347 for(i_1=1;i_1<10;i_1++)
348 {
349 for(j_1=1;j_1<10;j_1++)
350 {
351 a[i_1][j_1]=b[i_1][j_1];
352 }
353 }
354
355 Invalidate();
356
357 }
358
359 void CSudokuDlg::OnButton3()
360 {
361 // TODO: Add your control notification handler code here
362 b[9][9]++;
363 i_i=9;j_j=9;
364 i=9;j=9;
365 }