Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to come up with a boom the battleship game for my assignment. I'm facing a problem with creating an array that will fix the size of ship to 5 characters. Any kind help is truly appreciated, thanks.

What I have tried:

C++
void initializeShipsEasy(int ships[][2]) {

	int ship, last;

	for (ship = 0; ship < SHIPS_EASY; ship++) {
		ships[ship][0] = rand() % 20;
		ships[ship][1] = rand() % 60;

		//let's check if this shot was not tried
		//if it was, just get out of the 'do while' loop when draws a pair that was not tried 
		for (last = 0; last < ship; last++) {
			if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))
				do {
					ships[ship][0] = rand() % 20;
					ships[ship][1] = rand() % 60;
				} while ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]));
		}

	}
}
Posted
Updated 21-Nov-17 8:31am
v2
Comments
ZurdoDev 21-Nov-17 10:48am    
What exactly are you stuck on?
Member 13533562 21-Nov-17 10:49am    
i don't know how to make the ship to 5 character length and to place them in a 20 x 60 board
Sinisa Hajnal 21-Nov-17 10:57am    
Do you know how to make ship of 2 or 3 squares? What's the difference with 5?
Member 13533562 21-Nov-17 10:58am    
unfortunately no. 5 is required by the question
ZurdoDev 21-Nov-17 11:10am    
It will require a lot of code to do that and I don't think anyone will write that much code for you. I suggest you get started and then ask when you get stuck on something more specific.

It is very easy: you must count how many fields are already used and than stop the ship making. You must also check that the ship isnt going out of the playground. At best is to check that the starting point provides enough space and else move it correctly.

Use a fix playground array as global instance like a chess board:
C++
char playground[60][60] = {0};// 60 x 60 and set to zero

a horizontal ship:
C++
int startx = 50;// or some random
int starty = 10;// or some random
//check border x 
if( startx + 5 > 60 ) {
  startx = 60 - 5;
}
//TODO: check x on the upper side and check y !!!
for(int i = 0; i < 5; i++ ) {
  playground[startx+i][starty] = 1; //build ship 
}
If you write a function around it, you can make the "5" has input param to build other ships.
 
Share this answer
 
This is an assignment - I'll give you thoughts about what you need to think about and some pointers - but you should be putting in the thought yourself so you can learn to do this yourself!

1 - You need to pick a direction for it to grow (v or h)
2 - You need to pick a starting point for a ship (position range constrained by 1)
3 - you need to populate it's storage to whatever length ship you wish (like 5).
4 - you need to make sure none of its data overlaps with other ship data

Also - because your ships are straight, you only need to pick one point to draw the remainder. You can always draw it in the same direction by constraining the start range to keep it on the playing board.

Now - you know the jobs that need to be done (to populate the board) - but now you need to figure out how to do them.

Also, learn to tear a problem apart into the most elementary pieces you can. That's the most important part of the problem solving - and becomes instinctive if you get good at this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900