Hi,
I'm working in a 4x4 sudoku solver and i'm a beginner in c++.
I use Microsoft Visual Studio 6.0 and windows xp professional
Variables are:
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
First i wrote that program
//sudoku.cpp:
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>
void main(){
srand(time(0));
int a1;
int b1;
int c1;
int d1;
int a2;
int b2;
int c2;
int d2;
int a3;
int b3;
int c3;
int d3;
int a4;
int b4;
int c4;
int d4;
srand(time(0));
// 1st row
a1=rand()*5/32768;
printf("a1 = %d\n",a1);
while((b1!=0)||(b1=a1)){
b1=rand()*5/32768;
if ((b1!=0)&&(b1!=a1)){
break;
printf("b1 %d\n",b1);
}}
printf("b1 = %d\n",b1);
while((c1!=0)||(c1=a1)||(c1=b1)){
c1=rand()*5/32768;
if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
break;
}}
printf("c1 = %d\n",c1);
while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
d1=rand()*5/32768;
if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
break;
}}
printf("d1 = %d\n",d1);
// Column A
while((a2!=0)||(a2=a1)||(a2=b1)){
a2=rand()*5/32768;
if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
break;
}}
printf("a2 = %d\n",a2);
while((a3!=0)||(a3=a1)||(a3=a2)){
a3=rand()*5/32768;
if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
break;
}}
printf("a3 = %d\n",a3);
while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
a4=rand()*5/32768;
if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
break;
}}
printf("a4 = %d\n",a4);
// Column D
while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
d2=rand()*5/32768;
if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
break;
}}
printf("d2 = %d\n",d2);
while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
d3=rand()*5/32768;
if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
break;
}}
printf("d3 = %d\n",d3);
while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
d4=rand()*5/32768;
if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
break;
}}
printf("d4 = %d\n",d4);
// 4th row
while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
b4=rand()*5/32768;
if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
break;
}}
printf("b4 = %d\n",b4);
while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
c4=rand()*5/32768;
if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
break;
}}
printf("c4 = %d\n",c4);
// Central Square
while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
b2=rand()*5/32768;
if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
break;
}}
printf("b2 = %d\n",b2);
while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
c2=rand()*5/32768;
if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
break;
}}
printf("c2 = %d\n",c2);
while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
b3=rand()*5/32768;
if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
break;
}}
printf("b3 = %d\n",b3);
while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
c3=rand()*5/32768;
if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
break;
}}
printf("c3 = %d\n",c3);
}
It works but sometimes it reach absurd situations like this:
1 4 2 3
3 4
4 1
2 !
When that happens program stops and don't show the solced variables in that case a1=1 b1=4 c1=2 d1=3 a2=3 a3=4 a4=2 d2=4 d3=1
In order to solve that i've writen another code with a loop that doesn't stop the program until the last variable to be evaluated (c3) is not 0.
I've post it here:
//sudoku.cpp:
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
void main(){
int a1;
int b1;
int c1;
int d1;
int a2;
int b2;
int c2;
int d2;
int a3;
int b3;
int c3;
int d3;
int a4;
int b4;
int c4;
int d4;
int xval;
srand(time(0));
c3=0;
while(c3==0)
{
// 1st row
a1=(rand()%4)+1;
cout <<"a1 = "<<a1<<endl;
do {
b1=(rand()%4)+1;
}while(b1==a1);
{
cout <<"b1 = "<<b1<<endl;
}
do {
c1=(rand()%4)+1;
}while((c1==a1)||(c1==b1));
{
cout <<"c1 = "<<c1<<endl;
}
do {
d1=(rand()%4)+1;
}while((d1==a1)||(d1==b1)||(d1==c1));
{
cout <<"d1 = "<<d1<<endl;
}
// Column A
do {
a2=(rand()%4)+1;
}while((a2==a1)||(a2==b1));
{
cout <<"a2 = "<<a2<<endl;
}
do {
a3=(rand()%4)+1;
}while((a3==a1)||(a3==a2));
{
cout <<"a3 = "<<a3<<endl;
}
do {
a4=(rand()%4)+1;
}while((a4==a1)||(a4==a2)||(a4==a3));
{
cout <<"a4 = "<<a4<<endl;
}
// Column D
do {
d2=(rand()%4)+1;
}while((d2==c1)||(d2==d1)||(d2==a2));
{
cout <<"d2 = "<<d2<<endl;
}
do {
d3=(rand()%4)+1;
}while((d3==d1)||(d3==d2)||(d3==a3));
{
cout <<"d3 = "<<d3<<endl;
}
do {
d4=(rand()%4)+1;
}while((d4==d1)||(d4==d2)||(d4==d3)||(d4==a4));
{
cout <<"d4 = "<<d4<<endl;
}
// 4th row
do {
b4=(rand()%4)+1;
}while((b4==a3)||(b4==a4)||(b4==b1)||(b4==d4));
{
cout <<"b4 = "<<b4<<endl;
}
do {
c4=(rand()%4)+1;
}while((c4==d3)||(c4==d4)||(c4==c1)||(c4==a4)||(c4==b4));
{
cout <<"c4 = "<<c4<<endl;
}
// Central square
do {
b2=(rand()%4)+1;
}while((b2==a1)||(b2==b1)||(b2==a2)||(b2==d2)||(b2==b4));
{
cout <<"b2 = "<<b2<<endl;
}
do {
c2=(rand()%4)+1;
}while((c2==c1)||(c2==d1)||(c2==a2)||(c2==b2)||(c2==d2)||(c2==c4));
{
cout <<"c2 = "<<c2<<endl;
}
do {
b3=(rand()%4)+1;
}while((b3==a3)||(b3==a4)||(b3==b4)||(b3==b1)||(b3==b2)||(b3==d3));
{
cout <<"b3 = "<<b3<<endl;
}
do {
c3=(rand()%4)+1;
}while((c3==c4)||(c3==d4)||(c3==d3)||(c3==c1)||(c3==c2)||(c3==a3)||(c3==b3));
{
cout <<"c3 = "<<c3<<endl;
}
if (c3!=0)
{
break;
}
}
// It shows all sudoku
cout <<""<<endl;
cout <<"Sudoku"<<endl;
cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
cout <<a2<<" "<<b2<<" "<<c2<<" "<<d2<<endl;
cout <<a3<<" "<<b3<<" "<<c3<<" "<<d3<<endl;
cout <<a4<<" "<<b4<<" "<<c4<<" "<<d4<<endl;
cout <<""<<endl;
cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
cout <<a2<<" "<<"x"<<" "<<c2<<" "<<d2<<endl;
cout <<a3<<" "<<b3<<" "<<"y"<<" "<<d3<<endl;
cout <<"z"<<" "<<b4<<" "<<c4<<" "<<d4<<endl;
cout <<""<<endl;
while (xval!=b2){cout << "What's x value?\n";
cin >> xval;
if (xval==b2) {
cout << "OK\n";
break;
}
}
cout <<""<<endl;
while (xval!=c3){cout << "What's y value?\n";
cin >> xval;
if (xval==c3) {
cout << "OK\n";
break;
}
}
cout <<""<<endl;
while (xval!=a4){cout << "What's x value??\n";
cin >> xval;
if (xval==a4) {
cout << "OK\n";
break;
}
}
cout <<""<<endl;
First the loop seemed work but later i noticed it reach these absurd situacions too.
Anyone know why? i've no idea what's wrong
thank you