First, I am a beginner coder and I have tried to tackle and solve parts of my problem for hours and can not figure it out.
I have done all the task except for two tasks: 1.) I can not figure out how to use input and outfile mechanism of ifstream and ofstream to get my .txt file to set my values for my sudoku board. 2.) I cannot seem to figure out how to print out a sudoku board for each "possible values" for each xyposition in the cells.
I am trying to implement a text file that reads:
puzzle.setBoardValue(0,0,1);
puzzle.setBoardValue(1,1,3);
puzzle.setBoardValue(2,2,9);
puzzle.setBoardValue(3,2,6);
puzzle.setBoardValue(4,1,2);
puzzle.setBoardValue(5,0,7);
puzzle.setBoardValue(6,2,5);
puzzle.setBoardValue(7,0,9);
puzzle.setBoardValue(8,1,8);
puzzle.setBoardValue(0,5,6);
puzzle.setBoardValue(1,4,1);
puzzle.setBoardValue(2,3,5);
puzzle.setBoardValue(3,3,3);
puzzle.setBoardValue(4,4,8);
puzzle.setBoardValue(5,5,4);
puzzle.setBoardValue(6,3,9);
puzzle.setBoardValue(8,4,2);
puzzle.setBoardValue(0,6,3);
puzzle.setBoardValue(1,7,4);
puzzle.setBoardValue(2,8,7);
puzzle.setBoardValue(6,8,3);
puzzle.setBoardValue(7,6,1);
puzzle.setBoardValue(8,7,7); `
and basically use that to and automatically direction my program to the function void setBoardValue();
All I have figured out was in my int main();
where I put
cout << "Enter filename:" << endl;
cin >> filename;
in.open(filename);
while(getline(in,line)){
...
Secondly, I just can not figure out how to print all the possible values for each number cells.
Here is my entire program code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class SudokuPuzzle{
private:
unsigned short board[9][9];
char BlankChar;
bool debug;
void printTracerTryingValue(int xpos, int ypos);
bool solve(int xpos, int ypos);
bool verifyValue(int xpos, int ypos);
public:
SudokuPuzzle();
void print();
void setBoardValue(int xpos, int ypos, int value);
int getBoardValue(int xpos, int ypos);
bool solve();
};
SudokuPuzzle::SudokuPuzzle(){
debug = false;
for (int i = 0; i < 9; ++i){
for (int j = 0; j < 9; ++j){
board[j][i] = 0;
}
}
}
void SudokuPuzzle::print(){
for (int y = 0; y < 9; y++){
if (y % 3 == 0){
cout << "-------------------------------" << endl;
}
for (int x = 0; x < 9; x++){
if (x % 3 == 0){
cout << "|";
}
if (board[x][y] != 0){
cout << " " << board[x][y] << " ";
}
else{
cout << " . ";
}
}
cout << "|" << endl;
}
cout << "-------------------------------" << endl;
}
void SudokuPuzzle::setBoardValue(int xpos, int ypos, int value){
board[xpos][ypos] = value;
}
void SudokuPuzzle::setBoardValue(int xpos, int ypos, int value){
board[xpos][ypos] = value;
}
bool SudokuPuzzle::solve(){
return solve(0,0);
}
void SudokuPuzzle::setBoardValue(int xpos, int ypos, int value){
board[xpos][ypos] = value;
}
bool SudokuPuzzle::solve(){
return solve(0,0);
}
int SudokuPuzzle::getBoardValue(int xpos, int ypos){
return board[xpos][ypos];
}
bool SudokuPuzzle::solve(int xpos, int ypos){
if (board[xpos][ypos] != 0){
if (verifyValue(xpos, ypos)){
if (xpos == 8 && ypos == 8){
return true;
}
int next_x = xpos+1;
int next_y = ypos;
if (next_x >= 9){
next_x = 0;
next_y++;
}
return solve(next_x, next_y);
}
else{
return false;
}
}
for(int value = 1; value < 10; value++){
setBoardValue(xpos, ypos, value);
if (verifyValue(xpos, ypos)){
if (xpos == 8 && ypos == 8){
return true;
}
int next_x = xpos+1;
int next_y = ypos;
if (next_x >= 9){
next_x = 0;
next_y++;
}
if (solve(next_x, next_y)){
return true;
}
}
}
board[xpos][ypos] = 0;
return false;
}
bool SudokuPuzzle::verifyValue(int xpos, int ypos){
printTracerTryingValue(xpos, ypos);
int value = board[xpos][ypos];
for (int x_verify = 0; x_verify < 9; x_verify++){
if (x_verify == xpos){
continue;
}
int verifyValue = board[x_verify][ypos];
if (verifyValue == value){
return false;
}
}
for (int y_verify = 0; y_verify < 9; y_verify++){
if (y_verify == ypos){
continue;
}
int verifyValue = board[xpos][y_verify];
if (verifyValue == value){
return false;
}
}
int box_x = xpos / 3;
int box_y = ypos / 3;
for (int y_verify = box_y * 3; y_verify < box_y * 3 + 3; y_verify++){
for (int x_verify = box_x * 3; x_verify < box_x * 3 + 3; x_verify++){
if (x_verify == xpos && y_verify == ypos){
continue;
}
int verifyValue = board[x_verify][y_verify];
if (verifyValue == value){
return false;
}
}
}
return true;
}
void SudokuPuzzle::printTracerTryingValue(int xpos, int ypos){
if(debug){
for (int i = 0; i < xpos+ypos; i++){
cout << " ";
}
cout << "Trying value " << board[xpos][ypos] << " at board[" << xpos << "][" \
<< ypos <<"]" << endl;
}
}
int main(int argc, char *const argv[]){
SudokuPuzzle puzzle;
string filename;
string line;
ifstream in;
ofstream out;
cout << "Enter filename:" << endl;
cin >> filename;
in.open(filename);
while(getline(in, line)){
puzzle.line.print()
}
cout << endl;
if(puzzle.solve()){
cout << "Solution:" << endl;
puzzle.print();
}
else{
cout << "Puzzle is not solvable.";
}
cout << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire