I just need to add a move counter to the following code and make the outout as shown in the attached image. I need to display the number of moves made by the knight and wether or not a full tour of 64moves was completed or not.      import java.util.*;   public class Main {     private final static int base_value = 12;     private final static int[][] board_moves = {{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};     private static int[][] board_grid;     private static int total_moves;     public static void main(String[] args) {         board_grid = new int[base_value][base_value];         total_moves = (base_value - 4) * (base_value - 4);         for (int row = 0; row < base_value; row++)             for (int col = 0; col < base_value; col++)                 if (row < 2 || row > base_value - 3 || col < 2 || col > base_value - 3)                     board_grid[row][col] = -1;         int row = 2 + (int) (Math.random() * (base_value - 4));         int col = 2 + (int) (Math.random() * (base_value - 4));         board_grid[row][col] = 1;         if (sol(row, col, 2))             display_Result();         else System.out.println("no result");     }     private static boolean sol(int row, int col, int move_count) {         if (move_count > total_moves)             return true;          List nrst = nearest(row, col);         if (nrst.isEmpty() && move_count != total_moves)             return false;         Collections.sort(nrst, new Comparator() {             public int compare(int[] a_row, int[] b_col) {                 return a_row[2] - b_col[2];             }         });         for (int[] nt : nrst) {             row = nt[0];             col = nt[1];             board_grid[row][col] = move_count;             if (!orphan_Detected(move_count, row, col) && sol(row, col, move_count + 1))                 return true;             board_grid[row][col] = 0;         }         return false;     }     private static List nearest(int row, int col) {         List nrst = new ArrayList<>();         for (int[] move : board_moves) {             int row_x = move[0];             int col_y = move[1];             if (board_grid[row + col_y][col + row_x] == 0) {                 int num = count_nearest(row + col_y, col + row_x);                 nrst.add(new int[]{row + col_y, col + row_x, num});             }         }         return nrst;     }     private static int count_nearest(int row, int col) {         int num = 0;         for (int[] move : board_moves)             if (board_grid[row + move[1]][col + move[0]] == 0)                 num++;         return num;     }     private static boolean orphan_Detected(int count, int row, int col) {         if (count < total_moves - 1) {             List nrst = nearest(row, col);             for (int[] nt : nrst)                 if (count_nearest(nt[0], nt[1]) == 0)                     return true;         }         return false;     }     private static void display_Result() {         for (int[] row : board_grid) {             for (int i : row) {                 if (i == -1)                  continue;                 System.out.printf("%2d ", i);             }             System.out.println();         }     } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I just need to add a move counter to the following code and make the outout as shown in the attached image. I need to display the number of moves made by the knight and wether or not a full tour of 64moves was completed or not. 

 

 

import java.util.*;
 
public class Main {
    private final static int base_value = 12;
    private final static int[][] board_moves = {{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
    private static int[][] board_grid;
    private static int total_moves;
    public static void main(String[] args) {
        board_grid = new int[base_value][base_value];
        total_moves = (base_value - 4) * (base_value - 4);
        for (int row = 0; row < base_value; row++)
            for (int col = 0; col < base_value; col++)
                if (row < 2 || row > base_value - 3 || col < 2 || col > base_value - 3)
                    board_grid[row][col] = -1;
        int row = 2 + (int) (Math.random() * (base_value - 4));
        int col = 2 + (int) (Math.random() * (base_value - 4));
        board_grid[row][col] = 1;
        if (sol(row, col, 2))
            display_Result();
        else System.out.println("no result");
    }
    private static boolean sol(int row, int col, int move_count) {
        if (move_count > total_moves)
            return true;
         List<int[]> nrst = nearest(row, col);
        if (nrst.isEmpty() && move_count != total_moves)
            return false;
        Collections.sort(nrst, new Comparator<int[]>() {
            public int compare(int[] a_row, int[] b_col) {
                return a_row[2] - b_col[2];
            }
        });
        for (int[] nt : nrst) {
            row = nt[0];
            col = nt[1];
            board_grid[row][col] = move_count;
            if (!orphan_Detected(move_count, row, col) && sol(row, col, move_count + 1))
                return true;
            board_grid[row][col] = 0;
        }
        return false;
    }
    private static List<int[]> nearest(int row, int col) {
        List<int[]> nrst = new ArrayList<>();
        for (int[] move : board_moves) {
            int row_x = move[0];
            int col_y = move[1];
            if (board_grid[row + col_y][col + row_x] == 0) {
                int num = count_nearest(row + col_y, col + row_x);
                nrst.add(new int[]{row + col_y, col + row_x, num});
            }
        }
        return nrst;
    }
    private static int count_nearest(int row, int col) {
        int num = 0;
        for (int[] move : board_moves)
            if (board_grid[row + move[1]][col + move[0]] == 0)
                num++;
        return num;
    }
    private static boolean orphan_Detected(int count, int row, int col) {
        if (count < total_moves - 1) {
            List<int[]> nrst = nearest(row, col);
            for (int[] nt : nrst)
                if (count_nearest(nt[0], nt[1]) == 0)
                    return true;
        }
        return false;
    }
    private static void display_Result() {
        for (int[] row : board_grid) {
            for (int i : row) {
                if (i == -1) 
                continue;
                System.out.printf("%2d ", i);
            }
            System.out.println();
        }
    }
}

The tour ended with 39 moves.
This was not a full tour.
1
2
3
4
5
6
7
4
39
24
2
7
14
19
1
25
10
3
6
13
18
1
16
38
12
23
8
15
20
33
11
26
36
21
32
17
4
37
22
34
31
27
35
30
29
28
Transcribed Image Text:The tour ended with 39 moves. This was not a full tour. 1 2 3 4 5 6 7 4 39 24 2 7 14 19 1 25 10 3 6 13 18 1 16 38 12 23 8 15 20 33 11 26 36 21 32 17 4 37 22 34 31 27 35 30 29 28
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY