I need advice for this 2D array program?

closed account (LN3RX9L8)
I wrote the following code as 1D array now I have to convert everything and make it a 2D array:


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

int main()
{
    srand (time(NULL));
    const int SIZE= 10;
    char board [SIZE];
    int answer;
    fill(board, board+10, 'O');
    const int TOTAL_ATTEMPTS = 3;
    int attempts = TOTAL_ATTEMPTS;
    int index = rand() % 10;
    
    cout<<"Let's Play Battleship!!" << endl;

    for (int i=0; i<SIZE; i++)
    {
        cout << board[i] <<" ";
    }

    cout<< endl;
    
    
    do
    {   cout <<"Enter a guess from 0 to 10: ";
         cin >> answer;
      
        if (board[answer]=='X')
        {
            cout <<"You already guessed that location landlubber!"<< endl; 
            cout <<"Enter a guess from 0 to 10: ";
            cin >> answer;
        }
        if (answer >= 0 && answer <= SIZE)
        { board[answer] = 'X';
        }
      
        for (int i=0; i<SIZE; i++)
        {
            cout << board[i] <<" ";
        }        
        cout<< endl; 
        
        if(answer!=index)
        {
            cout<<"You missed by Battleship!" << endl; 
        }
        if(answer==index)
        {
            cout<<"YOU SANK MY BATTLESHIP!  Well done." << endl; 
            
            cout << "GAME OVER" << endl; 
        }
        
   
    }while ( --attempts && answer != index );
    
    if(answer!=index)
    {
    cout << "HA! You are out of guesses - I live to sail another day!" << endl;
    
    cout << "GAME OVER" << endl; 
        return 0;
    }

}


Last edited on
I think if you are doing an assignment like this you should be able to figure it out, however if I had a nickel for every time I got irked cos someone told me that and said nothing...I'd have like 50 bucks.

Anyway, this should help you get started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define BOARD_SIZE 25 //The board size (^2)

char boardSquares[BOARD_SIZE][BOARD_SIZE];

for(int x=0;x<=BOARD_SIZE;x++)
{
    for(int y=0;y<=BOARD_SIZE;y++)
    {
        //Get a random number, and once found

        if(getRandomNumber(odds))//Something to figure out if that square should be a ship
        {
            boardSquares[x][y]='X';//If so, then we put a ship
        }
        else{
            boardSquares[x][y]='O';//Otherwise its an empty space
        }
    }
}

Let me know if that helps.
Last edited on
closed account (LN3RX9L8)
@MajorTom

Well I am a beginner and in my first class of programming and I really appreciate your help... But I am stuck can you help me?

This is what I have so far
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
#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]);



int main()
{
	srand (time(NULL));
	int indexrow = rand() % 10;
	int indexcolumn=rand() % 10;
	int row, column;
    
    char ocean[S] [S];

    
    cout<< "Let's Play Battleship!!!"<< endl;
    
    init_board(ocean);
        
    
    cout<<"Enter a guess for the boat row: "<<endl;
    cin>> row;
    
    cout<<"Enter a guess for the boat column:" <<endl;
    cin>> column;
    

    
    
    
    return 0;
}

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';
            cout<< nums[i][j]<< " ";
            
        }
        cout<< endl; 
        
    }
    
    
    
}
Are there compile errors? If so what line? If not what is your question?
One thing, is for the size of the board I would use a #define S 4 instead of const int S=4;
#define is called a preprocessor directive, and basically it replaces every usage of S in your code with 4.
closed account (Dy7SLyTq)
but a const int would only replace one? const ints are much better because #defines can be very dangerous
Topic archived. No new replies allowed.