Code
Code
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include <time.h>
#include <windows.h>
int LivesInLevel=3;
class Sodoku
int Level[9][9]={{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0}};
int UserRows,UserColumns,UserChoice;
int storeAraay[9]={0,0,0,0,0,0,0,0,0};
int RandGenerator();
bool ArrayChecker(int);
void RandomArrayInsertion();
void LevelInsertion();
bool InsertionCheckerRow(int,int);
bool InsertionCheckerColumn(int,int);
bool InsertionCheckerMatrix(int,int,int);
public:
void UserInsertion();
void LevelMaker();
void Display();
int Moves();
void setUserRow(int);
void setUserColumn(int);
void setUserChoice(int);
};
void Sodoku::UserInsertion()
//THIS IF CHECKS WETHER THE USER HAVE ENTERED THE ROW AND COLUMN WHERE THERE IS EMPTY
SPACE IN MY CASE THE EMPTY SPACE IS ZERO
if(InsertionCheckerRow(UserRows,UserChoice)&&InsertionCheckerColumn(UserColumns,UserChoice)&
&InsertionCheckerMatrix(UserRows,UserColumns,UserChoice)&&Level[UserRows][UserColumns]==0)
Level[UserRows][UserColumns]=UserChoice;
system("cls");
else
LivesInLevel=LivesInLevel-1;
system("cls");
cout<<"LIVE LOST"<<endl;
getch();
system("cls");
}
int Sodoku::Moves()// THIS FUNCTION IS USED TO GET MOVES PRESENT IN THE GAME
int counter;
for(int i=0;i<9;++i)
for(int j=0;j<9;++j)
if(Level[i][j]==0)
counter=counter+1;
return counter;
//-----------Setters--------------
this->UserRows=RowFromMain-1;
this->UserColumns=ColumnFromMain-1;
this->UserChoice=ChoiceFromMain;
}
//-----------LevelMaker------------------
void Sodoku::LevelMaker() // REMOVES NUMBER AFTER THE INSERTION IN ORDER TO MAKE THE LEVEL
FOR THE USER
LevelInsertion();
int Store1;
Store1=RandGenerator();
for(int u=0;u<9;++u)
Store1=RandGenerator();
for(int h=0;h<9;++h)
if(Level[u][h]==Store1)
Level[u][h]=0;
//-----------LevelInsertion--------------
void Sodoku::LevelInsertion() //INSERTS THE NUMBER IN THE CHESS BOARD IT IS NECEESSARY BECAUSE
WE NEED TO CREATE A UNIQUE ONE SOLUTION
srand(time(0));// THIS FUNCTION IS USED TO CREATE A UNIQUE LEVEL EVERY TIME THE PROGRAM IS
RUN
RandomArrayInsertion();
int ArrayIterator = 0;
for(int i=0;i<9;++i)
for(int j = 0; j<9 ;)
if(InsertionCheckerColumn(j,storeAraay[ArrayIterator])&&InsertionCheckerRow(i,storeAraay[ArrayIterat
or])&&InsertionCheckerMatrix(i,j,storeAraay[ArrayIterator]))
Level[i][j]=storeAraay[ArrayIterator];
++j;
++ArrayIterator;
if(ArrayIterator>8||j>8) //THIS IS THE CONDITION FOR THE ARRAY THAT IS GENERATED TO FILL
THE LEVEL IN RANDOM ARRAY INSERTION
ArrayIterator=0; //IF ARRAY IS AT THE END THEN IT MUST START AGAIN TO LOOK FOR
POSSIBLE VALUES
else
++ArrayIterator;
ArrayIterator=0; ////IF ARRAY IS AT THE END THEN IT MUST START AGAIN TO LOOK FOR
POSSIBLE VALUES
}
//----------CheckerFunctionForBox---------
bool checker;
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
if(checker==false)
break;
}
else if((row>=0&&row<=2)&&(column>=3&&column<=5)) //THIS IF CHECKS THE SECOND BOX OF THE
GAMEBOARD
for(int i=0;i<3;++i)
for(int j=3;j<6;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
if(checker==false)
break;
for(int i=0;i<3;++i)
{
for(int j=6;j<9;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
if(checker==false)
break;
for(int i=3;i<6;++i)
for(int j=0;j<3;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
}
if(checker==false)
break;
for(int i=3;i<6;++i)
for(int j=3;j<6;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
if(checker==false)
break;
for(int i=3;i<6;++i)
for(int j=6;j<9;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
if(checker==false)
break;
for(int i=6;i<9;++i)
for(int j=0;j<3;++j)
if(Level[i][j]!=number)
checker=true;
else
{
checker=false;
break;
if(checker==false)
break;
for(int i=6;i<9;++i)
for(int j=3;j<6;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
if(checker==false)
break;
}
else if((row>5&&row<9)&&(column>5&&column<9)) //THIS IF CHECKS THE NINTH BOX OF THE
GAMEBOARD
for(int i=6;i<9;++i)
for(int j=6;j<9;++j)
if(Level[i][j]!=number)
checker=true;
else
checker=false;
break;
if(checker==false)
break;
return checker;
//-----------CheckerFunctionForColumn------
bool Sodoku::InsertionCheckerColumn(int column,int number) //CHECKS THE COLUMN IN WHICH THE
USER IS INSERTING
bool checker;
for(int r=0;r<9;r++)
if(Level[r][column]!=number)
checker=true;
else
checker=false;
break;
return checker;
//-----------CheckerFunctionForRow-------
bool Sodoku::InsertionCheckerRow(int row,int number) //CHECKS THE ROW IN WHICH THE USER IS
INSERTING
bool checker;
for(int e=0;e<9;e++)
if(Level[row][e]!=number)
checker=true;
else
checker=false;
break;
return checker;
//----------RandomArrayInsertion--------
int store;
for(int w=0;w<9;)
store=RandGenerator();
if(ArrayChecker(store))
storeAraay[w]=store;
++w;
else
continue;
}
//------------RandomNumberArrayChecker-------------
bool checker;
for(int q=0;q<9;q++)
if(randomNumberGenerated!=storeAraay[q])
checker=true;
else
checker=false;
break;
return checker;
//------------RandomNumberGenerator----------------
return rand()%9+1;
//------------DisplayFunction----------------------
void Sodoku::Display()
cout<<"-----------Your Level----------"<<endl;
for(int i=0;i<LivesInLevel;++i)
cout<<"-";
cout<<endl<<"-------------------------------"<<endl;
for(int i=0;i<9;++i)
for(int j=0;j<9;++j)
if(j==3||j==6)
else if(j==0||j==3)
else
if (i==2||i==5||i==8)
cout<<endl<<"-------------------------------";
cout<<endl;
else
cout<<endl;
}
}
main()
string SystemChoice,LevelChoice;
float MainRows,MainColumns,MainChoice;
bool SystemChecker,LevelChecker,RowChecker,ChoiceChecker,ColumnChecker;
cout<<"_______________________________________________________________________________
_________________________________________"<<endl;
do
cout<<"PRESS S TO START"<<endl;
cin>>SystemChoice;
if(SystemChoice.length()==1&&(SystemChoice=="s")||(SystemChoice=="S"))
cout<<"ACCEPTED"<<endl;
SystemChecker=true;
else
getch();
system("cls");
SystemChecker=false;
cin.clear();
cin.ignore(10000,'\n');
}
}
while(SystemChecker==false);
while(SystemChoice=="s"||SystemChoice=="S")
do
cin>>LevelChoice;
if(LevelChoice.length()==(1&&LevelChoice=="1")||(LevelChoice=="2"))
cout<<"ACCEPTED"<<endl;
LevelChecker=true;
else
getch();
system("cls");
LevelChecker=false;
cin.clear();
cin.ignore(10000,'\n');
while(LevelChecker==false);
if(LevelChoice=="1")
{
Sodoku User;
User.LevelMaker();
LivesInLevel=3;
while(User.Moves()>0&&LivesInLevel>0)
do
User.Display();
cout<<"ENTER THE ROW IN WHICH YOU WANT TO ADD NUMBER (between 0 and 10(0,10
exluded))"<<endl;
cin>>MainRows;
if(cin&&MainRows-(int)MainRows==0&&(int)MainRows>0&&MainRows<10)
RowChecker=true;
cout<<"ACCEPTED"<<endl;
else
cout<<"WRONG NUMBER"<<endl;
getch();
system("cls");
RowChecker=false;
cin.clear();
cin.ignore(10000,'\n');
}
}
while(RowChecker==false);
do
system("cls");
User.Display();
cin>>MainColumns;
if(cin&&MainColumns-
(int)MainColumns==0&&(int)MainColumns>0&&MainColumns<10)
ColumnChecker=true;
cout<<"ACCEPTED"<<endl;
else
cout<<"WRONG NUMBER"<<endl;
getch();
system("cls");
ColumnChecker=false;
cin.clear();
cin.ignore(10000,'\n');
}
}
while(ColumnChecker==false);
do
system("cls");
User.Display();
cin>>MainChoice;
if(cin&&MainChoice-(int)MainChoice==0&&(int)MainChoice>0&&MainChoice<10)
ChoiceChecker=true;
cout<<"ACCEPTED"<<endl;
else
cout<<"WRONG NUMBER"<<endl;
getch();
system("cls");
ChoiceChecker=false;
cin.clear();
cin.ignore(10000,'\n');
while(ChoiceChecker==false);
{
User.setUserRow(MainRows);
User.setUserColumn(MainColumns);
User.setUserChoice(MainChoice);
User.UserInsertion();
if(User.Moves()==0)
if(LivesInLevel==0)
else
break;
}
}