Hi, new to the forum,
I am currently studying the basics of C++ and we have an assignment in which we have to code a Sudoku validator. Basically the validator will ask for a file to be input, it will read the file and the user will be able to input options: 1) check columns, 2) check rows and 3) check minigrids and of course a display option.
I've gotten the check rows and check mini grids right... or at least I think. However, I am really stuck on the minigrids part. I know the code I computed here is wrong. I have searched for assistance online, but always come to a deadend. Can someone just point how the logic behind it (as in the pseudocode [not the actual code]).
Also, another question here is that the program is also supposed to read some files where there are some char, and it will point out where the char is, I think I know the logic behind this, however, if I change all my grid variables to 'char', when I try to compare the columns and rows, it says that the rows and columns all match, when in reality they shouldn't...
EDIT: We can only use 2D arrays here, no pointers, queues, stacks, vectors, etc... (I don't know how to do it because we haven't learn them yet)
// Purpose of program
// Sudoku Validator: checks rows, columns and minigrids to ensure that each row, column
// and minigrid contains no duplicates
// Program written by
// Eric Cheung, 40745708
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void printMenu();
bool readGrid(string fName, int grid[9][9]); //function to load and read the input files
int displayGrid(int grid[][9]); //function to display grid
bool checkColumns (int grid[][9]); //function to check columns for duplicates
bool checkRow (int grid[][9]); //function to check rows for duplcates
bool checkMini (int grid[][9]); // function to check minigrids for duplicates
int main()
{
int grid[9][9]; //9X9 grid
string fName; //file name
char choice = '*';
while (choice != 'Q')
{
printMenu();
cin >> choice;
switch (toupper(choice))
{
case 'L' : cout << "Enter file name" << endl;
cin >> fName; //enter file name
if( !readGrid(fName, grid)) //if function readGrid returns a false scenario
cout << "invalid file input!" << endl;
break;
case 'D' : displayGrid (grid);
break;
case 'R' : if (!checkRow(grid)) //if function checkRow returns a false scenario, it will output the required message as coded in the function
break;
else
cout << "All Rows are correct." << endl; //message to ouput for the true scenario
break;
case 'C' : if(!checkColumns(grid)) ////if function checkColumnsreturns a false scenario, it will output the required message as coded in the function
break;
else
cout << "All Columns are correct." << endl; //message to output for the true scenario
break;
case 'M' : if (!checkMini(grid))
break;
else
cout << "All Minigrids are correct."<< endl; //message to output for the true scenario
case 'Q' : break;
default : cout << "Invalid option " << endl; cin.ignore(100,'\n');
}
}
return 0;
}
void printMenu()
{
cout << "\n\tSudoku Checker " << endl << endl;
cout << "\t L\t Load file " << endl;
cout << "\t D\t Display " << endl;
cout << "\t C \t Check columns " << endl;
cout << "\t R \t Check rows " << endl;
cout << "\t M \t Check minigrids" << endl;
cout << "\t Q\t Quit " << endl;
cout << " Rows and columns are labelled from 0 to 8 " << endl;
cout << endl;
}
bool readGrid(string fName, int grid[9][9])
{
ifstream fileName;
fileName.open(fName.c_str());
if (! fileName) //if file doesn't not exist
return false;
while (! fileName.eof()) //if file is detected, fill in the array for grid
{
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
fileName >> grid[i][j];
}
return true;
}
int displayGrid(int grid[][9])
{
for (int row = 0; row < 9; row++) //2D array to go through both rows and columns
{
for (int col = 0; col < 9; col++)
{
cout << grid[row][col] << " "; //fill in the sudoku grid (seperated by a space)
}
cout << endl;
}
}
bool checkRow (int grid[][9])
{
bool status = true; //a status check
for (int row = 0; row<9; row++) // go through the rows of the 9X9 grid)
{
for (int check =1; check <9; check++) //a check variable to compare the rows
{
int occurance = 0; // a flag
for (int column = 0; column <9; column++) //go through each column of the 9X9 grid)
{
if (grid[row][column] == check) // if the cell matches the number in the check variable
{
occurance++; //increase the occurance flag to output the result
}
}
if (occurance > 1) //if there are more than one occurances
{
cout << "Row " << row << " is incorrect!" << endl;
status = false;
}
}
}
return status;
}
bool checkColumns (int grid[][9])
{
bool status2 = true; //a status check
for (int column = 0; column<9; column++) // go through the columns of the 9X9 grid)
{
for (int check =1; check <9; check++) //a check variable to compare the columns
{
int occurance = 0;
for (int row = 0; row<9; row++) // go through the rows of the 9X9 grid)
{
if (grid[row][column] == check) // if the cell matches the number in the check variable
{
occurance++;
}
}
if (occurance > 1)
{
cout << "Column " << column << " is incorrect!" << endl;
status2 = false;
}
}
}
return status2;
}
bool checkMini (int grid[][9])
{
bool status3= true;
int container[9];
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 9; column ++)
{
{
int occurances = 0;
int MiniCol = (column/3)*3; //calculates the minigrids for column (the 3 cells)
int MiniRow = (row/3)*3; // calculates the minigrids for the row (the 3 cells)
for (int nextRow = 0; nextRow < MiniRow +3; nextRow++) //loop to go through the 3X3 minigrids
{
for (int nextCol = 0; nextCol < MiniCol + 3; nextCol++)
{
container[9] == grid[row][column];
{
if (grid[nextRow][nextCol] == container[9])
{
occurances++;
}
}
}
if (occurances > 1)
{
cout << "Incorrect minigrid starting at row " << row << " column " << column << "."<< endl;
status3 = false;
}
}
}
return status3;
}
}
}
Please assist... very urgent! Been working on it for some time.
Thanks a lot.