Thread: Stuck on how to proceed with battleship program

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    9

    Stuck on how to proceed with battleship program

    Code:
    #include <time.h>
    
    
    void initgame();
    void battleship();
    void loadships(char board[], char mask[]);
    void drawgame(char mask[], int missilesFired, int missilesRemaining, int score, char previousMove[]);
    void updateData(char board[], char mask[], int *missilesFired, int *missilesRemaining,
        int *score, char previousMove[], char currentMove[]);
    int checkMove(char coord[]);
    
    
    int main(){
    
    
        battleship();
        return 0;
    }
    
    
    void initgame()
    
    
    {
        char mask[391];
        int i = 0, j = 0;
        for (i = 0; i<390; i++) {
            mask[i] = '~';
        }
        mask[i] = '\0'; /* add the null byte to use as a string if required */
    
    
    
    
        char board[391];
    
    
        for (i = 0; i<390; i++) {
            board[i] = '~';
        }
        board[i] = '\0'; /* add the null byte to use as a string if required */
    
    
        loadships(board, mask);
    
    
    
    
    }
    
    
    void battleship(){
        
        int missfire = 0;
        int missrem = 100;
        int score = 0;
        char prevmove;
    
    
    
    
    
    
    
    
        initgame();
        drawgame();
    
    
    }
    
    
    void loadships(char board[], char mask[]){
        
        srand(time(NULL));
        int i = 0,place=0;
        char carrier[8] = "[ccccc=>";
        char destroyer[6] = "[DDD=>";
        int randomR = (rand() % 14) + 1;
        int randomC = (rand() % 25) + 1;
    
    
        place = randomR * 26 + randomC;
    
    
        for (i = 0; i < 8;i++){
            board[place + i] = carrier[i];
        }
    
    
    }
    
    
    void drawgame(char mask[], int missilesFired, int missilesRemaining, int score, char previousMove[]) {
        int i = 0;
        int j = 0;
        printf("   C Battleship...\n");
        printf("   ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
        for (i = 0; i<15; i++) {                /* display 15 rows of information (incl. border) */
            if (i < 9) {
                printf(" %c|", i + 1 + '0');   /* for each digit character */
            }
            else {
                printf(" %c|", i + 1 + 55);    /* for each alphabetic character 'A' through 'F' */
            }
            for (j = 0; j<26; j++) {             /* display all 26 columns in each row */
                printf("%c", mask[j + (i * 26)]);
            }
            printf("|\n");
        }
            printf("\n");
    
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2015
    Posts
    9
    basically i loaded 1 ship for now add rest later... once ive updated the board array with the load ships function i need to take the board array updated and place into drawboard function somehow or at least i think it progresses like this

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Nice code. Care to explain how you got stuck? You should be able to tell us what the problem is and what you want to do, at least.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    9
    i want to get the updated board array from "loadships" and use it for the "drawboard" function

    so firstly i call "battleship" then which then calls "initgame" which fills 2 arrays with "~" then call the "loadship" which sticks ships into the "board" array

    now i gota somehow get that newley filled array to use for my "drawboard" function
    Last edited by Cody Filion; 03-27-2015 at 10:46 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > char mask[391];
    > char board[391];
    1. These need to be declared in main, and passed as parameters to your functions
    2. The length needs to be a #define constant, so you don't keep writing magic numbers in the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Battleship program, two questions?
    By Sam Maiden in forum C Programming
    Replies: 1
    Last Post: 11-25-2014, 06:51 PM
  2. help with battleship program
    By howardqd in forum C++ Programming
    Replies: 12
    Last Post: 12-15-2010, 08:40 PM
  3. question on battleship program.
    By newbc in forum C Programming
    Replies: 0
    Last Post: 12-07-2010, 08:32 AM
  4. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  5. Battleship program
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2008, 09:02 AM