Thursday 8 August 2013

A Sudoku Grid

My next task with the database program is to draw a grid for a calendar.

As I was considering how to go about this, I remembered that I once wrote a program to draw a Sudoku grid. This was inspired by some lectures I was listening to on YouTube (Yes I am taking my C hobby seriously ^_^. This is a link to one of the lectures, they are great: (https://www.youtube.com/watch?v=Rxvv9krECNw).

The program does not actually need you to declare the functions first. So you do not have to include the lines:

void draw_sudoku(void);
void firstLine(void);
void middleLine(int r);
void middleJointSmall(void);
void middleJoint(void);
void lastLine(void);

But if you don't, although the program will compile and run, it will give you a bunch of warnings. No warning is better than a warning...so....

On execution, it will resize the DOS screen, draw the grid, wait for keypress, resize the screen back to default and exit. Here is a sample..


Here is the code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


void draw_sudoku(void);
void firstLine(void);
void middleLine(int r);
void middleJointSmall(void);
void middleJoint(void);
void lastLine(void);

int a,b,c,d;

main()
{
    system("Mode 47,29");
    draw_sudoku();
    system("Mode 80,22");
    return(0);
}

void draw_sudoku(void)
{
    firstLine();
    middleLine(2);
    middleJointSmall();
    middleLine(2);
    middleJointSmall();
    middleLine(2);
    middleJoint();
    middleLine(2);
    middleJointSmall();
    middleLine(2);
    middleJointSmall();
    middleLine(2);
    middleJoint();
    middleLine(2);
    middleJointSmall();
    middleLine(2);
    middleJointSmall();
    middleLine(2);
    lastLine();
    getch();
}

void firstLine(void)
{
    //Beginning of first line
    printf("%c",201);

    for(a=0;a<3;a++)
    {
        for(b=0;b<3;b++)
        {
            for(c=0;c<4;c++)
            {
                printf("%c",205);
            }
            printf("%c",209);
        }
        printf("\b%c",203);
    }
    printf("\b%c\n",187);

}

void middleLine(int r)
{
    //Beginning of second and third line.

    for(a=0;a<r;a++)
    {
        printf("%c",186);
        for(b=0;b<3;b++)
        {
            for(c=0;c<3;c++)
            {
                for(d=0;d<4;d++)
                {
                    printf(" ");
                }
                printf("%c",179);
            }
            printf("\b%c",186);
        }
    printf("\b%c\n",186);
    }
}
void middleJoint(void)
{
    //Beginning of third line
    printf("%c",204);

    for(a=0;a<3;a++)
    {
        for(b=0;b<3;b++)
        {
            for(c=0;c<4;c++)
            {
                printf("%c",205);
            }
            printf("%c",216);
        }
        printf("\b%c",206);
    }
    printf("\b%c\n",185);
}

void middleJointSmall(void)
{
    printf("%c",199);

    for(a=0;a<3;a++)
    {
        for(b=0;b<3;b++)
        {
            for(c=0;c<4;c++)
            {
                printf("%c",196);
            }
            printf("%c",197);
        }
        printf("\b%c",215);
    }
    printf("\b%c\n",182);
}

void lastLine(void)
{
    printf("%c",200);

    for(a=0;a<3;a++)
    {
        for(b=0;b<3;b++)
        {
            for(c=0;c<4;c++)
            {
                printf("%c",205);
            }
            printf("%c",207);
        }
        printf("\b%c",202);
    }
    printf("\b%c\n",188);
}





No comments:

Post a Comment