Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Knight shortest path

shortest path

  • Please log in to reply
5 replies to this topic

#1 Ipolit

Ipolit

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 28 December 2011 - 08:41 AM

Hello!

I have a task to make a program that outputs a knights path on a 8*8 chessboard from one location to another. I have done it in C but my task is to make it in C# so I have used code2code.net to translate the code to C# but I can not compile the program although in C it works. Please can someone help me? The printf problem can be solved by console.writeline but I cant solve the problem with && in printMoves and futrLegalMove. It says that && operation cant be done with bool and int. im new to C# and dont know where to search for help. Hope you guys can help... thank you!

//Translation of code submitted from 90.190.199.134
//Converted from C++ by xlat on 2011Dec28.1137
//C++ parser version:2006Jan.A0

using System;
using System.Runtime.InteropServices;

public class stdio_h
{
	//suppressed printf
	[DllImport("stdio.h.xml", SetLastError=true)]
	public static extern int scanf(ref char arg0 ,params object[] args);

}
public class Globals
{
	public static int m1 = 0;
	public static int m2 = 0;
	///<summary>
	/// This array contains three columns and 37 rows: The rows signify the possible coordinate differences. The columns 1 and 2 contains the possible permutations of the row and column difference  between two positions on a chess board; The column 3 contains the minimum number of steps involved in traversing the knight's  path with the given permutation
	///</summary>
	public static int[] arr = new int[]{
		{ 0, 0, 0} ,
		{ 0, 1, 3} ,
		{ 0, 2, 2} ,
		{ 0, 3, 3} ,
		{ 0, 4, 2} ,
		{ 0, 5, 3} ,
		{ 0, 6, 4} ,
		{ 0, 7, 5} ,
		{ 1, 1, 2} ,
		{ 1, 2, 1} ,
		{ 1, 3, 2} ,
		{ 1, 4, 3} ,
		{ 1, 5, 4} ,
		{ 1, 6, 3} ,
		{ 1, 7, 4} ,
		{ 2, 2, 4} ,
		{ 2, 3, 3} ,
		{ 2, 4, 2} ,
		{ 2, 5, 3} ,
		{ 2, 6, 3} ,
		{ 2, 7, 5} ,
		{ 3, 3, 2} ,
		{ 3, 4, 3} ,
		{ 3, 5, 4} ,
		{ 3, 6, 3} ,
		{ 3, 7, 4} ,
		{ 4, 4, 4} ,
		{ 4, 5, 3} ,
		{ 4, 6, 4} ,
		{ 4, 7, 5} ,
		{ 5, 5, 4} ,
		{ 5, 6, 5} ,
		{ 5, 7, 4} ,
		{ 6, 6, 5} ,
		{ 6, 7, 5} ,
		{ 7, 7, 6} ,

	};
	///<summary>
	///Rule R189: 'main' changed into 'Main'
	///</summary>
	public static void Main()
	{
		Console.Write("KNIGHT'S SHORTEST PATH ON A 8*8 CHESSBOARD :\n");
		Console.Write("------------------------------------------");
		Console.Write("\nThe chessboard may be treated as a 8*8 array here i.e. the (1,1) ");
		Console.Write("\non chessboard is to be referred as (0,0) here and same for (8,8) ");
		Console.Write("\nwhich is to be referred as (7,7) and likewise.\n");
		int ix ,iy ,fx ,fy;
		Console.Write("\nEnter the initial position of the knight :\n");
		stdio_h.scanf(ref "%d%d",ix ,iy);
		Console.Write("\nEnter the final position to be reached :\n");
		stdio_h.scanf(ref "%d%d",fx ,fy);
		int px = ix ,py = iy;
		int temp;
		int tx ,ty;
		Console.Write("\nThe Knight's shortest path is given by :\n\n");
		Console.Write("({0}, {1})",ix ,iy);
		futrLegalMove(px ,py ,m1 ,m2);
		printMoves(px ,py ,fx ,fy ,m1 ,m2);
		getch();
	}
	///<summary>
	///   This method checkSteps() checks the minimum number of steps involved from current   position(a & b) to final position(c & d) by looking up in the array arr[][]. 
	///</summary>
	public static int checkSteps(int a ,int b ,int c ,int d)
	{
		int xdiff ,ydiff;
		int i ,j;
		if(c > a)
		{
			xdiff = c - a;
		}
		else
		{
			xdiff = a - c;
		}
		if(d > b)
		{
			ydiff = d - b;
		}
		else
		{
			ydiff = b - d;
		}
		for(i = 0;
		i < 37;(i)++)
		{
			if(xdiff == arr[i ][0] && ydiff == arr[i ][1] || xdiff == arr[i ][1] && ydiff == arr[i ][0])
			{
				j = arr[i ][2];
				break;
			}
		}
		return j;
	}
	///<summary>
	/// The method checkMove() checks whether the move in consideration is beyond the scope of board or not. 
	///</summary>
	public static int checkMove(int a ,int b)
	{
		if(a > 7 || b > 7 || a < 0 || b < 0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
		//return value: Added during conversion from C++
		return 0;
	}
	///<summary>
	/// This method printMoves() prints all the moves involved. 
	///</summary>
	public static void printMoves(int px ,int py ,int fx ,int fy ,int a ,int b)
	{
		int temp;
		int tx ,ty;
		int t1 ,t2;
		while(!(px == fx && py == fy)){
			Console.Write(" --> ");
			temp = checkSteps(px + a ,py + b ,fx ,fy);
			tx = px + a;
			ty = py + b;
			if(!(a == 2 && b == 1))
			{
				if(checkSteps(px + 2,py + 1,fx ,fy) < temp && checkMove(px + 2,py + 1))
				{
					temp = checkSteps(px + 2,py + 1,fx ,fy);
					tx = px + 2;
					ty = py + 1;
				}
			}
			if(!(a == 2 && b == -1))
			{
				if(checkSteps(px + 2,py - 1,fx ,fy) < temp && checkMove(px + 2,py - 1))
				{
					temp = checkSteps(px + 2,py - 1,fx ,fy);
					tx = px + 2;
					ty = py - 1;
				}
			}
			if(!(a == -2 && b == 1))
			{
				if(checkSteps(px - 2,py + 1,fx ,fy) < temp && checkMove(px - 2,py + 1))
				{
					temp = checkSteps(px - 2,py + 1,fx ,fy);
					tx = px - 2;
					ty = py + 1;
				}
			}
			if(!(a == -2 && b == -1))
			{
				if(checkSteps(px - 2,py - 1,fx ,fy) < temp && checkMove(px - 2,py - 1))
				{
					temp = checkSteps(px - 2,py - 1,fx ,fy);
					tx = px - 2;
					ty = py - 1;
				}
			}
			if(!(a == 1 && b == 2))
			{
				if(checkSteps(px + 1,py + 2,fx ,fy) < temp && checkMove(px + 1,py + 2))
				{
					temp = checkSteps(px + 1,py + 2,fx ,fy);
					tx = px + 1;
					ty = py + 2;
				}
			}
			if(!(a == 1 && b == -2))
			{
				if(checkSteps(px + 1,py - 2,fx ,fy) < temp && checkMove(px + 1,py - 2))
				{
					temp = checkSteps(px + 1,py - 2,fx ,fy);
					tx = px + 1;
					ty = py - 2;
				}
			}
			if(!(a == -1 && b == 2))
			{
				if(checkSteps(px - 1,py + 2,fx ,fy) < temp && checkMove(px - 1,py + 2))
				{
					temp = checkSteps(px - 1,py + 2,fx ,fy);
					tx = px - 1;
					ty = py + 2;
				}
			}
			if(!(a == -1 && b == -2))
			{
				if(checkSteps(px - 1,py - 2,fx ,fy) < temp && checkMove(px - 1,py - 2))
				{
					temp = checkSteps(px - 1,py - 2,fx ,fy);
					tx = px - 1;
					ty = py - 2;
				}
			}
			//the step taken in the current move in the x direction.
			t1 = tx - px;
			//" " " " " " " " " " " " " " " " " " " " " y " " " " ".
			t2 = ty - py;
			px = tx;
			py = ty;
			Console.Write("({0}, {1})",px ,py);
			futrLegalMove(px ,py ,t1 ,t2);
			a = m1;
			b = m2;
		}
		;
	}
	///<summary>
	///Out of the 8 possible moves, this function futrLegalMove() sets the valid move by   applying the following constraints       1. The next move should not be beyond the scope of the board.       2. The next move should not be the exact opposite of the previous move.   The 1st constraint is checked by sending all possible moves to the checkMove()    method;   The 2nd constraint is checked by passing as parameters(i.e. a and b) the steps of the    previous move and checking whether or not it is the exact opposite of the current move. 
	///</summary>
	public static void futrLegalMove(int px ,int py ,int a ,int b)
	{
		if(checkMove(px + 2,py + 1) && a != -2 && b != -1)
		{
			m1 = 2;
			m2 = 1;
		}
		else if(checkMove(px + 2,py - 1) && a != -2 && b != 1)
		{
			m1 = 2;
			m2 = -1;
		}
		else if(checkMove(px - 2,py + 1) && a != 2 && b != -1)
		{
			m1 = -2;
			m2 = 1;
		}
		else if(checkMove(px - 2,py - 1) && a != 2 && b != 1)
		{
			m1 = -2;
			m2 = -1;
		}
		else if(checkMove(px + 1,py + 2) && b != -2 && a != -1)
		{
			m2 = 2;
			m1 = 1;
		}
		else if(checkMove(px + 1,py - 2) && a != -1 && b != 2)
		{
			m2 = -2;
			m1 = 1;
		}
		else if(checkMove(px - 1,py + 2) && a != 1 && b != -2)
		{
			m2 = 2;
			m1 = -1;
		}
		else if(checkMove(px - 1,py - 2) && a != 1 && b != 2)
		{
			m2 = -2;
			m1 = -1;
		}
	}
}

  • 0

#2 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 28 December 2011 - 10:07 AM

Why did you not just write it in C# in the first place? Is there a reason you have to use C's scanf and not built-in C# functions?

I copied the code into Visual Studio and got A LOT of errors!

Error    1    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    24    3    ConsoleApplication1
Error    2    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    25    3    ConsoleApplication1
Error    3    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    26    3    ConsoleApplication1
Error    4    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    27    3    ConsoleApplication1
Error    5    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    28    3    ConsoleApplication1
Error    6    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    29    3    ConsoleApplication1
Error    7    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    30    3    ConsoleApplication1
Error    8    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    31    3    ConsoleApplication1
Error    9    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    32    3    ConsoleApplication1
Error    10    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    33    3    ConsoleApplication1
Error    11    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    34    3    ConsoleApplication1
Error    12    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    35    3    ConsoleApplication1
Error    13    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    36    3    ConsoleApplication1
Error    14    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    37    3    ConsoleApplication1
Error    15    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    38    3    ConsoleApplication1
Error    16    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    39    3    ConsoleApplication1
Error    17    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    40    3    ConsoleApplication1
Error    18    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    41    3    ConsoleApplication1
Error    19    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    42    3    ConsoleApplication1
Error    20    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    43    3    ConsoleApplication1
Error    21    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    44    3    ConsoleApplication1
Error    22    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    45    3    ConsoleApplication1
Error    23    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    46    3    ConsoleApplication1
Error    24    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    47    3    ConsoleApplication1
Error    25    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    48    3    ConsoleApplication1
Error    26    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    49    3    ConsoleApplication1
Error    27    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    50    3    ConsoleApplication1
Error    28    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    51    3    ConsoleApplication1
Error    29    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    52    3    ConsoleApplication1
Error    30    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    53    3    ConsoleApplication1
Error    31    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    54    3    ConsoleApplication1
Error    32    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    55    3    ConsoleApplication1
Error    33    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    56    3    ConsoleApplication1
Error    34    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    57    3    ConsoleApplication1
Error    35    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    58    3    ConsoleApplication1
Error    36    Array initializers can only be used in a variable or field initializer. Try using a new expression instead.    59    3    ConsoleApplication1
Error    37    The best overloaded method match for 'stdio_h.scanf(ref char, params object[])' has some invalid arguments    74    9    ConsoleApplication1
Error    38    Argument 1: cannot convert from 'string' to 'ref char'    74    27    ConsoleApplication1
Error    39    A ref or out argument must be an assignable variable    74    27    ConsoleApplication1
Error    40    The best overloaded method match for 'stdio_h.scanf(ref char, params object[])' has some invalid arguments    76    9    ConsoleApplication1
Error    41    Argument 1: cannot convert from 'string' to 'ref char'    76    27    ConsoleApplication1
Error    42    A ref or out argument must be an assignable variable    76    27    ConsoleApplication1
Error    43    The name 'getch' does not exist in the current context    84    9    ConsoleApplication1
Error    44    Cannot apply indexing with [] to an expression of type 'int'    112    26    ConsoleApplication1
Error    45    Cannot apply indexing with [] to an expression of type 'int'    112    48    ConsoleApplication1
Error    46    Cannot apply indexing with [] to an expression of type 'int'    112    70    ConsoleApplication1
Error    47    Cannot apply indexing with [] to an expression of type 'int'    112    92    ConsoleApplication1
Error    48    Cannot apply indexing with [] to an expression of type 'int'    114    21    ConsoleApplication1
Error    50    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    152    21    ConsoleApplication1
Error    51    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    161    21    ConsoleApplication1
Error    52    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    170    21    ConsoleApplication1
Error    53    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    179    21    ConsoleApplication1
Error    54    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    188    21    ConsoleApplication1
Error    55    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    197    21    ConsoleApplication1
Error    56    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    206    21    ConsoleApplication1
Error    57    Operator '&&' cannot be applied to operands of type 'bool' and 'int'    215    21    ConsoleApplication1
Error    58    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    240    13    ConsoleApplication1
Error    59    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    245    18    ConsoleApplication1
Error    60    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    250    18    ConsoleApplication1
Error    61    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    255    18    ConsoleApplication1
Error    62    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    260    18    ConsoleApplication1
Error    63    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    265    18    ConsoleApplication1
Error    64    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    270    18    ConsoleApplication1
Error    65    Operator '&&' cannot be applied to operands of type 'int' and 'bool'    275    18    ConsoleApplication1

Errors 1 to 36 are because you are using arr as a 2d array but it is only initalized as a 1D array.

This isn't how you declare a 2D array:

public static int[] arr = new int[] { }

This should be done like this:

public static int[,] arr = {
        { 0, 0, 0} ,
        { 0, 1, 3} ,
        { 0, 2, 2} ,
        { 0, 3, 3} ,
        { 0, 4, 2} ,
        { 0, 5, 3} ,
        { 0, 6, 4} ,
        { 0, 7, 5} ,
        { 1, 1, 2} ,
        { 1, 2, 1} ,
        { 1, 3, 2} ,
        { 1, 4, 3} ,
        { 1, 5, 4} ,
        { 1, 6, 3} ,
        { 1, 7, 4} ,
        { 2, 2, 4} ,
        { 2, 3, 3} ,
        { 2, 4, 2} ,
        { 2, 5, 3} ,
        { 2, 6, 3} ,
        { 2, 7, 5} ,
        { 3, 3, 2} ,
        { 3, 4, 3} ,
        { 3, 5, 4} ,
        { 3, 6, 3} ,
        { 3, 7, 4} ,
        { 4, 4, 4} ,
        { 4, 5, 3} ,
        { 4, 6, 4} ,
        { 4, 7, 5} ,
        { 5, 5, 4} ,
        { 5, 6, 5} ,
        { 5, 7, 4} ,
        { 6, 6, 5} ,
        { 6, 7, 5} ,
        { 7, 7, 6}
    };


That took a little while to figure out, as the syntax is much different than what I'm used to.

To access elements in an array you must do it as follows:

if (xdiff == arr[i,0] && ydiff == arr[i,1] || xdiff == arr[i,1] && ydiff == arr[i,0])

This in checkSteps and j must be initialized to a value. You have to return a value, if you don't initalize it C# won't let you return it.

In Main:

int ix, iy, fx, fy;
        Console.Write("\nEnter the initial position of the knight :\n");
        stdio_h.scanf(ref "%d%d", ix, iy);
        Console.Write("\nEnter the final position to be reached :\n");
        stdio_h.scanf(ref "%d%d", fx, fy);

C# requires all values to have a value before being passed to a function. My computer is erroring on the scanf lines for two reasons:

1. ix and iy need to have a value before being passed in to scanf.
2. You cannot pass by reference a value. Meaning if you pass something by reference it has to be in a variable.

Also this is problematic because the scanf function takes a char by reference. It needs to be a string. The converter would have seen scanf takes char* for a C-style string but it doesn't work this way in C#. So you can't use stdio.scanf the way you think. Try changing it to string.

I can't compile it because I don't have the C headers on my computer.

Another problem is simply that getch cannot be found. I think you would have to declare this as an extern method as well as scanf?

The error: "It says that && operation cant be done with bool and int." is because checkMove returns an int not a boolean. You can only join boolean expressions with && . You probably mean to make checkMove return bool. In C you can treat the int 1 as a boolean value true but this implicit conversion doesn't work in C#.

public static bool checkMove(int a, int b)
    {
        return !(a > 7 || b > 7 || a < 0 || b < 0);
    }

You probably should make use of the C# libraries and not external C code unless you really need it and in this case you don't. Also don't use converters they are more trouble than good. This messed up badly because there are a lot of differences in C and C# code.
  • 0

#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 28 December 2011 - 10:14 AM

i normally don't hand out code, but I was bored at work, and this gave me a quick jolt.

Program.cs

using System;

namespace KNights
{
	/// <summary>
	/// Description of Globals.
	/// </summary>

	public class Globals
	{
		public static int m1 = 0;
		public static int m2 = 0;
		///<summary>
		/// This array contains three columns and 37 rows: The rows signify the possible coordinate differences. The columns 1 and 2 contains the possible permutations of the row and column difference  between two positions on a chess board; The column 3 contains the minimum number of steps involved in traversing the knight's  path with the given permutation
		///</summary>
		public static int[][] arr = new int[][]{
			new int[] { 0, 0, 0} ,
			new int[] { 0, 1, 3} ,
			new int[] { 0, 2, 2} ,
			new int[] { 0, 3, 3} ,
			new int[] { 0, 4, 2} ,
			new int[] { 0, 5, 3} ,
			new int[] { 0, 6, 4} ,
			new int[] { 0, 7, 5} ,
			new int[] { 1, 1, 2} ,
			new int[] { 1, 2, 1} ,
			new int[] { 1, 3, 2} ,
			new int[] { 1, 4, 3} ,
			new int[] { 1, 5, 4} ,
			new int[] { 1, 6, 3} ,
			new int[] { 1, 7, 4} ,
			new int[] { 2, 2, 4} ,
			new int[] { 2, 3, 3} ,
			new int[] { 2, 4, 2} ,
			new int[] { 2, 5, 3} ,
			new int[] { 2, 6, 3} ,
			new int[] { 2, 7, 5} ,
			new int[] { 3, 3, 2} ,
			new int[] { 3, 4, 3} ,
			new int[] { 3, 5, 4} ,
			new int[] { 3, 6, 3} ,
			new int[] { 3, 7, 4} ,
			new int[] { 4, 4, 4} ,
			new int[] { 4, 5, 3} ,
			new int[] { 4, 6, 4} ,
			new int[] { 4, 7, 5} ,
			new int[] { 5, 5, 4} ,
			new int[] { 5, 6, 5} ,
			new int[] { 5, 7, 4} ,
			new int[] { 6, 6, 5} ,
			new int[] { 6, 7, 5} ,
			new int[] { 7, 7, 6} ,

		};
		///<summary>
		///   This method checkSteps() checks the minimum number of steps involved from current   position(a & b) to final position(c & d) by looking up in the array arr[][].
		///</summary>
		public static int checkSteps(int a ,int b ,int c ,int d)
		{
			int xdiff ,ydiff;
			int i = 0 ,j = 0;
			if(c > a)
			{
				xdiff = c - a;
			}
			else
			{
				xdiff = a - c;
			}
			if(d > b)
			{
				ydiff = d - b;
			}
			else
			{
				ydiff = b - d;
			}
			
			for(i = 0; i < 37;(i)++)
			{
				if(xdiff == arr[i ][0] && ydiff == arr[i ][1] || xdiff == arr[i ][1] && ydiff == arr[i][0])
				{
					j = arr[i ][2];
					break;
				}
			}
			return j;
		}
		///<summary>
		/// The method checkMove() checks whether the move in consideration is beyond the scope of board or not.
		///</summary>
		public static int checkMove(int a ,int b)
		{
			if(a > 7 || b > 7 || a < 0 || b < 0)
			{
				return 0;
			}
			else
			{
				return 1;
			}
			//return value: Added during conversion from C++
		}
		///<summary>
		/// This method printMoves() prints all the moves involved.
		///</summary>
		public static void printMoves(int px ,int py ,int fx ,int fy ,int a ,int b)
		{
			int temp;
			int tx ,ty;
			int t1 ,t2;
			while(!(px == fx && py == fy)){
				Console.Write(" --> ");
				temp = checkSteps(px + a ,py + b ,fx ,fy);
				tx = px + a;
				ty = py + b;
				if(!(a == 2 && b == 1))
				{
					if(checkSteps(px + 2,py + 1,fx ,fy) < temp && checkMove(px + 2,py + 1) == 1)
					{
						temp = checkSteps(px + 2,py + 1,fx ,fy);
						tx = px + 2;
						ty = py + 1;
					}
				}
				if(!(a == 2 && b == -1))
				{
					if(checkSteps(px + 2,py - 1,fx ,fy) < temp && checkMove(px + 2,py - 1) == 1)
					{
						temp = checkSteps(px + 2,py - 1,fx ,fy);
						tx = px + 2;
						ty = py - 1;
					}
				}
				if(!(a == -2 && b == 1))
				{
					if(checkSteps(px - 2,py + 1,fx ,fy) < temp && checkMove(px - 2,py + 1) == 1)
					{
						temp = checkSteps(px - 2,py + 1,fx ,fy);
						tx = px - 2;
						ty = py + 1;
					}
				}
				if(!(a == -2 && b == -1))
				{
					if(checkSteps(px - 2,py - 1,fx ,fy) < temp && checkMove(px - 2,py - 1) == 1)
					{
						temp = checkSteps(px - 2,py - 1,fx ,fy);
						tx = px - 2;
						ty = py - 1;
					}
				}
				if(!(a == 1 && b == 2))
				{
					if(checkSteps(px + 1,py + 2,fx ,fy) < temp && checkMove(px + 1,py + 2) == 1)
					{
						temp = checkSteps(px + 1,py + 2,fx ,fy);
						tx = px + 1;
						ty = py + 2;
					}
				}
				if(!(a == 1 && b == -2))
				{
					if(checkSteps(px + 1,py - 2,fx ,fy) < temp && checkMove(px + 1,py - 2) == 1)
					{
						temp = checkSteps(px + 1,py - 2,fx ,fy);
						tx = px + 1;
						ty = py - 2;
					}
				}
				if(!(a == -1 && b == 2))
				{
					if(checkSteps(px - 1,py + 2,fx ,fy) < temp && checkMove(px - 1,py + 2) == 1)
					{
						temp = checkSteps(px - 1,py + 2,fx ,fy);
						tx = px - 1;
						ty = py + 2;
					}
				}
				if(!(a == -1 && b == -2))
				{
					if(checkSteps(px - 1,py - 2,fx ,fy) < temp && checkMove(px - 1,py - 2) == 1)
					{
						temp = checkSteps(px - 1,py - 2,fx ,fy);
						tx = px - 1;
						ty = py - 2;
					}
				}
				//the step taken in the current move in the x direction.
				t1 = tx - px;
				//" " " " " " " " " " " " " " " " " " " " " y " " " " ".
				t2 = ty - py;
				px = tx;
				py = ty;
				Console.Write("({0}, {1})",px ,py);
				futrLegalMove(px ,py ,t1 ,t2);
				a = m1;
				b = m2;
			}
			;
		}
		///<summary>
		///Out of the 8 possible moves, this function futrLegalMove() sets the valid move by   applying the following constraints       1. The next move should not be beyond the scope of the board.       2. The next move should not be the exact opposite of the previous move.   The 1st constraint is checked by sending all possible moves to the checkMove()    method;   The 2nd constraint is checked by passing as parameters(i.e. a and b) the steps of the    previous move and checking whether or not it is the exact opposite of the current move.
		///</summary>
		public static void futrLegalMove(int px ,int py ,int a ,int b)
		{
			if(checkMove(px + 2,py + 1) == 1 && a != -2 && b != -1)
			{
				m1 = 2;
				m2 = 1;
			}
			else if(checkMove(px + 2,py - 1) == 1 && a != -2 && b != 1)
			{
				m1 = 2;
				m2 = -1;
			}
			else if(checkMove(px - 2,py + 1) == 1 && a != 2 && b != -1)
			{
				m1 = -2;
				m2 = 1;
			}
			else if(checkMove(px - 2,py - 1) == 1 && a != 2 && b != 1)
			{
				m1 = -2;
				m2 = -1;
			}
			else if(checkMove(px + 1,py + 2) == 1 && b != -2 && a != -1)
			{
				m2 = 2;
				m1 = 1;
			}
			else if(checkMove(px + 1,py - 2) == 1 && a != -1 && b != 2)
			{
				m2 = -2;
				m1 = 1;
			}
			else if(checkMove(px - 1,py + 2) == 1 && a != 1 && b != -2)
			{
				m2 = 2;
				m1 = -1;
			}
			else if(checkMove(px - 1,py - 2) == 1 && a != 1 && b != 2)
			{
				m2 = -2;
				m1 = -1;
			}
		}
	}

Globals.cs

namespace KNights
{
	class Program
	{
		///<summary>
		///Rule R189: 'main' changed into 'Main'
		///</summary>
		public static void Main()
		{
			Console.Write("KNIGHT'S SHORTEST PATH ON A 8*8 CHESSBOARD :\n");
			Console.Write("------------------------------------------");
			Console.Write("\nThe chessboard may be treated as a 8*8 array here i.e. the (1,1) ");
			Console.Write("\non chessboard is to be referred as (0,0) here and same for (8,8) ");
			Console.Write("\nwhich is to be referred as (7,7) and likewise.\n");
			
			int ix = 0;
			int iy = 0;
			int fx = 0;
			int fy = 0;
			
			Console.Write("\nEnter the initial position of the knight :\n");
			
			Console.Write("\nX:");
			int.TryParse( Console.ReadLine(), out ix);
			
			Console.Write("\nY:");
			int.TryParse( Console.ReadLine(), out iy);
				
			//Console.ReadLine("%d%d",ix ,iy);
			Console.Write("\nEnter the final position to be reached :\n");
			Console.Write("\nX:");
			int.TryParse( Console.ReadLine(), out fx);
			Console.Write("\nY:");
			int.TryParse( Console.ReadLine(), out fy);
			//stdio_h.scanf(ref "%d%d",fx ,fy);
			int px = ix ,py = iy;
			Console.Write("\nThe Knight's shortest path is given by :\n\n");
			Console.Write("({0}, {1})",ix ,iy);
			Globals.futrLegalMove(px ,py ,Globals.m1 ,Globals.m2);
			Globals.printMoves(px ,py ,fx ,fy ,Globals.m1 ,Globals.m2);
			Console.ReadLine();

		}
	}
	
}

  • 0

#4 Ipolit

Ipolit

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 29 December 2011 - 06:22 AM

Thank you for suggestions and the work you have done.
I have tested the code that lespauled wrote and I keep getting error when I enter the initial or final position
x or y coordinate 6 or bigger this error:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in ConsoleApplication4.exe
Additional information: Index was outside the bounds of the array.

although in C I dont get any errors. The algorith seems to be fine...
Maybe if you have some spare time you could take a look...
Thank you.
  • 0

#5 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 29 December 2011 - 06:45 AM

I believe the problem is that your initial array only contains 36 items, not 37 as mentioned in the remark.

---------- Post added at 09:45 AM ---------- Previous post was at 09:43 AM ----------

I simply changed the for loop to 36 instead of 37, and it seems to work.
  • 0

#6 Ipolit

Ipolit

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 29 December 2011 - 07:00 AM

Thanks alot. It realy does work. Thank you for your time.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download