Sudoku: The game. My first game.

Status
Not open for further replies.

sujithtom

Ambassador of Buzz
Hi guys. I made a game of Sudoku in C. It is my first working game :D and i made it for my school exhibition. I got the idea from someone eho made Sudoku in Java and posted here. Whoever he is thanks for the help he unknowingly had done to me ;-)

Link to the exe for Sudoku the game: RapidShare

Note: There are some problems with the game currently since it is not complete.

The first problem is that it does not check whether any numver repeats in 3x3 boxes but i will make sure i will rectify this soon.

Second is that it does not display You Won after winning.

Rest probs you find it urselves ;-)

Waiting for your feedback.
 
OP
sujithtom

sujithtom

Ambassador of Buzz
Opensource Yeah this is the sole reason i posted this here. Yeah this program is opensource and is completely made up of C. It does not comply with the latest ANISI standars but i hope it will do for now.
Source

Code:
/*This program has been licensed under GNU License Agreement. You must comply with it if you want to use this code or modify it.*/
 #include "graphics.h"
 #include "conio.h"
#include "dos.h"
#include "process.h"
#include "stdio.h"
   int s[9][9]={
			{7,9,0,0,0,8,0,5,2},
			{1,0,3,6,0,2,8,0,7},
			{0,0,0,0,0,0,0,0,0},
			{0,0,1,9,0,6,5,0,0},
			{0,7,0,5,0,4,0,1,0},
			{0,0,2,3,0,1,4,0,0},
			{0,0,0,0,0,0,0,0,0},
			{9,0,8,1,0,3,7,0,5},
			{5,3,0,0,0,0,0,6,8}
		};
   int sback[9][9]={
			{7,9,0,0,0,8,0,5,2},
			{1,0,3,6,0,2,8,0,7},
			{0,0,0,0,0,0,0,0,0},
			{0,0,1,9,0,6,5,0,0},
			{0,7,0,5,0,4,0,1,0},
			{0,0,2,3,0,1,4,0,0},
			{0,0,0,0,0,0,0,0,0},
			{9,0,8,1,0,3,7,0,5},
			{5,3,0,0,0,0,0,6,8}
		};
    int loop,flag=0,posx=20,posy=50,arrayx=0,arrayy=0;
main()
  {
   int gd=DETECT,gm,key;

/*Replace the path below with the path in which BGI files lie in your computer*/
  initgraph (&gd,&gm,"c:\\tc\\bgi");
printf ("How to Play Sudoku");
printf ("\n--------------------------------\n");
printf ("\n[Press any key to skip and start the game]\n");
printf ("\nFill the boxes with digits in such a manner that every column and every row and every 3x3 box accommodates the digits 1 to 9, without repeating any.");
  getch();
  clearviewport();
  intro();
  clearviewport();
  screen();
  key=getkey();
  while (key!=1)
  {
   switch (key)
   {
    case 72:up();break;
    case 80:down();break;
    case 75:left();break;
    case 77:right();break;
    case 11:zero();break;
    case 10:nine();break;
    case 9:eight();break;
    case 8:seven();break;
    case 7:six();break;
    case 6:five();break;
    case 5:four();break;
    case 4:three();break;
    case 3:two();break;
    case 2:one();break;
    case 1:exit(0);
   }
   key=0;
   key=getkey();
  }
}

  intro()
{
   setcolor (WHITE);
   rectangle (5,5,630,475);
   settextstyle (8,HORIZ_DIR,9);
   outtextxy (125,180,"su|do|ku");
   settextstyle (2,HORIZ_DIR,6);
   outtextxy (120,440,"Loading Complete, Press any key to play!");
   getch();
  }

screen()
{
   int j,h,x=40,y=70,temp,i;
   settextstyle (8,HORIZ_DIR,4);
   outtextxy (0,0,"su|do|ku");
   rectangle (20,50,380,410);
   for (i=20;i<=380;i=i+40)
   line (i,50,i,410);
   for (i=50;i<=410;i=i+40)
   line (20,i,380,i);
   setlinestyle (0,0,3);
   for (i=20;i<=380;i=i+120)
   line (i,50,i,410);
   for (i=50;i<=410;i=i+120)
   line (20,i,380,i);
   setlinestyle (0,0,0);
   rectangle (405,50,620,410);
   rectangle (20,420,620,470);
   settextstyle (2,HORIZ_DIR,8);
   outtextxy (435,50,"Instructions");
   settextstyle (2,HORIZ_DIR,5);
   outtextxy (410,80,"> Use arrow keys to");
   outtextxy (410,95,"  navigate through boxes");
   outtextxy (410,110,"> To select a box press the");
   outtextxy (410,125,"  Enter key");
   outtextxy (410,140,"> The currently selected");
   outtextxy (410,155," box is marked in red color");
   outtextxy (410,170,"> Press a number to fill");
   outtextxy (410,185," the box with that number");
   outtextxy (410,200,"> Press Esc to exit");
   setcolor (RED);
   rectangle (20,50,60,90);
   for (j=0;j<9;j++)
   {
     for (h=0;h<9;h++)
     {
       if (s[j][h]!=0)
       {
 switch (s[j][h])
 {
  case 1:outtextxy (x,y,"1");break;
  case 2:outtextxy (x,y,"2");break;
  case 3:outtextxy (x,y,"3");break;
  case 4:outtextxy (x,y,"4");break;
  case 5:outtextxy (x,y,"5");break;
  case 6:outtextxy (x,y,"6");break;
  case 7:outtextxy (x,y,"7");break;
  case 8:outtextxy (x,y,"8");break;
  case 9:outtextxy (x,y,"9");break;
 default:outtextxy (x,y," ");break;
 }
 }
      x=x+40;
     }
    x=40;
    y=y+40;

 }
}

getkey()
{
 union REGS i,o;
 while(!kbhit())
  ;
 i.h.ah=0;
 int86(22,&i,&o);
 return (o.h.ah);
}
up()
{
 if (arrayx>0)
 {
  setcolor (WHITE);
  rectangle (posx,posy,posx+40,posy+40);
  arrayx=arrayx-1;
  posy=posy-40;
  setcolor (RED);
  rectangle (posx,posy,posx+40,posy+40);
 }
}
down()
{
 if (arrayx<8)
 {
  setcolor (WHITE);
  rectangle (posx,posy,posx+40,posy+40);
  arrayx=arrayx+1;
  posy=posy+40;
  setcolor (RED);
  rectangle (posx,posy,posx+40,posy+40);
 }
}
left()
{
 if (arrayy>0)
 {
  setcolor (WHITE);
  rectangle (posx,posy,posx+40,posy+40);
  arrayy=arrayy-1;
  posx=posx-40;
  setcolor (RED);
  rectangle (posx,posy,posx+40,posy+40);
 }
 return(NULL);
}
right()
{
 if (arrayy<8)
 {
  setcolor (WHITE);
  rectangle (posx,posy,posx+40,posy+40);
  arrayy=arrayy+1;
  posx=posx+40;
  setcolor (RED);
  rectangle (posx,posy,posx+40,posy+40);
 }
 return (NULL);
}
zero()
{
 if (sback[arrayx][arrayy]==0)
  {
   s[arrayx][arrayy]=0;
   setcolor (BLACK);
   outtextxy (posx+20,posy+20,"Û");
  }
  return (NULL);
}
one()
{
 if (s[arrayx][arrayy]==0)
  {
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==1 || s[loop][arrayy]==1)
      flag=1;
    }
   if (flag!=1)
   {
   s[arrayx][arrayy]=1;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"1");
   }
   else {flag=0;}
  }
}
two()
{
 if (s[arrayx][arrayy]==0)
  {
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==2 || s[loop][arrayy]==2)
      flag=1;
    }
   if (flag!=1)
   {
   s[arrayx][arrayy]=2;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"2");
  }
}
flag=0;
  return (NULL);
}
three()
{
 if (s[arrayx][arrayy]==0)
  {
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==3 || s[loop][arrayy]==3)
      flag=1;
    }
   if (flag!=1)
   {
   s[arrayx][arrayy]=3;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"3");
  }
}
flag=0;
  return (NULL);
}
four()
{
 if (s[arrayx][arrayy]==0)
  {
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==4 || s[loop][arrayy]==4)
      flag=1;
    }
   if (flag!=1)
   {
   s[arrayx][arrayy]=4;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"4");
  }
}
flag=0;
  return (NULL);
}
five()
{
 if (s[arrayx][arrayy]==0)
  {
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==5 || s[loop][arrayy]==5)
      flag=1;
    }
   if (flag!=1)
   {
   s[arrayx][arrayy]=5;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"5");
  }
}
flag=0;
  return (NULL);
}
six()
{
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==6 || s[loop][arrayy]==6)
      flag=1;
    }
   if (flag!=1)
   {
 if (s[arrayx][arrayy]==0)
  {
   s[arrayx][arrayy]=6;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"6");
  }
}
flag=0;
  return (NULL);
}
seven()
{
 if (s[arrayx][arrayy]==0)
  {
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==7 || s[loop][arrayy]==7)
      flag=1;
    }
   if (flag!=1)
   {
   s[arrayx][arrayy]=7;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"7");
  }
}
flag=0;
  return (NULL);
}
eight()
{
 if (s[arrayx][arrayy]==0)
  {
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==8 || s[loop][arrayy]==8)
      flag=1;
    }
   if (flag!=1)
   {
   s[arrayx][arrayy]=8;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"8");
  }
}
flag=0;
  return (NULL);
}
nine()
{
   for (loop=0;loop<9;loop++)
    {
      if (s[arrayx][loop]==9 || s[loop][arrayy]==9)
      flag=1;
    }
   if (flag!=1)
   {
 if (s[arrayx][arrayy]==0)
  {
   s[arrayx][arrayy]=9;
   setcolor (WHITE);
   outtextxy (posx+20,posy+20,"9");
  }
}
flag=0;
  return (NULL);
}
 
Status
Not open for further replies.
Top Bottom