Thread: Mine sweeper help needed

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

    Mine sweeper help needed

    Hi guys,
    I'm in need for some help in my homework assignment. I'm kind of new at C programming.

    My problem is that for some reason when I print the board, the i counter jumps to astronomical number.
    I placed an *?* on that line. (it's down below)
    Oh and a major problem that I thought it was me but the debugger shows the board[][] after calling the mainProgram and transferring the board as board[], meaning it now has only 1 dimension (???).

    Thanks for helping !

    Code:
    #include <stdio.h>
    #define N 9
    #define A 40
    
    void mainProgram(char board[N][N]);
    int input_mines(char board[N][N],int size);
    void mainMenu();
    int insertMine(char board[N][N],int size, char mines[40]);
    void printBoard(char board[N][N],int size);
    
    
    int main()
    {
    	char board[N][N]={'0'};
    	mainProgram(board);
    	printf("Bye, please press enter to exit!\n");
    	getchar();
    	return 0;
    }
    
    void mainProgram(char board[N][N])
    {
    		
    	int i,j,flag=0,check,size;
    	char tempchar,Choice,input_s;
    
    	size=0;
    	do
    	{
    		SW: 
    			mainMenu();
    			scanf ("%c",&Choice);
    			getchar();
    			Choice-=48;
    			switch (Choice)
    			{
    				case 0:		//**exit**//
    					break;
    				case 1:		//**choose board size**//
    					do
    					{
    						if (flag)
    						{
    							printf("|-incorrect input value-|\n");
    						}
    						printf("Please choose board size : ");
    						scanf("%c",&size);
    						getchar();
    						size-=48;
    						if (size<2 || size>9) flag=1;
    						else flag=0;
    					}
    					while (size<2 || size>9);
    
    					for (i=0;i<=size-1;i++)		// '?' - the board //
    					{
    						for (j=0;j<=size-1;j++)
    						{	
    							board[i][j]='?';
    						}
    					}
    					break;
    				case 2:						//insert mines//
    					if (size!=0)
    					{
    						input_mines(board,size);
    					}
    					else
    					{
    						printf ("you must choose board size first !\n");
    					}
    					break;
    				case 4:
    					printBoard(board,size);
    					break;
    				default: 
    					printf("There's no such option\n");
    					goto SW;
    						 
    			}
    		}
    		while (Choice!=0);
    }
    int input_mines(char board[N][N],int size)
    {
    	char temp,mines[40];
    	printf ("enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn):\n");
    	scanf ("%s",&mines);
    	getchar();
     	insertMine(board,size,mines);
    }
    
    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 controls needed): \n");
    }
    
    
    
    
    int insertMine(char board[N][N],int size, char mines[40])
    {
    	char checking_arr[4],coordinates[160];
    	int i,j,k,flag;
    
    	checking_arr[1]='(';
    	checking_arr[2]=',';
    	checking_arr[3]=')';
    
    	i=0;
    	j=0;
    	while (mines[i]!=0) //extracting coordinates to var//
    	{
    		flag=0;
    		for (k=1;k<=3;k++)
    		{
    			do
    			{
    				i++;
    				if (checking_arr[k]==mines[i]) 
    				{
    					flag++;
    				}
    				else
    				{
    					if (48<mines[i]<57)
    					{
    						coordinates[j]=mines[i];
    						j++;
    					}
    				}
    			}
    			while (mines[i]==' ');
    		}
    	}
    	if (flag!=3)					//quality check for input//
    	{
    		printf("wrong input\n");
    		for (i=1;i<=160;i++){		//delete the mines[]//
    			mines[i]=0;
    		}
    		mainProgram(board,size);
    	}
    	else						//all ok ? put the mines into board//
    	{
    		i=0;
    		do
    		{
    			board[coordinates[i]][coordinates[i+1]]='*';
    			i+=2;
    		}
    		while (coordinates[i]!=0);
    	}
    }
    
    void printBoard(char board[N][N],int size)
    
    {
    	int i,j;
    	
    	for (i=0;i<=size-1;i++)
    	{ 
    		printf ("+");
    		for (j=0;j<=size-1;j++)
    		{
    			printf ("-+");
    		}
    		printf("\n|");
    		for (j=0;j<=size-1;j++)
    		{
    			printf("%s|",board[i][j]);      *?*
    		}
    		printf ("+");
    		for (j=0;j<=size-1;j++)
    		{
    			printf ("-+");
    		}
    	}
    	printf("\n\n");
    }
    Last edited by MoAv; 01-18-2009 at 01:54 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    %c format of the scanf requires pointer to char - you are passing pointer to int - dengerous thing to do

    do not use magic numbers, if you want something like
    choice-'0' - just write so.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    OK, thanks for the tip
    Anyone can figure how come my 2d array turning into 1d after calling mainProgram ?

    EDIT:

    ok nevermind,. the debugger show the array as 1dim though it's just fine.
    printf debugging did the trick.
    Last edited by MoAv; 01-18-2009 at 06:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mine sweeper help..
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 01-04-2009, 11:18 PM
  2. mine sweeper code help..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 01-01-2009, 09:44 AM
  3. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  4. Recursive Fibonacci, some help needed
    By cwafavre in forum C Programming
    Replies: 8
    Last Post: 11-04-2007, 02:20 PM
  5. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM