/*************************************************************************
* getOption: Gets the user's input.
*
************************************************************************/
void getOption(char sudokuBoard[][9])
{
char option;
cout << "> ";
cin >> option;
if (option == 'e' || option == 'E')
editSquare(sudokuBoard);
elseif (option == '?')
interact();
elseif (option == 'd' || option == 'D')
display(sudokuBoard);
elseif (option == 's' || option == 'S')
showValues();
elseif (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;
}
/******************************************************************************
* 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;
}
Please show us some examples, for example, when you want to save 9, what do you get instead?
Anyway, did you try this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//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);
}
else
{
cout << "\n";
sudokuBoard[letter - 65][number - 1] = value;
getOption(sudokuBoard);
}
here is a run i just did, after implementing your changes and others that I found.
Where is your board located? mygame.txt
Options:
? Show these instructions
D Display the board
E Edit one square
S Show the possible values for a square
Q Save and Quit
A B C D E F G H I
1 7 2 3| |1 5 9
2 6 |3 2| 8
3 8 | 1 | 2
-----+-----+-----
4 7 |6 5 4| 2
5 4|2 7|3
6 5 |9 3 1| 4
-----+-----+-----
7 5 | 7 | 3
8 4 |1 3| 6
9 9 3 2| |7 1 4
> e
What are the coordinates of the square: d1
What is the value at 'D1': 4
> d
A B C D E F G H I
1 7 2 3|♦ |1 5 9
2 6 |3 2| 8
3 8 | 1 | 2
-----+-----+-----
4 7 |6 5 4| 2
5 4|2 7|3
6 5 |9 3 1| 4
-----+-----+-----
7 5 | 7 | 3
8 4 |1 3| 6
9 9 3 2| |7 1 4
>
void editSquare(char sudokuBoard[][9])
{
//Declare variables
char letter;
int number;
// Change this to charint 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);
}
Not quite sure why you decided to go with a character array. I would have stuck with an int array and handle it the same way you read in your file, treat 0s as an empty space. To me, it just makes more sense since the only time you're actually using a char is for the space, but that only requires a simple if statement.
Also, an int array of 9x9 allows to check that the puzzle is done correctly. You can check the values up online for what you check for, but I believe it's 45, I could be, and most likely wrong, for the values across each row and down each column. Also, each 3x3 square should equal that value as well.
Aside from that. I haven't read over your code extensively, but I'm sure another set of eyes will pick out some more improvements.
well this was a semi finished code that I found. Im trying to finish it and from the way it was looking at first, I thought it would be a good challenge.