Thread: Schoolproject minesweeper

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    Schoolproject minesweeper

    Hi all,

    I'm making a schoolproject, but i'm having a crash... i don't know what i'm doing wrong. I'm debugged it and is crashing always on the same lines

    Code:
                FILE *veld = fopen("veld.bin","wb");
                fwrite(speelveld, sizeof(char),grootte,veld);
                fclose(veld);
    
                FILE *groot = fopen("grootte.bin","wb");
                fwrite(grootte, sizeof(int),100,groot);
                fclose(groot);
    
                FILE *stappen = fopen("stap.bin","wb");
                fwrite(stap, sizeof(int),(grootte*grootte),stappen);
                fclose(stappen);
    
    
                FILE *bommen = fopen("bommen.bin","wb");
                fwrite(bom, sizeof(MIJNEN),grootte/2,bommen);
                fclose(bommen);
    
                FILE *naam = fopen("naam.bin","wb");
                fwrite(speler, sizeof(char),100,naam);
                fclose(naam);
    
                 FILE *ontploft = fopen("dood.bin","wb");
                fwrite(dood, sizeof(int),40,ontploft);
                fclose(ontploft);
    the first file has no problems, but the 2nd and the 3rd are the mean bugs...

    Please help me fast!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Without knowing what those variables and sizes are,we can't really help. There is nothing DIRECTLY wrong with the posted code.

    However:
    Code:
                fwrite(grootte, sizeof(int),100,groot);
                fclose(groot);
    
                FILE *stappen = fopen("stap.bin","wb");
                fwrite(stap, sizeof(int),(grootte*grootte),stappen);
    Is grootte an integer? If so, you can't be sure to be able to write 100 integers from it's memory. If it's not an integer, grootte * grootte seems like a somewhat strange thing to pass as the size parameter on the second set of writes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did a very similar project its much simlpler then yours
    but you could use some functions.
    like the uncover function
    Code:
    #include <stdio.h>
    
    #define N 9
    int checkEndOfGame(char board[N][N],int size);
    void sprint(char board[N][N],int size);
    int same_digit(char ch_cords[40],int k);
    int is_valid(const char input[40],int leng);
    void mainPlayGame(char board[N][N]);
    int playGame(char board[N][N],int size);
    void printBoard(char board[N][N],int size);
    int removeMine(char board[N][N],int size,int x,int y);
    void mainMenu();
    int unCover(char board[N][N],int size,int x,int y);
    
    int main()
    {
        char board[N][N];
        mainPlayGame(board);
        printf("bye, please press enter to exit!\n");
        getchar();
        return 0;
    }
    
    void mainPlayGame(char board[N][N])
    {
    	int x_cord,y_cord,wins=0,loses=0;
    
        char input[40];
        char input2[40];
        int flag2=0;
        int opt,leng,size=-1;
        int index,kndex;
        char ch_cords[40];
        int tr;
        int cords[8][2];
        int tndex,result;
        int ind,knd,jnd,ch,lnd,x_rem,y_rem,flag4;
    
    
    	         for(index=0;index<N;index++)
                 {
                    for(kndex=0;kndex<N;kndex++)
                    {
                        board[index][kndex]=' ';
                    }
                 }
    
    
    
        do
        {///
            mainMenu();
            flag4=scanf("%d",&opt);
            while((lnd= getchar()) != '\n');
    	    if (flag4==1){
    		if ((opt<0)||(opt>9))
               {
    
    		}
    		else
    		{
            if (opt==1)
            {
                 for(index=0;index<N;index++)
                 {
                    for(kndex=0;kndex<N;kndex++)
                    {
                        board[index][kndex]=' ';
                    }
                 }
                printf("enter board size [1..9] (no input control is needed):\n");
                scanf("%d",&size);
    			while((lnd= getchar()) != '\n');
            }
    
            if (opt==2)
            {
                flag2=0;
                if (size==-1)
                {
                    printf("you must choose board size first!\n");
                }
            else
            {
                printf("enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn)\n");
                for (ind = 0; ind < 39 && (ch = getchar()) != '\n' && ch >=0; ++ind)
                {
                    input2[ind] = ch;
                }
    
                input2[ind] = '\0';
                knd=0;
               for(index=0;index<=ind;index++)
               {
                   if(input2[index]!=' ')
                   {
                       input[knd]=input2[index];
                       knd++;
                   }
               }
    
               leng=knd;//same digit input check
               if (is_valid(input,leng))
               {
    			   for(index=0;index<leng;index++)
                   {
                     if (((input[index])>='0')&&((input[index])<='9')&&((input[index+1])>='0')&&((input[index+1])<='9'))
                       {
    	                  flag2=1;
                       }
    			   }
                   knd=0;
               if (flag2!=1){
               for(index=0;index<leng;index++)
               {
                   if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
                   {
                       ch_cords[knd]=input[index];
                       knd++;
                   }
               }
    		   knd--;
    
    
               tndex=0;
               for(index=0;index<=leng;index++)
               {
                   if((input[index]==')'))
                   {
                       tndex++;
    
                   }
               }
    
    
                jnd=0;
                for(index=0;index<tndex;index++)
                {
                    for(kndex=0;kndex<2;kndex++)
                    {
                        tr=ch_cords[jnd]-'0';
                        cords[index][kndex]=tr;
                        jnd++;
                    }
                }
    
    
                result=same_digit(ch_cords,tndex); //i call the function here
    			if (result==0)
    			{
                   for(index=0;index<tndex;index++)
                   {
    
    
    						x_cord=cords[index][0];
    						y_cord=cords[index][1];
                    if((x_cord>size)||(y_cord>size)||(y_cord<0)||(x_cord<0))
    
                    {
                     //out of bound
                     printf("(%d,%d) is out of range\n",x_cord,y_cord);
                    }
                    else
                    {  //excepted
                      if (board[y_cord-1][x_cord-1]==' ')
                      {
                      board[y_cord-1][x_cord-1]='*';
                      printf("(%d,%d) mine inserted\n",x_cord,y_cord);
                      }
                      else
                      {
                          printf("(%d,%d) already full\n",x_cord,y_cord);
                      }
                    }
    
                   }
    
    			}
    
               }
               else
               {
                 printf("wrong input\n");
               }
    
           }
           else
           {
               printf("wrong input\n");
           }
    
           }
    
         }//end if opt 2///
         if (opt==3)
         {
             if (size==-1)
                {
                    printf("you must choose board size first!\n");
                }
            else
            {
                 printf("enter x,y of the  mine to remove (no input control is needed):\n");
                for (ind = 0; ind < 39 && (ch = getchar()) != '\n' &&  ch >=0; ++ind)
                {
                    input2[ind] = ch;
                }
    
                input2[ind] = '\0';
    
    
                       ch_cords[0]=input2[0];
                       ch_cords[2]=input2[2];
                       x_rem=ch_cords[0]-'0';
                       y_rem=ch_cords[2]-'0';
                       if (board[y_rem-1][x_rem-1]!='*')
                       {
                          printf("wrong input or no mine\n");
                       }
                       else
                       {
                           printf("mine removed\n");
                           board[y_rem-1][x_rem-1]=' ';
                       }
            }
         }
         if (opt==4)
         {
             if (size==-1)
                {
                    printf("you must choose board size first!\n");
                }
            else
            {
           printBoard(board,size);
            }
         }
         if (opt==5)
         {
       tr=playGame(board,size);
       if (tr==1){
           wins++;
           }
       if (tr==0){
        loses++;
       }
    
         }
    
         if (opt==0)
         {
             printf("you played %d games(s):\n",wins+loses);
              printf("you won %d\n",wins);
              printf("you lost %d\n",loses);
              printf("bye!");
         }
    	 }
    	   }
    
    	   else
    	   {
    	   }
        }while(opt!=0);
    
    }
    
    
    
    
    
    int same_digit(char ch_cords[40],int k)
    {
        int index;
        char ch;
        ch=ch_cords[0];
        for (index=0;index<=k;index++)
        {
          if (ch_cords[index]!=ch)
    	  {
    		  return 0;
    	  }
    
        }
        if(k>2)
        {
    	   return 1;
        }
        else
        {
         return 0;
        }
    }//end same digit
    
    void mainMenu()
    {
    
    
        printf("--------------------------\n");
        printf("welcome to the game\n");
        printf("1. choose board size\n");
        printf("2. place mines\n");
        printf("3. remove mines\n");
        printf("4. show mines\n");
        printf("5. start the game\n");
        printf("0. exit\n");
        printf("please enter your choice (input control is needed): \n");
    
    }
    
    
    
    void printBoard(char board[N][N],int size)
    {
        int index,kndex;
        for(index=0;index<size;index++)
        {
    		printf("+");
           for(kndex=0;kndex<size;kndex++)
           {
               printf("-+");
           }
           printf("\n");
    	   printf("|");
           for(kndex=0;kndex<size;kndex++)
           {
               printf("%c|",board[index][kndex]);
           }
           printf("\n");
    
     }
    	printf("+");
      for(kndex=0;kndex<size;kndex++)
      {
             printf("-+");
    
      }
      printf("\n");
    }
    
    int removeMine(char board[N][N],int size,int x,int y)
    {
        if (board[y-1][x-1]=='*')
        {
            board[y-1][x-1]=' ';
            printf("mine removed");
            return 1;
        }
        else
        {
            printf("wrong input or no mine");
            return 0;
        }
    
    
    }
    
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
     if ((x>size)||(y>size))
     {
       return -1;
    
     }
      if (board[y-1][x-1]=='*'){
       printf("you lost\n");
       board[y-1][x-1]='X';
    
       return 0;
      }
     else
     {
    
    
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x-1]='0' +count;
    
       return 1;
    
     }
    }
    
    
    
    
    int is_valid(const char input[40],int leng)
    {
    	int flag=3;
    	int ind=1;
        int index;
        for(index=0;index<leng;index++)
        {
            if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
            {
                if (((input[1])<'0')||((input[1])>'9'))
                {
    		        return 0;
    	        }
            }
        }
        if (input[0] != '(')
        {
            flag=0;
        }
        if (((input[1])<'0')||((input[1])>'9'))
        {
    	    flag=0;
        }
        ind++;
        while((input[ind]<='9')&&(input[ind]>='0'))
        {
            ind++; //increasing the  address by1
        } //it wiil get char '2'
        if (input[ind] == ',')
        {   //'2' differs ','
            if (((input[ind+1])<'0')||((input[ind+1])>'9'))
            {
    		   flag=0;
    	    }
            ind++;
            while((input[ind]<='9')&&(input[ind]>='0'))
            {
                input++; //increasing the  address by1
            }
            if (input[ind]==')')
            {
                ind++;
            }
            else
              flag=0;
            }
       else
       {
          flag=0;
       }
    
       if (flag ==0)
       {
           return 0;
       }
    
    
        while (input[ind]!= '\0')
        {
            if (input[ind] != '(')
            {
                flag=0;
            }
    	    if (((input[ind+1])<'0')||((input[ind+1])>'9'))
    	    {
    		   flag=0;
    	    }
    	        ind++;
            while((input[ind]<='9')&&(input[ind]>='0'))
            {
    
                ind++; //increasing the  address by1
            } //it wiil get char '2'
    
            if (input[ind] == ',')
            {   //'2' differs ','
                if (((input[ind+1])<'0')||((input[ind+1])>'9'))
                {
    		        flag=0;
    	        }
                ind++;
                while((input[ind]<='9')&&(input[ind]>='0'))
                {
                    ind++; //increasing the  address by1
                }
                if (input[ind]==')')
                {
                    ind++;
                }
                else
                flag=0;
                }
       else
       {
          flag=0;
       }
       if (flag ==0)
            {
                return 0;
            }
        }
        return 1;
    }
    
    
    int checkEndOfGame(char board[N][N],int size){
     int index,kndex;
    
     for (index=0;index<size;index++){
         for (kndex=0;kndex<size;kndex++){
         if  (board[index][kndex]==' '){
          return 0;
         }
         }
     }
     return 1;
    }
    
    void sprint(char board[N][N],int size)
    {
        int index,kndex;
        for(index=0;index<size;index++)
        {
    		printf("+");
           for(kndex=0;kndex<size;kndex++)
           {
               printf("-+");
           }
           printf("\n");
    	   printf("|");
           for(kndex=0;kndex<size;kndex++)
           {
               if ((board[index][kndex]=='*')||(board[index][kndex]==' '))
               {
            printf("?|");
               }
               else
               {
               printf("%c|",board[index][kndex]);
               }
           }
           printf("\n");
    
     }
    	printf("+");
      for(kndex=0;kndex<size;kndex++)
      {
             printf("-+");
    
      }
      printf("\n");
    }
    
    int playGame(char board[N][N],int size){
    int x_rem,y_rem,ind,ch,kent,wins=0,loses=0,end,tr;
    char input2[40];
              if (size==-1)
                {
                    printf("you must choose board size first!\n");
                }
            else
            {
             do
             {
    
                 printf("enter a square x,y to uncover : (no input control is needed):\n");
                for (ind = 0; ind < 39 && (ch = getchar()) != '\n' && ch >=0; ++ind)
                {
                    input2[ind] = ch;
                }
    
                   input2[ind] = '\0';
                   x_rem=input2[0]-'0';
                   y_rem=input2[2]-'0';
    
                    kent=unCover(board,size,x_rem,y_rem);
                if (kent==-1)
    			{
                   printf("wrong input\n");
      			}
    			else
    			{
                if (kent==0){
              end=1;
              loses++;
    
              printBoard(board,size);
             }
             else{
    
             tr=checkEndOfGame(board,size);
             if (tr==0)
             {
                sprint(board,size);
             }
             else
             {
                 printBoard(board,size);
                 end=1;
                 printf("all mines were uncovered - you won\n");
                 wins++;
             }
             }
    
    
    		 }
             }while(end!=1);
            }
    
        if (wins==1){
         return 1;
        }
    	else
        {
            return 0;
        }
    }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Quote Originally Posted by matsp View Post
    Without knowing what those variables and sizes are,we can't really help. There is nothing DIRECTLY wrong with the posted code.

    However:
    Code:
                fwrite(grootte, sizeof(int),100,groot);
                fclose(groot);
    
                FILE *stappen = fopen("stap.bin","wb");
                fwrite(stap, sizeof(int),(grootte*grootte),stappen);
    Is grootte an integer? If so, you can't be sure to be able to write 100 integers from it's memory. If it's not an integer, grootte * grootte seems like a somewhat strange thing to pass as the size parameter on the second set of writes.

    --
    Mats
    Yes, it are both integers...

    grootte is the large of a field (from 10 to 75 in my program), whe need this because the number of bombs depends on it...
    and stap is the number of turns...

    tried to make it 1 but it's the same ........ that happens...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i did a very similar project its much simlpler then yours
    but you could use some functions.
    like the uncover function
    First of all: DO NOT PROVIDE SOLUTIONS TO OTHER PEOPLES HOMEWORK!
    Second, it doesn't actually answer ANY of the original question.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    re

    Quote Originally Posted by matsp View Post
    First of all: DO NOT PROVIDE SOLUTIONS TO OTHER PEOPLES HOMEWORK!
    Second, it doesn't actually answer ANY of the original question.

    --
    Mats
    indeed... there is nothing in the code for saving

    and i want to learn it on my own

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Wushi View Post
    Yes, it are both integers...

    grootte is the large of a field (from 10 to 75 in my program), whe need this because the number of bombs depends on it...
    and stap is the number of turns...

    tried to make it 1 but it's the same ........ that happens...
    Have you enabled warnings in your compiler?

    If grootte is an integer, you probably should be passing &grootte to the function to store it. And do not try to write 100 integers from that address, as that is unlikely to be useful.

    By the way, are you actually saving ONE integer in it's own file? Would it not make more sense to save ALL of the data into one larger file?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    re

    euhm... i don't know how to do that... *shamed*

    EDIT: the & works!!!! THANK YOU ALL!
    Last edited by Wushi; 01-08-2009 at 04:52 PM.

  9. #9
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Ah, I was asked to make a minesweeper at the beginning of this term by accident;
    but I was trying using Windows API to do that.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Wushi View Post
    indeed... there is nothing in the code for saving

    and i want to learn it on my own
    You really don't want to use that code anyway. Don't even get any ideas off it, it'll screw with your mind and you'll learn all the same bad habbits.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minesweeper
    By mark69 in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 10:28 AM
  2. AI Contest - Minesweeper
    By Darryl in forum Contests Board
    Replies: 93
    Last Post: 04-24-2006, 05:48 PM
  3. Minesweeper Recursion
    By sql.scripter in forum C Programming
    Replies: 2
    Last Post: 03-21-2006, 08:02 AM
  4. minesweeper problem
    By adamrobbie in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 08:03 PM
  5. Home made Minesweeper using SDL
    By damyan in forum Game Programming
    Replies: 3
    Last Post: 11-02-2001, 06:17 PM