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;
}
}
}


Sign In
Create Account

Back to top









