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