0

For a school assignment I have to create a battleship game where one random 4 length battleship (horizontal or vertical) is generated in a 8x8 gameboard. The player has 15 torpedoes to try and sink the ship. I used a 2D vector and have finished most of the code:

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

using namespace std;

void generateship(vector<vector<int> >&field);
void fire(vector<vector<int> >&field);
void display(const vector<vector<int> >field);

int main()
{
    srand(time(0));

    vector<vector<int> >field(8);
    for (int x = 0; x < field.size(); x++)
        field[x].resize(8);
    for (int x = 0; x < field.size(); x++)
        for (int y = 0; y < field[y].size(); y++)
            field[x][y] = 0;

    generateship(field);
    fire(field);


    system("pause");

    return 0;
}
void generateship(vector<vector<int> >&field)
{
    int row1 = rand() % 8;
    int col1 = rand() % 8;
    do 
    {
        int row2 = rand() % 3 + (row1 - 1);
        int col2 = rand() % 3 + (col1 - 1);
    } while (row2 != row1 && col2)
    int col3 = rand()
    display(field);
}
void fire(vector<vector<int> >&field)
{
    int row, col;
    int torps = 15;
    int hitcounter = 0;
    while (hitcounter != 4 || torps != 0)
    {
        cout << torps << " torpedoes remain. Fire where? ";
        cin >> row >> col;
        switch (field[row][col])
        {
        case 0: cout << "Miss!" << endl << endl;
            field[row][col] = 2;
            break;
        case 1: cout << "Hit!" << endl << endl;
            field[row][col] = 3;
            hitcounter = hitcounter + 1;
            break;
        case 2: cout << "Missed again!" << endl << endl;
            break;
        case 3: cout << "Hit again!" << endl << endl;
            break;
        }
        torps = torps - 1;
        display(field);
    }
    if (hitcounter == 4)
        cout << "You win!";
    else if (torps == 0)
        cout << "You are out of torpedoes! Game over.";
}
void display(const vector<vector<int> >field)
{
    for (int row = 0; row < 8; row++)
    {
        for (int col = 0; col < 8; col++)
        {
            switch (field[row][col])
            {
            case 0:     cout << ". ";
                break;
            case 1:     cout << ". ";
                break;
            case 2:     cout << "X ";
                break;
            case 3:     cout << "O ";
                break;
            }
        }
        cout << endl;
    }
}

As you can probably see I am struggling with the "generateship" function. My goal is to generate RANDOMLY one 4x1 ship (horizontal or vertical) that is completely within my 8x8 2D vector. Any advice/help/comments appreciated!

pyb
  • 4,813
  • 2
  • 27
  • 45
e7kim
  • 61
  • 6
  • I added the `c++` tag so people working with this language will find your post. Also it automatically colors your code. – pyb Jan 03 '16 at 21:56

1 Answers1

1

There are 2 random choices: direction and position.

The direction determines which positions are valid, so it's probably best to first randomly choose the direction (horizontal or vertical).

Then randomly choose the topleft position of the ship. If direction is horizontal, the x position should be between 0 and 7-3, y between 0 and 7. If direction is vertical, the y position should be between 0 and 7-3, x between 0 and 7.

BTW, try not to hard-code any of these numbers. Better to use constants so you can easily change the size of the board and the ship later on.

Bartez
  • 172
  • 8
  • +1 for the no hard-code part. consider adding example code for the `generateship` funtion to make the answer better! – urban Jan 03 '16 at 21:41
  • 1
    Thanks! Since this is a homework assignment, I thought actual code doesn't really help the learning process. – Bartez Jan 03 '16 at 22:32