sudoko in c
TRANSCRIPT
-
8/7/2019 sudoko in c
1/54
#include
#include
#include
#include
#include
#include
extern short int user1=1,user2=0;
extern short int array[8][8]={{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,2,2,0,0,0},
{0,0,0,14,14,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}};
extern short int sp_color=0; // to create generic functions
extern short int current_yellow=2,current_green=2;
extern short int color=0;
extern short int color_back=0;
extern short int write=0; //to fill boxes during user profile execution
extern short int user1_validity_checked=0, user2_validity_checked=1;
//signals to check user's validity by condition(which avoid repeat calling
of big function in while loop)
extern short int gameover1=0,gameover2=0; // if both got 1 means, end the
-
8/7/2019 sudoko in c
2/54
game(consider total_poxes)
extern short int total_boxes=0; //to check whether all boxes
filled or not
extern short int print_box=0; // to exe fillbox only called bu user
profiles(to avoid printing of boxes while checking validity)
extern short int validity_enabler=0; //signal used to get validity of user
by checking it in by entering fillbox()
//single player game variables
extern short int dir_array[8]={0,0,0,0,0,0,0,0}; //0 to 7 represent
corresponding directions
//extern short int max_dir=0; //direction number which achieved
max value
extern short int max_pixel_i=0; //this i and j is to store pixel
postion at which max_dir have to applay at final
extern short int max_pixel_j=0;
extern short int max_value=0; //maximum value of the filled
pixels among all directionl algorithms
void get_max(int i, int j);
int select_box(int ,int );
void delay();
void backbox(int ,int );
void update_array();
void initial_setup();
void sound1();
void sound2();
-
8/7/2019 sudoko in c
3/54
//TO DRAW BOX AROUND CENRAL PIXEL
void setbox(int i,int j)
{
int x,y,count=0;
for(x=i-17; x
-
8/7/2019 sudoko in c
4/54
s1=(x-70)/35;
s2=(y-140)/35;
array[s1][s2]=c;
// printf("%d ",c );
}
}
void display_date()
{
// gotoxy(1,1);
gotoxy(10,3);
printf("
");
printf(" T H E G A M E O P P O S I T E
");
printf(" *************************************");
gotoxy(10,28);
printf("%s
",__DATE__);
printf(" %s",__TIME__);
gotoxy(55,27);
printf("By-
");
gotoxy(55,28);
printf("Mani kanta.C.V");
gotoxy(1,1);
}
-
8/7/2019 sudoko in c
5/54
void sound1()
{
int i;
for(i=700;i (140+245))
*j=140;
break;
-
8/7/2019 sudoko in c
6/54
case 100:
case 52: //left key
*i=*i-35;
if(*i < 70)
*i=70+245;
break;
case 103:
case 54: //right key
*i=*i+35;
if(*i > (70+245))
*i=70;
break;
case 114:
case 56: //up -key
*j=*j-35;
if(*j < 140)
*j=140+245;
break;
}
}
void whitebox(int i,int j)
{ int x,y;
-
8/7/2019 sudoko in c
7/54
for(x=i-17+2; x
-
8/7/2019 sudoko in c
8/54
for(y=0;y (140+245)) //boundary checking
{ write=0;
return(0);
}
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
int color1=array[s1][s2]; //checking for next blank box
if(color1==BLACK)
{
write=0;
return 0;
}
-
8/7/2019 sudoko in c
9/54
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
write=1;
checkdown(x,(y+35)); //recursive calling
}
if(write)
{
dir_array[0]=dir_array[0]+1; //one value get incremented per one
box fill
fillbox(x,y,color);
fillbox(x,y-35, color);
if(user1)
color_back=YELLOW; //back box color should be re-defined(if modified
anything means)
if(user2)
color_back=GREEN;
}
return(0);
}
-
8/7/2019 sudoko in c
10/54
int checkup(int x, int y)
{
if(y < 140) //boundary checking
{ write=0;
return(0);
}
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
int color1=array[s1][s2];; //checking for next blank box
if(color1==BLACK)
{
write=0;
return 0;
}
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
-
8/7/2019 sudoko in c
11/54
write=1;
checkup(x,(y-35)); //recursive calling
}
if(write)
{
dir_array[1]=dir_array[1]+1; //one value get incremented per one box
fill
fillbox(x,y,color);
fillbox(x,y+35, color);
if(user1)
color_back=YELLOW; //back box color should be re-defined(if modified
anything means)
if(user2)
color_back=GREEN;
}
return(0);
}
int checkleft(int x, int y)
{
if(x< 70) //boundary checking
{ write=0;
return(0);
}
-
8/7/2019 sudoko in c
12/54
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
int color1=array[s1][s2]; //checking for next blank box
if(color1==BLACK)
{
write=0;
return 0;
}
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
write=1;
checkleft((x-35),y); //recursive calling
}
if(write)
{
dir_array[2]=dir_array[2]+1; //one value get incremented per one box
fill
-
8/7/2019 sudoko in c
13/54
fillbox(x,y,color);
fillbox((x+35) ,y, color);
if(user1)
color_back=YELLOW; //back box color should be re-defined(if modified
anything means)
if(user2)
color_back=GREEN;
}
return(0);
}
int checkright(int x, int y)
{
if(x > (70+245)) //boundary checking
{ write=0;
return(0);
}
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
int color1=array[s1][s2]; //checking for next blank box
if(color1==BLACK)
{
-
8/7/2019 sudoko in c
14/54
write=0;
return 0;
}
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
write=1;
checkright((x+35),y); //recursive calling
}
if(write)
{
dir_array[3]=dir_array[3]+1; //one value get incremented per one
box fill
fillbox(x,y,color);
fillbox((x-35) ,y, color);
if(user1)
color_back=YELLOW; //back box color should be re-defined(if modified
anything means)
if(user2)
color_back=GREEN;
-
8/7/2019 sudoko in c
15/54
}
return(0);
}
int checkdia1(int x, int y) //starts from clock wise
{
if((x > (70+245)) || ( y < (140))) //boundary checking
{ write=0;
return(0);
}
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
int color1=array[s1][s2]; //checking for next blank box
if(color1==BLACK)
{
write=0;
return 0;
}
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
-
8/7/2019 sudoko in c
16/54
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
write=1;
checkdia1((x+35),(y-35)); //recursive calling
}
if(write)
{
dir_array[4]=dir_array[4]+1; //one value get incremented per one
box fill
fillbox(x,y,color);
fillbox((x-35) ,(y+35), color);
if(user1)
color_back=YELLOW; //back box color should be re-defined(if modified
anything means)
if(user2)
color_back=GREEN;
}
return(0);
}
int checkdia2(int x, int y)
-
8/7/2019 sudoko in c
17/54
{
if((x > (70+245)) || ( y > (140+245))) //boundary checking
{ write=0;
return(0);
}
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
int color1=array[s1][s2]; //checking for next blank box
if(color1==BLACK)
{
write=0;
return 0;
}
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
write=1;
checkdia2((x+35),(y+35)); //recursive calling
-
8/7/2019 sudoko in c
18/54
}
if(write)
{
dir_array[5]=dir_array[5]+1; //one value get incremented per one box
fill
fillbox(x,y,color);
fillbox((x-35) ,(y-35), color);
if(user1)
color_back=YELLOW; //back box color should be re-defined(if modified
anything means)
if(user2)
color_back=GREEN;
}
return(0);
}
int checkdia3(int x, int y)
{
if((x (140+245))) //boundary checking
{ write=0;
return(0);
}
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
-
8/7/2019 sudoko in c
19/54
int color1=array[s1][s2]; //checking for next blank box
if(color1==BLACK)
{
write=0;
return 0;
}
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
write=1;
checkdia3((x-35),(y+35)); //recursive calling
}
if(write)
{
dir_array[6]=dir_array[6]+1; //one value get incremented per one
box fill
fillbox(x,y,color);
fillbox((x+35) ,(y-35), color);
if(user1)
-
8/7/2019 sudoko in c
20/54
{color_back=YELLOW; //back box color should be re-defined(if modified
anything means)
}
if(user2)
{color_back=GREEN;
}
}
return(0);
}
int checkdia4(int x, int y)
{
if((x
-
8/7/2019 sudoko in c
21/54
return 0;
}
//reading next pixel,(1st time, it will be white)
if(color1==color) //color should be re-defined in user-profile
{
//if it contains no element in its sorroundings
return(0);
}
if(color1==sp_color)
{
write=1;
checkdia4((x-35),(y-35)); //recursive calling
}
if(write)
{
dir_array[7]=dir_array[7]+1; //one value get incremented per one box
fill
fillbox(x,y,color);
fillbox((x+35) ,(y+35), color);
if(user1)
{ color_back=YELLOW; //back box color should be re-defined(if
modified anything means)
//to check whether user1 striked even he is valid, by comparing
it with its previous value
}
-
8/7/2019 sudoko in c
22/54
if(user2)
{
color_back=GREEN;
}
}
return(0);
}
void check_all_dir(int x, int y, int print)
{
if(print==1) //in case of this function called by userprofiles,
then onlly print
print_box=1;
else print_box=0;
write=0;
update_array();
checkdown(x,(y+35));
checkup(x,(y-35));
checkleft((x-35), y);
checkright((x+35),y);
checkdia1(x+35, y-35);
checkdia2(x+35, y+35);
checkdia3(x-35, y+35);
checkdia4(x-35, y-35);
-
8/7/2019 sudoko in c
23/54
}
void update_array()
{
int i,j;
current_yellow=0; //reset values
current_green=0; // no use of previous yellow and green
for(i=0;i
-
8/7/2019 sudoko in c
24/54
validity_enabler=0;
update_array(); // no need, remove it later on
for(i=70; i
-
8/7/2019 sudoko in c
25/54
{ int i,j;
int col1,col2;
col1=color;
col2=sp_color;
color=GREEN;
sp_color=YELLOW;
write=0;
validity_enabler=0;
update_array(); // no need, remove it later on
for(i=70; i
-
8/7/2019 sudoko in c
26/54
sp_color=col2;
return(0);
}
//press esc to come out of the following loop
int select_box(int i, int j) //cursor block
{
int x,y,t1,t2;
char ch;
int gdriver=DETECT,gmode=0;
initgraph(&gdriver, &gmode, "c:\tc\bgi");
initial_setup();
gotoxy(0,0);
gotoxy(52,13);
printf("%d %d",current_yellow,current_green); //to display
initial score
gotoxy(0,0);
-
8/7/2019 sudoko in c
27/54
while(1)
{ x=i;
y=j;
char ch;
display_date();
color_back=getpixel(i,j);
update_array();
whitebox(i,j);
update_array();
//TO CHECK USER 1 VALIDITY
if((!user1_validity_checked) && user1) //if user1 validity not
yet checked
{ user1_validity_checked=1;
int color_back_retriver=color_back;
if(user1_validity())
{ user1=1;
user2=0;
gameover1=0;
gotoxy(0,0); //to display validity below score board
gotoxy(50,15);
printf(" play1");
gotoxy(0,0);
-
8/7/2019 sudoko in c
28/54
gotoxy(64,15);
printf(" ");
gotoxy(0,0);
}
else //disable user1 and eneble user2
{
user1=0;
user2=1;
gameover1=1;
user2_validity_checked=0;
gotoxy(0,0); //to display validity below score board
gotoxy(50,15);
printf(" NO ");
gotoxy(0,0);
}
color_back=color_back_retriver;
}
//TO CHECK USER2 VALIDITY
if((!user2_validity_checked) && user2)
{
int color_back_retriver=color_back;
user2_validity_checked=1;
-
8/7/2019 sudoko in c
29/54
if(user2_validity())
{
user2=1;
user1=0;
gameover2=0;
gotoxy(0,0); //to display validity below score board
gotoxy(64,15);
printf(" play2");
gotoxy(0,0);
gotoxy(50,15);
printf(" ");
gotoxy(0,0);;
}
else
{
gameover2=1;
user2=0;
user1=1;
user1_validity_checked=0;
//to display validity below score board
gotoxy(64,15);
printf(" NO ");
}
color_back=color_back_retriver;
-
8/7/2019 sudoko in c
30/54
}
if((gameover1==1) && (gameover2==1) || total_boxes==64 ||
current_yellow==0 || current_green==0)
{
update_array();
gotoxy(1,1);
if( current_yellow > current_green)
printf("
**************** USER1 IS THE
WINNER*************");
else if(current_yellow
-
8/7/2019 sudoko in c
31/54
**************** USER2 IS THE
WINNER*************");
else
printf("
-
8/7/2019 sudoko in c
32/54
************ M A T C H IS D R
A W *************");
printf("
**************** G A M E O V E R*************");
return(0);
}
ch=getch();
if(ch==27) break; //ESC butten action
if(ch==13 && user1) //if enter key pressed by user1
{ int yellow_p, yellow_c;
color=YELLOW;
sp_color=GREEN;
write=0;
update_array(); // update previous values of array
yellow_p=current_yellow;
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
if(array[s1][s2]==0) //if current box is empty
-
8/7/2019 sudoko in c
33/54
check_all_dir(x,y,1); // calls 8 directional alg
update_array(); //update rescent values of array
yellow_c=current_yellow;
if(yellow_c != yellow_p) //if user really strikes any box, then
only switch to yser2
{
user1=0;
user2=1;
user2_validity_checked=0;
sound1();
gotoxy(52,13);
printf("%d %d",current_yellow,current_green); //to
display initial score
}
}
if(ch==102 && user2) //if f key pressed by user2
{
int green_c, green_p;
color=GREEN;
sp_color=YELLOW;
-
8/7/2019 sudoko in c
34/54
write=0;
update_array();
green_p=current_green; //saved green value, bfore filling new
boxes
int s1,s2;
s1=(x-70)/435;
s2=(y-140)/35;
if(array[s1][s2]==0) //if current box is empty
check_all_dir(x,y,1);
update_array();
green_c=current_green; //new green values
if(green_c != green_p)
{
sound1();
user2=0;
user1=1;
user1_validity_checked=0;
gotoxy(52,13);
printf("%d %d",current_yellow,current_green); //to display
initial score
}
}
-
8/7/2019 sudoko in c
35/54
movement(&i,&j,ch); //movemwnts to the curser block
backbox(x,y); //fill box with its previous color
} //press arrow keys of NUM_PAD
return(0);
}
void initial()
{ print_box=1;
fillbox(175,245,2);
fillbox(210,245,2);
fillbox(175,280,14);
fillbox(210,280,14);
print_box=0;
}
void putbox() //which fixes central points of each box and call
function to draw the box
{
for(int i=70;i
-
8/7/2019 sudoko in c
36/54
}
void outline(int ch)
{
int x,y;
//starts from 51(70-17-2),121(140-17-2)
for(x=50; x
-
8/7/2019 sudoko in c
37/54
max_pixel_i=i;
max_pixel_j=j;
}
}
void print_computer()
{
int x,y;
update_array();
x=max_pixel_i;
y=max_pixel_j;
print_box=1;
color_back=GREEN;
check_all_dir(x,y,1);
}
int single_player_game()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver, &gmode, "c:\tc\bgi");
int i=70;
int j=140;
int x,y;
initial_setup();
-
8/7/2019 sudoko in c
38/54
gotoxy(0,0);
gotoxy(52,13);
printf("%d %d ",current_yellow,current_green); //to display
initial score
gotoxy(0,0);
while(1)
{ x=i;
y=j;
char ch;
display_date();
color_back=getpixel(i,j);
update_array();
whitebox(i,j);
update_array();
gotoxy(0,0);
gotoxy(52,13);
//TO CHECK USER 1 VALIDITY
if((!user1_validity_checked) && user1) //if user1 validity not
yet checked
{ user1_validity_checked=1;
int color_back_retriver=color_back;
if(user1_validity())
-
8/7/2019 sudoko in c
39/54
{ user1=1;
user2=0;
gameover1=0;
gotoxy(1,1); //to display validity below score board
gotoxy(50,15);
printf(" play1");
gotoxy(1,1);
gotoxy(64,15);
printf(" ");
gotoxy(1,1);
}
else //disable user1 and eneble user2
{
user1=0;
user2=1;
gameover1=1;
user2_validity_checked=0;
gotoxy(1,1); //to display validity below score board
gotoxy(50,15);
printf(" NO ");
gotoxy(1,1);
}
color_back=color_back_retriver;
}
-
8/7/2019 sudoko in c
40/54
ch=getch();
if(ch==27) break; //ESC butten action
if(ch==13 && user1) //if enter key pressed by user1
{ int yellow_p, yellow_c;
color=YELLOW;
sp_color=GREEN;
write=0;
update_array(); // update previous values of array
yellow_p=current_yellow;
int s1,s2;
s1=(x-70)/35;
s2=(y-140)/35;
if(array[s1][s2]==0) //if current box is empty
check_all_dir(x,y,1); // calls 8 directional alg
update_array(); //update rescent values of array
yellow_c=current_yellow;
if(yellow_c != yellow_p) //if user really strikes any box, then
only switch to yser2
{
-
8/7/2019 sudoko in c
41/54
sound1();
user1=0;
user2=1;
user2_validity_checked=0;
gotoxy(52,13);
printf("%d %d ",current_yellow,current_green);
//to display initial score
// printf("%d %d ",current_yellow,current_green);
}
}
gotoxy(0,0);
gotoxy(52,13);
//TO CHECK USER2 VALIDITY
if((!user2_validity_checked) && user2)
{
int color_back_retriver=color_back;
user2_validity_checked=1;
if(user2_validity())
{
user2=1;
user1=0;
gameover2=0;
-
8/7/2019 sudoko in c
42/54
//to display validity below score board
gotoxy(64,15);
printf("computer");
gotoxy(50,15);
printf(" ");
}
else
{
gameover2=1;
user2=0;
user1=1;
user1_validity_checked=0;
//to display validity below score board
gotoxy(64,15);
printf(" NO ");
}
color_back=color_back_retriver;
}
if((gameover1==1) && (gameover2==1) || total_boxes==64 ||
current_yellow==0 || current_green==0)
{
-
8/7/2019 sudoko in c
43/54
gotoxy(1,1);
update_array();
if( current_yellow > current_green)
printf("
**************** USER1 IS THE
WINNER*************");
else if(current_yellow
-
8/7/2019 sudoko in c
44/54
************ * COMPUTER IS THE
WINNER*************");
else
printf("
************ M A T C H IS D R
A W *************");
gotoxy(1,1);
-
8/7/2019 sudoko in c
45/54
printf("
**************** G A M E O V E
R*************");
return(0);
}
if(user2) //its the turn of computer
{
// int green_c, green_p;
int m,n;
int color_back_retriver=color_back;
-
8/7/2019 sudoko in c
46/54
color=GREEN;
sp_color=YELLOW;
max_value=0;
write=0;
update_array();
for(int t=0;t
-
8/7/2019 sudoko in c
47/54
delay(); //let the user1 see his results
delay();
print_computer(); //if comp(user2) valid means i wil print automatically
without
// user interaction, so not checking for pixel color upgradation of
array[][]
sound1();
color_back=color_back_retriver;
if(getpixel(i,j)==GREEN) //if white box interacts with newly
paved green boxes means, xtract back color and proceed
color_back=GREEN;
user2=0;
user1=1;
user1_validity_checked=0;
update_array();
gotoxy(52,13);
printf("%d %d ",current_yellow,current_green); //to
display initial score
// printf("%d %d ",current_yellow,current_green);
}
movement(&i,&j,ch); //movemwnts to the curser block
-
8/7/2019 sudoko in c
48/54
backbox(x,y); //fill box with its previous color
//press arrow keys of NUM_PAD
} //end of while loop
return(0);
}
void display_instr()
{
printf("This is the game already implemented in Nokia 2300 set
. you
are the user1 always and got to play using YELLOW color.");
printf("first is user1
turn, you have to put the white box at
appropriate place and have to press
ENTER(num pad) key to fill the
boxes with yellow,");
-
8/7/2019 sudoko in c
49/54
printf("so that your cursor current
position should be at empty box
and contains Green boxes in between your cursor
and another Yellow box,
that yellow box belongs to ");
printf("upward,downward,leftside,
rightside or any diagonal direction
to your cursor.
");
printf(" Same thing to user2 also, he got to play with Green
boxe's.
");
printf("USER1 CONTROL KEYS:
");
printf("*******************
");
printf("The directional keys in num-pad and enter key in num-pad
");
printf("
USER2 CONTROL KEYS:
");
printf("*********************
-
8/7/2019 sudoko in c
50/54
");
printf(" D = left movement
R = up-ward movement
G = right movement
V
= down-ward movement
F = Enter key
");
getch();
clrscr();
}
int preamble()
{ int options;
while(1)
{
printf("
SELECT YOUR OPTION
");
printf("******************
");
printf(" 1. Instructions
2. Single player
game(player2=computer)
-
8/7/2019 sudoko in c
51/54
");
printf(" 3. Two player game
");
printf(" 4. Exit from the game
");
scanf("%d",&options);
clrscr();
switch(options)
{
case 1:display_instr(); break;
case 2: single_player_game(); return(0);
case 3:select_box(70,140); return(0);
case 4:exit(0);
default: printf("please enter the valid choice
");
}
}
}
void initial_setup()
{
outline(RED);
-
8/7/2019 sudoko in c
52/54
-
8/7/2019 sudoko in c
53/54
Hi, this is Manikanta.c.v, completed B.E (TCE) at
Dr.Ambedkar Institute
Of Tehnology,Bangalore. ");
printf("if you have any comments regarding this game means
please
let me know through
-THANK YOU
");
getch();
clrscr();
}
main()
{
//int gdriver=DETECT,gmode=0;
clrscr();
welcome_note();
preamble(); //OPTIONS GIVEN MENU
-
8/7/2019 sudoko in c
54/54
getch();
closegraph();
// printf("GAME OVER");
return 0;
}