Help with my code?

closed account (LN3RX9L8)
This is what I have so far but with only one battleship now I have to do the following:

Add a second boat to the board. You must make sure that the two boats are not in the same location before the game starts. Write a function called place_boats to find locations for the two boats. The player must now find both boats in order to win the game.

Ps the 'B' is for debugging.
and can someone give me ideas on what to do next I am really stuck... I would appreciate your help
Thanks

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
#include <iostream>
#include <ctime>
using namespace std;

const int S=4;// can't change size once declared. Global any fucntion can use this v.


void init_board(char nums [][S]);
void print_board(char ocean [][S]);
void place_boats(char boat [] [S]); 

int main()
{
	srand (time(NULL));
	int indexrow = rand() % 4;
	int indexcolumn=rand() % 4;
    
    int row2 =rand() % 4;//random generator for the second boat
    int column2= rand() % 4;// index generated for the second boat
    
	int row, column, count=0;
    
    char ocean[S] [S];
    
    
    cout<< "Let's Play Battleship!!!"<< endl;
    
    init_board(ocean);
    ocean [indexrow][indexcolumn]='B';
    
    print_board (ocean);
    
    do
    {
        cout<<"Enter a guess for the boat row: "<<endl;
        cin>> row;
        
        cout<<"Enter a guess for the boat column:" <<endl;
        cin>> column;
        count++;
        
        if(row>=S || column>=S)
        {
            cout<< "That guess isn't even in the ocean!" << endl;
            
        }
        if(ocean[row][column]=='X')
        {
            cout<< "You already guessed that location!" << endl;
        }
        if(row!=indexrow || column!=indexcolumn)
        {
            cout<<"You missed my ship!" << endl;
            ocean[row][column]='X';
        }
        
        print_board(ocean);
        
    } while(row!=indexrow || column!=indexcolumn);
    
    if(row==indexrow && column==indexcolumn)
    {
        cout<<"YOU SANK MY BATTLESHIP!!!" << endl;
        cout<<"It took you " << count <<" turns!" << endl;
    }
    
}

void init_board(char nums [][S])/// function defen
{
    
    for (int i=0; i<4; i++)
        
    {
        for(int j=0; j<4; j++)
        {
            nums[i] [j]='O';
        }
    }
    
}
void print_board(char ocean [][S])
{
    
    for (int i=0; i<4; i++)
        
    {
        for(int j=0; j<4; j++)
        {
            
            cout<< ocean[i][j]<< " ";
        }
        cout<< endl;
    }
}

void place_boats(char boat [] [S])
{
    
}
Last edited on
How do your boats look, as in are they made of a single letter, or more?

B

or more like

BBBB

B
B
B
B
closed account (3qX21hU5)
Well we could use more information then just help ;p. What are you stuck on exactly? The assignment tells you exactly what to do next so you shouldn't be stuck on that.

Basically you need to write the place_boats() function. In this function you are going to place 2 boats onto the board randomly I am guessing (Didn't give much info on this). You will need to make sure that you don't place a boat on a location that already has a boat so you will need to check that.


Basically select a random location within your multi demesional array to place the first boat on.

After you have placed the first boat do the same with the second boat (Select a random location within your array) but this time before you place it do a check to make sure there is not already a boat on that space, if there is generate another random location if there isn't place the boat on that location.

Example for the check.

1
2
3
4
5
6
7
8
9
10
11
// [i][j] represents the randomly selected element in the array where you want
// to place the boat.
if (array[i][j] == 'B')
{
    // Generate a new element randomly and do the check again
}
else
{
    // Place the boat on that element if there isn't a boat on there already
    array[i][j] = 'B';
}


That should give you what you need to do.

Also Catfish its in a single letter like this is his board.

O O O O
B O O O
O O B O
O O O O

The 'O's are open the 'B's are boats.
Last edited on
closed account (LN3RX9L8)
@zereo wow thanks I will start working on it and then post on here if I have any more questions
closed account (3qX21hU5)
Decided to be nice to ya and give you most of the function. You will still need to make it work and get the rest of the program to work with it though ;p.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void place_boats(char boat[][S], const int boatsToPlace)
{ 
    // This will keep track of how many boats have been placed
    int boatsPlaced = 0;

    while (boatsPlaced != boatsToPlace)  // Determines how many boats to place
    {
        // Something here. Hint: It getting a random number for the multi array
        // Something here. Hint: Same as the above comment

        if (boat[i][j] == 'B')
            continue;
        else
        {
            boat[i][j] = 'B';
            ++boatsPlaced;
        }
    }

}


Added another parameter so in the future you can make it easier to place more then 2 boats.
closed account (LN3RX9L8)
Thanks so much!!
Topic archived. No new replies allowed.