Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <unistd.h>
- int player1ShipPlacement();
- int player2ShipPlacement();
- int player1Turn();
- int player2Turn();
- int player1Board[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
- int player2Board[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
- int player1BoardDisplayed[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
- int player2BoardDisplayed[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
- char carrierVertical;
- char battleshipVertical;
- char cruiserVertical;
- char submarineVertical;
- char destroyerVertical;
- /*
- Carrier: 5
- Battleship: 4
- Cruiser: 3
- Submarine: 3
- Destroyer: 2
- */
- int main() {
- printf(" ____ ____ _ ____ _____ ____ _ _____ _____ _ _____ ____ _ _ ___ ____ ____ \n");
- printf("/ ___|| _ \\ / \\ / ___| ____| | __ ) / \\|_ _|_ _| | | ____/ ___|| | | |_ _| _ \\/ ___| \n");
- printf("\\___ \\| |_) / _ \\| | | _| | _ \\ / _ \\ | | | | | | | _| \\___ \\| |_| || || |_) \\___ \\ \n");
- printf(" ___) | __/ ___ \\ |___| |___ | |_) / ___ \\| | | | | |___| |___ ___) | _ || || __/ ___) |\n");
- printf("|____/|_| /_/ \\_\\____|_____| |____/_/ \\_\\_| |_| |_____|_____|____/|_| |_|___|_| |____/ \n\n");
- int i;
- for (i=0;i<50;i++) {
- system("color 17");
- system("color 40");
- }
- system("color f0");
- system("pause");
- player1ShipPlacement();
- player2ShipPlacement();
- return 0;
- }
- int player1ShipPlacement() {
- system("cls");
- system("color 0b");
- printf("PLAYER 1 SHIP PLACEMENT\n");
- bool check1 = false;
- bool check2 = false;
- bool check3 = false;
- bool check4 = false;
- bool check5 = false;
- int coordinateX;
- int coordinateY;
- while (check1 == false) {
- printf("The carrier ship is 5 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &carrierVertical);
- check1 = true;
- printf("Where would you like to place the carrier?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (carrierVertical){
- case ('V'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX][coordinateY+1] == 0) &&
- (player1Board[coordinateX][coordinateY+2] == 0) &&
- (player1Board[coordinateX][coordinateY+3] == 0) &&
- (player1Board[coordinateX][coordinateY+4] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX][coordinateY + 1] = 1;
- player1Board[coordinateX][coordinateY + 2] = 1;
- player1Board[coordinateX][coordinateY + 3] = 1;
- player1Board[coordinateX][coordinateY + 4] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check1 = false;
- }
- break;
- case ('H'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX + 1][coordinateY] == 0) &&
- (player1Board[coordinateX + 2][coordinateY] == 0) &&
- (player1Board[coordinateX + 3][coordinateY] == 0) &&
- (player1Board[coordinateX + 4][coordinateY] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX + 1][coordinateY] = 1;
- player1Board[coordinateX + 2][coordinateY] = 1;
- player1Board[coordinateX + 3][coordinateY] = 1;
- player1Board[coordinateX + 4][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check1 = false;
- }
- }
- }
- while (check2 == false) {
- check2 = true;
- printf("The battleship is 4 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &battleshipVertical);
- printf("Where would you like to place the battleship?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (battleshipVertical){
- case ('V'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX][coordinateY+1] == 0) &&
- (player1Board[coordinateX][coordinateY+2] == 0) &&
- (player1Board[coordinateX][coordinateY+3] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX][coordinateY + 1] = 1;
- player1Board[coordinateX][coordinateY + 2] = 1;
- player1Board[coordinateX][coordinateY + 3] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check2 = false;
- }
- break;
- case ('H'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX + 1][coordinateY] == 0) &&
- (player1Board[coordinateX + 2][coordinateY] == 0) &&
- (player1Board[coordinateX + 3][coordinateY] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX + 1][coordinateY] = 1;
- player1Board[coordinateX + 2][coordinateY] = 1;
- player1Board[coordinateX + 3][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check2 = false;
- }
- break;
- }
- }
- while (check3 == false) {
- printf("The cruiser ship is 3 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &cruiserVertical);
- check3 = true;
- printf("Where would you like to place the cruiser?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (cruiserVertical){
- case ('V'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX][coordinateY+1] == 0) &&
- (player1Board[coordinateX][coordinateY+2] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX][coordinateY + 1] = 1;
- player1Board[coordinateX][coordinateY + 2] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check3 = false;
- }
- break;
- case ('H'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX + 1][coordinateY] == 0) &&
- (player1Board[coordinateX + 2][coordinateY] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX + 1][coordinateY] = 1;
- player1Board[coordinateX + 2][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check3 = false;
- }
- }
- }
- while (check4 == false) {
- printf("The submarine is 3 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &submarineVertical);
- check4 = true;
- printf("Where would you like to place the submarine?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (submarineVertical){
- case ('V'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX][coordinateY+1] == 0) &&
- (player1Board[coordinateX][coordinateY+2] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX][coordinateY + 1] = 1;
- player1Board[coordinateX][coordinateY + 2] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check4 = false;
- }
- break;
- case ('H'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX + 1][coordinateY] == 0) &&
- (player1Board[coordinateX + 2][coordinateY] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX + 1][coordinateY] = 1;
- player1Board[coordinateX + 2][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check4 = false;
- }
- }
- }
- while (check5 == false) {
- printf("The destroyer is 2 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &destroyerVertical);
- check5 = true;
- printf("Where would you like to place the destroyer?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (submarineVertical){
- case ('V'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX][coordinateY+1] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX][coordinateY + 1] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check5 = false;
- }
- break;
- case ('H'):
- if ((player1Board[coordinateX][coordinateY] == 0) &&
- (player1Board[coordinateX + 1][coordinateY] == 0)) {
- player1Board[coordinateX][coordinateY] = 1;
- player1Board[coordinateX + 1][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check5 = false;
- }
- }
- }
- int w;
- int x;
- system("cls");
- printf("PLAYER 1 SHIP PLACEMENT:\n\n");
- for(w=0;w<10;w++) {
- for(x=0;x<10;x++) {
- printf("%d ", player1Board[x][w]);
- }
- printf("\n\n");
- }
- system("pause");
- return 0;
- }
- int player2ShipPlacement() {
- system("cls");
- system("color 0c");
- printf("PLAYER 2 SHIP PLACEMENT\n");
- bool check1 = false;
- bool check2 = false;
- bool check3 = false;
- bool check4 = false;
- bool check5 = false;
- int coordinateX;
- int coordinateY;
- while (check1 == false) {
- printf("The carrier ship is 5 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &carrierVertical);
- check1 = true;
- printf("Where would you like to place the carrier?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (carrierVertical){
- case ('V'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX][coordinateY+1] == 0) &&
- (player2Board[coordinateX][coordinateY+2] == 0) &&
- (player2Board[coordinateX][coordinateY+3] == 0) &&
- (player2Board[coordinateX][coordinateY+4] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX][coordinateY + 1] = 1;
- player2Board[coordinateX][coordinateY + 2] = 1;
- player2Board[coordinateX][coordinateY + 3] = 1;
- player2Board[coordinateX][coordinateY + 4] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check1 = false;
- }
- break;
- case ('H'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX + 1][coordinateY] == 0) &&
- (player2Board[coordinateX + 2][coordinateY] == 0) &&
- (player2Board[coordinateX + 3][coordinateY] == 0) &&
- (player2Board[coordinateX + 4][coordinateY] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX + 1][coordinateY] = 1;
- player2Board[coordinateX + 2][coordinateY] = 1;
- player2Board[coordinateX + 3][coordinateY] = 1;
- player2Board[coordinateX + 4][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check1 = false;
- }
- }
- }
- while (check2 == false) {
- check2 = true;
- printf("The battleship is 4 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &battleshipVertical);
- printf("Where would you like to place the battleship?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (battleshipVertical){
- case ('V'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX][coordinateY+1] == 0) &&
- (player2Board[coordinateX][coordinateY+2] == 0) &&
- (player2Board[coordinateX][coordinateY+3] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX][coordinateY + 1] = 1;
- player2Board[coordinateX][coordinateY + 2] = 1;
- player2Board[coordinateX][coordinateY + 3] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check2 = false;
- }
- break;
- case ('H'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX + 1][coordinateY] == 0) &&
- (player2Board[coordinateX + 2][coordinateY] == 0) &&
- (player2Board[coordinateX + 3][coordinateY] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX + 1][coordinateY] = 1;
- player2Board[coordinateX + 2][coordinateY] = 1;
- player2Board[coordinateX + 3][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check2 = false;
- }
- break;
- }
- }
- while (check3 == false) {
- printf("The cruiser ship is 3 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &cruiserVertical);
- check3 = true;
- printf("Where would you like to place the cruiser?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (cruiserVertical){
- case ('V'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX][coordinateY+1] == 0) &&
- (player2Board[coordinateX][coordinateY+2] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX][coordinateY + 1] = 1;
- player2Board[coordinateX][coordinateY + 2] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check3 = false;
- }
- break;
- case ('H'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX + 1][coordinateY] == 0) &&
- (player2Board[coordinateX + 2][coordinateY] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX + 1][coordinateY] = 1;
- player2Board[coordinateX + 2][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check3 = false;
- }
- }
- }
- while (check4 == false) {
- printf("The submarine is 3 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &submarineVertical);
- check4 = true;
- printf("Where would you like to place the submarine?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (submarineVertical){
- case ('V'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX][coordinateY+1] == 0) &&
- (player2Board[coordinateX][coordinateY+2] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX][coordinateY + 1] = 1;
- player2Board[coordinateX][coordinateY + 2] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check4 = false;
- }
- break;
- case ('H'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX + 1][coordinateY] == 0) &&
- (player2Board[coordinateX + 2][coordinateY] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX + 1][coordinateY] = 1;
- player2Board[coordinateX + 2][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check4 = false;
- }
- }
- }
- while (check5 == false) {
- printf("The destroyer is 2 long. Would you like it to be vertical or horizontal?\n");
- fflush(stdin);
- scanf("%c", &destroyerVertical);
- check5 = true;
- printf("Where would you like to place the destroyer?\n");
- printf("X:\n");
- fflush(stdin);
- scanf("%d", &coordinateX);
- printf("Y:\n");
- fflush(stdin);
- scanf("%d", &coordinateY);
- switch (submarineVertical){
- case ('V'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX][coordinateY+1] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX][coordinateY + 1] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check5 = false;
- }
- break;
- case ('H'):
- if ((player2Board[coordinateX][coordinateY] == 0) &&
- (player2Board[coordinateX + 1][coordinateY] == 0)) {
- player2Board[coordinateX][coordinateY] = 1;
- player2Board[coordinateX + 1][coordinateY] = 1;
- } else {
- printf("Coordinates are invalid. Please try again.\n");
- check5 = false;
- }
- }
- }
- int w;
- int x;
- system("cls");
- printf("PLAYER 2 SHIP PLACEMENT:\n\n");
- for(w=0;w<10;w++) {
- for(x=0;x<10;x++) {
- printf("%d ", player2Board[x][w]);
- }
- printf("\n\n");
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement