Sudoku C++
Publicado por Héctor Tello (1 intervención) el 06/11/2013 05:57:31
Alguien me puede decir como puedo comenzar un proyecto que se trata de hacer funcionar un juego de sudoku, el usuario podrá usar el juego.
Valora esta pregunta


0
#include<iostream>
#include<conio2.h>
#define COLOR1 system("color 8f";
#define SELLO {gotoxy(70,2); cout<<"by Mc Pool"<<endl;}
#define t "t"
using namespace std;
int sudoku[9][9] =
{
{6,0,1,0,0,4,0,8,0},
{0,4,9,7,3,0,0,0,0},
{8,2,3,0,0,0,0,4,9},
{0,0,4,0,0,0,0,9,6},
{0,0,7,0,0,0,2,0,0},
{9,8,0,0,0,0,3,0,0},
{4,1,0,0,0,0,9,6,5},
{0,0,0,0,4,1,8,3,0},
{0,5,0,2,0,0,4,0,7}
};
void Sudoku();
void resSudoku(int,int);
bool buscaCeldaVacia(int&,int&;
bool puedoColocar(int,int,int);
void impSudoku();
main()
{
SELLO;
COLOR1;
Sudoku();
resSudoku(0,0);
getch();
}
void Sudoku(void)
{
int i,j;
cout<<t<<" /****************************/"<<endl
<<t<<" SUDOKU "<<endl
<<t<<" /****************************/"<<endl;
for(i=0;i<9;i++)
{
cout<<endl<<t<<t;
for(j=0;j<9;j++)
{
cout<<sudoku[j]<<" ";
if(j==2 || j==5)
cout<<" ";
}
cout<<endl<<t<<t;
if(i==2 || i==5)
cout<<endl;
}
cout<<endl;
}
void resSudoku(int x,int y)
{
bool hayVacia = buscaCeldaVacia(x,y);
if(!hayVacia) impSudoku();
else
{
for(int nro=1;nro<=9;nro++)
if(puedoColocar(x,y,nro))
{
sudoku[x][y] = nro;
resSudoku(x,y+1);
sudoku[x][y] = 0;
}
}
}
bool buscaCeldaVacia(int& x,int& y)
{
for(;x<9;x++)
{
for(;y<9;y++)
if(sudoku[x][y] == 0)
return true;
y = 0;
}
return false;
}
bool puedoColocar(int f,int c, int nro)
{
int i,j;
//chequeo fila
for(j=0;j<9;j++)
if(sudoku[f][j] == nro)
return false;
//chequeo columna
for(i=0;i<9;i++)
if(sudoku[c] == nro)
return false;
//chequeo subcuadrado
int iniCol = (c/3)*3;
int finCol = iniCol + 3;
int iniFil = (f/3)*3;
int finFil = iniFil + 3;
for(i=iniFil;i<finFil;i++)
for(j=iniCol;j<finCol;j++)
if(sudoku[j] == nro)
return false;
return true;
}
void impSudoku(void)
{
int i,j;
SELLO;
cout<<t<<" /****************************/"<<endl
<<t<<" SOLUCION "<<endl
<<t<<" /****************************/"<<endl;
for(i=0;i<9;i++)
{
cout<<endl<<t<<t;
for(j=0;j<9;j++)
{
cout<<sudoku[j]<<" ";
if(j==2 || j==5)
cout<<" ";
}
cout<<endl<<t<<t;
if(i==2 || i==5)
cout<<endl;
}
cout<<endl<<t<<t;
}