Thread: Trouble with two two dimensional arrays

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Question Trouble with two two dimensional arrays

    I am in the process of writing code for a Battleship game, and I am just trying to get the game to work properly before I worry about creating functions to reduce the code. I am selecting 5, 5, 5, 4 for the first four user inputs, placing an aircraft carrier on the board. I then select 5, 5, and it does show up correctly as a hit on the second board, a "11" means a hit. If I select 4, 4, it says I missed, but records a "11" instead of a "00" Can I use the int row and int column variables in both the ships_a and guess_b arrays? I can't figure out what I am doing wrong, please help me out.

    Code:
    * The name of this program is Battleship */
    
    /* This program is Battleship game */
    
    #include <stdio.h>
    
    /*     DEFINE ARRAYS     */
    
    int ships_a[11][11];
    int guess_b[11][11];
    
    
    /*     DECLARE VARIABLES     */
    
    /* these 2 integers are the coodinates a player selects for ship placement, or when taking a shot */
    int row;
    int column;
    
    int direction;
    int ship_type;
    int ship_count = 0;
    
    /* these integers are used to determine if this ship_type has already been selected by a player */
    int pb = 0;
    int sb = 0;
    int dd = 0;
    int bb = 0;
    int ca = 0;
    
    /* maximum number of misses is 83 */
    int miss = 00;
    int miss_count = 0;
    
    /* maximum nuber of hits is 17 */
    int hit = 11;
    int hit_count = 0;
    
    /* the player a & player b boards needs to be 10 rows x 10 columns  */
    /* need to present each player with an empty board at start of the game */
    /* need to present each player with a view of just their board after placing ships */
    
    /* Define Functions */
    
    int main(void)
    {
    
    			
    	
    			printf( "\n\n\n\n" );
    
    			printf( "Here is the Player's empty ship board \n\n" );
    
    		/* array has 11 rows, but "rows = 1" is used, because we do not want row 0 printed */
    		/* this for loop starts at row 1 and steps through each row until row 11 is reached */
    		for(row = 1; row < 11; ++row)
    		{
    		
    		/* array has 11 columns, but "column = 1" is used, because we do not want column 0 printed */
    		/* this for loop starts at column 1 and steps through each row until column 11 is reached */
    		for(column = 1; column < 11; ++column)
    
    			/* prints actual array populated with all zero's */
    			printf("\t%i", ships_a[row][column]);
    			printf("\n\n");
    		}
    
    			printf( "It is now time for the player to place his or her's navy \n\n" );
    			/* prints out rules for ship placement and how many grid locations each ship takes up */
    			printf( "Pay attention, you only get one of each ship type for a total of 5 ships \n\n" );
    			printf( "You have to place all 5 of your ships, so use all your fingers to keep tack of the count \n\n" ); 
    			printf( "Get this straight, ships cannot be placed diagonally, so don't be a fool \n\n" );
    			printf( "First player to score a total of 17 hits wins!!!! \n\n" );
    			printf( "Aircraft Carrier = 5   take up horizontal or vertical grid locations \n" );
    			printf( "Battleship = 4         take up horizontal or vertical grid locations \n" );
    			printf( "Destroyer = 3          take up horizontal or vertical grid locations \n" );
    			printf( "Submarine = 3          take up horizontal or vertical grid locations \n" );
    			printf( "Patrol Boat = 2        take up horizontal or vertical grid locations \n\n" );
    		
    			/* prompts player to select the ship type for ship placement */
    			printf( "CA = %i and Ship Count = %i \n\n", ca, ship_count ); /* FOR TROUBLESHOOTING */
    			printf( "Select the ship, 1 - Patrol Boat, 2 - Submarine,3 - Destroyer, 4 - Battleship, 5 - Aircraft Carrier:  " );
    			scanf( "%i", &ship_type );
    			printf( "\n" );
    			
    		/* error checking to make sure ship type selected by the player is between 1 and 5 */
    		if (( ship_type < 1 ) || ( ship_type >= 6 ))
    		{
    			printf( "Hey dummy, you made an incorrect choice, feasting on moron sandwiches again, pick another number for ship type \n\n" );
    		}
    
    		/* informs player which ship type they selected and how many grid locations the ship takes up */
    		else if (( ship_type == 1 ) && ( pb == 0 ))
    		{
    			printf( "You selected %i, the Patrol Boat which takes up 2 grid locations \n\n", ship_type );
    			++pb; /* increments pb count from 0 to 1 so patrol boat cannot be selected again */
    			++ship_count; /* increments the ship count by 1 */
    		}
    
    		else if (( ship_type == 2 ) & ( sb == 0 ))
    		{
    			printf( "You selected %i, the Submarine which takes up 3 grid locations \n\n", ship_type );
    			++sb; /* increments sb count from 0 to 1 so submarine cannot be selected again */
    			++ship_count; /* increments the ship count by 1 */
    		}
    
    		else if (( ship_type == 3 ) && ( dd == 0 ))
    		{
    			printf( "You selected %i, the Destroyer which takes up 3 grid locations \n\n", ship_type );
    			++dd; /* increments dd count from 0 to 1 so destroyer cannot be selected again */
    			++ship_count; /* increments the ship count by 1 */
    		}
    
    		else if (( ship_type == 4 ) && ( bb == 0 ))
    		{
    			printf( "You selected %i, the Battleship which takes up 4 grid locations \n\n", ship_type );
    			++bb; /* increments bb count from 0 to 1 so battleship cannot be selected again */
    			++ship_count; /* increments the ship count by 1 */
    		}
    
    		else if (( ship_type == 5 ) && ( ca ==0 ))
    		{
    			printf( "You selected %i, the Aircraft Carrier which takes up 5 grid locations \n\n", ship_type );
    			++ca; /* increments ca count from 0 to 1 so aircraft carrier cannot be selected again */
    			++ship_count; /* increments the ship count by 1 */
    			printf( "CA = %i and Ship Count = %i \n\n", ca, ship_count ); /* FOR TROUBLESHOOTING */
    		}
    
    		else
    		{
    			printf( "Not paying attention, you already selected that ship type, pony up to the bar and pick another ship type \n\n" );
    		}
    
    			/* prompts player to select a row coordinate between 1 and 10 */
    			printf( "Select the horizontal Row coordinate:  " );
    			scanf( "%i", &row );
    			printf( "\n" );
    
    		/* error checking to make sure the player selected a row coordinate between 1 and 10 */
    		if (( row < 1 ) || ( row >= 11 ))
    
    			printf( "Hey dummy, you made an incorrect choice, we see you're in the remedial group, give it another shot \n\n" );
    		
    		else
    
    			/* informs player of the row number they selected */
    			printf( "You selected Row coordinate %i\n\n", row );
    		
    			
    			/* prompts player to select a column coordinate between 1 and 10 */
    			printf( "Select the vertical Column coordinate:  " );
    			scanf( "%i", &column );
    			printf( "\n" );
    			
    		/* error checking to make sure player selected a column coordinate between 1 and 10 */
    		if (( column < 1 ) || ( column >= 11 ))
    
    			printf( "Hey dummy, you made an incorrect choice, pay attention, and try again \n\n" );
    		
    		else
    			
    			/* informs player of the column number they selected */
    			printf( "You selected Column coordinate %i\n\n", column );
    
    		
    			/* Prompts player to select the direction the ship is point from the selected coordinate */
    			printf( "Select Ship Orientation-Direction, 1 for UP, 2 for DOWN, 3 for LEFT, or 4 for RIGHT:  " );
    			scanf( "%i", &direction );
    			printf( "\n" );
    
    		/* error checking to make sure player selected a direction number between 1 and 4 */
    		if (( direction < 1 ) || ( direction >= 5 ))
    
    			printf( "Hey dummy, you made an incorrect choice, get it together, and give it another go \n\n" );
    		
    		/* informs player what direction they selected */
    		else if ( direction == 1 )
    		
    			printf( "You selected UP for your orientation-direction \n\n", direction );
    		
    		else if (direction == 2)
    		
    			printf( "You selected DOWN for your orientation-direction \n\n", direction );
    		
    		else if (direction == 3)
    		
    			printf( "You selected LEFT for your orientation-direction \n\n", direction );		
    
    		else 
    		
    			printf( "You selected RIGHT for your orientation-direction \n\n\n\n", direction );
    
    
    		/* UP from selected coordinate for aircraft carrier */
    		if (( ship_type == 5 ) && ( direction == 1 ) && ( ships_a[row][column] == 0 ) && ( row - 5 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row - 4][column] = 1;
    			ships_a[row - 3][column] = 1;
    			ships_a[row - 2][column] = 1;
    			ships_a[row - 1][column] = 1;
    		}
    
    		/* DOWN from selected coordinate for aircraft carrier */
    		if (( ship_type == 5 ) && ( direction == 2 ) && ( ships_a[row][column] == 0 ) && ( row + 5 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row + 4][column] = 1;
    			ships_a[row + 3][column] = 1;
    			ships_a[row + 2][column] = 1;
    			ships_a[row + 1][column] = 1;
    		}
    
    		/* LEFT from selected coordinate for aircraft carrier */
    		if (( ship_type == 5 ) && ( direction == 3 ) && ( ships_a[row][column] == 0 ) && ( column - 5 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column - 4] = 1;
    			ships_a[row][column - 3] = 1;
    			ships_a[row][column - 2] = 1;
    			ships_a[row][column - 1] = 1;
    		}
    
    		/* RIGHT from selected coordinate for aircraft carrier */
    		if (( ship_type == 5 ) && ( direction == 4 ) && ( ships_a[row][column] == 0 ) && ( column + 5 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column + 4] = 1;
    			ships_a[row][column + 3] = 1;
    			ships_a[row][column + 2] = 1;
    			ships_a[row][column + 1] = 1;
    		}
    
    		/* UP from selected coordinate for battleship */
    		if (( ship_type == 4 ) && ( direction == 1 ) && ( ships_a[row][column] == 0 ) && ( row - 4 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row - 3][column] = 1;
    			ships_a[row - 2][column] = 1;
    			ships_a[row - 1][column] = 1;
    		}
    
    		/* DOWN from selected coordinate for battleship */
    		if (( ship_type == 4 ) && ( direction == 2 ) && ( ships_a[row][column] == 0 ) && ( row + 4 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row + 3][column] = 1;
    			ships_a[row + 2][column] = 1;
    			ships_a[row + 1][column] = 1;
    		}
    
    		/* LEFT from selected coordinate for battleship */
    		if (( ship_type == 4 ) && ( direction == 3 ) && ( ships_a[row][column] == 0 ) && ( column - 4 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column - 3] = 1;
    			ships_a[row][column - 2] = 1;
    			ships_a[row][column - 1] = 1;
    		}
    
    		/* RIGHT from selected coordinate for battleship */
    		if (( ship_type == 4 ) && ( direction == 4 ) && ( ships_a[row][column] == 0 ) && ( column + 4 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column + 3] = 1;
    			ships_a[row][column + 2] = 1;
    			ships_a[row][column + 1] = 1;
    		}
    
    		/* UP from selected coordinate for destroyer */
    		if (( ship_type == 3 ) && ( direction == 1 ) && ( ships_a[row][column] == 0 ) && ( row - 3 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row - 2][column] = 1;
    			ships_a[row - 1][column] = 1;
    		}
    
    		/* DOWN from selected coordinate for destroyer */
    		if (( ship_type == 3 ) && ( direction == 2 ) && ( ships_a[row][column] == 0 ) && ( row + 3 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row + 2][column] = 1;
    			ships_a[row + 1][column] = 1;
    		}
    
    		/* LEFT from selected coordinate for destroyer */
    		if (( ship_type == 3 ) && ( direction == 3 ) && ( ships_a[row][column] == 0 ) && ( column - 3 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column - 2] = 1;
    			ships_a[row][column - 1] = 1;
    		}
    
    		/* RIGHT from selected coordinate for destroyer */
    		if (( ship_type == 3 ) && ( direction == 4 ) && ( ships_a[row][column] == 0 ) && ( column + 3 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column + 2] = 1;
    			ships_a[row][column + 1] = 1;
    		}
    
    		/* UP from selected coordinate for submarine */
    		if (( ship_type == 2 ) && ( direction == 1 ) && ( ships_a[row][column] == 0 ) && ( row - 3 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row - 2][column] = 1;
    			ships_a[row - 1][column] = 1;
    		}
    
    		/* DOWN from selected coordinate for submarine */
    		if (( ship_type == 2 ) && ( direction == 2 ) && ( ships_a[row][column] == 0 ) && ( row + 3 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row + 2][column] = 1;
    			ships_a[row + 1][column] = 1;
    		}
    
    		/* LEFT from selected coordinate for submarine */
    		if (( ship_type == 2 ) && ( direction == 3 ) && ( ships_a[row][column] == 0 ) && ( column - 3 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column - 2] = 1;
    			ships_a[row][column - 1] = 1;
    		}
    
    		/* RIGHT from selected coordinate for submarine */
    		if (( ship_type == 2 ) && ( direction == 4 ) && ( ships_a[row][column] == 0 ) && ( column + 3 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column + 2] = 1;
    			ships_a[row][column + 1] = 1;
    		}
    
    		/* UP from selected coordinate for patrol boat */
    		if (( ship_type == 1 ) && ( direction == 1 ) && ( ships_a[row][column] == 0 ) && ( row - 2 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row - 1][column] = 1;
    		}
    
    		/* DOWN from selected coordinate for patrol boat */
    		if (( ship_type == 1 ) && ( direction == 2 ) && ( ships_a[row][column] == 0 ) && ( row + 2 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row + 1][column] = 1;
    		}
    
    		/* LEFT from selected coordinate for patrol boat */
    		if (( ship_type == 1 ) && ( direction == 3 ) && ( ships_a[row][column] == 0 ) && ( column - 2 >= 0 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column - 1] = 1;
    		}
    
    		/* RIGHT from selected coordinate for patrol boat */
    		if (( ship_type == 1 ) && ( direction == 4 ) && ( ships_a[row][column] == 0 ) && ( column + 2 <= 11 ))
    		{
    			ships_a[row][column] = 1;
    			ships_a[row][column + 2] = 1;
    			ships_a[row][column + 1] = 1;
    		}
    
    		else
    		{
    			printf( "The ship will not fit the grid you selected, get on the ball, select a different grid \n\n" );
    		}
    
    		
    
    		/* array has 11 rows, but "rows = 1" is used, because we do not want row 0 printed */
    		/* this for loop starts at row 1 and steps through each row until row 11 is reached */
    		for(row = 1; row < 11; ++row)
    		{
    		
    		/* array has 11 columns, but "column = 1" is used, because we do not want column 0 printed */
    		/* this for loop starts at column 1 and steps through each row until column 11 is reached */
    		for(column = 1; column < 11; ++column)
    
    			printf("\t%i", ships_a[row][column]);
    			printf("\n\n");
    		}
    
    
    
    			printf( "Print blank board for Player B to shoot at Player A's ships \n\n" ); 
    		
    		/* array has 11 rows, but "rows = 1" is used, because we do not want row 0 printed */
    		/* this for loop starts at row 1 and steps through each row until row 11 is reached */
    		for(row = 1; row < 11; ++row)
    		{
    		
    		/* array has 11 columns, but "column = 1" is used, because we do not want column 0 printed */
    		/* this for loop starts at column 1 and steps through each row until column 11 is reached */
    		for(column = 1; column < 11; ++column)
    
    			/* prints actual array populated with all zero's */
    			printf("\t%i", guess_b[row][column]);
    			printf("\n\n");
    		}
    
    
    			/* prompts player to select a row coordinate between 1 and 10 */
    			printf( "Select the horizontal Row coordinate:  " );
    			scanf( "%i", &row );
    			printf( "\n" );
    
    		/* error checking to make sure the player selected a row coordinate between 1 and 10 */
    		if (( row < 1 ) || ( row >= 11 ))
    
    			printf( "Hey dummy, you made an incorrect choice, we see you're in the remedial group, give it another shot \n\n" );
    		
    		else
    
    			/* informs player of the row number they selected */
    			printf( "You selected Row coordinate %i\n\n", row );
    		
    			
    			/* prompts player to select a column coordinate between 1 and 10 */
    			printf( "Select the vertical Column coordinate:  " );
    			scanf( "%i", &column );
    			printf( "\n" );
    			
    		/* error checking to make sure player selected a column coordinate between 1 and 10 */
    		if (( column < 1 ) || ( column >= 11 ))
    
    			printf( "Hey dummy, you made an incorrect choice, pay attention, and try again \n\n" );
    		
    		else
    			
    			/* informs player of the column number they selected */
    			printf( "You selected Column coordinate %i\n\n", column );
    		
    		if ( ships_a[row][column] = 1 )
    		{
    			printf( "IT'S A HIT!!! \n\n" );
    			guess_b[row][column] = hit;
    			++hit_count;
    			printf( "Hit count is %i \n\n", hit_count ); /* FOR TROUBLESHOOTING */
    		}
    
    		else
    		{
    			printf( "You missed \n\n" );
    			guess_b[row][column] = miss;
    			++miss_count;
    			printf( "Miss count is %i \n\n", miss_count ); /* FOR TROUBLESHOOTING */
    		}
    
    		/* array has 11 rows, but "rows = 1" is used, because we do not want row 0 printed */
    		/* this for loop starts at row 1 and steps through each row until row 11 is reached */
    		for(row = 1; row < 11; ++row)
    		{
    		
    		/* array has 11 columns, but "column = 1" is used, because we do not want column 0 printed */
    		/* this for loop starts at column 1 and steps through each row until column 11 is reached */
    		for(column = 1; column < 11; ++column)
    
    			/* prints actual array populated with all zero's */
    			printf("\t%i", guess_b[row][column]);
    			printf("\n\n");
    		}
    
    
    			printf( "\n\n\n\n" );
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if ( ships_a[row][column] = 1 )
    That will always evaluate to true.

    = is assignment.
    == is comparison.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Post Missing the second = was the problem

    Arrgh, missing the second = was the problem, and thanks for you help. Still having an issue with the miss not posting a "00" in the array, and I will reply if I can't fix it. Please post something if you see another issue with my code that could be causing this

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Smile Nevermind

    Nevermind, I change the int miss = 00; int miss = 55;, and that fixed the issue. If I had set it to 0000, the value is still zero, and only a single 0 will be displayed. Besides, the 55 is like the ss in miss.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there any reason you're starting at 1 instead of 0?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    I was eventuallu going to label the rows and columns 1 through 10 using the 0 row and 0 column for the labeling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  2. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 03:26 AM
  3. Multidementional arrays, need concept explanation??
    By Leojeen in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 11:56 AM
  4. Help with 2 dimensional arrays
    By riotact in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2002, 10:19 AM
  5. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM