part2: Make modifications to your solution to Sudoku part 1 that 1) defines grid
ID: 3846433 • Letter: P
Question
part2:
Make modifications to your solution to Sudoku part 1 that
1) defines grids of "arbitrary" size.
9 x 9 grids are possible
16 x 16 grids are possible
25 x 25 grids are possible
..
The criterion that must be met is that the grid dimension be a perfect square
here is the solution to part 1 that needs to be modified:
1.Sudoku.h:
#include <vector>// for delcare grid
#include <fstream> // for input nad output stream
using namespace std;
class Sudoku
{
public:
Sudoku();
Sudoku(int g[][9]);
void initializeSudokuGrid();
void initializeSudokuGrid(int g[][9]);
Sudoku(ifstream &inFile);//new consrtuctor to the Sudoku class that reads the initial configuration from a file
void initializeSudokuGrid(ifstream &inFile);
void printSudokuGrid();
void printSudokuGridOnFile(ofstream &outFile);//writes the final Sudoku grid to a file
bool solveSudoku();
bool findEmptyGridSlot(int &row, int &col);
bool canPlaceNum(int row, int col, int num);
bool numAlreadyInRow(int row, int num);
bool numAlreadyInCol(int col, int num);
bool numAlreadyInBox(int smallGridRow, int smallGridCol, int num);
private:
vector<vector<int> > grid;//declare grid as vector
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2.Sudoku.cpp:
#include <iostream>
#include <string>
#include "Sudoku.h"
using namespace std;
Sudoku::Sudoku(void)
{
grid.resize(9,vector<int>(9));
initializeSudokuGrid();
}
Sudoku::Sudoku(int g[][9])
{
grid.resize(9,vector<int>(9));
initializeSudokuGrid(g);
}
Sudoku::Sudoku(ifstream &inFile)//new consrtuctor to the Sudoku class that reads the initial configuration from a file
{
grid.resize(9,vector<int>(9));
initializeSudokuGrid(inFile);
}
void Sudoku::initializeSudokuGrid(ifstream &inFile)
{
int n;
if(inFile.is_open())
{
for(int row = 0; row < 9; row++)
for(int col = 0; col < 9; col++)
{
inFile>>n;//read value from file
grid[row].push_back(n);//push back n into vector grid
}
inFile.close();
}
else
{
cout<<"File could not opend."<<endl;
initializeSudokuGrid();//if file is not opend then intialise grid with zeros
}
}
void Sudoku::initializeSudokuGrid()
{
for(int row = 0; row < 9; row++)
for(int col = 0; col < 9; col++)
grid[row].push_back(0);
}
void Sudoku::initializeSudokuGrid(int g[][9])
{
for(int row = 0; row < 9; row++)
for(int col = 0; col < 9; col++)
grid[row].push_back(g[row][col]);
}
void Sudoku::printSudokuGrid()
{
for(int row = 0; row < 9; row++)
{
for(int col = 0; col < 9; col++)
cout << grid[row].at(col);
cout << endl;
}
}
void Sudoku::printSudokuGridOnFile(ofstream &outFile)//writes the final Sudoku grid to a file
{
for(int row = 0; row < 9; row++)
{
for(int col = 0; col < 9; col++)
{
outFile<<grid[row].at(col)<<" ";//write grid to a file
}
outFile<<endl;//print next line in file
}
}
bool Sudoku::solveSudoku()
{
int row = 0, col = 0;
if(findEmptyGridSlot(row, col))
{
for(int num = 1; num <= 9; num++)
{
if(canPlaceNum(row, col, num))
{
grid[row][col]=num;
if(solveSudoku())
return true;
grid[row][col]=0;
}
}
return false;
}
else
return true;
}
bool Sudoku::findEmptyGridSlot(int &row, int &col)
{
//find empty grid slot is return true empty slot finds
for (row = 0; row < 9; row++)
for (col = 0; col < 9; col++)
if (grid[row].at(col) == 0)
return true;
row = -1;
col = -1;
return false;//other wise false
}
bool Sudoku::canPlaceNum(int row, int col, int num)
{
return !numAlreadyInRow(row, num) &&//check number already in row or not
!numAlreadyInCol(col, num) &&//check number already in coloumn or not
!numAlreadyInBox(row, col, num);//check number already in box( that is 3 x 3 grid) or not
}
bool Sudoku::numAlreadyInRow(int row, int num)
{//check number already in row or not
for(int col = 0; col < 9; col++)
if(grid[row].at(col)== num) return true;
return false;
}
bool Sudoku::numAlreadyInCol(int col, int num)
{//check number already in coloumn or not
for (int row = 0; row < 9; row++)
if (grid[row].at(col)== num) return true;
return false;
}
bool Sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
{//check number already in box( that is 3 x 3 grid) or not
int beginSmallGridRow = smallGridRow - smallGridRow % 3;
int endSmallGridRow = beginSmallGridRow + 3;
int beginSmallGridCol = smallGridCol - smallGridCol % 3;
int endSmallGridCol = beginSmallGridCol + 3;
for (int row = beginSmallGridRow; row < endSmallGridRow; row++)
for (int col = beginSmallGridCol; col < endSmallGridCol; col++)
if (grid[row].at(col)== num)
return true;
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SudukoTest.cpp:
#include <iostream>
#include <string>
#include <fstream>
#include "Sudoku.h"
using namespace std;
int main()
{
int g[][9] =
{
{6,0,3,0,2,0,0,9,0},
{0,0,0,0,5,0,0,8,0},
{0,2,0,4,0,7,0,0,1},
{0,0,6,0,1,4,3,0,0},
{0,0,0,0,8,0,0,5,6},
{0,4,0,6,0,3,2,0,0},
{8,0,0,2,0,0,0,0,7},
{0,1,0,0,7,5,8,0,0},
{0,3,0,0,0,6,1,0,5}
};
//create two files for input and output
ifstream inFile("inputFile.txt");
ofstream outFile("outputFile.txt");
cout<<" Sudoko 1:"<<endl;
Sudoku sudoku1;
sudoku1.printSudokuGrid();
sudoku1.solveSudoku();
cout << endl;
sudoku1.printSudokuGrid();
sudoku1.printSudokuGridOnFile(outFile);//write sudoko on file
cout << endl;
cout<<" Sudoko 2:"<<endl;
Sudoku sudoku2(g);
sudoku2.solveSudoku();
sudoku2.printSudokuGrid();
cout << endl;
cout<<" Sudoko 3:"<<endl;
//read soduko from file
Sudoku sudoku3(inFile);
sudoku3.solveSudoku();
sudoku3.printSudokuGrid();
sudoku3.printSudokuGridOnFile(outFile);//write sudoko on file
cout << endl;
outFile.close();
system("pause");
return 0;
}
Explanation / Answer
1.Sudoku.h:
#include <vector>// for delcare grid
#include <fstream> // for input nad output stream
using namespace std;
class Sudoku
{
public:
Sudoku();
Sudoku(int g[][9]);
void initializeSudokuGrid();
void initializeSudokuGrid(int g[][9]);
Sudoku(ifstream &inFile);//new consrtuctor to the Sudoku class that reads the initial configuration from a file
void initializeSudokuGrid(ifstream &inFile);
void printSudokuGrid();
void printSudokuGridOnFile(ofstream &outFile);//writes the final Sudoku grid to a file
bool solveSudoku();
bool findEmptyGridSlot(int &row, int &col);
bool canPlaceNum(int row, int col, int num);
bool numAlreadyInRow(int row, int num);
bool numAlreadyInCol(int col, int num);
bool numAlreadyInBox(int smallGridRow, int smallGridCol, int num);
private:
vector<vector<int> > grid;//declare grid as vector
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2.Sudoku.cpp:
#include <iostream>
#include <string>
#include "Sudoku.h"
using namespace std;
Sudoku::Sudoku(void)
{
grid.resize(9,vector<int>(9));
initializeSudokuGrid();
}
Sudoku::Sudoku(int g[][9])
{
grid.resize(9,vector<int>(9));
initializeSudokuGrid(g);
}
Sudoku::Sudoku(ifstream &inFile)//new consrtuctor to the Sudoku class that reads the initial configuration from a file
{
grid.resize(9,vector<int>(9));
initializeSudokuGrid(inFile);
}
void Sudoku::initializeSudokuGrid(ifstream &inFile)
{
int n;
if(inFile.is_open())
{
for(int row = 0; row < 9; row++)
for(int col = 0; col < 9; col++)
{
inFile>>n;//read value from file
grid[row].push_back(n);//push back n into vector grid
}
inFile.close();
}
else
{
cout<<"File could not opend."<<endl;
initializeSudokuGrid();//if file is not opend then intialise grid with zeros
}
}
void Sudoku::initializeSudokuGrid()
{
for(int row = 0; row < 9; row++)
for(int col = 0; col < 9; col++)
grid[row].push_back(0);
}
void Sudoku::initializeSudokuGrid(int g[][9])
{
for(int row = 0; row < 9; row++)
for(int col = 0; col < 9; col++)
grid[row].push_back(g[row][col]);
}
void Sudoku::printSudokuGrid()
{
for(int row = 0; row < 9; row++)
{
for(int col = 0; col < 9; col++)
cout << grid[row].at(col);
cout << endl;
}
}
void Sudoku::printSudokuGridOnFile(ofstream &outFile)//writes the final Sudoku grid to a file
{
for(int row = 0; row < 9; row++)
{
for(int col = 0; col < 9; col++)
{
outFile<<grid[row].at(col)<<" ";//write grid to a file
}
outFile<<endl;//print next line in file
}
}
bool Sudoku::solveSudoku()
{
int row = 0, col = 0;
if(findEmptyGridSlot(row, col))
{
for(int num = 1; num <= 9; num++)
{
if(canPlaceNum(row, col, num))
{
grid[row][col]=num;
if(solveSudoku())
return true;
grid[row][col]=0;
}
}
return false;
}
else
return true;
}
bool Sudoku::findEmptyGridSlot(int &row, int &col)
{
//find empty grid slot is return true empty slot finds
for (row = 0; row < 9; row++)
for (col = 0; col < 9; col++)
if (grid[row].at(col) == 0)
return true;
row = -1;
col = -1;
return false;//other wise false
}
bool Sudoku::canPlaceNum(int row, int col, int num)
{
return !numAlreadyInRow(row, num) &&//check number already in row or not
!numAlreadyInCol(col, num) &&//check number already in coloumn or not
!numAlreadyInBox(row, col, num);//check number already in box( that is 3 x 3 grid) or not
}
bool Sudoku::numAlreadyInRow(int row, int num)
{//check number already in row or not
for(int col = 0; col < 9; col++)
if(grid[row].at(col)== num) return true;
return false;
}
bool Sudoku::numAlreadyInCol(int col, int num)
{//check number already in coloumn or not
for (int row = 0; row < 9; row++)
if (grid[row].at(col)== num) return true;
return false;
}
bool Sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
{//check number already in box( that is 3 x 3 grid) or not
int beginSmallGridRow = smallGridRow - smallGridRow % 3;
int endSmallGridRow = beginSmallGridRow + 3;
int beginSmallGridCol = smallGridCol - smallGridCol % 3;
int endSmallGridCol = beginSmallGridCol + 3;
for (int row = beginSmallGridRow; row < endSmallGridRow; row++)
for (int col = beginSmallGridCol; col < endSmallGridCol; col++)
if (grid[row].at(col)== num)
return true;
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SudukoTest.cpp:
#include <iostream>
#include <string>
#include <fstream>
#include "Sudoku.h"
using namespace std;
int main()
{
int g[][9] =
{
{6,0,3,0,2,0,0,9,0},
{0,0,0,0,5,0,0,8,0},
{0,2,0,4,0,7,0,0,1},
{0,0,6,0,1,4,3,0,0},
{0,0,0,0,8,0,0,5,6},
{0,4,0,6,0,3,2,0,0},
{8,0,0,2,0,0,0,0,7},
{0,1,0,0,7,5,8,0,0},
{0,3,0,0,0,6,1,0,5}
};
//create two files for input and output
ifstream inFile("inputFile.txt");
ofstream outFile("outputFile.txt");
cout<<" Sudoko 1:"<<endl;
Sudoku sudoku1;
sudoku1.printSudokuGrid();
sudoku1.solveSudoku();
cout << endl;
sudoku1.printSudokuGrid();
sudoku1.printSudokuGridOnFile(outFile);//write sudoko on file
cout << endl;
cout<<" Sudoko 2:"<<endl;
Sudoku sudoku2(g);
sudoku2.solveSudoku();
sudoku2.printSudokuGrid();
cout << endl;
cout<<" Sudoko 3:"<<endl;
//read soduko from file
Sudoku sudoku3(inFile);
sudoku3.solveSudoku();
sudoku3.printSudokuGrid();
sudoku3.printSudokuGridOnFile(outFile);//write sudoko on file
cout << endl;
outFile.close();
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.