Thread: (battleship) function not returning correct sum value & fprintf() not outputting

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    (battleship) function not returning correct sum value & fprintf() not outputting

    main.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "header.h"
    #define numrows 10
    #define numcolumn 10
    #define _CRT_SECURE_NO_WARNINGS
    #define _CRT_SECURE_NO_WARNINGS_GLOBALS
    
    
    
    
    int main(void)
    {
        int *t1ptr = NULL, *t2ptr = NULL;
        FILE *outfile = NULL;
        char gamestart = '\0', c1 = '~', c2 = '~', b1 = '~', b2 = '~', z1 = '~', z2 = '~', s1 = '~', s2 = '~', d1 = '~', d2 = '~';
        char c3 = '~', c4 = '~', b3 = '~', b4 = '~', z3 = '~', z4 = '~', s3 = '~', s4 = '~', d3 = '~', d4 = '~';
        int placefleet = 0, plyr1 = 0, plyr2 = 0, directionC = 0, directionB = 0, directionZ = 0, directionS = 0, directionD = 0, c = 0, select = 0;
        int firstturn = 0, t1 = 0, t2 = 0, sum = 0, sum2 = 0, turncount = 1;
    
    
        srand((unsigned int)time(NULL));
    
    
    
    
        //initializing arrays
        char plyr1board[10][10] = { 0 };
        char plyr2board[10][10] = { 0 };
        char board[10][10] = { 0 };
        int sunk[5] = { 0 };
        int sunk2[5] = { 0 };
    
    
        //initializing functions to avoid C4013 warning
        int placeships(placefleet);
    
    
        outfile = fopen("output.txt", "w");
    
    
        do
        {
            system("cls"); //clears the screen in the case of invalid input to avoid clutter
            printf("\t\t\t\tWelcome to BATTLESHIP!\n\nYou'll be acting as Player 1, and the computer shall fight you to the death as Player 2!");
            printf("\n\n\n\t\t      You can press Y when you're ready to start.\n\n\n\n\t      ...you can also press N if you're scared to close the game\n");
            scanf(" %c", &gamestart);
        } while (gamestart != 'y' && gamestart != 'Y' && gamestart != 'n' && gamestart != 'N'); //repeats until a valid input is received
        
    
    
        if (gamestart == 'n' || gamestart == 'N')
        {
            return 0;
        }
        if (gamestart == 'y' || gamestart == 'Y') //Start of new game
        {
            do
            {
                system("cls");
                placefleet = placeships(placefleet);
            } while (placefleet != 1 && placefleet != 2);
            if (placefleet == 1) 
            {
                system("cls");
                initializeboard(plyr1board, numrows, numcolumn); //sets board to '~'
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn); //shows board so player can see coordinates
                place_carrier(plyr1board, directionC, c1, c2, numrows, numcolumn); //places carrier
                place_battlship(plyr1board, directionB, b1, b2, numrows, numcolumn); //places battleship
                place_cruiser(plyr1board, directionZ, z1, z2, numrows, numcolumn); //places cruiser
                place_submarine(plyr1board, directionS, s1, s2, numrows, numcolumn); //places submarine
                place_destroyer(plyr1board, directionD, d1, d2, numrows, numcolumn); //places destroyer
            }
            if (placefleet == 2) 
            {
                system("cls");
                initializeboard(plyr1board, numrows, numcolumn); //sets board to '~'
                place_random_carrier(plyr1board, directionC, c1, c2); //randomly places carrier
                place_random_battlship(plyr1board, directionB, b1, b2); //randomly places battleship
                place_random_cruiser(plyr1board, directionZ, z1, z2); //randomly places cruiser
                place_random_submarine(plyr1board, directionS, s1, s2); //randomly places submarine
                place_random_destroyer(plyr1board, directionD, d1, d2); //randomly places destroyer
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
            }
        }
    
    
        system("pause");
        printf("\n\nPlayer 2:\n");
        initializeboard(plyr2board, numrows, numcolumn);
        place_random_carrier(plyr2board, directionC, c1, c2); //randomly places carrier
        place_random_battlship(plyr2board, directionB, b1, b2); //randomly places battleship
        place_random_cruiser(plyr2board, directionZ, z1, z2); //randomly places cruiser
        place_random_submarine(plyr2board, directionS, s1, s2); //randomly places submarine
        place_random_destroyer(plyr2board, directionD, d1, d2); //randomly places destroyer
        print_board(plyr2board, numrows, numcolumn); //prints out the gameboard
        firstturn = player_select(select); //randomly selects who goes first
        if (firstturn == 1)
        {
            printf("\n\n\t\t\t\tYou go first - Choose your target wisely\n\n\t\t\t\t(row, column): ");
            scanf("%d %d", &t1, &t2);
            if (plyr2board[t1][t2] != '~')
            {
                printf("\n\n\t\t\t\t\t\tHit!");
                check_sunk(sunk, plyr1board, t1, t2, sum); //checks to see if any of plyr1's ships have been sunk & checks for winner
                check_sunk2(sunk2, plyr2board, t1, t2, sum2); //checks to see if any of plyr2's ships have been sunk & checks for winner
                plyr2board[t1][t2] = '*';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 1: HIT", turncount);
                turncount++;
            }
            if (plyr2board[t1][t2] == '~')
            {
                printf("\n\n\t\t\t\t\tMISS - Stop wasting ammo.");
                plyr2board[t1][t2] = 'm';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 1: MISS", turncount);
                turncount++;
            }
            if (sum < 17 && sum2 < 17)
            {
                printf("\n\n\t\t\t\t\t*Computer's turn*\n\n");
                generate_coordinate(plyr1board, &t1, &t2);
                printf("\t\t\t\t\tTarget: (%d, %d)", t1, t2);
            }
            if (plyr1board[t1][t2] != '~')
            {
                printf("\n\n\t\t\t\t\t\tHit!");
                check_sunk(sunk, plyr1board, t1, t2, sum);
                check_sunk2(sunk2, plyr2board, t1, t2, sum2);
                plyr1board[t1][t2] = '*';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 2: HIT", turncount);
                turncount++;
            }
            if (plyr1board[t1][t2] == '~')
            {
                printf("\n\n\t\t\t\t\tMISS!");
                plyr1board[t1][t2] = 'm';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 2: MISS", turncount);
                turncount++;
            }
            ////////////////////Second Turn - Start Game Loop//////////////////////////
            while (sum < 17 && sum2 < 17) //once either sum is equal to 17, someone has won the game
            {
                if (sum2 < 17 && sum < 17)
                {
                    printf("\n\n\t\t\t\t\t\t\t\tTake Your Best Shot!\n\n\t\t\t\t\t\t\t\t(row, column): ");
                    scanf("%d %d", &t1, &t2);
                    if (plyr2board[t1][t2] == 'm' || plyr2board[t1][t2] == '*')
                    {
                        do
                        {
                            printf("\n\n\t\tSee that mark on the board? That means you already tried those coordinates. Don't be stupid - Enter a different set\n\n\t\t\t\t(row, column): ");
                            scanf("%d %d", &t1, &t2);
                        } while (plyr2board[t1][t2] == 'm' || plyr2board[t1][t2] == '*');
                    }
                }
                if (plyr2board[t1][t2] != '~')
                {
                    printf("\n\n\t\t\t\t\t\tHit!");
                    check_sunk(sunk, plyr1board, t1, t2, sum);
                    check_sunk2(sunk2, plyr2board, t1, t2, sum2);
                    plyr2board[t1][t2] = '*';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 1: HIT", turncount);
                    turncount++;
                }
                if (plyr2board[t1][t2] == '~')
                {
                    printf("\n\n\t\t\t\t\tMISS - Stop wasting ammo.");
                    plyr2board[t1][t2] = 'm';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 1: MISS", turncount);
                    turncount++;
                }
                if (sum < 17 && sum2 < 17)
                {
                    printf("\n\n\t\t\t\t\t*Computer's turn*\n\n");
                    generate_coordinate(plyr1board, &t1, &t2);
                    printf("\t\t\t\t\t\tTarget: (%d, %d)", t1, t2);
                }
                if (plyr1board[t1][t2] != '~')
                {
                    printf("\n\n\t\t\t\t\t\tHit!");
                    check_sunk(sunk, plyr1board, t1, t2, sum);
                    check_sunk2(sunk2, plyr2board, t1, t2, sum2);
                    plyr1board[t1][t2] = '*';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 2: HIT", turncount);
                    turncount++;
                }
                if (plyr1board[t1][t2] == '~')
                {
                    printf("\n\n\t\t\t\t\tMISS!");
                    plyr1board[t1][t2] = 'm';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 2: MISS", turncount);
                    turncount++;
                }
            }
    
    
        }
        if (firstturn == 2)
        {
            printf("\n\n\t\t\t\tComputer goes first. Are you ready to see your fate?!\n\n");
            system("pause");
            generate_coordinate(plyr1board, &t1, &t2); //generates random coordinate while checking for dublicate hits
            printf("\t\t\t\t\t\tTarget: (%d, %d)", t1, t2);
    
    
            if (plyr1board[t1][t2] != '~')
            {
                printf("\n\n\t\t\t\t\t\tBAM! You got hit.");
                check_sunk(sunk, plyr1board, t1, t2, sum); //checks to see if any of plyr1's ships have been sunk & checks for winner
                check_sunk2(sunk2, plyr2board, t1, t2, sum2); //checks to see if any of plyr2's ships have been sunk & checks for winner
                plyr1board[t1][t2] = '*';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 2: HIT", turncount);
                turncount++;
                
            }
            if (plyr1board[t1][t2] == '~')
            {
                printf("\n\n\t\t\t\t\tThat was close..");
                plyr1board[t1][t2] = 'm';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 2: MISS", turncount);
                turncount++;
            }
            printf("\n\n\t\t\t\t\t\t\t\tTake Your Best Shot!\n\n\t\t\t\t\t\t\t\t(row, column): ");
            scanf("%d %d", &t1, &t2);
            if (plyr2board[t1][t2] != '~')
            {
                printf("\n\n\t\t\t\t\t\tHit!");
                check_sunk(sunk, plyr1board, t1, t2, sum); //checks to see if any of plyr1's ships have been sunk & checks for winner
                check_sunk2(sunk2, plyr2board, t1, t2, sum2); //checks to see if any of plyr2's ships have been sunk & checks for winner
                plyr2board[t1][t2] = '*';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 1: HIT", turncount);
                turncount++;
            }
            if (plyr2board[t1][t2] == '~')
            {
                printf("\n\n\t\t\t\t\tMISS - Stop wasting ammo.");
                plyr2board[t1][t2] = 'm';
                printf("\n\nPlayer 2:\n");
                print_board(plyr2board, numrows, numcolumn);
                printf("\n\nPlayer 1:\n");
                print_board(plyr1board, numrows, numcolumn);
                system("pause");
                fprintf(outfile, "Turn %d, Player 1: MISS", turncount);
                turncount++;
            }
            /////////////////////Second Turn - Start of Game Loop/////////////////////
            while (sum < 17 && sum2 < 17) //sum2 variable reset to 0 for some reason
            {
                if (sum < 17 && sum2 < 17)
                {
                    printf("\n\n\t\t\t\t\t*Computer's turn*\n\n");
                    generate_coordinate(plyr1board, &t1, &t2);
                    printf("\t\t\t\t\t\tTarget: (%d, %d)", t1, t2);
                }
                if (plyr1board[t1][t2] != '~')
                {
                    printf("\n\n\t\t\t\t\t\tHit!");
                    check_sunk(sunk, plyr1board, t1, t2, sum);
                    check_sunk2(sunk2, plyr2board, t1, t2, sum2);
                    plyr1board[t1][t2] = '*';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 2: HIT", turncount);
                    turncount++;
                }
                if (plyr1board[t1][t2] == '~')
                {
                    printf("\n\n\t\t\t\t\tMISS!");
                    plyr1board[t1][t2] = 'm';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 2: MISS", turncount);
                    turncount++;
                }
                if (sum2 < 17 && sum < 17)
                {
                    printf("\n\n\t\t\t\tTake Your Best Shot!\n\n\t\t\t\t(row, column): ");
                    scanf("%d %d", &t1, &t2);
                    if (plyr2board[t1][t2] == 'm' || plyr2board[t1][t2] == '*')
                    {
                        do
                        {
                            printf("\n\n\t\tSee that mark on the board? That means you already tried those coordinates. Don't be stupid - Enter a different set\n\n\t\t\t\t(row, column): ");
                            scanf("%d %d", &t1, &t2);
                        } while (plyr2board[t1][t2] == 'm' || plyr2board[t1][t2] == '*');
                    }
                }
                if (plyr2board[t1][t2] != '~')
                {
                    printf("\n\n\t\t\t\t\t\tHit!");
                    check_sunk(sunk, plyr1board, t1, t2, sum);
                    check_sunk2(sunk2, plyr2board, t1, t2, sum2);
                    plyr2board[t1][t2] = '*';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 1: HIT", turncount);
                    turncount++;
                }
                if (plyr2board[t1][t2] == '~')
                {
                    printf("\n\n\t\t\t\t\tMISS - Stop wasting ammo.");
                    plyr2board[t1][t2] = 'm';
                    printf("\n\nPlayer 2:\n");
                    print_board(plyr2board, numrows, numcolumn);
                    printf("\n\nPlayer 1:\n");
                    print_board(plyr1board, numrows, numcolumn);
                    system("pause");
                    fprintf(outfile, "Turn %d, Player 1: MISS", turncount);
                    turncount++;
                }
            }
    
    
        }
        
    
    
    
    
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        fclose(outfile);
    
    
        return 0;
    }
    source.c:
    Code:
    #include "header.h"
    
    
    int placeships(int placefleet)
    {
        do
        {
            system("cls");
            printf("\n\nSelect 1 to manually place fleet\n\nSelect 2 to let the computer randomly place fleet\n\nSelection: ");
            scanf("%d", &placefleet);
        } while (placefleet != 1 && placefleet != 2);
        return placefleet;
    }
    void initializeboard(char plyr1board[][10], int numrows, int numcolumn)
    {
        int rowindex = 0, clmnindex = 0;
        for (; rowindex < numrows; ++rowindex)
        {
            for (clmnindex = 0; clmnindex < numcolumn; ++clmnindex)
            {
                plyr1board[rowindex][clmnindex] = '~';
            }
        }
    }
    void print_board(char plyr1board[][10], int numrows, int numcolumn)
    {
        int i = 0, index = 0;
        printf("   0 1 2 3 4 5 6 7 8 9\n");
        for (; index < 10; ++index)
        {
            printf("%d  ", index);
            i = 0;
            while(i < 10)
            {
                printf("%c ", plyr1board[index][i]); //row = index, column = i
                i++;
            }
            printf("\n");
        }
    }
    int player_select(int select)
    {
        select = rand() % 2 + 1;
        return select;
    }
    void place_carrier(char plyr1board[][10], int directionC, int c1, int c2, int numrows, int numcolumn)
    {
        do 
        {
            printf("\n\nEnter direction you would like to place the Carrier: \n\n1. Vertical\n2. Horizontal\n\nSelection: ");
            scanf("%d", &directionC);
        } while (directionC != 1 && directionC != 2);
        if (directionC == 1)
        {
            ////////////////////////////PLACE CARRIER//////////////////////////////////
            printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
            scanf("%d%d", &c1, &c2);
            while (c1 > 5)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Carrier (row, column):");
                scanf("%d%d", &c1, &c2);
            }
            for (int i = c1; i < (c1 + 4); ++i)
            {                        
                plyr1board[i][c2];
                if (plyr1board[i][c2] != '~')
                {
                    printf("PAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &c1, &c2);
                    while (c1 > 5)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &c1, &c2);
                    }
                    i = c1;
                }
            }
            for (int a = c1, q = 0; q < 5; ++q, ++a) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
            {
                plyr1board[a][c2] = 'C'; //a = row, c2 = column (for vertical, column shouldn't change)
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
    
    
        }
        if (directionC == 2)
        {
            ////////////////////////////PLACE CARRIER//////////////////////////////////
            printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
            scanf("%d%d", &c1, &c2);
            while (c2 > 5)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Carrier (row, column):");
                scanf("%d%d", &c1, &c2);
            }
            for (int i = c2; i < (c2 + 4); ++i)
            {
                plyr1board[i][c1];
                while (plyr1board[i][c1] != '~')
                {
                    printf("PAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &c1, &c2);
                    while (c2 > 5)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &c1, &c2);
                    }
                    i = c2;
                }
            }
            for (int a = c1, q = 0; q < 5; ++q, ++c2) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
            {
                plyr1board[a][c2] = 'C'; //a = row, c2 = column (for vertical, column shouldn't change)
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
    }
    void place_battlship(char plyr1board[][10], int directionB, int b1, int b2, int numrows, int numcolumn)
    {
        do
        {
            printf("\n\nEnter direction you would like to place the Battleship: \n\n1. Vertical\n2. Horizontal\n\nSelection: ");
            scanf("%d", &directionB);
        } while (directionB != 1 && directionB != 2);
        if (directionB == 1)
        {
            ///////////////////////////////PLACE BATTLESHIP///////////////////////////////
            printf("\n\nEnter coordinate where you would like to place the Battleship: ");
            scanf("%d%d", &b1, &b2);
            while (b1 > 6)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (4 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Battleship:");
                scanf("%d%d", &b1, &b2);
            }
            for (int i = b1; i < (b1 + 4); ++i)
            {
                plyr1board[i][b2];
                while (plyr1board[i][b2] != '~')
                {
                    printf("\n\n\t\t\t\tPAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Battleship (row, column): ");
                    scanf("%d%d", &b1, &b2);
                    while (b1 > 6)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Battleship (row, column):");
                        scanf("%d%d", &b1, &b2);
                    }
                    i = b1;
                }
            }
            for (int a = b1, q = 0; q < 4; ++q, ++a) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
            {
                plyr1board[a][b2] = 'B'; //a = row, c2 = column (for vertical, column shouldn't change)
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
        if (directionB == 2)
        {
            printf("\n\nEnter coordinate where you would like to place the Battleship: ");
            scanf("%d%d", &b1, &b2);
            while (b2 > 6)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (4 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Battleship:");
                scanf("%d%d", &b1, &b2);
            }
            for (int i = b2; i < (b2 + 4); ++i)
            {
                plyr1board[b1][i];
                while (plyr1board[b1][i] != '~')
                {
                    printf("PAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Battleship (row, column): ");
                    scanf("%d%d", &b1, &b2);
                    while (b2 > 6)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Battleship (row, column):");
                        scanf("%d%d", &b1, &b2);
                    }
                    i = b2;
                }
            }
            for (int a = b1, q = 0; q < 4; ++q, ++b2) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
            {
                plyr1board[a][b2] = 'B'; //a = row, c2 = column (for vertical, column shouldn't change)
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
    }
    void place_cruiser(char plyr1board[][10], int directionZ, int z1, int z2, int numrows, int numcolumn)
    {
        do
        {
            printf("\n\nEnter direction you would like to place the Cruiser: \n\n1. Vertical\n2. Horizontal\n\nSelection: ");
            scanf("%d", &directionZ);
        } while (directionZ != 1 && directionZ != 2);
        if (directionZ == 1)
        {
            /////////////////////////////PLACE CRUISER///////////////////////////////////
            printf("\n\nEnter coordinate where you would like to place the Cruiser: ");
            scanf("%d%d", &z1, &z2);
            while (z1 > 7)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (3 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Cruiser:");
                scanf("%d%d", &z1, &z2);
            }
            for (int i = z1; i < (z1 + 3); ++i)
            {
                plyr1board[i][z2];
                while (plyr1board[i][z2] != '~')
                {
                    printf("\n\n\t\t\t\tPAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &z1, &z2);
                    while (z1 > 7)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &z1, &z2);
                    }
                    i = z1;
                }
            }
            for (int a = z1, q = 0; q < 3; ++q, ++a) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
            {
                plyr1board[a][z2] = 'R'; //a = row, c2 = column (for vertical, column shouldn't change)
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
        if (directionZ == 2)
        {
            printf("\n\nEnter coordinate where you would like to place the Cruiser: ");
            scanf("%d%d", &z1, &z2);
            while (z2 > 7)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (3 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Cruiser:");
                scanf("%d%d", &z1, &z2);
            }
            for (int i = z2; i < (z2 + 3); ++i)
            {
                plyr1board[z1][i];
                while (plyr1board[z1][i] != '~')
                {
                    printf("PAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &z1, &z2);
                    while (z2 > 7)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &z1, &z2);
                    }
                    i = z2;
                }
            }
            for (int a = z1, q = 0; q < 3; ++q, ++z2) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
            {
                plyr1board[a][z2] = 'R'; //a = row, c2 = column (for vertical, column shouldn't change)
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
    }
    void place_submarine(char plyr1board[][10], int directionS, int s1, int s2, int numrows, int numcolumn)
    {
        do
        {
            printf("\n\nEnter direction you would like to place the Submarine: \n\n1. Vertical\n2. Horizontal\n\nSelection: ");
            scanf("%d", &directionS);
        } while (directionS != 1 && directionS != 2);
        if (directionS == 1)
        {
            /////////////////////////////PLACE SUBMARINE///////////////////////////////////
            printf("\n\nEnter coordinate where you would like to place the Submarine: ");
            scanf("%d%d", &s1, &s2);
            while (s1 > 7)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (3 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Submarine:");
                scanf("%d%d", &s1, &s2);
            }
            for (int i = s1; i < (s1 + 3); ++i)
            {
                plyr1board[i][s2];
                while (plyr1board[i][s2] != '~')
                {
                    printf("\n\n\t\t\t\tPAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &s1, &s2);
                    while (s1 > 7)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &s1, &s2);
                    }
                    i = s1;
                }
            }
            for (int a = s1, q = 0; q < 3; ++q, ++a) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
            {
                plyr1board[a][s2] = 'S'; //a = row, c2 = column (for vertical, column shouldn't change)
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
        if (directionS == 2)
        {
            printf("\n\nEnter coordinate where you would like to place the Submarine: ");
            scanf("%d%d", &s1, &s2);
            while (s2 > 7)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (3 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Submarine:");
                scanf("%d%d", &s1, &s2);
            }
            for (int i = s2; i < (s2 + 3); ++i)
            {
                plyr1board[s1][i];
                while (plyr1board[s1][i] != '~')
                {
                    printf("PAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &s1, &s2);
                    while (s2 > 7)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &s1, &s2);
                    }
                    i = s2;
                }
            }
            for (int a = s1, q = 0; q < 3; ++q, ++s2) 
            {
                plyr1board[a][s2] = 'S'; 
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
    }
    void place_destroyer(char plyr1board[][10], int directionD, int d1, int d2, int numrows, int numcolumn)
    {
        do
        {
            printf("\n\nEnter direction you would like to place the Destroyer: \n\n1. Vertical\n2. Horizontal\n\nSelection: ");
            scanf("%d", &directionD);
        } while (directionD != 1 && directionD != 2);
        if (directionD == 1)
        {
            printf("\n\nEnter coordinate where you would like to place the Destroyer: ");
            scanf("%d%d", &d1, &d2);
            while (d1 > 8)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (2 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Destroyer:");
                scanf("%d%d", &d1, &d2);
            }
            for (int i = d1; i < (d1 + 2); ++i)
            {
                plyr1board[i][d2];
                while (plyr1board[i][d2] != '~')
                {
                    printf("\n\n\t\t\t\tPAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &d1, &d2);
                    while (d1 > 8)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &d1, &d2);
                    }
                    i = d1;
                }
            }
            for (int a = d1, q = 0; q < 2; ++q, ++a) 
            {
                plyr1board[a][d2] = 'D'; 
            }
            printf("\n\n");
            system("cls");
        }
        if (directionD == 2)
        {
            printf("\n\nEnter coordinate where you would like to place the Destroyer: ");
            scanf("%d%d", &d1, &d2);
            while (d2 > 8)
            {
                printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (2 Spaces)\n\n");
                printf("Enter coordinate where you would like to place the Destroyer:");
                scanf("%d%d", &d1, &d2);
            }
            for (int i = d2; i < (d2 + 2); ++i)
            {
                plyr1board[d1][i];
                while (plyr1board[d1][i] != '~')
                {
                    printf("PAY ATTENTION. Don't overlap your ships");
                    printf("\n\nEnter coordinate where you would like to place the Carrier (row, column): ");
                    scanf("%d%d", &d1, &d2);
                    while (d2 > 8)
                    {
                        printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
                        printf("Enter coordinate where you would like to place the Carrier (row, column):");
                        scanf("%d%d", &d1, &d2);
                    }
                    i = d2;
                }
            }
            for (int a = d1, q = 0; q < 2; ++q, ++d2) 
            {
                plyr1board[a][d2] = 'D'; 
            }
            printf("\n\n");
            system("cls");
            printf("\n\nPlayer 1:\n");
            print_board(plyr1board, numrows, numcolumn);
        }
    }
    void place_random_carrier(char plyr1board[][10], int directionC, int c1, int c2)
    {
        directionC = rand() % 2 + 1; //randomizes 1-2
        if (directionC == 1) //vertical - carrier
        {
            c1 = rand() % 6; //randomizes row within board limits (0-5)
            c2 = rand() % 10; //randomized column 0-9
            for (int i = c1; i < (c1 + 4); ++i)
            {
                plyr1board[i][c2];
                while (plyr1board[i][c2] != '~')
                {
                    c1 = rand() % 6; //randomizes row within board limits (0-5)
                    c2 = rand() % 10; //randomized column 0-9
                    i = c1;
                }
            }
            for (int a = c1, q = 0; q < 5; ++q, ++a) 
            {
                plyr1board[a][c2] = 'C'; 
            }
        }
        if (directionC == 2) //horizontal - carrier
        {
            c2 = rand() % 6; //randomizes column within board limits (0-5)
            c1 = rand() % 10; //randomizes row 0-9
            for (int i = c2; i < (c2 + 5); ++i)
            {
                plyr1board[i][c1];
                while (plyr1board[i][c1] != '~')
                {
                    c2 = rand() % 6; //randomizes column within board limits (0-5)
                    c1 = rand() % 10; //randomizes row 0-9
                    i = c2;
                }
            }
            for (int a = c1, q = 0; q < 5; ++q, ++c2) 
            {
                plyr1board[a][c2] = 'C'; //a = row, c2 = column (for horizontal, row shouldn't change)
            }
        }
    }
    void place_random_battlship(char plyr1board[][10], int directionB, int b1, int b2)
    {
        directionB = rand() % 2 + 1;
        if (directionB == 1) //vertical - battleship
        {
            b1 = rand() % 7; //randomizes row within board limits (0-6)
            b2 = rand() % 10; //randomized column 0-9
            for (int i = b1; i < (b1 + 4); ++i)
            {
                plyr1board[i][b2];
                while (plyr1board[i][b2] != '~')
                {
                    b1 = rand() % 7; //randomizes row within board limits (0-6)
                    b2 = rand() % 10; //randomized column 0-9
                    i = b1;
                }
            }
            for (int a = b1, q = 0; q < 4; ++q, ++a)
            {
                plyr1board[a][b2] = 'B';
            }
        }
        if (directionB == 2) //horizontal - battleship
        {
            b2 = rand() % 7; //randomizes column within board limits (0-6)
            b1 = rand() % 10; //randomizes row 0-9
            for (int i = b2; i < (b2 + 4); ++i)
            {
                plyr1board[b1][i];
                while (plyr1board[b1][i] != '~')
                {
                    b2 = rand() % 7; //randomizes column within board limits (0-6)
                    b1 = rand() % 10; //randomizes row 0-9
                    i = b2;
                }
            }
            for (int a = b1, q = 0; q < 4; ++q, ++b2) 
            {
                plyr1board[a][b2] = 'B'; //a = row, b2 = column (for horizontal, row shouldn't change)
            }
        }
    }
    void place_random_cruiser(char plyr1board[][10], int directionZ, int z1, int z2)
    {
        directionZ = rand() % 2 + 1;
        if (directionZ == 1) //vertical - cruiser
        {
            z1 = rand() % 8; //randomizes row within board limits (0-7)
            z2 = rand() % 9; //randomized column 0-9
            for (int i = z1; i < (z1 + 3); ++i)
            {
                plyr1board[i][z2];
                while (plyr1board[i][z2] != '~')
                {
                    z1 = rand() % 8; //randomizes row within board limits (0-7)
                    z2 = rand() % 9; //randomized column 0-9
                    i = z1;
                }
            }
            for (int a = z1, q = 0; q < 3; ++q, ++a)
            {
                plyr1board[a][z2] = 'R';
            }
        }
        if (directionZ == 2) //horizontal - cruiser
        {
            z2 = rand() % 8; //randomizes column within board limits (0-7)
            z1 = rand() % 9; //randomizes row 0-9
            for (int i = z2; i < (z2 + 3); ++i)
            {
                plyr1board[z1][i];
                while (plyr1board[z1][i] != '~')
                {
                    z2 = rand() % 8; //randomizes column within board limits (0-7)
                    z1 = rand() % 9; //randomizes row 0-9
                    i = z2;
                }
            }
            for (int a = z1, q = 0; q < 3; ++q, ++z2) 
            {
                plyr1board[a][z2] = 'R'; //a = row, z2 = column (for horizontal, row shouldn't change)
            }
        }
    }
    void place_random_submarine(char plyr1board[][10], int directionS, int s1, int s2)
    {
        directionS = rand() % 2 + 1; //randomizes 1-2
        if (directionS == 1) //vertical - submarine
        {
            s1 = rand() % 8; //randomizes row within board limits (0-7)
            s2 = rand() % 9; //randomized column 0-9
            for (int i = s1; i < (s1 + 3); ++i)
            {
                plyr1board[i][s2];
                while (plyr1board[i][s2] != '~')
                {
                    s1 = rand() % 8; //randomizes row within board limits (0-7)
                    s2 = rand() % 9; //randomized column 0-9
                    i = s1;
                }
            }
            for (int a = s1, q = 0; q < 3; ++q, ++a)
            {
                plyr1board[a][s2] = 'S';
            }
        }
        if (directionS == 2) //horizontal - submarine
        {
            s2 = rand() % 8; //randomizes column within board limits (0-7)
            s1 = rand() % 9; //randomizes row 0-9
            for (int i = s2; i < (s2 + 3); ++i)
            {
                plyr1board[s1][i];
                while (plyr1board[s1][i] != '~')
                {
                    s2 = rand() % 8; //randomizes column within board limits (0-7)
                    s1 = rand() % 9; //randomizes row 0-9
                    i = s2;
                }
            }
            for (int a = s1, q = 0; q < 3; ++q, ++s2) 
            {
                plyr1board[a][s2] = 'S'; //a = row, s2 = column (for horizontal, row shouldn't change)
            }
        }
    }
    void place_random_destroyer(char plyr1board[][10], int directionD, int d1, int d2)
    {
        directionD = rand() % 2 + 1; //randomizes 1-2
        if (directionD == 1) //vertical - destroyer
        {
            d1 = rand() % 9; //randomizes row within board limits (0-8)
            d2 = rand() % 10; //randomized column 0-9
            for (int i = d1; i < (d1 + 2); ++i)
            {
                plyr1board[i][d2];
                while (plyr1board[i][d2] != '~') //checks for other ships
                {
                    d1 = rand() % 9; //randomizes row within board limits (0-8)
                    d2 = rand() % 10; //randomized column 0-9
                    i = d1;
                }
            }
            for (int a = d1, q = 0; q < 2; ++q, ++a)
            {
                plyr1board[a][d2] = 'D';
            }
        }
        if (directionD == 2) //horizontal - destroyer
        {
            d2 = rand() % 9; //randomizes column within board limits (0-8)
            d1 = rand() % 10; //randomizes row 0-9
            for (int i = d2; i < (d2 + 2); ++i)
            {
                plyr1board[d1][i];
                while (plyr1board[d1][i] != '~') //checks for other ships
                {
                    d2 = rand() % 9; //randomizes column within board limits (0-8)
                    d1 = rand() % 10; //randomizes row 0-9
                    i = d2;
                }
            }
            for (int a = d1, q = 0; q < 2; ++q, ++d2) //q represents the size of the destroyer (2)
            {
                plyr1board[a][d2] = 'D'; //a = row, c2 = column (for horizontal, row shouldn't change)
            }
        }
    }
    void generate_coordinate(char plyr1board[][10], int *t1ptr, int *t2ptr)
    {
        *t1ptr = rand() % 10;
        *t2ptr = rand() % 10;
        while (plyr1board[*t1ptr][*t2ptr] == '*' || plyr1board == 'm') 
        {
            *t1ptr = rand() % 10;
            *t2ptr = rand() % 10;
        }
    }
    int check_sunk(int sunk[], char plyr1board[][10], int t1, int t2, int sum)
    {
        
        if (plyr1board[t1][t2] == 'C')
        {
            sunk[0] += 1;
            if (sunk[0] == 5) //carrier
            {
                printf("Carrier has been sunk!!");
            }
        }
        if (plyr1board[t1][t2] == 'B')
        {
            sunk[1] += 1;
            if (sunk[1] == 4) //battleship
            {
                printf("Battleship has been sunk!");
            }
        }
        if (plyr1board[t1][t2] == 'R')
        {
            sunk[2] += 1;
            if (sunk[2] == 3) //cruiser
            {
                printf("Cruiser has been sunk!");
            }
        }
        if (plyr1board[t1][t2] == 'S')
        {
            sunk[3] += 1;
            if (sunk[3] == 3) //submarine
            {
                printf("Submarine has been sunk!");
            }
        }
        if (plyr1board[t1][t2] == 'D')
        {
            sunk[4] += 1;
            if (sunk[4] == 2) //destroyer
            {
                printf("Destroyer has been sunk!");
            }
        }
        for (int i = 0; i < 5; ++i)
        {
            
            sum += sunk[i];
        }
        if (sum == 17)
        {
            printf("\n\n\t\t\t\t\t\t\t\tALL SHIPS SUNK - Player 2 Wins!");
            return 0;
        }
        return sum;
    }
    int check_sunk2(int sunk2[], char plyr2board[][10], int t1, int t2, int sum2)
    {
    
    
        if (plyr2board[t1][t2] == 'C')
        {
            sunk2[0] += 1;
            if (sunk2[0] == 5) //carrier
            {
                printf("Carrier has been sunk!!");
            }
        }
        if (plyr2board[t1][t2] == 'B')
        {
            sunk2[1] += 1;
            if (sunk2[1] == 4) //battleship
            {
                printf("Battleship has been sunk!");
            }
        }
        if (plyr2board[t1][t2] == 'R')
        {
            sunk2[2] += 1;
            if (sunk2[2] == 3) //cruiser
            {
                printf("Cruiser has been sunk!");
            }
        }
        if (plyr2board[t1][t2] == 'S')
        {
            sunk2[3] += 1;
            if (sunk2[3] == 3) //submarine
            {
                printf("Submarine has been sunk!");
            }
        }
        if (plyr2board[t1][t2] == 'D')
        {
            sunk2[4] += 1;
            if (sunk2[4] == 2) //destroyer
            {
                printf("Destroyer has been sunk!");
            }
        }
        for (int i = 0; i < 5; ++i)
        {
    
    
            sum2 += sunk2[i];
        }
        if (sum2 == 17)
        {
            printf("\n\n\t\t\t\t\t\t\t\tALL SHIPS SUNK - Player 1 Wins!");
            return 0;
        }
        return sum2;
    }

    header.h:

    Code:
    #pragma once
    
    
    int placeships(int placefleet);
    void initializeboard(char board[][10], int numrows, int numcolumn);
    void print_board(char board[][10], int numrows, int numcolumn);
    int player_select(int select);
    void place_carrier(char plyr1board[][10], int directionC, int c1, int c2, int numrows, int numcolumn);
    void place_battlship(char plyr1board[][10], int directionB, int b1, int b2, int numrows, int numcolumn);
    void place_cruiser(char plyr1board[][10], int directionZ, int z1, int z2, int numrows, int numcolumn);
    void place_submarine(char plyr1board[][10], int directionS, int s1, int s2, int numrows, int numcolumn);
    void place_destroyer(char plyr1board[][10], int directionD, int d1, int d2, int numrows, int numcolumn);
    void place_random_carrier(char plyr1board[][10], int directionC, int c1, int c2);
    void place_random_battlship(char plyr1board[][10], int directionB, int b1, int b2);
    void place_random_cruiser(char plyr1board[][10], int directionZ, int z1, int z2);
    void place_random_submarine(char plyr1board[][10], int directionS, int s1, int s2);
    void place_random_destroyer(char plyr1board[][10], int directionD, int d1, int d2);
    int check_sunk(int sunk[], char plyr1board[][10], int t1, int t2, int sum);
    int check_sunk2(int sunk2[], char plyr2board[][10], int t1, int t2, int sum2);


    Issues:

    1. When using the check_sunk() & check_sunk2() functions, the sum is being added to the array, but when I try to return the values for sum & sum2, they return as zero which is causing my game loop (the while sum < 17 && sum2 < 17) to keep running even after all ships have been sunk. It returns the message "All ships sunk" from the function, so it seems like it is keeping track of the hits, but just not sending that value to main.

    2. I need to be able to print the turn count and whether it was a hit or a miss to the output.txt file. Every code sample I've looked at is identical to the way I have the outfile declared and called with fprintf, so I'm really at a loss as to why this isn't printing anything to the outfile.


    Thank you in advance for your help.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Compiling with a proper warning level gives about 40 warnings of the following types: unused parameter; statement has no effect; comparison between pointer and integer; parameter names without types; unused variable. Maybe you should fix those.

    You have a HUGE amount of duplicate code. Presumably you could have one function for placing ships (randomly or otherwise). Whenever your program becomes mindlessly repetitive, you should consider ways of reducing that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array function not returning correct valuse
    By phillyflyers in forum C Programming
    Replies: 2
    Last Post: 04-01-2013, 09:19 PM
  2. fprintf isn't outputting anything to file
    By RagingPenguin4 in forum C Programming
    Replies: 8
    Last Post: 09-18-2012, 12:02 AM
  3. function not returning correct values
    By Paul Adams in forum C Programming
    Replies: 2
    Last Post: 02-24-2012, 09:58 PM
  4. Not returning correct results
    By drty2 in forum C Programming
    Replies: 4
    Last Post: 01-19-2009, 12:39 PM
  5. correct way of returning strings
    By marsface in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2003, 10:33 AM

Tags for this Thread