Games that I've modded or created levels: Titan Quest, The Elder Scrolls IV:Oblivion, The Elder Scrolls V:Skyrim, Tony hawk series, C&D Generals, C&D Generals:Zero hour, Battle For Middle Earth I, Battle For Middle Earth II, Battle For Middle Earth II:The Rise Of Witch King, Worms:Mayhem, Worms:Armageddon, Warcraft 3, Warcraft 3:Frozen Throne, Fallout 3, Medieval Lords, Crysis, Age Of Empires III, Age Of Empires III:Warchiefs, Age Of Empires III:Asian Dynasties, Trackmania Forever, Civilization IV, Mount & Blade

Image RSS Feed Latest Screens
Bandit Loren Lighthouse A waterfall
Blog RSS Feed Report abuse Latest Blog: Minesweeper In C

0 comments by pkyrkos7 on Jan 7th, 2013

I've created a minesweeper game in C for a school project of mine and I decided to upload it here.Of course it needs to be compiled first since below there's only code.

c code:
#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

#define dimensions_small_table 15
int tile_choiceX,tile_choiceY,gbl_current_round=-1,gbl_mines=-1,tile_revealed_small_table[dimensions_small_table][dimensions_small_table],tile_checkZeros_small_table[dimensions_small_table][dimensions_small_table]; //global variables for small table
char gbl_Small_table[dimensions_small_table][dimensions_small_table],gbl_gameEnd='A';

int main()
   {
  int gotostart;
    char menu01sel,choice01;
    printf("Welcome to Minesweeper Game!\n");
    do
      {
     gotostart=0;
       printf("Press 1 to start playing!\n");
       printf("Press 2 for information\n");
       printf("Press 3 for legend\n");
       printf("Press 4 to exit\n");
       do
       { menu01sel=getch(); }
       while (menu01sel!='1' &amp;&amp; menu01sel!='2' &amp;&amp; menu01sel!='3' &amp;&amp; menu01sel!='4');
       switch(menu01sel)
       {
        case '1':
         SmallTableGame();
           break;
        case '2':
           HelpFunction();
           gotostart=1;
           break;
        case '3':
           printf("\nLEGEND: *=tile not revealed,E=tile empty,B=tile has bomb\nNumber(1,...,8)=the number is equal to the amount of bombs at the nearby tiles\n\n");
           gotostart=1;
           break;
        default:
           return ProgramEnd();
       }
       if (gotostart!=1)
          {
           if (gbl_gameEnd=='L')
             {
              printf("\n\nYou found a mine!I am sorry but you lost!Do you want to try again?(Y=Yes,N=No)\n");
              do
               { choice01=getch(); }
              while (choice01!='N' &amp;&amp; choice01!='n' &amp;&amp; choice01!='Y' &amp;&amp; choice01!='y');
             }
           else if (gbl_gameEnd=='W')
             {
              printf("\n\n\nCongratulations!You have won the game!Want to play again?(Y=Yes,N=No)\n");
              do
               { choice01=getch(); }
              while (choice01!='N' &amp;&amp; choice01!='n' &amp;&amp; choice01!='Y' &amp;&amp; choice01!='y');
             }
           if (choice01=='Y' || choice01=='y')
              gotostart=1;
          }
      } while (gotostart==1);
    return ProgramEnd();
   }




/*==================================This is the function used for the small table game==============================*/
int SmallTableGame()  //this table contains 225 tiles
   {                                    
    //tile_choiceX and tile_choiceY have real number values --should be changed to use in arrays (real value-1)
    int i,j,rand01,rand02,TotalTilesReveal,tilecounted[dimensions_small_table][dimensions_small_table],gotocoordinates;
    //start up things below
    gbl_gameEnd='A';//the value A means nothing.It could be anything apart from W or L
    gbl_current_round=0;
    TotalTilesReveal=0;
    for (i=0;i<dimensions_small_table;i++)  
       {
        for (j=0;j<dimensions_small_table;j++)
           {
            gbl_Small_table[i][j]=0;
            tile_revealed_small_table[i][j]=0;
            tile_checkZeros_small_table[i][j]=0;
            tilecounted[i][j]=0;
           }
       }  
    /*---------------------------------------------*/
    do
      {
       printf("Enter the amount of mines you want on your minefield(Max mines %.lf):",pow(dimensions_small_table,2)-1);
       scanf("%d",&amp;gbl_mines);
      } while (gbl_mines>=pow(dimensions_small_table,2) || gbl_mines<=0);
    /*------------------*/
    //this part of script will generate the minefield//
    srand(time(0));//this line will make sure each time your run the program the first mine doesn't appear at the same place
    for (i=0;i<gbl_mines;i++)
        {
         do
           {
            rand01=rand()%dimensions_small_table;
            rand02=rand()%dimensions_small_table;
           } while(gbl_Small_table[rand01][rand02]=='B');
         gbl_Small_table[rand01][rand02]='B';
         srand(time(0));//this line is very important DO NOT ERASE!Makes the minefield random
        }
    /*-------------------------------------------------*/
    printf("\nGame has started!\n=====================\n");
    do
     {
      putchar('\n');
      if (gbl_current_round!=0)
          {
           if(gbl_Small_table[tile_choiceX-1][tile_choiceY-1]=='B')
              gbl_gameEnd='L';
           if (gbl_gameEnd!='L' &amp;&amp; gbl_gameEnd!='W' &amp;&amp; gbl_Small_table[tile_choiceX-1][tile_choiceY-1]!='B')
              NearbyMineCheck(tile_choiceX,tile_choiceY);
           for (i=0;i<dimensions_small_table;i++)  
               {
                for (j=0;j<dimensions_small_table;j++)
                    {
                     if (tile_revealed_small_table[i][j]==1 &amp;&amp; tilecounted[i][j]==0 &amp;&amp; gbl_Small_table[i][j]!='B')
                         {
                          tilecounted[i][j]=1;
                          TotalTilesReveal++;
                         }
                    }
               }
           if ((TotalTilesReveal+gbl_mines)==pow(dimensions_small_table,2))
               gbl_gameEnd='W';
          }
      gbl_current_round++;
      printf("   ");
      for (i=0;i<dimensions_small_table;i++)
          {
           if (i<9)
              printf(" 0%d",i+1);
           else
              printf(" %d",i+1);
          }
      putchar('\n');
      printf("   ");
      for (i=0;i<dimensions_small_table*3;i++)
          putchar('-');
      for (i=0;i<dimensions_small_table;i++)
         {
          putchar('\n');
          if (i<9)
             printf("0%d| ",i+1);
          else
             printf("%d| ",i+1);
          for (j=0;j<dimensions_small_table;j++)
              {
               if (gbl_gameEnd=='L')
                  {
                   if(gbl_Small_table[i][j]=='B' || tile_revealed_small_table[i][j]==1)
                       printf("%c  ",gbl_Small_table[i][j]);
                   else printf("*  ");
                  }
               else
                  {
                   if (tile_revealed_small_table[i][j]==0)
                       printf("*  ");
                   else
                      printf("%c  ",gbl_Small_table[i][j]);
                  }
              }
         }
      if (gbl_gameEnd!='L' &amp;&amp; gbl_gameEnd!='W')
         {
          do
            {
           gotocoordinates=0;
             printf("\n\nEnter the coordinates of the tile you want to reveal.");
             printf("\nX coordinate(row): ");
             tile_choiceX=ReadCoordinates();
             printf("\nY coordinate(column): ");
           tile_choiceY=ReadCoordinates();
             if (tile_revealed_small_table[tile_choiceX-1][tile_choiceY-1]==1)
                 {
                  printf("\nThe tile at %d,%d has already been revealed.Choose another.\n",tile_choiceX,tile_choiceY);
                  gotocoordinates=1;
                 }
            } while (gotocoordinates==1);
         }
     } while (gbl_gameEnd!='L' &amp;&amp; gbl_gameEnd!='W');  //gbl_gameEnd will take the value L when the game is lost or W if it is won
    return 0;
   }
 
/*--------------------IMPORTANT FUNCTIONS BELOW-------------------------*/

//This function will check for nearby mines of the tile that was chosen//
NearbyMineCheck(int tempX,int tempY)
  {
   int i,j,tile_nearMinesCount,gotostart;
   do
      {
     gotostart=0;
       tile_nearMinesCount=NearbyMineCheck2(tempX,tempY);  
       gbl_Small_table[tempX-1][tempY-1]=tile_nearMinesCount+48; //48='0'
       tile_revealed_small_table[tempX-1][tempY-1]=1;
       tile_checkZeros_small_table[tempX-1][tempY-1]=1;
       if (tile_nearMinesCount==0)
          {
           if (tempY>1)
              NearbyMineCheck2(tempX,tempY-1);//left
           if (tempY<dimensions_small_table)
              NearbyMineCheck2(tempX,tempY+1);//right
           if (tempX>1)
              NearbyMineCheck2(tempX-1,tempY);//up
           if (tempX<dimensions_small_table)
              NearbyMineCheck2(tempX+1,tempY);//down
           if (tempX>1 &amp;&amp; tempY>1)
              NearbyMineCheck2(tempX-1,tempY-1);//up-left
           if (tempX>1 &amp;&amp; tempY<dimensions_small_table)
              NearbyMineCheck2(tempX-1,tempY+1);//up-right
           if (tempX<dimensions_small_table &amp;&amp; tempY>1)
              NearbyMineCheck2(tempX+1,tempY-1);//down-left
           if (tempX<dimensions_small_table &amp;&amp; tempY<dimensions_small_table)
              NearbyMineCheck2(tempX+1,tempY+1);//down-right
           for (i=0;i<dimensions_small_table;i++)  
              {
               for (j=0;j<dimensions_small_table;j++)
                  {
                   if (gbl_Small_table[i][j]=='0' &amp;&amp; tile_checkZeros_small_table[i][j]==0 &amp;&amp; tile_revealed_small_table[i][j]==1)
                      {
                       tempX=i+1;
                       tempY=j+1;
                       tile_checkZeros_small_table[i][j]=1;        
                       gotostart=1;
                      }
                   if (gotostart==1)
                       break;
                  }
               if (gotostart==1)
                  break;
              }
          }
      } while (gotostart==1);
  }


int NearbyMineCheck2(int tempX,int tempY) //This Function is part of NearbyMineCheck Function
   {
    int tile_nearMinesCount=0;
    /*This function will reveal the nearby tiles*/
    if (tempY>1  &amp;&amp; gbl_Small_table[tempX-1][tempY-2]=='B')
        {tile_nearMinesCount++;}//left
    if (tempY<dimensions_small_table &amp;&amp; gbl_Small_table[tempX-1][tempY]=='B')
        {tile_nearMinesCount++;}//right
    if (tempX>1 &amp;&amp; gbl_Small_table[tempX-2][tempY-1]=='B')
        {tile_nearMinesCount++;}//up
    if (tempX<dimensions_small_table &amp;&amp; gbl_Small_table[tempX][tempY-1]=='B')
        {tile_nearMinesCount++;}//down
    if (tempX>1 &amp;&amp; tempY>1)
       {
        if (gbl_Small_table[tempX-2][tempY-2]=='B')
            {tile_nearMinesCount++;}//up-left
       }
    if (tempX>1 &amp;&amp; tempY<dimensions_small_table)
       {
        if (gbl_Small_table[tempX-2][tempY]=='B')
            {tile_nearMinesCount++;}//up-right
       }
    if (tempX<dimensions_small_table &amp;&amp; tempY>1)
       {
        if (gbl_Small_table[tempX][tempY-2]=='B')
            {tile_nearMinesCount++;}//down-left
       }
    if (tempX<dimensions_small_table &amp;&amp; tempY<dimensions_small_table)
       {
        if (gbl_Small_table[tempX][tempY]=='B')
           {tile_nearMinesCount++;}//down-right
       }    
    gbl_Small_table[tempX-1][tempY-1]=tile_nearMinesCount+48; //48='0' 
    tile_revealed_small_table[tempX-1][tempY-1]=1;
    return tile_nearMinesCount;
   }

int ReadCoordinates()
    {
     int tile_choice,tile_choiceValid=-1;        
     do
       {
        scanf("%d",&amp;tile_choice);
        if (tile_choice>=1 &amp;&amp; tile_choice<=dimensions_small_table)
            tile_choiceValid=1; //Y is column
        else
           {
            tile_choiceValid=0;
            printf("\nY coordinate not within bounds.Must be within 1 and %d\n",dimensions_small_table);
           }
       } while (tile_choiceValid==0);
       return tile_choice;
    }

HelpFunction(void)
   //This function is called when the user needs help
   {
    printf("\nThe player is initially presented with a grid of undifferentiated squares.Some randomly selected squares, unknown to the player, are designated to contain mines.The game is played by revealing squares of the grid.If a square containing a mine is revealed, the player loses the game. Otherwise, a digit is revealed in the square, indicating the number of adjacent squares (typically, out of the possible eight) that contain mines.\n\n");
   }

int ProgramEnd(void)
   {
  printf("\n=================================\nMade by pkyrkos7 aka Panos\n=================================\n");
    system("pause");
    return 0;
   }

 
Groups
2D Games

2D Games

Fans & Clans group with 114 members, open to all members

They are places where "Z axis" does not exist, only YX reign, the word is unknown 3D, games are simple but should be played, many of them will make us...

Age of Fans

Age of Fans

Fans & Clans group with 561 members, open to all members

For all fans of Age of Empires & Age of Mythology!

Aliens vs Predator Source

Aliens vs Predator Source

Fans & Clans group with 275 members, open to all members

Aliens vs Predator Source for all the avp games and movies. Your AvP Source of the latest AvP news, videos, images,and more. Join the AvP Source.

Biohazard Fans

Biohazard Fans

Fans & Clans group with 157 members, open to all members

All fans of the Resident Evil games, join here and share knowledge about the RE series and have fun!

Day Z Survivors

Day Z Survivors

Fans & Clans group with 86 members, open to all members

The purpose of this group is to schedule DayZ gameplay events, which will make it much easier to get ahead in DayZ. Eventually, through teamwork and determination...

Dead Frontier

Dead Frontier

Fans & Clans group with 3 members, open to all members

this group was made for those who play Dead Frontier.

Fans of Silent Hill

Fans of Silent Hill

Hobbies & Interests group with 440 members, open to all members

This group is for the fans of silent hill games to stay up to date with all things Silent Hill.

Greece

Greece

Geographic group with 82 members, open to all members

This is The Greek Group In ModDb. A small taste of home for every greek in the huge website of moddb. all greek friendly and greek members of moddb can...

Post comment Comments
aldodf
aldodf Jan 2 2014, 7:12am says: Online

Hello, my name is aldodf and am a big fan of Gabrilduro in particular the mod old America in 1860, I and other fans, as a gift for the 2014 Gabrilduro we want to increase the vows, because I do not know if you've seen, but it is the victim of some idiots who put 1 for no reason, you andremme to put 10 as voting, sarabbe a great gift for a great modder who never asks for anything and is always friendly and polite!

Thanks in advance: D

+1 vote     reply to comment
RedkixGames
RedkixGames Jun 15 2012, 9:23pm says:

Thanks for watching Raw F.O.X!

+2 votes     reply to comment
zuthen
zuthen Jun 7 2011, 4:37am says:

sorry i never got back to you on your friend request. didnt even realize i had gotten it, i dont check this sites mail as much as i should.

+2 votes     reply to comment
iQew
iQew Apr 30 2011, 7:31pm says:

Hey, thanks for tracking Mindworld on indieDB, we really appreciate your interest :)

+2 votes     reply to comment
pkyrkos7
pkyrkos7 May 1 2011, 6:06am replied:

It seems really interesting!

+2 votes     reply to comment
iQew
iQew May 28 2011, 8:19pm replied:

Thanks!

+2 votes     reply to comment
NGS616
NGS616 Oct 4 2010, 9:30am says:

Hey Panos, thanks for watching Crusader's Quests!

+2 votes     reply to comment
Bfranx
Bfranx Aug 2 2010, 12:41pm says: Online

Oops, ignore this post :P

+2 votes     reply to comment
Bfranx
Bfranx Aug 1 2010, 9:33pm says: Online

Hey, Thanks for watching our mod!

+2 votes     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Level
Avatar
Avatar
Offline Since
Jul 23, 2014
Country
Greece Greece
Gender
Male
Member Watch
Track this member
Accolades
ModDB
Statistics
Activity Points
2,029
Rank
760 of 462,896
Watchers
3 members
Time Online
1 day
Comments
445
Site Visits
9,556
Profile Visitors
5,581 (2 today)
Contact
Homepage
Moddb.com
Private Message
Send Now
Email
Members Only
TheMasquerader
TheMasquerader friends since Apr 22, 2014
Nuvendil
Nuvendil friends since Apr 29, 2012
Enpremi
Enpremi friends since Nov 17, 2013
xristos10
xristos10 friends since Dec 16, 2012
Stormrage256
Stormrage256 friends since May 28, 2014
ztree1
ztree1 friends since Apr 24, 2011
Angel_of_Death2
Angel_of_Death2 friends since Jul 18, 2013
DIFTOW
DIFTOW friends since Apr 15, 2011
French_Fry
French_Fry friends since Jan 11, 2012
ElijahHouck
ElijahHouck friends since Dec 9, 2013