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" ); }