Looking for a little help finishing a school project.

Hello, I am creating a c++ game for school. And need a little help finishing. It is a battle ship game, just single player and nothing fancy. What I have so far works, but for the next part of the project i need the ships to be randomly placed vertically on the grid in addition to horizontally. Intially my ships had to be 3 units in length, now they must vary 2-5. How can i incorporate that into my code? I want to still have 7 ships total. Here is my code so far.

#include <iostream>

using namespace std;

int main()
{
// Welcome message
cout << "Welcome to Battle Ships!" << endl;


// grid 10x10,, ships 7
char grid[100];
int i,j, points=0, ships= 7,m,n;
for(i=0; i<100; i++)
grid[i] = 'O';
for(i=0; i<7; i++)
{
// place ships randomly
j = rand()%4;
grid[i*10+j] = 'S';
grid[i*10+j+1] = 'S';
grid[i*10+j+2] = 'S';
}
// ask user where to fire
while(ships!=0){
cout << "Enter the row you would like to fire upon:" << endl;
cin >> m;
cout << "Enter the column you would like to fire upon:" << endl;
cin >> n;
cout << "Firing Torpedos!" << endl;

if (n<1||n>10)
if (m<1||m>10)
cout << "Invalid coordinates!" << endl;

while(ships==0)
cout << "You've sunk all of the ships" << endl;


// If user chooses a coordinate already deemed hit or miss, "already fired upon"
if (grid[m*10+n]=='H'|| grid[m*10+n]=='M')
cout << "You've already fired upon this position" << endl;

// if coordinate = O it's a miss
if (grid[m*10+n]=='O')
cout << "You Missed!" << endl;
// if S its a hit
else
cout << "Thats a hit!" << endl;
{
grid[m*10+n] ='M';
}
if (grid[m*10+n]=='S')
{
grid[m*10+n] ='H';
}
// points int
int c =0;
for(i=0; i<100; i++)
{
if (grid[i]=='H' && grid[i+1]=='H' && grid[i+2]=='H')
c++;
}
points = c;
ships = ships - points;
}
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
cout << grid[i*10+j];
cout<< " ";
}
cout << "Final Score is: " << points << endl;

return 0;
}
You need 3 random values.
an x coordinate, a y coordinate, and a horizontal/vertical Boolean (rand()%2 using your approach).
say you roll 3,4, 1 … start at x = 3, y = 4, and go horizontal. If you roll 4,2,0 you start at x = 4, y = 2, and go vertical... you may want to detect that you don't try to put 2 ships in the same place (should have done this for horiz only version too), and you either need (a simple way) an if/else to place horizontal or vertical depending on that roll.

Does that make sense?
Last edited on
I think you will find it mush easier to use a two dimensional array for your grid rather than computing the index yourself every time. Let the compiler do the work.

Your logic allows a ship to wrap from one row to the next.

1
2
const int SIZE = 10;
char grid[SIZE][SIZE];


It's a good idea to use a named constant for the size of your grid. This allows you to change the size of your grid easily without having to change every "10" in your program.

This doesn't do what you think it does:
1
2
3
if (n<1||n>10)
    if (m<1||m>10)
        cout << "Invalid coordinates!" << endl;

Both if statements have to be true to display "Invalid coordinates". You want an OR condition.
1
2
    if (n<1||n>SIZE||m<1||m>SIZE)
        cout << "Invalid coordinates!" << endl;


Your logic for determining if a ship is sunk flawed. It only works if the last hit is at the front of the ship. You're also equating points with ships. They're not the same. If you have 3 hits, you're subtracting 3 from the number of ships.

The following code will go out of bounds when i is 98.
 
if (grid[i]=='H' && grid[i+1]=='H' && grid[i+2]=='H')


You will also find it a good idea to break your game up into smaller functions.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.