Hola buenas a ver si alguien puede ayudarme con un problema de mi sudoku tengo creado el codigo para quue sea capaz de introducir numeros en el sudoku de 0 a 9 pero el problema es que no se me ocurre nada para la restriccion de añadir numeros que esten ya incluidos en la fila en la columna o en la submatriz, se lo agradeceria mucho!!
#include <iostream>
#include<fstream>
using namespace std;
typedef char tab[9][9];
bool inicializarSudoku(tab Tablero);
void mostrarSudoku(tab Tablero);
void RellenarSudoku(tab Tablero);
int main()
{
int a=0;
tab Tablero;
inicializarSudoku(Tablero);
mostrarSudoku(Tablero);
while(a==0){
RellenarSudoku(Tablero);
mostrarSudoku(Tablero);
}
return 0;
}
void RellenarSudoku(tab Tablero)
{
int x, y;
char valor;
cout<<"introduce fila";
cin>>x;
cout<<"introduce columna";
cin>>y;
cout<<"introduce valor";
cin>>valor;
if((x>=9)&&(y>=9)&&(valor>9))
{cout <<"error de numero"<<endl;}
else Tablero[x][y]=valor;
}
bool inicializarSudoku(tab Tablero)
{
int a, b;
for (a=0;a<9;a++)
{
for (b=0;b<9;b++)
{
Tablero[a][b]=' ';
}
}
int i, j;
char c;
ifstream archivo;
archivo.open("sudoku.txt");
do
{
archivo >> j>>i>>c;
Tablero[i][j] = c;
}
while(!archivo.eof());
archivo.close();
}
void mostrarSudoku(tab Tablero)
{cout<<" - - - - - - - - - "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=0;j<1;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=0;j<1;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=0;j<1;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=1;j<2;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=1;j<2;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=1;j<2;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=2;j<3;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=2;j<3;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=2;j<3;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl;
cout<<" - - - - - - - - - "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=3;j<4;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=3;j<4;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=3;j<4;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=4;j<5;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=4;j<5;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=4;j<5;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=5;j<6;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=5;j<6;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=5;j<6;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl;
cout<<" - - - - - - - - - "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=6;j<7;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=6;j<7;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=6;j<7;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=7;j<8;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=7;j<8;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=7;j<8;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl<<"|"<<" ";
for(int i=0;i<3;i++)
for(int j=8;j<9;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=3;i<6;i++)
for(int j=8;j<9;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" ";
for(int i=6;i<9;i++)
for(int j=8;j<9;j++){
cout<<Tablero[i][j]<<" ";}
cout<<"|"<<" "<<endl;
cout<<" - - - - - - - - - - - "<<endl<<" ";
}
if((x>=9)&&(y>=9)&&(valor>9))
{
cout <<"error de numero"<<endl;
}
else Tablero[x][y]=valor;
Esto debería ser así:
if((x>=9) || (y>=9) || (valor>9))
{
cout <<"error de numero"<<endl;
}
else Tablero[x][y]=valor;
Te falta validar si el número ingresado ya existe en la fila actual, columna actual, o cuadro actual. Deberías hacer una función para cada uno que retorne un valor bool. Solo permitís el ingreso del número cuando estas funciones retornen( todas ) false.
Un ejemplo para la fila:
bool validar_fila( Tab tablero, int fila, char valor ){
for( int a = 0; a<9; a++ ){
if( tablero[ fila ][ a ] == valor )return true;
}
return false;
}
mas o menos entiendo la idea pero no se como integrar la funcion booleana que dices con la funcion rellenar tablero
Sería algo así:
cout << "introduce valor";
cin>>valor;
if( ( x>=9 ) || ( y>=9 ) || ( valor>9 ) ){
cout << "numero fuera de rango" << endl;
} else {
if( validar_fila( ... ) && validar_columna( ... ) && validar_cuadro( ... ) ){
Tablero[ x ][ y ]=valor;
} else {
cout << "El numero ya se ingreso" << endl;
}
}