Thread: Weird problem with recursive function..

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Weird problem with recursive function..

    I am creating a battleship game for my class and I have this function that allows the user to place their ships onto the playing board. However, whenever an error occurs (i.e. try to place a ship outside the board or on another ship) and I recall the function in order to re ask the user for input, the while loop in the function does not end when it should. If I test the program and put in all correct values with no errors in the input then it works. Therefore, it has something to do with me recalling the function inside itself:
    Heres the code:

    Code:
    void manually_place_ships_on_board(int i, char board[10][10])
    {
    	int row_st, col_st, dir, ship_length, ship_id;
    	char ship_symbol[5] = {'c', 'b', 'r', 's', 'd'};
    	char ship_name[5][11] = {"Carrier", "BattleShip", "Cruiser", "Submarine", "Destroyer"};
    	
    	while(i<5)
    	{
    		switch(i)
    		{
    		case 0:
    			ship_length = 5;
    			break;
    		case 1:
    			ship_length = 4;
    			break;
    		case 2:
    			ship_length = 3;
    			break;
    		case 3:
    			ship_length = 3;
    			break;
    		case 4:
    			ship_length = 2;
    			break;
    		}
    		ship_id = i;
    		
    		printf("Please select the starting position of one end of your %s ship: %d\n", ship_name[ship_id], ship_length);
    		printf("Row: ");
    		scanf("%d", &row_st);
    		printf("Column: ");
    		scanf("%d", &col_st);
    		
    		if((row_st < 0 || row_st > 9) || (col_st < 0 || row_st > 9) || board[col_st][row_st] != '-')
    		{
    		printf("Cannot place ship here..Try Again\n");
    		getch();
    		manually_place_ships_on_board(ship_id, board);
    		}
    
    
    		printf("What direction do you want the ship to point? ship_id: %d i: %d\n", ship_id, i);
    		printf("1) Up\n");
    		printf("2) Down\n");
    		printf("3) Left\n");
    		printf("4) Right\n");
    		scanf("%d", &dir);
    
    
    		switch(dir)
    		{
    		case 1:
    			if((row_st+1-ship_length)>=0)
    			{
    				if(check_for_other_ships(board, row_st, col_st, dir, ship_length)==0)
    				{
    				set_ship(row_st, col_st, dir, ship_symbol[i], board, ship_length);
    				}else{
    				printf("There is a ship in the way already!\n");
    				getch();
    				update_boards(board, 1);
    				manually_place_ships_on_board(ship_id, board);
    				}
    			}else{
    
    
    			printf("Out of bounds\n");
    			getch();
    			update_boards(board, 1);
    			manually_place_ships_on_board(ship_id, board);
    			}
    			break;
    		case 2:
    			if((row_st+1+ship_length)<=9)
    			{
    
    
    				if(check_for_other_ships(board, row_st, col_st, dir, ship_length)==0)
    				{
    				printf("%d", check_for_other_ships(board, row_st, col_st, dir, ship_length));
    				set_ship(row_st, col_st, dir, ship_symbol[i], board, ship_length);
    				}else{
    				printf("There is a ship in the way already!\n");
    				getch();
    				update_boards(board, 1);
    				manually_place_ships_on_board(ship_id, board);
    				}
    			}else{
    			printf("Out of bounds\n");
    			getch();
    			update_boards(board, 1);
    			manually_place_ships_on_board(ship_id, board);
    			}
    			break;
    		case 3:
    			if((col_st+1-ship_length)>=0)
    			{
    				if(check_for_other_ships(board, row_st, col_st, dir, ship_length)==0)
    				{
    				getch();
    				set_ship(row_st, col_st, dir, ship_symbol[i], board, ship_length);
    				}else{
    				printf("There is a ship in the way already!\n");
    				getch();
    				update_boards(board, 1);
    				manually_place_ships_on_board(ship_id, board);
    				}
    			}else{
    			printf("Out of bounds\n");
    			getch();
    			update_boards(board, 1);
    			manually_place_ships_on_board(ship_id, board);
    			}
    			break;
    		case 4:
    			if((col_st+1+ship_length)<=9)
    			{
    				if(check_for_other_ships(board, row_st, col_st, dir, ship_length)==0)
    				{
    				set_ship(row_st, col_st, dir, ship_symbol[i], board, ship_length);
    				}else{
    				printf("There is a ship in the way already!\n");
    				getch();
    				update_boards(board, 1);
    				manually_place_ships_on_board(ship_id, board);
    				}
    			}else{
    			printf("Out of bounds\n");
    			getch();
    			update_boards(board, 1);
    			manually_place_ships_on_board(ship_id, board);
    			}
    			break;
    			}
    	
    		i++;
    	}
    }
    Any help is appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Generally, asking for user input recursively is not the way to go. You can check for a conflict using a recursive function, and actually place the ship using a recursive function, but asking the user for input, printing status messages, etc is generally best done with loops. Something like:

    Code:
    do
        ask for ship type, coordinates, direction
        valid_input = recursive_check_out_of_bounds_or_other_ship_conflict(ship type, coords, direction)
    while (!valid_input)
    recursive_place_ship

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with recursive function
    By Posto2012 in forum C Programming
    Replies: 5
    Last Post: 12-05-2009, 03:32 AM
  2. Recursive function problem
    By trnd in forum C Programming
    Replies: 5
    Last Post: 01-30-2009, 12:36 AM
  3. recursive function problem
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 10-25-2002, 06:02 AM
  4. Weird Recursive Function :: C++
    By kuphryn in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2002, 04:59 PM
  5. Problem with a recursive function
    By fofone in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 08:21 AM