Code:
/* 5 types of ship: Aircraft Carrier(5), Battleship(4), Submarine(3), Cruiser(3), Destroyer(2) */
#include <stdio.h>
//NEED TO FIND A WAY TO COMPARE 17 VARIABLES TO CHECK FOR UNIQUE VALUES!!!//
int main()
{
int p1_a_y1, p1_a_y2, p1_a_y3, p1_a_y4, p1_a_y5, p1_b_y1, p1_b_y2, p1_b_y3, p1_b_y4, p1_s_y1, p1_s_y2, p1_s_y3, p1_c_y1, p1_c_y2, p1_c_y3, p1_d_y1, p1_d_y2, p2_a_y1, p2_a_y2, p2_a_y3, p2_a_y4, p2_a_y5, p2_b_y1, p2_b_y2, p2_b_y3, p2_b_y4, p2_s_y1, p2_s_y2, p2_s_y3, p2_c_y1, p2_c_y2, p2_c_y3, p2_d_y1, p2_d_y2;
/* These are y positions */
char p1_a_x1, p1_a_x2, p1_a_x3, p1_a_x4, p1_a_x5, p1_b_x1, p1_b_x2, p1_b_x3, p1_b_x4, p1_s_x1, p1_s_x2, p1_s_x3, p1_c_x1, p1_c_x2, p1_c_x3, p1_d_x1, p1_d_x2, p2_a_x1, p2_a_x2, p2_a_x3, p2_a_x4, p2_a_x5, p2_b_x1, p2_b_x2, p2_b_x3, p2_b_x4, p2_s_x1, p2_s_x2, p2_s_x3, p2_c_x1, p2_c_x2, p2_c_x3, p2_d_x1, p2_d_x2;
/* These are x postions, one for each section of the corresponding ship */
char p1_a_pos, p1_b_pos, p1_s_pos, p1_c_pos, p1_d_pos, p2_a_pos, p2_b_pos, p2_s_pos, p2_c_pos, p2_d_pos;
/* These are either horizontal or vertical */
char p1_a_coord1[2], p1_a_coord2[2], p1_a_coord3[2], p1_a_coord4[2], p1_a_coord5[2], p1_b_coord1[2], p1_b_coord2[2], p1_b_coord3[2], p1_b_coord4[2], p1_s_coord1[2], p1_s_coord2[2], p1_s_coord3[2], p1_c_coord1[2], p1_c_coord2[2], p1_c_coord3[2], p1_d_coord1[2], p1_d_coord2[2];
char p2_a_coord1[2], p2_a_coord2[2], p2_a_coord3[2], p2_a_coord4[2], p2_a_coord5[2], p2_b_coord1[2], p2_b_coord2[2], p2_b_coord3[2], p2_b_coord4[2], p2_s_coord1[2], p2_s_coord2[2], p2_s_coord3[2], p2_c_coord1[2], p2_c_coord2[2], p2_c_coord3[2], p2_d_coord1[2], p2_d_coord2[2];
/* These are to check if ships overlap or not using sprintf() */
int grid_y=1, y_loc, menu, p1_ship, p2_ship, a;
char grid_x='A', x_loc, c, buffer[3], grid[9][9];
printf("\n###### WELCOME TO BATTLESHIPS! ######\n\n");
/*Main Menu*/
do
{
printf("### MAIN MENU ###");
printf("\n1) View Grid");
printf("\n2) Setup Player 1 Ships");
printf("\n3) Setup Player 2 Ships");
printf("\n4) Start Firing!");
printf("\n5) Quit\n");
scanf("%i", &menu);
switch(menu)
{
case 1: /* Print the grid to play on */
grid_y=1;
grid_x='A';
for(grid_y=1;grid_y<10;grid_y++)
{
for(grid_x='A';grid_x<'J';grid_x++)
{
printf("%c%i ",grid_x,grid_y);
}
printf("\n");
}
printf("\n");
break;
case 2: /* PLayer 1 setup */
do{
printf("\n### Player 1 Setup ###");
printf("\n1) Aircraft Carrier");
printf("\n2) Battleship");
printf("\n3) Submarine");
printf("\n4) Destroyer");
printf("\n5) Patrol Boat");
printf("\n6) Check Boat Alignment");
printf("\n7) Main Menu\n");
scanf(" %i", &p1_ship);
switch(p1_ship)
{
case 1: /* Player 1 Aircraft Carrier */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p1_a_x1);
if (p1_a_x1>'J'||p1_a_x1<'A')
{
printf("\nInvalid choice!\n");
break;
}
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p1_a_y1);
if(p1_a_y1<1||p1_a_y1>9)
{
printf("\nInvalid choice!\n");
break;
}
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p1_a_pos);
if (p1_a_pos=='h'||p1_a_pos=='H')
{
p1_a_x2=p1_a_x1+1;
p1_a_x3=p1_a_x1+2;
p1_a_x4=p1_a_x1+3;
p1_a_x5=p1_a_x1+4;
p1_a_y2=p1_a_y1;
p1_a_y3=p1_a_y1;
p1_a_y4=p1_a_y1;
p1_a_y5=p1_a_y1;
if(p1_a_x5>='K')
{
printf("\nSelected position is out of bounds!\n");
}
}
else if (p1_a_pos=='v'||p1_a_pos=='V')
{
p1_a_y2=p1_a_y1+1;
p1_a_y3=p1_a_y1+2;
p1_a_y4=p1_a_y1+3;
p1_a_y5=p1_a_y1+4;
p1_a_x2=p1_a_x1;
p1_a_x3=p1_a_x1;
p1_a_x4=p1_a_x1;
p1_a_x5=p1_a_x1;
if(p1_a_y5>=10)
{
printf("\nSelected position is out of bounds!\n");
}
}
else
{
printf("\nInvalid choice!\n");
}
sprintf(p1_a_coord1,"%c%i",p1_a_x1,p1_a_y1);
sprintf(p1_a_coord2,"%c%i",p1_a_x2,p1_a_y2);
sprintf(p1_a_coord3,"%c%i",p1_a_x3,p1_a_y3);
sprintf(p1_a_coord4,"%c%i",p1_a_x4,p1_a_y4);
sprintf(p1_a_coord5,"%c%i",p1_a_x5,p1_a_y5);
printf("%s, %s, %s, %s, %s",p1_a_coord1,p1_a_coord2,p1_a_coord3,p1_a_coord4,p1_a_coord5);
break;
case 2: /* Player 1 Battleship */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p1_b_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p1_b_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p1_b_pos);
if (p1_b_pos='h')
{
p1_b_x2=p1_b_x1+1;
p1_b_x3=p1_b_x1+2;
p1_b_x4=p1_b_x1+3;
p1_b_y2=p1_b_y1;
p1_b_y3=p1_b_y1;
p1_b_y4=p1_b_y1;
}
else if(p1_b_pos='v')
{
p1_b_y2=p1_b_y1+1;
p1_b_y3=p1_b_y1+2;
p1_b_y4=p1_b_y1+3;
p1_b_x2=p1_b_x1;
p1_b_x3=p1_b_x1;
p1_b_x4=p1_b_x1;
}
else
{
printf("\nNot a valid selection.\n");
}
sprintf(p1_b_coord1,"%c%i",p1_b_x1,p1_b_y1);
sprintf(p1_b_coord2,"%c%i",p1_b_x2,p1_b_y2);
sprintf(p1_b_coord3,"%c%i",p1_b_x3,p1_b_y3);
sprintf(p1_b_coord4,"%c%i",p1_b_x4,p1_b_y4);
printf("%s, %s, %s, %s",p1_b_coord1,p1_b_coord2,p1_b_coord3,p1_b_coord4);
break;
case 3: /* Player 1 Submarine */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p1_s_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p1_s_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p1_s_pos);
if (p1_s_pos='h')
{
p1_s_x2=p1_s_x1+1;
p1_s_x3=p1_s_x1+2;
p1_s_y2=p1_s_y1;
p1_s_y3=p1_s_y1;
}
else if(p1_s_pos='v')
{
p1_s_y2=p1_s_y1+1;
p1_s_y3=p1_s_y1+2;
p1_s_x2=p1_s_x1;
p1_s_x3=p1_s_x1;
}
else
{
printf("\nNot a valid selection.\n");
}
sprintf(p1_s_coord1,"%c%i",p1_s_x1,p1_s_y1);
sprintf(p1_s_coord2,"%c%i",p1_s_x2,p1_s_y2);
sprintf(p1_s_coord3,"%c%i",p1_s_x3,p1_s_y3);
printf("%s, %s, %s, %s",p1_s_coord1,p1_s_coord2,p1_s_coord3);
break;
case 4: /* Player 1 Cruiser */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p1_c_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p1_c_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p1_c_pos);
if (p1_c_pos='h')
{
p1_c_x2=p1_c_x1+1;
p1_c_x3=p1_c_x1+2;
p1_c_y2=p1_c_y1;
p1_c_y3=p1_c_y1;
}
else if(p1_c_pos='v')
{
p1_c_y2=p1_c_y1+1;
p1_c_y3=p1_c_y1+2;
p1_c_x2=p1_c_x1;
p1_c_x3=p1_c_x1;
}
else
{
printf("\nNot a valid selection.\n");
}
sprintf(p1_c_coord1,"%c%i",p1_c_x1,p1_c_y1);
sprintf(p1_c_coord2,"%c%i",p1_c_x2,p1_c_y2);
sprintf(p1_c_coord3,"%c%i",p1_c_x3,p1_c_y3);
printf("%c, %c, %c, %c",p1_c_coord1,p1_c_coord2,p1_c_coord3);
break;
case 5: /* Player 1 Destroyer */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p1_d_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p1_d_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p1_d_pos);
if (p1_d_pos='h')
{
p1_d_x2=p1_d_x1+1;
p1_d_y2=p1_d_y1;
}
else if(p1_d_pos='v')
{
p1_d_y2=p1_d_y1+1;
p1_d_x2=p1_d_x1;
}
else
{
printf("\nNot a valid selection.\n");
}
sprintf(p1_d_coord1,"%c%i",p1_d_x1,p1_d_y1);
sprintf(p1_d_coord2,"%c%i",p1_d_x2,p1_d_y2);
printf("%c, %c, %c, %c",p1_d_coord1,p1_d_coord2);
break;
case 6: /* Player 1 boat check */
if()
{
}
break;
}
}while (p1_ship!=7);
break;
case 3: /* Player 2 Setup */
do{
printf("\n### Player 2 Setup ###");
printf("\n1) Aircraft Carrier");
printf("\n2) Battleship");
printf("\n3) Submarine");
printf("\n4) Destroyer");
printf("\n5) Patrol Boat");
printf("\n6) Boat Alignment Check");
printf("\n7) Main Menu\n");
scanf(" %i", &p2_ship);
switch(p2_ship)
{
case 1: /* Player 2 Aircraft carrier */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p2_a_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p2_a_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p2_a_pos);
if (p2_a_pos='h')
{
p2_a_x2=p2_a_x1+1;
p2_a_x3=p2_a_x1+2;
p2_a_x4=p2_a_x1+3;
p2_a_x5=p2_a_x1+4;
}
else if (p2_a_pos='v')
{
p2_a_y2=p2_a_y1+1;
p2_a_y3=p2_a_y1+2;
p2_a_y4=p2_a_y1+3;
p2_a_y5=p2_a_y1+4;
}
else
{
printf("\nNot a valid selection.\n");
}
break;
case 2: /* Player 2 Battleship */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p2_b_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p2_b_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p2_b_pos);
if (p2_b_pos='h')
{
p2_b_x2=p2_b_x1+1;
p2_b_x3=p2_b_x1+2;
p2_b_x4=p2_b_x1+3;
}
else if(p2_b_pos='v')
{
p2_b_y2=p2_b_y1+1;
p2_b_y3=p2_b_y1+2;
p2_b_y4=p2_b_y1+3;
}
else
{
printf("\nNot a valid selection.\n");
}
break;
case 3: /* Player 2 Submarine */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p2_s_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p2_s_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p2_s_pos);
if (p2_s_pos='h')
{
p2_s_x2=p2_s_x1+1;
p2_s_x3=p2_s_x1+2;
}
else if(p2_s_pos='v')
{
p2_s_y2=p2_s_y1+1;
p2_s_y3=p2_s_y1+2;
}
else
{
printf("\nNot a valid selection.\n");
}
break;
case 4: /* Player 2 Cruiser */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p2_c_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p2_c_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p2_c_pos);
if (p2_c_pos='h')
{
p2_c_x2=p2_c_x1+1;
p2_c_x3=p2_c_x1+2;
}
else if(p2_c_pos='v')
{
p2_c_y2=p2_c_y1+1;
p2_c_y3=p2_c_y1+2;
}
else
{
printf("\nNot a valid selection.\n");
}
break;
case 5: /* Player 2 Destroyer */
printf("\nEnter starting X co-ordinate (A-J): ");
scanf(" %c", &p2_d_x1);
printf("\nEnter starting Y co-ordinate (1-9): ");
scanf(" %i", &p2_d_y1);
printf("\nHorizontal(h) or vertical(v) alignment: ");
scanf(" %c", &p2_d_pos);
if (p2_d_pos='h')
{
p2_d_x2=p2_d_x1+1;
}
else if(p2_d_pos='v')
{
p2_d_y2=p2_d_y1+1;
}
else
{
printf("\nNot a valid selection.\n");
}
break;
case 6: /* Player 2 Boat check */
break;
}
}while (p2_ship!=7);
break;
case 4:
printf("Let's play battleships!\n\n");
break;
}
}
while(menu!=5);
printf("Quitting...\n");
}