Click here to Skip to main content
12,401,324 members (60,655 online)

I program battleship c++ but it has problem

homa sh asked:

Open original thread
Hello
I want to program for ship game : according to below text
For example we have 4 ship and we have a table 4*4 and we want put ship in this table according to capacity of column and row.
Therefore I used backtracking and for this shape is true answer .

http://upload.ugm.ac.id/434shape.docx

It is true answer when we have ships enter with this order : ship 1-ship2- ship3-ship4
But if ships had this order : ship 2-ship1- ship3-ship4 the answer will be wrong.
Actually if input shape was according to below shape , my answer is wrong.

http://upload.ugm.ac.id/434shape.docx[^]

Can any body help me?
My code is :

void General::Ship(int count,int k,int n)  //  count <= k  
{
	 int lenght;
 
	lenght=L[count];    // L contan ships length means for example 2:is 3-2-2-2

	for (int x=1;x<=n;x++)
	{
		for(int y=1;y<=n;y++)
		{
			if (IsOkhorizontal(x,y,lenght,n)==1 )
			{
				Horizontally(x,y,lenght,count,n);
				if(count==k)
				{
					Print();
				}
				else
					Ship(count+1,k,n);
			}
		    if (IsOkvertical(x,y,lenght,n)==1)
			{
			    Vertically(x,y,lenght,count,n);
				if(count==k)
				{
					Print();
				}
				else
					Ship(count+1,k,n);
			}
			
		}	
	}
}

For this example we should calling ship(1,4,4)
thanks
Tags: C++

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100