A Simple Game


Below I have given the code to a simple game that i made, I wish that some reader may try it and modify it, this game was maybe one of the big work that i have done in C. It has been released under the GPL so pls feel free to download, play, share and modify. Expecting feedbacks ūüôā

This is how the game works there  is a matrix of 9 numbers 1-9, select a submatrix w for top d for right s for down and a for let, then press f to rotate the sub matrix and do so till the matrix is in order

/****************************This is a simple game*******************************************************
   Copyright (C) <2010> <Vineeth Kartha>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation;version 2 of the License.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   02111-1307 USA

you can contact me for help at vineeth.kartha@gmail.com
*/

#include<stdio.h>
#include<stdlib.h>
void licence()
{
     printf("Copyright (C) <2010> <Vineeth Kartha> \nThis program is free software; you can redistribute it and/or modify\n it under the terms of the                GNU General Public License as published by\n the Free Software Foundation;version 2 of the License.   \n This program is distributed in              the hope that it will be useful,   \n but WITHOUT ANY WARRANTY; without even the implied warranty of   \n MERCHANTABILITY or FITNESS FOR A              PARTICULAR PURPOSE. See the   \n GNU General Public License for more details.   \n You should have received a copy of the GNU General              Public License   \n along with this program; if not, write to the Free Software   \n Foundation, Inc., 59 Temple Place, Suite 330, Boston,              MA   02111-1307 USA\n\nyou can contact me for help at vineeth.kartha@gmail.com");
     printf("\n\n Press any key to continue");
}
int main()
    {    
     system("clear");
     int x=0,y=0,i,j,flag=0;
     int row=0,col=0,count=0;
     int arr[3][3],corr[3][3];
     int t,choice=1,num=1,score=0;
     char ch='q';
     for(i=0;i<3;i++)
        {
         for(j=0;j<3;j++)
            {
             corr[i][j]=num;
            }
         num++;
        }
     srand(time(NULL));
     while(choice!=3)
     {
      system("clear");
      licence();
     printf("\n 1) Play \n 2) Help \n 3) Exit \n");
     scanf("%d",&choice);
     if(choice==1)
        {
/* To fill the array with numbers from 1 - 9 in random order*/
         while(flag<9)
            {
              num=rand()%10;            //to generate random numbers
              for(i = 0;i<3;i++)
                {
                  for(j=0;j<3;j++)
                    {
                     if(num == arr[i][j]||num==0)    // to verify if number is 0 or if its already present
                        {
                         count++;
                        }            
                    }
                }
              if(count==0)
                 {
                    arr[row][col]=num;
                  flag++;
                  col++;
                  if(col>=3)
                    {
                      row++;
                      col=0;
                    }
                }
            else
                {
                 count=0;
                }
            }
/* To display the array*/

        for(i=0;i<3;i++)
            {
             for(j=0;j<3;j++)
                {
                printf("%d",arr[i][j]);
                printf("    ");            
                }
              printf("\n");
            }

        while(1)
            {
            scanf("%c",&ch);
/* Select upper square*/
            if(ch=='w')
                {
                 x=0;
                }    
/* Select lower square*/
            if(ch=='s')
                {
                 x=1;
                }
/* Select left square*/

            if(ch=='a')
                {
                 y=0;
                }
/* Select right square*/

            if(ch=='d')
                {
                 y=1;
                }
/* Rotate the digits*/
            if(ch=='f')
                {
                 t=arr[x][y];
                 arr[x][y]=arr[x+1][y];
                 arr[x+1][y]=arr[x+1][y+1];
                 arr[x+1][y+1]=arr[x][y+1];
                 arr[x][y+1]=t;
                 score++;
                }
/* Exit*/
            if(ch=='q')
                {
                 break;
                }
            system("clear");
            printf("\n\n\n\n\t\t\t");
            for(i=0;i<3;i++)
                {
                 for(j=0;j<3;j++)
                    {
                      printf("%d",arr[i][j]);
                     printf("    ");            
                    }
                 printf("\n\t\t\t");
                }
            num=0;
            for(i=0;i<3;i++)
                {
                 for(j=0;j<3;j++)
                    {
                      if(arr[i][j]==corr[i][j])
                        {
                         num++;
                        }            
                    }
                }
            if(num==9)
                {
                 printf("\n\n\t\t\tYOU WON\n");
                }
            }
    }
    if(choice==2)
    {
     system("clear");
     printf("\n This is a game where you have to arrange numbers 1-9 in a matrix in order.\n w --> select the top sub matrix \n s --> select bottom          submatrix \n a --> select left submatrix \n d --> select right submatrix.\n f--> rotate the numbers.");
     printf("\n press any  NUMBER key to continue.......");
     scanf("%d", &num);
    }
    }
    if(choice ==3)
    {
     system("exit");
     system("clear");
    }
}
Advertisements

About vineethkartha

Hi, I am Vineeth Kartha, I hold a Masters degree in Embedded Systems from BITS Pilani, Goa Campus. I currently work as a Software Developer. I am A free software promoter and electronics hobbyist. In my free time I do a little bit of birding and Wildlife photography. I love travelling, reading and blogging.

Posted on August 20, 2011, in Technical and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: