-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessComplete.java
73 lines (66 loc) · 1.46 KB
/
ChessComplete.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Recursion;
public class ChessComplete {
private static int[][] board;
private int position;
private int count, numSteps;
public ChessComplete(int size) {
board = new int[size][size];
count = 0;
numSteps = size*size;
}
public boolean isEmpty(int r, int c)
{
if (board[r][c] == 0)
return true;
else
return false;
}
public boolean move(int x, int y) {
count++;
if (x < 0 || y < 0 || x >= board.length || y >= board.length)
{
return false;
}
else if (numSteps == position)
return true;
else if (!isEmpty(x, y))
{
return false;
}
else {
position++;
board[x][y] = position;
if (move(x+2, y-1) || move(x+2, y+1) || move(x-2, y+1) || move(x-2, y-1)
|| move(x+1, y+2) || move(x-1, y+2) || move(x+1, y-2) || move(x-1, y-2))
return true;
else {
position--;
board[x][y] = 0;
return false;
}
}
}
public int getSteps()
{
return count;
}
public void displayBoard() {
for (int i = 0; i < board.length; i++)
{
for (int j = 0; j < board[0].length; j++)
{
//formatting
System.out.printf("%2d ", board[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
ChessComplete one = new ChessComplete(6);
one.move(0, 0);
one.displayBoard();
System.out.print("Recursive method call count:");
System.out.printf("%,2d ", +one.getSteps());
System.out.println();
}
}