This is a project we're doing in class. In the randomizeShips function, I will get two horizontal ships and one vertical ship, which is what I want. Although, sometimes I think they might be overlapping because sometimes there will only be two ships instead of three, any help? Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
#define BAT_NO_SEL 0
#define NOBAT_NO_SEL 1
#define MISS 2
#define HIT 3
#define NUM_SHIPS 3
void printHidden(int arr[SIZE][SIZE]);
void printBoard(int arr[SIZE][SIZE]);
void randomizeShips(int arr[SIZE][SIZE], int num_ships);
int checkHit(int arr[SIZE][SIZE], int row, int col);
int checkWin(int arr[SIZE][SIZE]);
void initializeBoard(int arr[SIZE][SIZE]);
int main (void) {
int board[SIZE][SIZE];
int numShips;
srand((unsigned int) time (0));
printf("Let's play Battleship!\n");
printf("* is a hit\n");
printf("o is a miss\n\n");
initializeBoard(board);
randomizeShips(board, NUM_SHIPS);
printBoard(board);
printHidden(board);
}
//set all board values to no battleship/not selected
void initializeBoard(int arr[SIZE][SIZE]) {
int r, c;
for(r = 0; r < SIZE; r++) {
for(c = 0; c < SIZE; c++) {
arr[r][c] = NOBAT_NO_SEL;
}
}
}
//print out the board the user will see
//print "~" if no battleship selected or battleship not selected
//print "*" if hit
//print "o" if miss
void printBoard(int arr[SIZE][SIZE]) {
int r,c,i;
//Number coordinates
printf(" ");
for(i = 1; i < SIZE+1; i++) {
printf("%i ", i);
}
printf("\n");
for(r = 0; r < SIZE; r++) {
//Letter coordinates
printf("%c ", (char) (r+65));
for(c = 0; c < SIZE ; c++) {
if(arr[r][c] == NOBAT_NO_SEL || arr[r][c] == BAT_NO_SEL) {
printf("%s", "~ ");
}
else if(arr[r][c] == MISS ) {
printf("%s", "o ");
}
else if(arr[r][c] == HIT ) {
printf("%s", "* ");
}
}
printf("\n");
}
}
//print out the DEBUG board
//print "s" if there is battleship that hasnt been selected
//print "0" if there is no battleship that hasnt been selected
//print "h" if hit
//print "m" if miss
void printHidden(int arr[SIZE][SIZE]) {
int r, c;
for(r = 0; r < SIZE; r++) {
for(c = 0; c < SIZE; c++) {
if(arr[r][c] == BAT_NO_SEL) {
printf("%c", '!');
}
else if(arr[r][c] == NOBAT_NO_SEL) {
printf("%c", '0');
}
else if(arr[r][c] == MISS ) {
printf("%c", 'm');
}
else if(arr[r][c] == HIT ) {
printf("%c", 'h');
}
}
printf("\n");
}
}
//Place three ships on the board
//These need to be a random selection of:
//- horizontal/vertical
//- starting at different locations
//Ensure they stay in bounds
//Ensure they don't overlap
void randomizeShips(int arr[SIZE][SIZE], int num_ships) {
int horizontal, row, col, i, j;
for(i = 0; i < num_ships; i++) {
horizontal = i % 2;
if(horizontal == 0) {
col = rand() % 8;
row = rand() % 10;
if(arr[row][col] == BAT_NO_SEL) {
continue;
}
else if(arr[row][col+1] == BAT_NO_SEL) {
continue;
}
else if(arr[row][col+2] == BAT_NO_SEL) {
continue;
}
else {
for(j = 0; j < 3; j++) {
arr[row][col+j] = BAT_NO_SEL;
}
}
}
else {
col = rand() % 10;
row = rand() % 8;
if(arr[row][col] == BAT_NO_SEL) {
continue;
}
else if(arr[row+1][col] == BAT_NO_SEL) {
continue;
}
else if(arr[row+2][col] == BAT_NO_SEL) {
continue;
}
else {
for(j = 0; j < 3; j++) {
arr[row+j][col] = BAT_NO_SEL;
}
}
}
}
}
int checkHit(int arr[SIZE][SIZE], int row, int col) {
}
int checkWin(int arr[SIZE][SIZE]) {
}