How can I get the user to change the size of the board in Battleship?
I'm trying to do that as an extra credit option for my class. The teacher said to malloc the array, but I dont really understand how to do this. Any help is appreciated, 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];
char letter;
int number;
int count = 0;
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);
while(checkWin(board) == 0 && count/2 <= 100) {
fflush(stdin);
printf("Enter letter coordinate:\n");
scanf("%c", &letter);
printf("Enter number coordinate:\n");
scanf("%i", &number);
if(checkHit(board, letter, number)== 4) {
count--;
}
printBoard(board);
printHidden(board);
checkWin(board);
count++;
}
printf("Turns it took to win: %i\n", count);
}
//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++) {
if(i > 9) {
printf("%i ", i);
}
else {
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");
}
}
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("%i", BAT_NO_SEL);
}
else if(arr[r][c] == NOBAT_NO_SEL) {
printf("%i", NOBAT_NO_SEL);
}
else if(arr[r][c] == MISS ) {
printf("%i", MISS);
}
else if(arr[r][c] == HIT ) {
printf("%i", HIT );
}
}
printf("\n");
}
}
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) {
i--;
continue;
}
else if(arr[row][col+1] == BAT_NO_SEL) {
i--;
continue;
}
else if(arr[row][col+2] == BAT_NO_SEL) {
i--;
continue;
}
else {
for(j = 0; j < 3; j++) {
arr[row][col+j] = BAT_NO_SEL;
}
}
}
else {
col = rand() % 8;
row = rand() % 10;
if(arr[col][row] == BAT_NO_SEL) {
i--;
continue;
}
else if(arr[col+1][row] == BAT_NO_SEL) {
i--;
continue;
}
else if(arr[col+2][row] == BAT_NO_SEL) {
i--;
continue;
}
else {
for(j = 0; j < 3; j++) {
arr[col+j][row] = BAT_NO_SEL;
}
}
}
}
}
int checkHit(int arr[SIZE][SIZE], int row, int col) {
int i;
col--;
if(row >= 'A' && row <= 'J') {
row = row -'A';
}
else if (row >= 'a' || row <= 'j') {
row = row - 'a';
}
else {
//Tell user if letter is out of bounds
printf("**Letter and/or number out of bounds, try again!**\n");
return 4;
}
//Tell user if number is out of bounds
if(col > SIZE-1) {
printf("**Letter and/or number out of bounds, try again!**\n");
return 4;
}
//Tell user if position is already taken
if(arr[row][col] == HIT || arr[row][col] == MISS) {
printf("**Position already taken, try again!**\n");
return 4;
}
//Tell user if they HIT or MISS
for(i = 0; i < 100; i++) {
if(arr[row][col] == BAT_NO_SEL) {
arr[row][col] = HIT;
printf("\n****HIT!****\n");
return 3;
}
else if(arr[row][col] == NOBAT_NO_SEL) {
arr[row][col] = MISS;
printf("\n****MISS!****\n");
return 2;
}
}
}
int checkWin(int arr[SIZE][SIZE]) {
int r, c, count = 0;
for(r = 0; r < SIZE; r++) {
for(c = 0; c < SIZE; c++) {
if(arr[r][c] == HIT) {
count++;
if(count == 9)
return 1;
}
}
}
return 0;
}