sudoku game

Status
Not open for further replies.

aniket.awati

I am the Legend.........
Hello friends,
I have devoloped a sudoku game using cpp. Please evaluate this
code. I have used Borland Turbo cpp 3.0 compiler and hence it is windows based and 16 bit.I know iI should shift to more advanced and open source devolopement enviornments, and I am going to do just that.
But it would be very nice of you to overlook this drawback and give me your suggestions on the logic in the code. I would also like to know about open source graphics libraries.

One more thing, initialy i wanted to code this thing using c language. But I couldn't use clock_t in 'c'.
Hence i changed it to 'cpp' just for that. As a result you may see printf and scanf used in 'cpp' coding.
Though it is not advisable, I can't help it as it is very time consuming to change it, now that the code has gone on to 800 lines.
 
Last edited:
OP
A

aniket.awati

I am the Legend.........
sorry if that was corrupted file.
Due to the size of the code i posted the archive. Be it then, let me post the whole code.
I have done many improvements on the code now. i.e. its solver gives average time for solution of any problem as 9 milli seconds or less. (I calculated average time by solving the same problem 100000 times and taking mean of the time!). And once it has solved worlds hardest sudoku puzzle (google if you want the puzzle), in 3 milliseconds. configuration:p4 256RAM

Code:
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<timer.h>
#include<string.h>
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
int problemmatrix[9][9];//global decl for storage of problem nos.
int answermatrix[9][9];
int promat[9][9];
int gd ,gm,x,y,fine=0;
int tagrow[9][9],tagcol[9][9],tagblock[9][9],flag[9][9];
clock_t start, end;//objects to store the clock ticks during the program
class record
{
	private:
		struct data
		{
			int sec;
			char name[30];
		}d;

		fstream file;

	public:
		record();
		void addrecord(int,int);
		void listrecord();
		int search(int);
		void clearrec();
}
record::record()
{
			file.open("rec.bin",ios::binary|ios::in|ios::out);
			if(!file)
			{
				cout<<endl<<"Unable to open file";
			}
}
void record::clearrec()
{
	file.close();
	remove("rec.bin");
	record();
}
void record::addrecord(int cnt,int sec)
{
	int cntg=0;
	file.seekg(0L,ios::beg);
	ofstream outfile;
	outfile.open("tmp.bin",ios::out);
	do
	{
		if(cntg==cnt)
		{
			cout<<"enter name";
			cin>>d.name;
			d.sec=sec;
			outfile.write((char*)&d,sizeof(d));
			cntg++;
			file.seekg(cnt*sizeof(d),ios::beg);
		}
		if(cntg>9)
			break;
		if(file.read((char*)&d,sizeof(d)))
			outfile.write((char*)&d,sizeof(d));
		else
			break;
		cntg++;
	}while(1);
	outfile.close();
	file.close();
	remove("rec.bin");
	rename("tmp.bin","rec.bin");
	file.open("rec.bin",ios::binary|ios::in|ios::out);
}
int record::search(int sec)
{
	file.seekg(0L,ios::beg);
	int cnt=0;
	while(file.read((char*)&d,sizeof(d)))
	{
		if(sec<d.sec)
			break;
		cnt++;
	}
	file.clear();
	return cnt;
}
void record::listrecord()
{
	file.seekg(0L,ios::beg);
	int i=1;
	char ar1[30];
	setcolor(3);
	settextstyle(3,0,1);
	int x=getmaxx()/2;
	int y=getmaxy()/2;
	while(file.read((char*)&d,sizeof(d)))
	{
		outtextxy(x/4,y/4+40*i,"Record#");
		sprintf(ar1,"%d",i);
		outtextxy(x/4+100,y/4+40*i,ar1);
		sprintf(ar1,"%s",d.name);
		outtextxy(x/4+170,y/4+40*i,ar1);
		sprintf(ar1,"%d",d.sec);
		outtextxy(x/4+300,y/4+40*i,ar1);
		i++;
	}
	file.clear();
}
//the function for checking validity of a number at the specified position
int check(int i,int j,int c)
{
	c--;
	if(tagrow[i][c]==-1)
		return 0;
	if(tagcol[c][j]==-1)
		return 0;
	if(tagblock[(i/3)*3+j/3][c]==-1)
		return 0;
	return 1;

}
//initialisation of arrays
void taginitialise()
{
	for(int k=0;k<9;k++)
	{
		for(int j=0;j<9;j++)
		{
			tagrow[k][j]=0;
			tagcol[k][j]=0;
			tagblock[k][j]=0;
		}
	}
}

void initialise()
{
	for(int k=0;k<9;k++)
	{
		for(int j=0;j<9;j++)
		{
			answermatrix[k][j]=0;     //array initialisation
			problemmatrix[k][j]=0;
			promat[k][j]=0;
			flag[k][j]=0;
		}
	}
}
void assign(int i,int j,int no)
{
	if(no==0)
		return;
	no--;
	tagrow[i][no]=-1;
	tagcol[no][j]=-1;
	tagblock[(i/3)*3+j/3][no]=-1;
}
//function that generates the numbers for the problem.
//array answermatrix[][] contains the generated numbers
//by trial and error method.
void generate()
{
	int i,j,k,l,ct,flg=0;
	char ar1[10];
	randomize();
	while(1)  //grand loop for trials.
	{
	initialise();
	taginitialise();
	for(l=0;l<9;l++)
	{
		while(1)
		{
		k=random(9); //any 9 sqr block is selected
		if(answermatrix[(k/3)*3][(k%3)*3]>0) //checks if the block is selcted beforehand.
			continue;
		else
			break;
		}
		for(i=(k/3)*3;i<((k/3)*3+3);i++)//loop that generates nos.
		{
			for(j=(k%3)*3;j<((k%3)*3+3);j++)
			{
				ct=0;
			while(1)
			{
				answermatrix[i][j]=random(10); //random no is assigned to particular position in the block.
				if(check(i,j,answermatrix[i][j])==0) //checks validity of no.
				{
					ct++;   //numbers are tried 100 times if it fails flg=1 and process is repeted.
					if(ct>100)
					{
						flg=1;
						break;
					}
					continue;
				}
				if(answermatrix[i][j]>0)
					break;
			}
			assign(i,j,answermatrix[i][j]);
			if(flg==1)        //flags to break loops.
				break;
			}
			if(flg==1)
				break;
		}
		if(flg==1)
			break;
	}
	if(flg==1)
	{
		flg=0; //flag reinitialisation and grand loop continues for another set of trial.
		continue;
	}
	else
		break; //successful attempt breaks the grand loop.
	}

}
//function to check if game has ended successfully.
int checkfinish()
{
	int i,j;
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		{
			if(problemmatrix[i][j]>0&&flag[i][j]==0)
				continue;
			else
				return 0;
		}
	}
	return 1;
}
//function to print clock on the o/p screen
void printclock(int fine)
{
	int h,m,s;
	//conversion  of clock ticks to hh:mm:ss
	h=(end - start+fine) / (CLK_TCK*3600);
	m=((end - start+fine) / (CLK_TCK*60))-60*h;
	s=((end - start+fine) / CLK_TCK)-3600*h-60*m;
	gotoxy(60,2);
	if(s<10)
		printf("%d:%d:0%d\n",h,m,s);
	else
		printf("%d:%d:%d\n",h,m,s);
}
//function to print instruction file.
void instructionfile()
{
	char ch;
	FILE *fp;
	fp=fopen("ins.txt","r");
	gotoxy(0,0);
	if(fp==NULL)
		return;
	while(1)
	{
		ch=fgetc(fp);
		if(ch==EOF)
			break;
		printf("%c",ch);
	}
	fclose(fp);
	printf("\n\n\nPress any key to go back to Main Menu");
	getch();
	return;
}
//bullish method of solution. It solves the sudoku board by brute force algorithm.
void bullish()
{
	int i,j,bflg=0,ct=0,no;
	char ar1[10];
	int possible[9];
	randomize();
	void logical();
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
			promat[i][j]=problemmatrix[i][j];
	}
	while(1)
	{
		taginitialise();
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
			{
				assign(i,j,promat[i][j]);
				problemmatrix[i][j]=promat[i][j];
			}
		}
	for(i=0;i<9;i++)//loop that generates nos.
	{
		for(j=0;j<9;j++)
		{
			bflg=0;
			if(problemmatrix[i][j]>0)
				continue;
			for(no=0;no<9;no++)
				possible[no]=0;
			no=1;
			ct=0;
			while(1)
			{
				if(check(i,j,no))
				{
					possible[ct]=no;
					ct++;
				}
				no++;
				if(no>9)
					break;
			}
			for(no=1;no<100;no++)
			{
			ct=possible[random(9)];
			if(ct==0)
				continue;
			if(check(i,j,ct)==1)
					break;
			}
			if(no==100)
				bflg=1;
			if(bflg==1)
				break;
			problemmatrix[i][j]=ct;
			assign(i,j,ct);
			logical();
		}
		if(bflg==1)
			break;
	}
	if(bflg==1)
		continue;
	else
		break;
	}

}
// solves the sudoku problem by logical method.
void logical()
{
	int i,j,k,l,row,col,cnt=0,nflg=0,lflg=0;
	while(1)
	{
	lflg=0;
	for(k=0;k<9;k++)
	{
		for(l=1;l<10;l++)
		{
		cnt=0;
		nflg=0;
		for(i=(k/3)*3;i<((k/3)*3+3);i++)
		{
			for(j=(k%3)*3;j<((k%3)*3+3);j++)
			{
				if(problemmatrix[i][j]>0)
					continue;
				if(check(i,j,l))
				{
					row=i;
					col=j;
					cnt++;
				}
				if(cnt>1)
				{
					nflg=1;
					break;
				}
			}
			if(nflg==1)
				break;
		}
		if(cnt==1)
		{
			problemmatrix[row][col]=l;
			assign(row,col,l);
			lflg=1;
		}
		}
	}
	for(i=0;i<9;i++)
	{
		for(l=1;l<9;l++)
		{
		cnt=0;
		for(j=0;j<9;j++)
		{
			if(problemmatrix[i][j]>0)
					continue;
			if(check(i,j,l))
			{
					row=i;
					col=j;
					cnt++;
			}
			if(cnt>1)
			{
				nflg=1;
				break;
			}
		}
		if(cnt==1)
		{
			problemmatrix[row][col]=l;
			assign(row,col,l);
			lflg=1;
		}
		}
	}
	for(i=0;i<9;i++)
	{
		for(l=1;l<9;l++)
		{
		cnt=0;
		for(j=0;j<9;j++)
		{
			if(problemmatrix[j][i]>0)
					continue;
			if(check(j,i,l))
			{
					row=j;
					col=i;
					cnt++;
			}
			if(cnt>1)
			{
					nflg=1;
					break;
			}
		}
		if(cnt==1)
		{
			problemmatrix[row][col]=l;
			assign(row,col,l);
			lflg=1;
		}
		}
	}
	if(lflg==0)
		break;
	}
}
void average()
{
	float x=0;
	for(int i=0;i<9;i++)
	{
		for(int j=0;j<9;j++)
			promat[i][j]=problemmatrix[i][j];
	}
	for(i=0;i<10000;i++)
	{
		Timer p;
		p.reset();
		for(int k=0;k<9;k++)
		{
			for(int j=0;j<9;j++)
				problemmatrix[k][j]=promat[k][j];
		}
		p.start();
		logical();
		bullish();
		p.stop();
		x+=p.time();
	}
	x/=10000;
	gotoxy(60,2);
	printf("%f",x);
	getch();
}
// interface of sudoku solver.
void solver(int x,int y)
{
	int i,j,a,b,c;
	char ch;
	char ar1[10];
	setcolor(1);
	i=0;
	j=0;
	cleardevice();
	initialise();
	taginitialise();
	while(i<10) //platform of the game is printed on screen.
	{
		line(x-180+40*i,y-180,x-180+40*i,y+180);
		line(x-180,y-180+40*i,x+180,y-180+40*i);
		if(i==3||i==6)
		{
			line(x-180+40*i+2,y-180,x-180+40*i+2,y+180);
			line(x-180,y-180+40*i+2,x+180,y-180+40*i+2);
		}
		i++;
	}
	setfillstyle(SOLID_FILL,WHITE);
	setcolor(10);
	i=0;
	j=0;
	line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
	line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);
	while(1)//loop to control the input and cursir movement.
	{
		setfillstyle(SOLID_FILL,WHITE);
		ch=getch();  //input is stored in ch.
		if(ch==13)
			break;
		if(ch=='a')
		{
			average();
			return;
		}
		if(ch==83)  //delet operation.
		{
			if(promat[j][i]>0)
				continue;
			a=problemmatrix[j][i]-1;
			sprintf(ar1,"%d",(a+1));
			setcolor(15);
			tagrow[j][a]=0;
			tagcol[a][i]=0;
			tagblock[(j/3)*3+(i/3)][a]=0;
			problemmatrix[j][i]=0;
			outtextxy(x-160+40*i,y-160+40*j,ar1);
			sound(250);
			delay(100);
			sound(450);
			delay(100);
			sound(350);
			delay(100);
			sound(450);
			delay(100);
			sound(350);
			delay(100);
			nosound();
			continue;
		}
		if(ch>48&&ch<58)  //number identification.
		{
			if(problemmatrix[j][i]>0)
			{
				sound(500);
				delay(100);
				nosound();
				continue;
			}
			sprintf(ar1,"%c",ch);
			c=ch-48;
			if(check(j,i,c)==0)
			{
				problemmatrix[j][i]=0;
				flag[j][i]=-1;
				sound(500);
				delay(200);
				nosound();
				continue;
			}
			else
				setcolor(7);
			outtextxy(x-160+40*i,y-160+40*j,ar1);
			problemmatrix[j][i]=c;
			assign(j,i,c);
			flag[j][i]=0;
			continue;
		}
		c=0;
		a=i;
		b=j;
		setcolor(15);
		line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
		line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);
		if(ch==27)
			return;
		//right left up down cursor movements by d,a,w,s.
		if(ch==75)
			i--;
		else if(ch==77)
			i++;
		else if(ch==72)
			j--;
		else if(ch==80)
			j++;
		if(i<0||i>8||j<0||j>8) //moving limit for cursor.
		{
			i=a;
			j=b;
			setcolor(10);
			line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
			line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);
			continue;
		}
		setfillstyle(SOLID_FILL,WHITE);
		setcolor(10);
		line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
		line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);

	}
	taginitialise();
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		{
			answermatrix[i][j]=problemmatrix[i][j];
			assign(i,j,problemmatrix[i][j]);
		}
	}
	Timer p;
	p.reset();
	p.start();
	logical();
	bullish();
	p.stop();
	gotoxy(60,2);
	printf("%lf",p.time());
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		{
			if(problemmatrix[i][j]==answermatrix[i][j])
				continue;
			sprintf(ar1,"%d",problemmatrix[i][j]);
			setfillstyle(SOLID_FILL,BLACK);
			setcolor(3);
			outtextxy(x-160+40*j,y-160+40*i,ar1);
		}
	}
	getch();
	return;
}
void firstwindow(int x, int y)
{
	setbkcolor(15);
	setcolor(1);
	settextstyle(4,0,10);
	outtextxy(x-300,y-100,"SUDOKU");
	settextstyle(1,0,4);
	setcolor(4);
	outtextxy(x-50,y+50,"BY ANIKET AWATI");
	delay(1000);
	settextstyle(0,0,0);
	cleardevice();
	return;
}

void record1(int x,int y,int sec)
{
	cleardevice();
	setbkcolor(15);
	settextstyle(1,0,4);
	setcolor(4);
	outtextxy(x-x/2,y/2,"CONGRATULATIONS !!!");
	outtextxy(x-x/4,y/2+50,"YOU WIN");
	getch();
	cleardevice();
	setbkcolor(15);
	settextstyle(1,0,4);
	setcolor(4);
	record p;
	int cnt=p.search(sec);
	if(cnt<10)
	{
		p.addrecord(cnt,sec);
		p.listrecord();
	}
	getch();
	return ;
}
void rec(int x,int y)
{
	record p;
	while(1) //loop for options window.
	{
		int i=0;
		cleardevice();
		setbkcolor(15);
		setfillstyle(SOLID_FILL,BLACK);
		setcolor(4);
		settextstyle(0,0,2);
		outtextxy(x/2+40,5*y/8,"Show Records");
		outtextxy(x/2+40,7*y/8,"Delet All Records");
		outtextxy(x/2+40,9*y/8,"Back");
		line(x/2,y/2,3*x/2,y/2);
		line(x/2,y/2,x/2,3*y/4);
		line(x/2,3*y/4,3*x/2,3*y/4);
		line(3*x/2,y/2,3*x/2,3*y/4);
		while(1)
		{
			char ch=getch();
			if(ch==13)
			{
				if(i==0)
				{
					cleardevice();
					p.listrecord();
					getch();
					break;
				}
				else if(i==1)
				{
					p.clearrec();
					cleardevice();
					setbkcolor(15);
					setfillstyle(SOLID_FILL,BLACK);
					setcolor(4);
					settextstyle(0,0,3);
					outtextxy(x/2,y-60,"Records Cleared");
					settextstyle(0,0,1);
					outtextxy(x/2+260,400,"Press any key to continue...");
					getch();
					break;
				}
				else
					return;
			}
			if(ch==72)    //up movement of selection box
			{
					setcolor(15);
					line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
					line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
					line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
					line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
					if(i==0)
						i=3;
					i--;
					setcolor(2);
					line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
					line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
					line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
					line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
					continue;
				}
				if(ch==80)   //down movement of selection box
				{
						setcolor(15);
						line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
						line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
						line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
						line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
						if(i==2)
							i=-1;
						i++;
						setcolor(2);
						line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
						line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
						line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
						line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
					continue;
				}
			}
		}
}


void main()
{
	int gd ,gm,x,y,errorcode,i=0,j=0;
	int a,b,c,col,flg=0;
	char ar1[10],ch;
	errorcode = graphresult();
	if (errorcode != grOk)      /* an error occurred */
	{
		   printf("Graphics error: %s\n", grapherrormsg(errorcode));
		   printf("Press any key to halt:");
		   getch();
		   return;                 /* terminate with an error code */
	}
	detectgraph(&gd,&gm);            //graphics initialisations.
	initgraph(&gd,&gm,"D:\New Folder\TURBOC3\TC\BGI");
	x=getmaxx()/2;
	y=getmaxy()/2;
	//starting window
	firstwindow(x,y);
	//grand loop for restart of the game.
	while(1)
	{
		while(1) //loop for options window.
		{
			i=0;
			cleardevice();
			setbkcolor(15);
			setfillstyle(SOLID_FILL,BLACK);
			setcolor(4);
			settextstyle(0,0,2);
			if(flg==1)
			{
				outtextxy(x/2+60,3*y/8,"Resume");
				j=-1;
			}
		//options in the window.
		outtextxy(x/2+60,5*y/8,"New Game");
		outtextxy(x/2+60,7*y/8,"Solver");
		outtextxy(x/2+60,9*y/8,"Instructions");
		outtextxy(x/2+60,11*y/8,"Records");
		outtextxy(x/2+60,13*y/8,"Quit");
		settextstyle(0,0,0);
		setcolor(2);
		//draws	the selection box.
		line(x/2,y/2,3*x/2,y/2);
		line(x/2,y/2,x/2,3*y/4);
		line(x/2,3*y/4,3*x/2,3*y/4);
		line(3*x/2,y/2,3*x/2,3*y/4);
		int breakflg=0;	//for option selection
		while(1)
		{
			ch=getch(); //gets input from keyboard
			if(ch==13)
			{
				if(i==0)
				{
					flg=0;    //starts new game
				}
				else if(i==2)
				{
					cleardevice();
					instructionfile();   //prints instructions
					break;
				}
				else if(i==4)
					return; //quit game
				else if(i==3)
				{
					rec(x,y);
					break;
				}
				else if(i==1)
				{
					initialise();
					solver(x,y);  //sudoku solver
					flg=2;
				}
				breakflg=1;
				break;
				}
				if(ch==72)    //up movement of selection box
				{
					if(flg==1)   //flg=1 indicates game is paused
					{
						if(i==j)
						continue;
					}
					setcolor(15);
					line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
					line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
					line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
					line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
					if(i==0)
						i=5;
					i--;
					setcolor(2);
					line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
					line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
					line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
					line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
					continue;
				}
				if(ch==80)   //down movement of selection box
				{
						setcolor(15);
						line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
						line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
						line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
						line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
						if(i==4)
							i=-1;
						i++;
						setcolor(2);
						line(x/2,y/2+i*y/4,3*x/2,y/2+i*y/4);
						line(x/2,y/2+i*y/4,x/2,3*y/4+i*y/4);
						line(x/2,3*y/4+i*y/4,3*x/2,3*y/4+i*y/4);
						line(3*x/2,y/2+i*y/4,3*x/2,3*y/4+i*y/4);
					continue;
				}
			}
			if(flg==2)
				continue;
			if(breakflg==1)
				break;
		}
		cleardevice();
		x=getmaxx()/2;
		y=getmaxy()/2;
		setbkcolor(15);
		setfillstyle(SOLID_FILL,WHITE);
		setcolor(11);
		if(flg!=1)      //if game is paused it doesn't generate numbers
			generate();  //answer set is generated in problemmatrixay answermatrix[].
		setbkcolor(15);
		if(flg==1)
		{
			for(i=0;i<9;i++)
			{
				for(j=0;j<9;j++)
				{
				       if(problemmatrix[i][j]>0)
				       {
						sprintf(ar1,"%d",problemmatrix[i][j]);
						setfillstyle(SOLID_FILL,BLACK);
						if(promat[i][j]==problemmatrix[i][j])
							setcolor(7);
						else
							setcolor(3);
						outtextxy(x-160+40*j,y-160+40*i,ar1);
				       }
				       else
						continue;
				}
			}
		}
		else
		{
		while(1) // loop to ensure puzzle has unique solution
		{
			for(i=0;i<9;i++) //initialisation
			{
				for(j=0;j<9;j++)
				{
					promat[i][j]=0;
					problemmatrix[i][j]=0;
				}
			}
			taginitialise();
			for(c=0;c<(21+random(15));c++)
			{
				while(1)
				{
					i=random(9);
					j=random(9);
					if(problemmatrix[i][j]>0)
						continue;
					else
						break;
				}
				problemmatrix[i][j]=answermatrix[i][j];// selected numbers answermatrixe copied to problem matrix problemmatrix.
				promat[i][j]=problemmatrix[i][j];
				assign(i,j,promat[i][j]);
			}
			logical();  //solver checks the solution
			if(checkfinish())   // checks if problem is solved completely
			{
				for(i=0;i<9;i++)
				{
					for(j=0;j<9;j++)
					{
						problemmatrix[i][j]=promat[i][j];
						if(problemmatrix[i][j]>0)
						{
							sprintf(ar1,"%d",answermatrix[i][j]);
							setfillstyle(SOLID_FILL,BLACK);
							setcolor(7);
							outtextxy(x-160+40*j,y-160+40*i,ar1);
						}
					}
				}
				break;
			}
		}
	}
	setcolor(1);
	i=0;
	j=0;
	while(i<10) //platform of the game is printed on screen.
	{
		line(x-180+40*i,y-180,x-180+40*i,y+180);
		line(x-180,y-180+40*i,x+180,y-180+40*i);
		if(i==3||i==6)
		{
			line(x-180+40*i+2,y-180,x-180+40*i+2,y+180);
			line(x-180,y-180+40*i+2,x+180,y-180+40*i+2);
		}
		i++;
	}
	setfillstyle(SOLID_FILL,WHITE);
	setcolor(10);
	i=0;
	j=0;
	col=3;
	// draws section box
	line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
	line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);
	if(flg!=1)
		start=clock();
	flg=0;
	fine=0;
	while(1)//loop to control the input and cursir movement.
	{
		while(!kbhit()) //refreshes clock untill keystroke is generated
		{
			end=clock();
			gotoxy(60,2);
			printclock(fine);
		}
		setfillstyle(SOLID_FILL,WHITE);
		ch=getch();  //input is stored in ch.
		if(ch=='m')  //marking i/p that changes color of i/p for trial and error process of solving game.
		{
			col=2;
			continue;
		}
		if(ch=='u')   //unmarking.
		{
			col=3;
			continue;
		}
		if(ch==83)  //delet operation.
		{
			if(promat[j][i]>0)
				continue;
			sprintf(ar1,"%d",problemmatrix[j][i]);
			setcolor(15);
			tagrow[j][problemmatrix[j][i]]=0;
			tagcol[problemmatrix[j][i]][i]=0;
			tagblock[(j/3)*3+i/3][problemmatrix[j][i]]=0;
			problemmatrix[j][i]=0;
			outtextxy(x-160+40*i,y-160+40*j,ar1);
			setcolor(col);
			continue;
		}
		if(ch>48&&ch<58)  //number identification.
		{
			if(problemmatrix[j][i]>0)
			{
				sound(500);
				delay(100);
				nosound();
				continue;
			}
			sprintf(ar1,"%c",ch);
			c=ch-48;
			problemmatrix[j][i]=c;
			if(check(j,i,c)==0)
			{
				setcolor(RED);
				sound(500);
				delay(200);
				nosound();
			}
			else
				setcolor(col);
			assign(j,i,c);
			outtextxy(x-160+40*i,y-160+40*j,ar1);
			if(checkfinish()==1)//checks if problem is solved
			{
				flg=2;
				record1(x,y,(end-start+fine)/CLK_TCK);
				break;
			}
			continue;
		}
		if(ch=='h') //hint.actualy it displays directly the answer no at that postions.of cource not to be used.
		{
			if(problemmatrix[j][i]>0)
				continue;
			problemmatrix[j][i]=answermatrix[j][i];
			assign(j,i,problemmatrix[j][i]);
			sprintf(ar1,"%d",problemmatrix[j][i]);
			setcolor(10);
			outtextxy(x-160+40*i,y-160+40*j,ar1);
			setcolor(col);
			fine+=30*CLK_TCK;
			if(checkfinish()==1)
			{
				flg=2;
				record1(x,y,(end-start+fine)/CLK_TCK);
				break;
			}
			continue;
		}
		c=0;
		a=i;
		b=j;
		setcolor(15);
		line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
		line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);
		if(ch==27)  //pauses the game
		{
			flg=1;
			break;
		}
		//right left up down cursor movements by d,a,w,s.
		if(ch==75)
			i--;
		else if(ch==77)
			i++;
		else if(ch==72)
			j--;
		else if(ch==80)
			j++;
		if(i<0||i>8||j<0||j>8) //moving limit for cursor.
		{
			i=a;
			j=b;
			setcolor(10);
			line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
			line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);
			continue;
		}
		setfillstyle(SOLID_FILL,WHITE);
		setcolor(10);
		line(x-177+40*i,y-177+40*j,x-177+40*i,y-143+40*j);
		line(x-143+40*i,y-177+40*j,x-143+40*i,y-143+40*j);
	}
	if(flg==1)
		continue;
	if(flg==2)
	{
		flg=0;
		continue;
	}
	}
}
 

Faun

Wahahaha~!
Staff member
sorry if that was corrupted file.
Due to the size of the code i posted the archive.
everything in attachment gets corrupt in this forum:rolleyes:, some admin did a spell on it, spell went wrong, things got screwed :D

:!:Please bear with the inconvenience:smile:
 
OP
A

aniket.awati

I am the Legend.........
lol really. And thats certainly true. I have tried it many times and it DOES get corrupted.
I have said it once. I say it again. Please convert this to a ISO C++ compatible program.
I know, but it is not really a simple thing to change whole graphics in it using some new graphics library.
I am planning to do this in java. Lets see how it goes.
One more thing, if you run the program on instructions option you will see nothing. So if you create a text file ins.txt and write instructions you will see those instructions. My file is like this:

INSTRUCTIONS

1. To move the cursor use arrow keys.

2. Use 'delete' key to delete entry.

3. Use 'm' to mark entries for trial and error method.

4. Use 'u' to unmark entries.

5. Use 'h' for hint.

6. For every hint '40 seconds' are added to the time.

7. Use 'Esc' to quit.
 
Last edited:
Status
Not open for further replies.
Top Bottom