-1

here will be my code for famous Knights Tour for 8x8 deck. So, the main idea of my code is: we will choose from Turns our destination, check it with isPossible and then go recursevly to it, marked this cell to '1'. So, check every cell, and if we will be in 64's cell - return true. But my code goes to infinite recurssion, and I can't debug it, any recommendation will be greatly appreciated.

class Class1
{
    static void Main(string[] args)
    {
        int x = 0;
        int y = 0;
        Console.WriteLine("Enter X and press enter");
        x = Int32.Parse(Console.ReadLine());
        Console.WriteLine("Enter Y and press enter");
        y = Int32.Parse(Console.ReadLine());
        TurnVariation Turns = new TurnVariation(); 
        EmptyBoard Board = new EmptyBoard();
        if (TryPut.Put(Board, x, y, Turns, 1, false))
        {
            Console.WriteLine("МОЖНА!!!!");
        }
        else
        {
            Console.WriteLine("NET!!");
        }
    }
}

public class TryPut : EmptyBoard
{
    public static bool Put(EmptyBoard Board, int x, int y, TurnVariation Turns, int count, bool flag)
    {
        int tempX = 0;
        int tempY = 0;
        if (count >= 64)
        {
            Console.WriteLine("yeab");
            return true;
        }
        for (int i = 0; i <= 7; i++)
        {
            tempX = x + Turns.Turns[i,0];
            tempY = y + Turns.Turns[i,1];
            //Console.WriteLine(count); 
            if (IsPossible(Board, tempX, tempY))
            {
                Board.Array[tempX, tempY] = 1;

                flag = Put(Board, tempX, tempY, Turns, count+1, flag);
                if (flag)
                {
                    break;
                }
                Board.Array[tempX, tempY] = 0;
            }
        }
        if (flag)
            return true;
        else
            return false;
    }

    public static bool IsPossible(EmptyBoard Board, int x, int y)
    {
        if ((x < 0) || (x > 7) || (y < 0) || (y > 7))
            return false;
        if (Board.Array[x, y] == 1)
            return false;
        return true;
    }
}

public class TurnVariation
{
    public int[,] Turns = new int[8, 2];
    public TurnVariation()
    {
        Turns[0, 0] = -2; Turns[0, 1] = 1;
        Turns[1,0] = -2; Turns[1,1] = -1;
        Turns[2,0] = -1; Turns[2,1] = 2;
        Turns[3,0] = 1; Turns[3,1] = 2;
        Turns[4,0] = 2; Turns[4,1] = 1;
        Turns[5,0] = 2; Turns[5,1] = -1;
        Turns[6,0] = 1; Turns[6,1] = -2;
        Turns[7,0] = -1; Turns[7,1] = -2;
    }
}

public class EmptyBoard
{
    public const int N = 8;
    public int[,] Array = new int[N, N];
    public EmptyBoard()
    {
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                Array[i, j] = 0;
    }
}
false
  • 10,264
  • 13
  • 101
  • 209
ratkke
  • 469
  • 1
  • 4
  • 13
  • 3
    "I can't debug it" Why not? I recommend you learn how to debug it. – spender Oct 19 '14 at 21:49
  • But here is bust of all variants, how can i debug it? – ratkke Oct 19 '14 at 21:58
  • 1
    You can easily debug with the Visual Studio debugger using breakpoints.. or even just putting some trace output in the right places and ensuring that all your values are in an expected range. – spender Oct 19 '14 at 22:01
  • What is your time complexity? try every permutation for 8x8 (which is 64!) is impossible for any normal computer, so it is normal if this is work for 4x4 (16!) but not 8x8 – Pham Trung Oct 20 '14 at 05:57

1 Answers1

3

I think your problem is that your testing for count<64, but you never assign to count. You just pass (by value!) 'Count + 1' to the put method. You are probably thinking that this will bewritten back to the count variable. But that is not the case...

Do note that debugging is the first skill you need to learn!

Steven
  • 166,672
  • 24
  • 332
  • 435
Ric .Net
  • 5,540
  • 1
  • 20
  • 39
  • Thank you for you recommendation, but after i change my 'count + 1' to '++count' this program actually works for 8x8. But it works on 4x4 too, which is impossible. – ratkke Oct 19 '14 at 22:25
  • @ratkke What is your time complexity? try every permutation for 8x8 (which is 64!) is impossible for any normal computer, so it is normal if this is work for 4x4 (16!) but not 8x8 – Pham Trung Oct 20 '14 at 05:58
  • 1
    Your question was why your code was running indefinitely. I pointed that out to you. Why it is also working for 4x4....? I think you should look at your design... – Ric .Net Oct 20 '14 at 12:14