0% found this document useful (0 votes)
5 views

Code

The document describes a C++ Sudoku game program. It includes a Sodoku class with methods for generating a random starting puzzle board, allowing user input and checking if it is valid, tracking lives, and checking rows, columns and 3x3 boxes for validity. The class has arrays to store the puzzle board and randomly generated numbers, and methods for initialization, display, input handling and win checking.

Uploaded by

MASTER JII
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Code

The document describes a C++ Sudoku game program. It includes a Sodoku class with methods for generating a random starting puzzle board, allowing user input and checking if it is valid, tracking lives, and checking rows, columns and 3x3 boxes for validity. The class has arrays to store the puzzle board and randomly generated numbers, and methods for initialization, display, input handling and win checking.

Uploaded by

MASTER JII
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Code:

#include <iostream>

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

#include <time.h>

#include <windows.h>

using namespace std;

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}, // THE LEVEL THAT NEEDS TO BE FILLED

{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;

cout<<"PRESS ANY KEY TO CONYINUE"<<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--------------

void Sodoku::setUserRow(int RowFromMain)

this->UserRows=RowFromMain-1;

void Sodoku::setUserColumn(int ColumnFromMain)

this->UserColumns=ColumnFromMain-1;

void Sodoku::setUserChoice(int ChoiceFromMain)

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

{ // YOU MUST STUDY SODOKU MATH

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;

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

}
//----------CheckerFunctionForBox---------

bool Sodoku::InsertionCheckerMatrix(int row ,int column , int number)

bool checker;

if((row>=0&&row<=2)&&(column>=0&&column<=2)) //THIS IF CHECKS THE FIRST BOX OF THE


GAMEBOARD

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;

else if((row>=0&&row<=2)&&(column>=6&&column<=8)) //THIS IF CHECKS THE THIRD BOX OF THE


GAMEBOARD

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;

else if((row>2&&row<6)&&(column>=0&&column<3)) //THIS IF CHECKS THE FOURTH BOX OF THE


GAMEBOARD

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;

else if((row>2&&row<6)&&(column>2&&column<6)) //THIS IF CHECKS THE FIFTH BOX OF THE


GAMEBOARD

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;

else if((row>2&&row<6)&&(column>5&&column<9)) //THIS IF CHECKS THE SIXTH BOX OF THE


GAMEBOARD
{

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;

else if((row>5&&row<9)&&(column>-1&&column<3)) //THIS IF CHECKS THE SEVENTH BOX OF THE


GAMEBOARD

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;

else if((row>5&&row<9)&&(column>2&&column<6)) //THIS IF CHECKS THE EIGHT BOX OF THE


GAMEBOARD

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--------

void Sodoku::RandomArrayInsertion() //CREATES A RANDOM ARRAY FROM WHICH LEVELINSERTION IS


DONE

int store;

for(int w=0;w<9;)

store=RandGenerator();

if(ArrayChecker(store))

storeAraay[w]=store;

++w;

else

continue;

}
//------------RandomNumberArrayChecker-------------

bool Sodoku::ArrayChecker(int randomNumberGenerated) //CHECKS THAT THE NUMBER SHOULD NOT


BE REPEAATED IN RANDOM ARRAY GENERATION

bool checker;

for(int q=0;q<9;q++)

if(randomNumberGenerated!=storeAraay[q])

checker=true;

else

checker=false;

break;

return checker;

//------------RandomNumberGenerator----------------

int Sodoku::RandGenerator() //GENERATES RANDOM NUMBER FOR THE ARRAY

return rand()%9+1;

//------------DisplayFunction----------------------
void Sodoku::Display()

cout<<"-----------Your Level----------"<<endl;

cout<<"YOU REMAINING MOVES: "<<Moves()<<endl;

cout<<"THE NUMBER OF LIVES: ";

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)

cout<<"| "<<Level[i][j]<<" ";

else if(j==0||j==3)

cout<<"| "<<Level[i][j]<<" ";

else

cout<<" "<<Level[i][j]<<" ";

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<<" WELCOME TO SODOKU BY JANNAT IRFAN"<<endl;

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

cout<<"WRONG VALUE MUST BE S "<<endl;

cout<<"press any key to cintinue"<<endl;

getch();

system("cls");

SystemChecker=false;

cin.clear();

cin.ignore(10000,'\n');

}
}

while(SystemChecker==false);

while(SystemChoice=="s"||SystemChoice=="S")

do

cout<<"PRESS 1 TO PLAY THE GAME"<<endl<<"PRESS 2 TO EXIT"<<endl;

cin>>LevelChoice;

if(LevelChoice.length()==(1&&LevelChoice=="1")||(LevelChoice=="2"))

cout<<"ACCEPTED"<<endl;

LevelChecker=true;

else

cout<<"WRONG MUST BE 1 OR 2"<<endl;

cout<<"press any key to cintinue"<<endl;

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;

cout<<"press any key to cintinue"<<endl;

getch();

system("cls");

RowChecker=false;

cin.clear();

cin.ignore(10000,'\n');

}
}

while(RowChecker==false);

do

system("cls");

User.Display();

cout<<"ENTER THE COLUMN IN WHICH YOU WANT TO ADD NUMBER(between 0 and


10(0,10 exluded))"<<endl;

cin>>MainColumns;

if(cin&&MainColumns-
(int)MainColumns==0&&(int)MainColumns>0&&MainColumns<10)

ColumnChecker=true;

cout<<"ACCEPTED"<<endl;

else

cout<<"WRONG NUMBER"<<endl;

cout<<"press any key to cintinue"<<endl;

getch();

system("cls");

ColumnChecker=false;

cin.clear();

cin.ignore(10000,'\n');

}
}

while(ColumnChecker==false);

do

system("cls");

User.Display();

cout<<"ENTER YOUR CHOICE( must be a number b/w 0 to 10s )"<<endl;

cin>>MainChoice;

if(cin&&MainChoice-(int)MainChoice==0&&(int)MainChoice>0&&MainChoice<10)

ChoiceChecker=true;

cout<<"ACCEPTED"<<endl;

else

cout<<"WRONG NUMBER"<<endl;

cout<<"press any key to cintinue"<<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)

cout<<"CONGRATULATION YOU WON"<<endl;

if(LivesInLevel==0)

cout<<"THE LIVES HAVE ENDED"<<endl;

else

break;

}
}

You might also like