# Is my code correct in java for backtracking where I have to find solution from corner? I am getting few errors

retagged Jul 12, 2021
public class main{
public static void main(String []args){
}
}
class Knight {
static int N = 8;

static boolean check(int x, int y, int result[][])
{
return (x >= 0 && x < N && y >= 0 && y < N
&& result[x][y] == -1);
}

static void result(int result[][])
{
for (int x = 0; x < N; x++) {
for (int y = 0; y < N; y++)
System.out.print(result[x][y] + " ");
System.out.println();
}
boolean knight_func()
{
int result[][] = new int[8][8];

for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
result[x][y] = -1;

int xMove[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int yMove[] = { 1, 2, 2, 1, -1, -2, -2, -1 };

result[0][0] = 1;

if (!knight_func(0, 0, 2, result, xMove, yMove))
{
return false;
}
else
{
for(int x = 0; x < N; x++)
{
for (int y = 0; y < N; y++)
{
printresult(result);
}

}
}
return true;
};

boolean knight_func()
{
int k, next_x, next_y;
if (movei == N * N)
return true;

for (k = 0; k < 8; k++) {
next_x = x + xMove[k];
next_y = y + yMove[k];
if (check(next_x, next_y, sol)) {
result[next_x][next_y] = movei;
if (knight_func(next_x, next_y, movei + 1,
result, xMove, yMove))
return true;
else
result[next_x][next_y]
= -1;
}
}

return false;
}

class main{
public static void main(String args[])
{
knight_func();
}
}
}

answered Jul 12, 2021 by (89,630 points)

Your code is a bit messy. If you'd just format your code (at least use indentation correctly) then you'd see the missing or extra braces.

I did this for you.

Apart from mismatch braces, you had two main classes.

I also applied the fix we talked about in http://question.onlinegdb.com/10412/why-getting-in-my-solution-for-chessboard-where-is-my-mistake.

And last, but not least, I made your code more like a proper Java code: using classes that can be instantiated and not just being a collection of static functions. Now you can just instantiate your Knight class with any size and see if you can find any results there.

```public class Main
{
public static void main(String []args)
{
Knight knight = new Knight(8);
knight.solve();
}
}

class Knight
{
private static int xMove[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
private static int yMove[] = { 1, 2, 2, 1, -1, -2, -2, -1 };

private int size;
private int result[][];

public Knight(int _size)
{
size = _size;
result = new int[size][size];
}

private boolean check(int x, int y)
{
return x >= 0 && x < size && y >= 0 && y < size && result[x][y] == -1;
}

private void printResult()
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
System.out.print(result[x][y] + "\t");
System.out.println();
}
}

public boolean solve()
{
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
result[x][y] = -1;

result[0][0] = 1;

if (!solve(0, 0, 2))
{
return false;
}

printResult();
return true;
}

private boolean solve(int x, int y, int moveCount)
{
if (moveCount == size * size + 1)
return true;

for (int k = 0; k < 8; k++)
{
int next_x = x + xMove[k];
int next_y = y + yMove[k];
if (check(next_x, next_y))
{
result[next_x][next_y] = moveCount;
if (solve(next_x, next_y, moveCount + 1))
return true;
result[next_x][next_y] = -1;
}
}

return false;
}
}```
commented Jul 12, 2021 by (1,440 points)
edited Jul 13, 2021 by npatel300
Okay got it. I will fix my code again. Thank you for the help
Can you please proof read my code? I am still getting errors. i didn't do really exact same thing that you did.
public class main
{
public static void main(String args[])
{
knight_func = new knight_func(8);
knight_func();
}
}
class knight_func
{
private static int move_x[] = {2, 1, -1, -2, -2, -1, 1, 2};
private static int move_y[] = { 1, 2, 2, 1, -1, -2, -2, -1};

private int size;
private int result[][];

public void knight_func(int size1)
{
size = size1;
result = new int[size][size];
}

private boolean check(int x, int y)
{
return (x >= 0 && x < size && y >= 0 && y < size
&& result[x][y] == -1);
}

private void result(int result[][])
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
System.out.print(result[x][y] + " ");
system.out.printIn();
}
}

private boolean knight_func()
{
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
result[x][y] = -1;

result[0][0] = 1;

if (!knight_func(0, 0, 2))
{
return false;
}
printresult();

return true;
};

private boolean knight_func(int x, int y, int movei)
{
if (movei == size * size + 1)
return true;

for (int k = 0; k < 8; k++)
{
next_x = x + move_x[k];
next_y = y + move_y[k];
if (check(next_x, next_y))
{
result[next_x][next_y] = movei;
if (knight_func(next_x, next_y, movei + 1))
return true;
result[next_x][next_y] = -1;
}
}
return false;
}
}
commented Jul 15, 2021 by (89,630 points)
Sorry, I didn't see your comment until now.

Yeah, no problem rewriting your code to your own liking. There are plenty of ways to formulate a solution to the same problem. :)

Also, it is advised that you try to compile the code and fix the problems. The compiler often clearly explains what the problem is. (e.g. cannot find symbol printresult(); ---> so you'd know there's no such function -- visible to the compiler -- as "printresult". Maybe wrong spelling?!)

Problem #1
public class Main // "Main" with capital M, not "main" with all lower case

Problem #2
You have to understand classes. Classes have constructors, that initialize the class. They have the same name as the class itself.
When you call your class "knight_func" you are doing multiple things wrong.
First of all, your constructor has the same name as your original function that does the calculation. So this becomes your constructor, but you cannot call a constructor (which is your main calculation function) directly, so your code would not compile.
Second, you should name a class a noun, an object; while you name your functions verbs, actions. E.g. a class could be called Dog while one of it's function could be Eat(). So your could write code like dog.Eat(), which is pretty easy to read and understand. Try to stick to this rule of thumb. :)

So, knowing this, your class really should be renamed e.g. to KnightTour or something similar. So should the constructor:

class KnightTour
{
// ...
public KnightTour(int size1)
{
size = size1;
result = new int[size][size];
}
// ...
}

Problem #3
knight_func = new knight_func(8);

This does not work like this. The syntax is
[type] [identifier] = new [constructor]
So this should be correctly:
public class Main
{
public static void main(String args[])
{
KnightTour knightTour = new KnightTour(8);
knightTour.knight_func();
}
}

Problem #4
Your knight_func() is private. You have to make it public so it could be called from outside of the KnightTour class itself:
public boolean knight_func()

Problem #5
system.out.printIn();
Wrong spelling. It should be with capital S and it is println not printIn (lower case 'L' and not capital 'i')
Correctly: System.out.println();

Problem #6
printresult();
There is no such function. Your function is called "resut()", so your code would look like this "correctly":
result(result);
Which is pretty confusing as the first result is the function name, while the second result it the data passed to the function. I'd recommend having a look at what I did and doing that instead.

Problem #7 and #8
next_x = x + move_x[k];
next_y = y + move_y[k];
Neither next_x nor next_y is declared (the compiler has no idea what type they are).
Correctly:
int next_x = x + move_x[k];
int next_y = y + move_y[k];

And now the code runs and seems to produce the correct output.

Summary: you should try to run your code and fix any problems the compiler finds for you.

Good luck! :)