c++ code (console) to gui/windows app

Hi,
I am a student, 12th standard. I recently made this program to solve a sudoku (enter numbers column wise and 0 for empty boxes) in Turbo C++ 3.0 (as it was part of my school project, only this compiler was allowed). I might be going for an inter-school competition for the software display and I wanted to know that if i could make this code work as a windows app.
I have Dev C++.
Any help would be appreciated.
Thanks.
Here is my code:
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
#include <iostream.h>
#include <conio.h>
#include<stdio.h>

int input[9][9]; 
int output[9][9]; 
int input_value(int x, int y, int value)
{ 
 int i,j;
 // Scan horizontally and vertically 
 for (i = 0; i < 9; i++) {
  if (value == output[i][y] || value == output[x][i]) 
{ 
 return 0;
    } 
 } 
 // Scan its own square 
 if (x < 3)
 {
  if (y < 3)
 { 
   for (i = 0; i < 3; i++)
 { 
    for (j = 0; j < 3; j++)
 { 
     if (value == output[i][j]) 
{
     
            return  0; 
          } 
        } 
      } 
  }
 else if (y < 6) 
{ 
   for (i = 0; i < 3; i++)
 { 
    for (j = 3; j < 6; j++)
 {
     if (value == output[i][j])
 {
            return  0;           } 
        } 
      } 
  }
 else
 {
   for (i = 0; i < 3; i++)
 { 
    for (j = 6; j < 9; j++)
 { 
     if (value == output[i][j])
 { 
            return  0; 
          } 
        } 
      } 
    } 
 }
 else if (x < 6)
 {
  if (y < 3)
 { 
   for (i = 3; i < 6; i++)
 { 
    for (j = 0; j < 3; j++)
 { 
     if (value == output[i][j])
 {
            return  0; 
	  }
        } 
      } 
  }
 else if (y < 6)
 { 
   for (i = 3; i < 6; i++)
 { 
    for (j = 3; j < 6; j++) 
{ 
     if (value == output[i][j])
 { 
	    return  0;
          } 
        } 
      } 
  }
 else
 { 
   for (i = 3; i < 6; i++) 
{
    for (j = 6; j < 9; j++) 
{ 
     if (value == output[i][j])
 { 
            return  0; 
          } 
        } 
      } 
    } 
 }
 else
 { 
  if (y < 3)
 {
   for (i = 6; i < 9; i++)
 { 
    for (j = 0; j < 3; j++) 
{ 
     if (value == output[i][j])
 { 
	    return  0;
          } 
        } 
      } 
  } 
else if (y < 6) 
{ 
   for (i = 6; i < 9; i++) 
{ 
    for (j = 3; j < 6; j++)
 { 
     if (value == output[i][j])
 { 
            return  0; 
	  }
        } 
      } 
  }
 else
 { 
   for (i = 6; i < 9; i++)
 {
    for (j = 6; j < 9; j++) 
{ 
     if (value == output[i][j])
 { 
	    return  0;
	  }         }
      } 
    } 
 } 
 return value; 
}
int solve(int x, int y)
 {
 int temp,i,j;

 if (output[x][y] == 0) 
{

  for (i = 1; i < 10; i++)
 {
   temp = input_value(x,y,i);
   if (temp > 0) 
{
    output[x][y] = temp;

	if (x == 8 && y == 8)
 {
	  return 1;
	}
 else if (x == 8)
 {
	  if (solve(0,y+1)) return 1;
	} 
else 
{
	  if (solve(x+1,y)) return 1 ;
	}
      }
    }
  if (i == 10)
 {
      if (output[x][y] !=  input[x][y]) output[x][y] = 0;
   return 0;
    }
 }
 else
 {
  if (x == 8 && y == 8) 
{ 
   return 1; 
  }
 else if (x == 8) 
{
    if (solve(0,y+1)) return 1; 
  }
 else
 { 
   if (solve(x+1,y)) return 1;
    }
 }

}
int main()
{
 int i,j; 
clrscr();
for(int x=0;x<9;x++)
{
for(int y=0;y<9;y++)
{
gotoxy(x+1,y+1);
cin>>input[x][y];
}
}

 for (i = 0; i < 9; i++)
 { 
  for (j = 0; j < 9; j++)
 { 
   output[i][j] = input[i][j]; 
    }
 }


 if (solve(0,0))
 {

 gotoxy(2,10);
 cout<<"Solution is :";
    for (i = 0; i < 9; i++)
 {
   for (j = 0; j < 9; j++) 
{
   gotoxy(i+1,j+12);
   cout<<output[i][j];
      }
   printf("\n");
    }
 } 
else
  {
 clrscr();
  printf("No Soln\n");  
}
  getch();
 return 0;
}
I don't know anything about windows, so I can only offer some style points. Sorry, this is not going to help in any way towards your goal, but hopefully you might learn a couple things that might be useful.

My main comment is that you could do with splitting the whole thing into more functions, will make it easier to read & understand.

The input_value function is very long. Where is the call to input_value? Where is the definition of gotoxy?

The normal way of presenting code, is to have the declaration of functions, then main(), then the definition of functions. This way we don't have to scroll through all the code to get to main.

Your indenting makes it hard to read the code, there are various styles. I put the opening brace at the end of the line after the if or while for etc. Others put it on the following line, immediately below (that is the same column number as) the if, for, while etc. The closing brace is probably more important, it needs to be in the same column as the opening if, for etc, so that one can see where the block ends. Indenting is important too, a lot of people set a tab to be 4 spaces. I put braces around one liners, as you have done, to save myself when I add code in the future.Like this:

1
2
3
4
5
for (i = 0; i < 9; i++){ 
    for (j = 0; j < 9; j++){ 
        output[i][j] = input[i][j]; 
    }
}


or this:

1
2
3
4
5
6
7
for (i = 0; i < 9; i++)
{ 
    for (j = 0; j < 9; j++)
    { 
        output[i][j] = input[i][j]; 
    }
}


This is a matter of style, choose one you like (there are others also) & stick to it.

Also comments, are good always. It is good practice to put them with variable declarations, and at the start of functions. Your current code makes it hard to see the start / end of functions.

Hope this helps a little bit.
Topic archived. No new replies allowed.