# C++ Notes: Example - Battleship ver 2

This is working version of a one-dimensional Battleship game. The next version should support two dimensions.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    //  battleship2/main.cpp -- Version 2 of Battleship game.
    //  Fred Swartz, 2003-11-25

    // This second version adds the following to version 1.
    //  * Prints coordinate along bottom edge.
    //  * Verifies that user bombing coordinates are good.
    //  * Implements bomb() function.
    //  * Places ship randomly on grid.
    //  * Checks for end of game.
    //
    // FUNCTIONALITY TO BE ADDED
    //  * Make it two-dimensional.
    //  * Should be a developer command to display where ships are.
    //
    // USER INTERFACE
    //  * Not robust - crashes if bombing coordinate missing.

    //==================================================== includes
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <ctime>    // For time()
    #include <cstdlib>  // For srand() and rand()
    using namespace std;

    //========================================= global declarations
    // Damage enum defines possible values at each grid point.
    enum Damage {WATER_UNBOMBED, WATER_BOMBED,
                 SHIP_UNBOMBED , SHIP_BOMBED};

    // The display array defines what to display for
    //     each of the Damage enum values.  Changes to the
    //     Damage enum require this to be changed also.
    const char display[] = {'-', 'O', '-', 'X'};

    const  int WIDTH = 10;   // Width of the grid.
    Damage grid[WIDTH];      // Records status of every grid cell.

    //================================================== prototypes
    void dropBomb(int coord);
    void executeCommand(char cmd);
    void initGrid();
    bool isGameOver();
    void print();

    //======================================================== main
    int main() {

        srand(time(0));  // Initialize random number generator

        char ans;
        do {
            initGrid();  // Randomly place ships on the grid.
            print();

            char commandCode;
            while (cin >> commandCode) {

                executeCommand(commandCode);

                if (isGameOver()) {
                    cout << "Congratuations, you win" << endl;
                    break;
                }
            }
            cout << "Do you want to play again? (y/n) ";
            ans = 'n';
            cin >> ans;
        } while (ans == 'y');

        return 0;
    }//end main

    //======================================================= executeCommand
    void executeCommand(char cmd) {
        switch (cmd) {

            case 'q':           // quit
                exit(0);
                break;

            case 'b':           // bomb
                int loc;
                cin >> loc;
                if (loc>=0 && loc<WIDTH) {
                    dropBomb(loc);
                } else {
                    cout << "Bombing coordinate must be between 0 and " << WIDTH-1 << endl;
                }
                print();
                break;

            default:            // error
                cerr << "Bad input " << cmd << endl;
                break;
        }
    }

    //======================================================= dropBomb
    void dropBomb(int coord) {
        switch (grid[coord]) {

        case WATER_UNBOMBED:
              grid[coord] = WATER_BOMBED;
              break;

        case SHIP_UNBOMBED:
              grid[coord] = SHIP_BOMBED;
              break;
        }
        return;
    }

    //======================================================= initGrid
    // initGrid() places random ships on the grid.
    void initGrid() {
        for (int i=0; i<WIDTH; i++) {  // clear grid
            grid[i] = WATER_UNBOMBED;
        }

        int start = rand() % (WIDTH-4); // Place a 3-cell ship in grid.
        grid[start+0] = SHIP_UNBOMBED;
        grid[start+1] = SHIP_UNBOMBED;
        grid[start+2] = SHIP_UNBOMBED;
    }

    //========================================================== print
    void print() {
        cout << endl;
        for (int i=0; i<WIDTH; i++) {
            cout << " " << display[grid[i]];
        }
        cout << endl;

        //-- Print coordinates.
        for (int coord=0; coord<WIDTH; coord++) {
            cout << setw(2) << coord;
        }
        cout << endl << endl;
    }

    //====================================================== isGameOver
    bool isGameOver() {
        for (int coord=0; coord<WIDTH; coord++) {
            if (grid[coord] == SHIP_UNBOMBED) {
                return false;
            }
        }
        return true;
    }
The text from the above example can be selected, copied, and pasted into an editor.