Sudoku Game

I'm currently in the process of coding a Sudoku Game. When running the program, it reads in a file (that I named 'myGame.txt' that looks like this:

1
2
3
4
5
6
7
8
9
7 2 3 0 0 0 1 5 9
6 0 0 3 0 2 0 0 8
8 0 0 0 1 0 0 0 2
0 7 0 6 5 4 0 2 0
0 0 4 2 0 7 3 0 0
0 5 0 9 3 1 0 4 0
5 0 0 0 7 0 0 0 3
4 0 0 1 0 3 0 0 6
9 3 2 0 0 0 7 1 4


Then, the program allows you to do some various things (those aren't all completed yet). I'm having problems with two things:

1) According to the rules of Sudoku, a value that the user inputs cannot be in the same 3x3 grid, row, or column that the user selects. For example, if the user inputs the number '8' in square 'B2', it should get an error, because there is already an '8' in square 'I2'. I have absolutely no idea whatsoever how to compare a number that the user inputs with other rows, columns, and the little 3x3 region that it lies in.

2) When I select option 'E' in my program to edit a square, when I go back to view the game board by selecting 'D', it doesn't insert the number that I selected. Instead, it just messes up the game board by placing a space or something instead. What am I doing wrong?

I'll place my code in another post, because this code is getting too long. Thank you!
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

void readFile(char sudokuBoard[][9]);
void writeFile(char sudokuBoard[][9]);
void display(char sudokuBoard[][9]);
void interact();
void getOption(char sudokuBoard[][9]);
void editSquare(char sudokuBoard[][9]);
void showValues();

/**********************************************************************
* Main: Basically a delegator. Just makes other functions do its'
*       dirty work for it.
***********************************************************************/
int main()
{
   //Declare array
   char sudokuBoard[9][9];

   //Calling other functions/pass array
   readFile(sudokuBoard);
   interact();
   display(sudokuBoard);

   return 0;
}


/**********************************************************************
* readFile: Asks the user for a filename, reads a gameboard in from
*           that file name, and then places it in an array.
***********************************************************************/
void readFile(char sudokuBoard[][9])
{
   //Declare filename
   char sourceFile[256];

   //Declare file input
   ifstream fin;

   //Get filename from user
   cout << "Where is your board located? ";
   cin  >> sourceFile;

   //Open file with error checking
   fin.open(sourceFile);
   if (fin.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }

   //Read file into array
   for (int col = 0; col < 9; col++)
   {
      for (int row = 0; row < 9; row++)
      {
         fin >> sudokuBoard[row][col];
         if (sudokuBoard[row][col] == '0')
         {
            sudokuBoard[row][col] = ' ';
         }
      }
   }

   //Close the file
   fin.close();
}

/***********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char sudokuBoard[][9])
{
   //Declare variables
   char option;

   //Display Column Header
   cout << "   A B C D E F G H I" << endl

   //Row 1
        << "1  "
        << sudokuBoard[0][0]
        << " " << sudokuBoard[1][0]
        << " " << sudokuBoard[2][0]
        << "|"
        << sudokuBoard[3][0]
        << " " << sudokuBoard[4][0]
        << " " << sudokuBoard[5][0]
        << "|"
        << sudokuBoard[6][0]
        << " " << sudokuBoard[7][0]
        << " " << sudokuBoard[8][0]
        << endl

   //Row 2
        << "2  "
        << sudokuBoard[0][1]
        << " " << sudokuBoard[1][1]
        << " " << sudokuBoard[2][1]
        << "|"
        << sudokuBoard[3][1]
        << " " << sudokuBoard[4][1]
        << " " << sudokuBoard[5][1]
        << "|"
        << sudokuBoard[6][1]
        << " " << sudokuBoard[7][1]
        << " " << sudokuBoard[8][1]
        << endl

   //Row 3
        << "3  "
        << sudokuBoard[0][2]
        << " " << sudokuBoard[1][2]
        << " " << sudokuBoard[2][2]
        << "|"
        << sudokuBoard[3][2]
        << " " << sudokuBoard[4][2]
        << " " << sudokuBoard[5][2]
        << "|"
        << sudokuBoard[6][2]
        << " " << sudokuBoard[7][2]
        << " " << sudokuBoard[8][2]
        << endl

   //Visual Separator
        << "   -----+-----+-----" << endl

   //Row 4
        << "4  "
        << sudokuBoard[0][3]
        << " " << sudokuBoard[1][3]
        << " " << sudokuBoard[2][3]
        << "|"
        << sudokuBoard[3][3]
        << " " << sudokuBoard[4][3]
        << " " << sudokuBoard[5][3]
        << "|"
        << sudokuBoard[6][3]
        << " " << sudokuBoard[7][3]
        << " " << sudokuBoard[8][3]
        << endl

   //Row 5
        << "5  "
        << sudokuBoard[0][4]
        << " " << sudokuBoard[1][4]
        << " " << sudokuBoard[2][4]
        << "|"
        << sudokuBoard[3][4]
        << " " << sudokuBoard[4][4]
        << " " << sudokuBoard[5][4]
        << "|"
        << sudokuBoard[6][4]
        << " " << sudokuBoard[7][4]
        << " " << sudokuBoard[8][4]
        << endl

   //Row 6
        << "6  "
        << sudokuBoard[0][5]
        << " " << sudokuBoard[1][5]
        << " " << sudokuBoard[2][5]
        << "|"
        << sudokuBoard[3][5]
        << " " << sudokuBoard[4][5]
        << " " << sudokuBoard[5][5]
        << "|"
        << sudokuBoard[6][5]
        << " " << sudokuBoard[7][5]
        << " " << sudokuBoard[8][5]
        << endl

   //Visual Separator
        << "   -----+-----+-----" << endl

   //Row 7
        << "7  "
        << sudokuBoard[0][6]
        << " " << sudokuBoard[1][6]
        << " " << sudokuBoard[2][6]
        << "|"
        << sudokuBoard[3][6]
        << " " << sudokuBoard[4][6]
        << " " << sudokuBoard[5][6]
        << "|"
        << sudokuBoard[6][6]
        << " " << sudokuBoard[7][6]
        << " " << sudokuBoard[8][6]
        << endl

   //Row 8
        << "8  "
        << sudokuBoard[0][7]
        << " " << sudokuBoard[1][7]
        << " " << sudokuBoard[2][7]
        << "|"
        << sudokuBoard[3][7]
        << " " << sudokuBoard[4][7]
        << " " << sudokuBoard[5][7]
        << "|"
        << sudokuBoard[6][7]
        << " " << sudokuBoard[7][7]
        << " " << sudokuBoard[8][7]
        << endl

   //Row 9
        << "9  "
        << sudokuBoard[0][8]
        << " " << sudokuBoard[1][8]
        << " " << sudokuBoard[2][8]
        << "|"
        << sudokuBoard[3][8]
        << " " << sudokuBoard[4][8]
        << " " << sudokuBoard[5][8]
        << "|"
        << sudokuBoard[6][8]
        << " " << sudokuBoard[7][8]
        << " " << sudokuBoard[8][8]
        << "\n"
        << "\n";

   getOption(sudokuBoard);
}


/*************************************************************************
* Interact: Allows the user to interact and manipulate the game board.
*
************************************************************************/
void interact()
{
   cout << "Options:\n"
        << "   ?  Show these instructions\n"
        << "   D  Display the board\n"
        << "   E  Edit one square\n"
        << "   S  Show the possible values for a square\n"
        << "   Q  Save and Quit\n"
        << "\n";

   return;
}


/*************************************************************************
* getOption: Gets the user's input.
*
************************************************************************/
void getOption(char sudokuBoard[][9])
{
   char option;
   cout << "> ";
   cin >> option;

   if (option == 'e' || option == 'E')
      editSquare(sudokuBoard);
   else if (option == '?')
      interact();
   else if (option == 'd' || option == 'D')
      display(sudokuBoard);
   else if (option == 's' || option == 'S')
      showValues();
   else if (option == 'q' || option == 'Q')
      writeFile(sudokuBoard);
   else
      cout << "ERROR: Invalid command";

   return;
}


/***********************************************************************
* editSquare: Edits one square of the table based on the coordinates
*             entered by the user.
************************************************************************/
void editSquare(char sudokuBoard[][9])
{
   //Declare variables
   char letter;
   int number;
   int value = 0;

   //Gets letter/number coordinates
   cout << "What are the coordinates of the square: ";
   cin  >> letter >> number;

   //Converts letter to uppercase
   letter = toupper(letter);

   //If square is full, display "read only" message
   if (sudokuBoard[letter - 65][number - 1] != ' ')
   {
      cout << "ERROR: Square \'" << letter << number << "\' is read-only\n";
      cout << "\n";
      getOption(sudokuBoard);
   }
   else
   {
      //Gets value to place in specified coordinates
      cout << "What is the value at \'" << letter << number << "\': ";
      cin  >> value;

      //Makes sure value is within correct range
      if (value < 1 || value > 9)
      {
         cout << "ERROR: Value \'" << value << "\' in square \'" << letter << number << "\' is invalid\n";
         cout << "\n";
         getOption(sudokuBoard);
      }

      cout << "\n";
      sudokuBoard[letter - 65][number - 1] = value;
      getOption(sudokuBoard);
   }

   return;
}
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
/******************************************************************************
* writeFile: Writes the contents of the board to a file to be picked up later.
*
*****************************************************************************/
void writeFile(char sudokuBoard[][9])
{
   //Declare file output
   ofstream fout;
   char destinationFile[256];

   //Asking for user input
   cout << "What file would you like to write your board to: ";
   cin  >> destinationFile;

   //Open destination file & error checking
   fout.open(destinationFile);
   if (fout.fail())
   {
      cout << "Output file opening failed.\n";
      exit (1);
   }
   else
      cout << "Board written successfully";

   //Writes board to file
   for (int col = 0; col < 9; col++)
   {
      for (int row = 0; row < 9; row++)
      {
         if (sudokuBoard[row][col] == ' ')
         {
            sudokuBoard[row][col] = '0';
         }

         fout << sudokuBoard[row][col];

         //Makes sure it's a 9x9 grid
         if (row % 9 == 0)
         {
            fout << endl;
         }
      }
   }

   //Close file
   fout.close();
}


/**************************************************************************
* showValues: Shows all the possible values for a given set of coordinates.
*
**************************************************************************/
void showValues()
{
   //Declare variables
   char letter;
   int number;

   //Gets letter/number coordinates
   cout << "What are the coordinates of the square: ";
   cin  >> letter >> number;

   //Converts letter to uppercase
   letter = toupper(letter);

   return;
}
i am still a beginner at all of this, but I've done sudoku's a fair amount. Could you maybe create an array or a vector of some sort that stores 1-9 for each 3x3 grid, and then have one that checks rows and columns. I mean there is probably a better way to do it, but that's what my first instinct would be for checking the columns and rows and 3x3 grid.
Topic archived. No new replies allowed.