Public Group

# Help w/ Battleship Game

This topic is 3520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have to create a Battleship game for one of my C++ classes and cannot think of a good way to create ships. We are not allowed to uses classes or any objects. Here is the description of the part of th problem I am having trouble with: * Your program should determine if the user’s guess was a hit or miss and store the result in a second 2-Dimensional array. If the result was a hit, your program will need to check whether the specific ship that was hit has been hit in all of that ship’s location and is therefore sunk. If a ship has been sunk, your program should output which ship was sunk. * The way I have the game set up now is I have a 2D array where 0 is an unmarked and unfilled spot on the grid,1 is a miss, 2 is a ship that has not yet been marked, and 3 is a hit. I use char disp[] = {' ', 'O', ' ', 'X'} in my drawGrid function to mark the map. I just cannot figure out how I am supposed to make the ships specific like that without using any structures. Thanks for the help, Andy [Edited by - neipo13 on April 21, 2009 5:47:38 PM]

##### Share on other sites

There are a lot of non-class ways to represent ships. For example:

1) create a thread for each ship & give it an int which is the number of points for that ship
2) each ship thread creates n more threads: 1 for each point of the ship
3) each of those point-threads is monitoring a point in your array, when that point goes to "hit" the thread terminates and sets a semaphore in the ship-thread
4) when all the semaphores are == true, the ship thread terminates and sets a semaphore in the main application loop that the particular ship is dead.

There are some details of message passing in there but all the semaphores are just arrays and the point threads are just evaluating the value of an int in your array so there shouldn't be any structs or classes there.

pull that off and you should get an A

There's a simpler way, but this would be one of the the more awesome ways.

Also, we don't like to give homework advice here. Go talk to your teacher [smile]. Make a stab at a solution and come for advice then but right now you're just asking us to solve the problem for you.

-me

##### Share on other sites
Quote:
 Original post by PalidineAlso, we don't like to give homework advice here. Go talk to your teacher [smile]. Make a stab at a solution and come for advice then but right now you're just asking us to solve the problem for you.-me

haha thank you for that, i apologize for not posting this but my original solution was to just assign random 2's to the grid in the same way that ships would be arranged. It worked for the game but was tackled by the need to say when ships were sunk
and my bad about the homework thing.. I use this site for my personal games and figured this homework was a game so who would know how to help better than you guys! I'm headed to a help session now anywho.
I'll post my solution to the problem when all is said and done.

##### Share on other sites
To be honest, you should be asking your teacher. I'm assuming you're getting a degree of some sort. How will you manage in a job setting if you're unable to complete your assignments without help?

I will give you a small example anyhow.

#include <iostream>int main(){	int Map[5][8] = {		{0, 0, 0, 0, 0, 0, 0, 0},		{0, 0, 2, 0, 0, 3, 0, 0},		{0, 0, 0, 1, 0, 0, 0, 0},		{0, 0, 0, 0, 0, 0, 0, 0},		{0, 0, 0, 0, 0, 0, 0, 0}	};	int Y = 0;	int X = 0;	bool Game_Active = true;	while (Game_Active == true)	{		std::cout << "Enter for 'Y': ";		std::cin >> Y;		std::cin.get();		std::cout << "\n\n";		std::cout << "Enter for 'X': ";		std::cin >> X;		std::cin.get();		std::cout << "\n\n";		if (Map[Y][X] == 1)		{			std::cout << "You sunk ship 1!";			Game_Active = false;		}		else if (Map[Y][X] == 2)		{			std::cout << "You sunk ship 2!";			Game_Active = false;		}		else if (Map[Y][X] == 3)		{			std::cout << "You sunk ship 3!";			Game_Active = false;		}		std::cout << "\n\n NEW TURN!\n\n";	}	std::cin.get();	return 0;}

So if you type for Y: 1 and X: 2 you will sink ship 2. After you sink a ship, just add in some code to change [1][2] in your map array to 0, or whatever counts as a miss.

##### Share on other sites
I apologize again for posting a hw question here and for forgetting to say the ships have to be randomly placed as well as being of varying sizes (5 blocks or 3 blocks etc)

I have everything else created and ready to play except the creation and placement of these ships.

##### Share on other sites
Placing your ships can be done with minor changes to what I showed above. Enter your Y and X then enter your ship you want to place.

Enter Y: 1
Enter X: 4
Enter Ship: 5

Then have your array change that spot.

Map[Y][X] = 5;

Randomly placed ship can be done from using rand while going through your array.

For placing parts of the ship, just use 1, 1, 1, as one ship. Once that array doesn't contain any 1's that ship is gone.

##### Share on other sites
Quote:
 Original post by MrCpawFor placing parts of the ship, just use 1, 1, 1, as one ship. Once that array doesn't contain any 1's that ship is gone.

That makes a lot more sense. That's what I was struggling to understand. Much appreciated MrCpaw. I thank you for the help.

##### Share on other sites
No problem, best of luck!

##### Share on other sites
Quote:
 Original post by neipo13*Your program should determine if the user’s guess was a hit or miss and store the result in a second 2-Dimensional array. If the result was a hit, your program will need to check whether the specific ship that was hit has been hit in all of that ship’s location and is therefore sunk. If a ship has been sunk, your program should output which ship was sunk. *The way I have the game set up now is I have a 2D array where 0 is an unmarked and unfilled spot on the grid,1 is a miss, 2 is a ship that has not yet been marked, and 3 is a hit. I use char disp[] = {' ', 'O', ' ', 'X'} in my drawGrid function to mark the map. I just cannot figure out how I am supposed to make the ships specific like that without using any structures.

That's not enough information. Use the number to indicate which ship the square is a part of, when it's an unmarked part of a ship. For example, 2 = hit, 3 = un-hit submarine, 4 = un-hit destroyer, etc. Then when you hit a segment, check whether any other squares have the same marking as the segment that was hit. Hint: You don't need to check the whole grid, just squares that are in a line with the square that was hit, out to a certain distance.

##### Share on other sites
Quote:
 Original post by ZahlmanThat's not enough information. Use the number to indicate which ship the square is a part of, when it's an unmarked part of a ship. For example, 2 = hit, 3 = un-hit submarine, 4 = un-hit destroyer, etc.

To extend on that you could make use of certain bits of the integer meaning certain things in the numbers you pick, eg:

0x0000 //empty
0x1000 //miss
0x0001 //destroyer tile
0x1001 //hit destroyer tile
0x0002 //battelship
0x1002 //hit battle ship

Then you can use bit operators rather than switch/if chains.
For example to mark a tile hit you simply do:
map[x][y] |= 0x1000;

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013224
×