Click here to Skip to main content
15,897,371 members

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 :

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
Tags: C++

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

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.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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