Single Player Battleship

Here is the code for the 2 boards, both need to be visible when running.
reate 2 boards (Player view and CPU view) - 10 x 10 character array

Display the boards with a function that will also display the letters A-J and 0-9 for rows and cols

Display both boards as you are testing your program.

Make sure the CPU board is displayed when you finalize and submit your program

Populate the board with void placeShip(char ships, int size); -

Randomly determine a vertical or horizontal orientation.

It will randomly select a valid spot on the grid that will correctly place the length size of the ship on the board.

It will also determine if there are unavailable spots to prevent overlapping ships. If it cannot place the ship on the spot, it will roll for orientation and roll for spot again.
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
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <string>


using namespace std;

char board[10][10] = {};
char cpuboard[10][10] = {};





int main(){
	string coordinate;
	int turns;
	
	for(int j=0; j<=9; j++)
       cout << j << " ";
    cout<<endl;
	for (int r=0; r<10; r++){
       for(int c=0; c<10;c++){
           board[r][c] = '_';
       }
   }    
   for(int i=0; i<10; i++){
       for(int j=0; j<10; j++)
           cout << board[i][j] <<" ";
       cout << endl;
   }


    cout<<endl;
    cout<<endl;
    
    
    for(int j=0; j<=9; j++)
       cout << j << " ";
    cout<<endl;
	for (int r=0; r<10; r++){
       for(int c=0; c<10;c++){
           cpuboard[r][c] = '_';
       }
   }    
	for(int i=0; i<10; i++){
       for(int j=0; j<10; j++)
           cout << cpuboard[i][j] <<" ";
       cout << endl;
   }
	
    cout<<endl;
    
    
    
    cout<<"You have 50 tries."<<endl;
    cout<<"Enter ship coordinates: ";
	cin>>coordinate;
}

void placeship(char ships, int size){
 

	
}


I need help with the ship part!
Last edited on
Start without the randomization. Start by trying to place a single ship, length=4, orientation=horizontal, starting at location C5. Then print out the board.

Then, change the orientation to vertical and make sure that's correct.

Next, make the starting point I9 and see what happens. Try both horizontal and vertical.

Then try to place 2 ships, length = 4. Have them both start at the same location in the middle of the board and see what happens.

When you can handle collisions, then randomize where they start and their orientations.
Topic archived. No new replies allowed.