Help with Battleship, ships are sometimes overlapping

This is a discussion on Help with Battleship, ships are sometimes overlapping within the C Programming forums, part of the General Programming Boards category; This is a project we're doing in class. In the randomizeShips function, I will get two horizontal ships and one ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Help with Battleship, ships are sometimes overlapping

    This is a project we're doing in class. In the randomizeShips function, I will get two horizontal ships and one vertical ship, which is what I want. Although, sometimes I think they might be overlapping because sometimes there will only be two ships instead of three, any help? 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];
           int numShips;
           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);
       
    
    }
    //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++) {
            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");
           }
        
    
    }
    //print out the DEBUG board
    //print "s" if there is battleship that hasnt been selected
    //print "0" if there is no battleship that hasnt been selected
    //print "h" if hit
    //print "m" if miss
    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("%c", '!');
                           }
                           else if(arr[r][c] == NOBAT_NO_SEL) {
                                   printf("%c", '0');
                           }
                           else if(arr[r][c] == MISS ) {
                                   printf("%c", 'm');
                           }
                           else if(arr[r][c] == HIT ) {
                                   printf("%c", 'h');
                           }
                   }
                   printf("\n");
           }
    
    }
    //Place three ships on the board
    //These need to be a random selection of:
    //- horizontal/vertical
    //- starting at different locations
    //Ensure they stay in bounds
    //Ensure they don't overlap
    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) {
                            continue;
                    }
                    else if(arr[row][col+1] == BAT_NO_SEL) {
                            continue;
                    }
                    else if(arr[row][col+2] == BAT_NO_SEL) {
                            continue;
                    }
                    else {
                            for(j = 0; j < 3; j++) {
                                arr[row][col+j] = BAT_NO_SEL;
                            }
              }
                    
            
                }
                else {
                    col = rand() % 10;
                    row = rand() % 8;
    
                    if(arr[row][col] == BAT_NO_SEL) {
                        continue;
                    }
                    else if(arr[row+1][col] == BAT_NO_SEL) {
                        continue;
                    }
                    else if(arr[row+2][col] == BAT_NO_SEL) {
                        continue;
                    }
                    else {
                        for(j = 0; j < 3; j++) {
                            arr[row+j][col] = BAT_NO_SEL;
                        }
                    }
                }
        }   
    
    }
     
    int checkHit(int arr[SIZE][SIZE], int row, int col) {
    
    
    }
    int checkWin(int arr[SIZE][SIZE]) {
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The continue statement simply moves on to the next iteration of the loop. So if a BAT_NO_SEL is encountered on the first random attempt to place a ship, that ship is simply skipped. You need another loop that may only "loop" once, but that allows you to loop back and pick another random position if necessary.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Quote Originally Posted by oogabooga View Post
    The continue statement simply moves on to the next iteration of the loop. So if a BAT_NO_SEL is encountered on the first random attempt to place a ship, that ship is simply skipped. You need another loop that may only "loop" once, but that allows you to loop back and pick another random position if necessary.
    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Frankly, the use of continue here not good at all. I suggest:
    Code:
    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 &&
                    arr[row][col + 1] != BAT_NO_SEL &&
                    arr[row][col + 2] != BAT_NO_SEL) {
                for (j = 0; j < 3; j++) {
                    arr[row][col + j] = BAT_NO_SEL;
                }
            }
        } else {
            col = rand() % 10;
            row = rand() % 8;
            if (arr[row][col] != BAT_NO_SEL &&
                    arr[row + 1][col] != BAT_NO_SEL &&
                    arr[row + 2][col] != BAT_NO_SEL) {
                for (j = 0; j < 3; j++) {
                    arr[row + j][col] = BAT_NO_SEL;
                }
            }
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should add an else clause to the two inner if's that performs i-- in order to try to place that ship again.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie battle ships
    By titanicmango in forum C++ Programming
    Replies: 14
    Last Post: 05-02-2012, 08:15 PM
  2. Battle ships help
    By eisiminger in forum C Programming
    Replies: 2
    Last Post: 01-25-2010, 01:44 PM
  3. Overlapping Memory
    By hammer1234 in forum C Programming
    Replies: 1
    Last Post: 04-05-2006, 03:05 AM
  4. ai for enemy ships in asteroids
    By ichijoji in forum Game Programming
    Replies: 7
    Last Post: 04-09-2003, 04:09 PM
  5. Problem with std lists and my ships
    By Vorok in forum Game Programming
    Replies: 1
    Last Post: 01-15-2003, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21