-
Battle ships help
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;
}
-
To debug and test a game like Battleship, you MUST be able to show the current array, which is the game board.
So first, add a function to display the board:
Code:
void ShowBoard(void) {
int r, c;
for(r = 0; r < ROW; r++) {
printf("\n");
for(c = 0; c < COLUMN; c++) {
printf("%c ", gameBoard[r][c]);
}
}
}
NOW you can see what's going on with your gameBoard, as the game goes on. For testing, call this function after every move, and check that the move you made, was the move that was reflected properly on the gameBoard array.
I do wish when you're working on a program, that you worked ONLY on the basic algorithm and flow control at first, and left all the detail functions and lines of code, UNTIL THE VERY LAST.
They simply make it much more difficult to study your code, and find problems.
Anyway, try adding that, and see what it shows you.
-
hi,
u should first work out in our mind ( or on paper :p ) how ur program is supposed to work:
u put ur ships in an 10x10 char array.
a position that contains a ship is marked with an identifying character ( D- destroyer, B Battleship,...) a position that is empty, contains an X (maybe a space ' ' would be better for visibility )
in ur gameplay loop the player must input a position (x,y) now u must now do what ?
- yes check, if there is a non empty ( != X and != * ) char at this position
( btw: gameBoard[column][row] = mark; places the mark into/ overwrites the gameboard u would probably want it the other way round )
if yes, u have a hit and maybe count the no# of hits and mark this position as hit ( maybe by putting an * at that position ) so that this position couldn't be counted again as hit.
work ur way through step by step , first with paper&pencil before u start to code, make small changes to the code, check the output after each modification ( by printing the board out as eisiminger already pointed out ) then u will succeed ;)