Hi,
I'm just starting out in programming with C. Our first assignment is to write a battleships game with the following parameters.
Place five ships in the array:
Destroyer (2 squares)
Submarine (3 squares)
Cruiser (3 squares)
Battleship (4 squares)
Aircraft Carrier (5 squares)
You may hard-code the ships into place so that they are in the same place every game. Code to do this is attached.
You do not need to display any game screen. This will be a one-person game.
Example:
Fire to what coordinates? (x y): 3 2
Spot 3,2 is a miss.
Fire again? (Y/N): Y
Fire to what coordinates? (x y): 4 6
i am a little lost right now, so any help is appreciated. My code is
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROW
#define COLUMN
int gameBoard[10][10];
int gamePlay(void);
void initializeBoard(void);
void setShips(void);
void splash(void);
int main ( void )
{
initializeBoard();
setShips();
splash();
gamePlay();
}
void initializeBoard(void)
{
for (int i = 0; i<10; i++)
{
for (int j = 0; j < 10; j++)
{
gameBoard[i][j] = 'X';
}
}
}
void setShips(void)
{
for (int d=0; d<=2; d++)
{
gameBoard[1][d] = 'D';
};
for (int s=0; s<=3; s++)
{
gameBoard[3][s] = 'S';
};
for (int c=0; c<=3; c++)
{
gameBoard[5][c] = 'C';
};
for (int b=0; b<=4; b++)
{
gameBoard[7][b] = 'B';
};
for (int a=0; a<=5; a++)
{
gameBoard[9][a] = 'A';
};
}
void splash(void)
{
system("tput clear");
printf("Welcome to my battleships game");
getchar();
system("tput clear");
}
int gamePlay(void)
{
char miss ='M';
char hit ='H';
char mark ='*';
int column, row;
system("tput clear");
printf("Fire to what coordinates? (x y): ");
scanf("%d %d", &column, &row);
column -=1;
row -=1;
printf("%d and %d", column, row);
gameBoard[column][row] = mark;
if (mark == 'X')
{
switch(mark)
{
case 1:
miss = 'M';
printf(" You missed!");
break;
case 2:
hit = 'H';
printf("Direct hit!");
break;
};
};
return 0;
}