Click here to Skip to main content
15,868,016 members

I program battleship c++ but it has problem

Revision 6
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 is :

C++
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
Posted 20-Jan-13 22:12pm by homa sh.
Tags: