You are on page 1of 8

sudoku.

BAK
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
randomize();
clrscr();
int sudoku[9][9],ctr=0,ctrc=0,ctrw=0,chk=0,ayush=0,at=0,at1=0,at2=0;
int randomr,randomc;
for(int r=0;r<9;r++)
{
for(int c=0;c<9;c++)
{
sudoku[c][r]=0;
}
}
for(int varctr=1;varctr<=9;varctr++)
{
randomr=random(9);
randomc=random(9);
sudoku[randomr][randomc]=varctr;
ayush++;
}
int gtx=10,gty=5;
for(int r1=0;r1<9;r1++)
{
for(int c1=0;c1<9;c1++)
{
gotoxy(gtx,gty);
cout<<sudoku[c1][r1];
gtx=gtx+6;
}
gty=gty+4;
gtx=10;
}
while(ayush!=81)
{
chk=0;
gotoxy(4,42);
cout<<"Enter the row you wish to edit: ";
int redit;
cin>>redit;
gotoxy(4,44);
cout<<"Enter the column you wish to edit: ";
Page 1

sudoku.BAK
int cedit;
cin>>cedit;
int cedp3=cedit%3,redp3=redit%3;
gotoxy(4,46);
cout<<"Enter the value to be inserted in the cell: ";
int vedit;
int gtx1=10+6*(redit-1), gty1=5+4*(cedit-1);
gotoxy(gtx1,gty1);
cin>>vedit;
ctr++;
at=0;
for(int c2=0;c2<9;c2++)
{
if(sudoku[redit-1][c2]!=vedit)
{
at++;
}
}
at1=0;
if(at==9)
{
for(int r2=0;r2<9;r2++)
{
if(sudoku[r2][cedit-1]!=vedit)
{
at1++;
}
}
}
if(at!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
if(at1==9)
{
//1st if
if(redp3==0 && cedp3==0)
{
for(int rx=redit-3;rx<redit;rx++)
{
for(int cx=cedit-3;cx<cedit;cx++)
{
Page 2

sudoku.BAK
if(sudoku[rx][cx]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
//2nd if
if(redp3==0 && cedp3==1)
{
for(int rx1=redit-3;rx1<redit;rx1++)
{
for(int
cx1=cedit-1;cx1<cedit+2;cx1++)
{
if(sudoku[rx1][cx1]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
//3rd if
if(redp3==0 && cedp3==2)
{
for(int rx2=redit-3;rx2<redit;rx2++)
{
Page 3

sudoku.BAK
for(int
cx2=cedit-2;cx2<cedit+1;cx2++)
{
if(sudoku[rx2][cx2]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
//4th if
if(redp3==1 && cedp3==0)
{
for(int rx3=redit-1;rx3<redit+2;rx3++)
{
for(int cx3=cedit-3;cx3<cedit;cx3++)
{
if(sudoku[rx3][cx3]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
//5th if
if(redp3==1 && cedp3==1)
Page 4

sudoku.BAK
{
for(int rx4=redit-1;rx4<redit+2;rx4++)
{
for(int
cx4=cedit-1;cx4<cedit+2;cx4++)
{
if(sudoku[rx4][cx4]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
//6th if
if(redp3==1 && cedp3==2)
{
for(int rx5=redit-1;rx5<redit+2;rx5++)
{
for(int
cx5=cedit-2;cx5<cedit+1;cx5++)
{
if(sudoku[rx5][cx5]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
Page 5

sudoku.BAK
ctrw++;
}
}
//7th if
if(redp3==2 && cedp3==0)
{
for(int rx6=redit-2;rx6<redit+1;rx6++)
{
for(int cx6=cedit-3;cx6<cedit;cx6++)
{
if(sudoku[rx6][cx6]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
//8th if
if(redp3==2 && cedp3==1)
{
for(int rx7=redit-2;rx7<redit+1;rx7++)
{
for(int
cx7=cedit-1;cx7<cedit+2;cx7++)
{
if(sudoku[rx7][cx7]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
Page 6

sudoku.BAK
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
//9th if
if(redp3==2 && cedp3==2)
{
for(int rx8=redit-2;rx8<redit+1;rx8++)
{
for(int
cx8=cedit-2;cx8<cedit+1;cx8++)
{
if(sudoku[rx8][cx8]!=vedit);
chk++;
}
}
if(chk==9)
{
sudoku[redit-1][cedit-1]=vedit;
ctrc++;
}
if(chk!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
}
}
if(at1!=9)
{
gotoxy(5,49);
cout<<"Incorrect Input\a";
ctrw++;
}
clrscr();
gtx=10;
gty=5;
for(int r9=0;r9<9;r9++)
{
for(int c9=0;c9<9;c9++)
Page 7

sudoku.BAK
{
if(sudoku[c9][r9]!=0)
{
ayush++;
}
gotoxy(gtx,gty);
cout<<sudoku[c9][r9];
gtx=gtx+6;
}
gty=gty+4;
gtx=10;
}
}//while close
clrscr();
gotoxy(30,24);
cout<<"You win";
getch();
}

Page 8

You might also like