Created
December 8, 2019 21:03
-
-
Save pkakelas/99ecdfb489a447cfbc7f010582f5ae57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <time.h> | |
#define SIZE 6 | |
// Do you understand this code? This is how proper validation is done | |
//Board print | |
void printBoard(char B[SIZE][SIZE]) { | |
int i, j; | |
for (i = 0; i < SIZE; ++i) { | |
printf("[ "); | |
for (j = 0; j < SIZE; ++j) { | |
printf("%c ", B[i][j]); | |
} | |
printf("]\n"); | |
} | |
} | |
// Return random number using rand | |
int getRandomNumber(int from, int to) { | |
// Create borders of random number | |
return from + rand() % (to + 1 - from); | |
} | |
//TODO: Handle validation if ship doesn't fit to the position | |
int getStartX() { | |
int start_x; | |
while (true) { | |
printf("Please select the start x location of your ship: "); | |
scanf("%i", &start_x); | |
if (start_x >= 0 && start_x < SIZE) { | |
break; | |
} | |
printf("Please enter a valid start_x\n"); | |
} | |
return start_x; | |
} | |
//TODO: Handle validation if ship doesn't fit to the position | |
int getStartY() { | |
int start_y; | |
while (true) { | |
printf("Please select the start y location of your ship: "); | |
scanf("%i", &start_y); | |
if (start_y >= 0 && start_y < SIZE) { | |
break; | |
} | |
printf("Please enter a valid start_y\n"); | |
} | |
return start_y; | |
} | |
int getOrientation() { | |
int orientation; | |
while (true) { | |
printf("Please select the start y location of your ship: "); | |
printf("Horizontally (1) or vertically (2): "); | |
scanf("%i", &orientation); | |
if (orientation == 1 || orientation == 2) { | |
break; | |
} | |
printf("Please enter a valid orientation\n"); | |
} | |
return orientation; | |
} | |
// Asks user for all the data and creates a 2d array which every row has this format: | |
// {lengthOfBoard, start_x, start_y, orientation} | |
void getShipData(int shipData[2][4]) { | |
printf("Enter data for your battleship\n"); | |
shipData[0][0] = 4; | |
shipData[0][1] = getStartX(); | |
shipData[0][2] = getStartY(); | |
shipData[0][3] = getOrientation(); | |
printf("Enter data for your patrol ship\n"); | |
shipData[1][0] = 2; | |
shipData[1][1] = getStartX(); | |
shipData[1][2] = getStartY(); | |
shipData[1][3] = getOrientation(); | |
} | |
//Fill board with 'o' | |
void fillBoardWithWater(char B[SIZE][SIZE]) { | |
int i, j; | |
for (i = 0; i < SIZE; ++i) { | |
for (j = 0; j < SIZE; ++j) { | |
B[i][j] = 'o'; | |
} | |
} | |
} | |
void addShipsToBoard(char B[SIZE][SIZE], int data[2][4]) { | |
int i, j; | |
for (j = 0; j < 2; ++j) { | |
// if horizontal | |
if (data[j][3] == 1) { | |
// Then add 's' to the row | |
for (i = data[j][2]; i <= data[j][0]; ++i) { | |
B[data[j][1]][i] = 's'; | |
} | |
} | |
else { | |
// Then add 's' to the column | |
for (i = data[j][1]; i <= data[j][0]; ++i) { | |
B[i][data[j][2]] = 's'; | |
} | |
} | |
} | |
} | |
//Main function for creating the board of the user filled with ships | |
void getuserBoard() { | |
char userBoard[SIZE][SIZE]; | |
int shipData[2][4]; | |
fillBoardWithWater(userBoard); | |
getShipData(shipData); | |
addShipsToBoard(userBoard, shipData); | |
printBoard(userBoard); | |
printf("Battleship %i %i %i %i", shipData[0][0], shipData[0][1], shipData[0][2], shipData[0][3]); | |
} | |
void createFakeShips(int shipData[2][4]) { | |
//Length of ship | |
shipData[0][0] = 4; | |
shipData[0][1] = getRandomNumber(0, SIZE - shipData[0][0]); //Random x_start | |
shipData[0][2] = getRandomNumber(0, SIZE - shipData[0][0]); //Random y_start | |
shipData[0][3] = getRandomNumber(0, 1); //Random orientation | |
shipData[1][0] = 2; | |
shipData[1][1] = getRandomNumber(0, SIZE - shipData[0][0]); //Random x_start | |
shipData[1][2] = getRandomNumber(0, SIZE - shipData[0][0]); //Random y_start | |
shipData[1][3] = getRandomNumber(0, 1); //Random orientation | |
} | |
//Main function for creating the board of the computer filled with RANDOM ships | |
void getComputerBoard() { | |
char computerBoard[SIZE][SIZE]; | |
int shipData[2][4]; | |
fillBoardWithWater(computerBoard); | |
createFakeShips(shipData); | |
} | |
int main() { | |
// Create randomness using time | |
srand(time(NULL)); | |
//getuserBoard(); | |
getComputerBoard(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment