Jump to content

I'm not sure why this isn't working. Ask me any questions if you need to about what it does or whatever.

  • it randomly places boats on the grid
  • it makes sure they are within the boundaries of the 10x10
  • but it isn't making sure that all the squares the boat is going to be placed in are empty
  • as sometimes the boats will go on top of each other
#include <iostream>#include <string>#include <ctime>using namespace std;char grid[10][10] = { { ' ' } };class Ships {public:	Ships(string s, int l, char d, bool a) : name(s), length(l), display(d), afloat(a) {}		string name;		int length;		char display;		bool afloat;};void showGrid() {	char r = 'A';	//system("cls");	cout << "   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |" << endl << "___|___|___|___|___|___|___|___|___|___|___|" << endl;	for (int y = 0; y < 10; y++) {		cout << " " << r++ << " ";		for (int x = 0; x < 10; x++)			cout << "| " << grid[x][y] << " "; // if grid[x][y] is a boat character don't show it		cout << "|" << endl << "___|___|___|___|___|___|___|___|___|___|___|" << endl;	}}bool emptySpot() {	return false;}void placeShips() {	Ships ship[5] { Ships("Aircraft Carrier", 5, 'A', false), 		Ships("Battleship", 4, 'B', false),		Ships("Cruiser", 3, 'C', false),		Ships("Submarine", 3, 'S', false),		Ships("Destroyer", 2, 'D', false)	};	srand(time(NULL));	for (int s = 0; s < 5; s++) {		while (!ship[s].afloat) {			int x = rand() % 10;			int y = rand() % 10;			int d = rand() % 4;			switch (d) { // could seperate them into 2 directions, vertical and horizontal			case 0: // up				if (y - ship[s].length < 0)					break;				else {					for (int q = 0; q < ship[s].length; q++)						if (grid[x][y - q] != ' ')							break;					for (int e = 0; e < ship[s].length; e++) {						grid[x][y - e] = ship[s].display;					}					ship[s].afloat = true;					break;				}			case 1: // right				if (x + ship[s].length > 9)					break;				else {					for (int q = 0; q < ship[s].length; q++)						if (grid[x + q][y] != ' ')							break;					for (int e = 0; e < ship[s].length; e++) {						grid[x + e][y] = ship[s].display;					}					ship[s].afloat = true;					break;				}			case 2: // down				if (y + ship[s].length > 9)					break;				else {					for (int q = 0; q < ship[s].length; q++)						if (grid[x][y + q] != ' ')							break;					for (int e = 0; e < ship[s].length; e++) {						grid[x][y + e] = ship[s].display;					}					ship[s].afloat = true;					break;				}			case 3: // left				if (x - ship[s].length < 0)					break;				else {					for (int q = 0; q < ship[s].length; q++)						if (grid[x - q][y] != ' ')							break;					for (int e = 0; e < ship[s].length; e++) {						grid[x - e][y] = ship[s].display;					}					ship[s].afloat = true;					break;				}			}		}	}}int main() {	placeShips();	showGrid();	return 0;}
Link to comment
https://linustechtips.com/topic/249154-c-troubleshooting/
Share on other sites

Link to post
Share on other sites

There are 2 problems. First you're getting your X and Ys mixed up. If you want the X to be the horizontal axis and Y the vertical, the Y should be the first coordinate.

Second, when you find a tile that already contains a ship you're only breaking from the for loop, not the entire switch which I'm guessing is what you want. To fix that, before breaking set a flag that there was some error and only do the second for loop and set afloat to true if that error flag isn't set.

 

Edit: Third problem, you're only initializing the first element of grid to a space, you need to either declare the whole 10x10 grid or use a loop to initialize it before calling place ships.

1474412270.2748842

Link to comment
https://linustechtips.com/topic/249154-c-troubleshooting/#findComment-3417257
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×