Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Knights Tour: I'm having some issues with my code and I am not sure why. I am having a lot of undeclared and I

The Knights Tour: I'm having some issues with my code and I am not sure why. I am having a lot of undeclared and I cannot fix them.

#include /* printf() */ #include /* EXIT_SUCCESS */

#define BOLD "\x1b[1m" #define BOW "\x1b[1m\x1b[7m" #define GRAY "\x1b[1m\x1b[30m" #define RED "\x1b[1m\x1b[31m " #define GREEN "\x1b[1m\x1b[32m " #define RESET "\x1b[0m"

/* used for printing the chessboard */ #define WHITE_SQUARE (((i % 2) && (j % 2)) || (!(i % 2) && !(j % 2)))

#define N (8) /* NxN chessboard */ #define POSSIBLE_MOVES (8) #define SLEEP_DURATION (500000) #define CLEAR "clear"

static const int possible_moves[POSSIBLE_MOVES][2] = { {1, 2}, {1, -2}, {2, 1}, {2, -1}, {-1, 2}, {-1, -2}, {-2, 1}, {-2, -1} };

static int status = 0;

/******************************************************************************/

static void FindTour(int x_pos, int y_pos); static size_t Find2ndLevelMinDegree(int board[][N], int x_pos, int y_pos, int idx); static int BreakTie(int board[][N], int x_pos, int y_pos, int idx1, int idx2); static void FindNextMove(int board[][N], int x_pos, int y_pos, size_t moves_count); static int IsKnightsTour(int board[][N]); static int IsInBoundaries(int x_pos, int y_pos); static int IsMoveValid(int board[][N], int x_pos, int y_pos); static size_t GetDegree(int board[][N], int x_pos, int y_pos); static void PrintBoard(int board[][N]); static void PrintStatus(void);

/******************************************************************************/

int main(int agrc, const char* agrv[]) { int i = 0; int j = 0; size_t counter = 1;

for (i = 0; i < N; ++i) { for (j = 0; j < N; ++j) { printf("tour no. %lu: ", counter); ++counter; FindTour(i, j); } }

PrintStatus();

return (EXIT_SUCCESS); }

/******************************************************************************/

static void FindTour(int x_pos, int y_pos) { int board[N][N] = { {0} };

FindNextMove(board, x_pos, y_pos, 1);

if (IsKnightsTour(board)) { #ifndef ANIMATE PrintBoard(board); #endif } else { puts("tour"RED"failed"RESET" "); ++status; } }

static void FindNextMove(int board[][N], int x_pos, int y_pos, size_t moves_count) { int i = 0; int next_x_pos = 0; int next_y_pos = 0; size_t degree = 0; size_t min_degree = N + 1; size_t second_min_degree = 0; int min_deg_index = 0; int second_min_deg_index = 0;

board[x_pos][y_pos] = moves_count;

#ifdef ANIMATE PrintBoard(board); usleep(SLEEP_DURATION); system(CLEAR); #endif

if (moves_count == (N * N)) { return; }

for (i = 0; i < POSSIBLE_MOVES; ++i) { next_x_pos = x_pos + possible_moves[i][0]; next_y_pos = y_pos + possible_moves[i][1];

if (IsMoveValid(board, next_x_pos, next_y_pos)) { degree = GetDegree(board, next_x_pos, next_y_pos); if (degree <= min_degree) { second_min_degree = min_degree; second_min_deg_index = min_deg_index; min_degree = degree; min_deg_index = i; } } }

if (min_degree == second_min_degree) { min_deg_index = BreakTie(board, x_pos, y_pos, min_deg_index, second_min_deg_index); }

next_x_pos = x_pos + possible_moves[min_deg_index][0]; next_y_pos = y_pos + possible_moves[min_deg_index][1];

++moves_count;

FindNextMove(board, next_x_pos, next_y_pos, moves_count); }

static int BreakTie(int board[][N], int x_pos, int y_pos, int idx1, int idx2) { size_t degree1 = Find2ndLevelMinDegree(board, x_pos, y_pos, idx1); size_t degree2 = Find2ndLevelMinDegree(board, x_pos, y_pos, idx2);

return ((degree1 <= degree2) ? idx1 : idx2); }

static size_t Find2ndLevelMinDegree(int board[][N], int x_pos, int y_pos, int idx) { size_t degree = 0; size_t min_degree = N + 1; int i = 0; int temp_x_pos = 0; int temp_y_pos = 0;

for (i = 0; i < POSSIBLE_MOVES; ++i) { temp_x_pos = x_pos + possible_moves[idx][0] + possible_moves[i][0]; temp_y_pos = y_pos + possible_moves[idx][1] + possible_moves[i][1];

if (IsMoveValid(board, temp_x_pos, temp_y_pos)) { degree = GetDegree(board, temp_x_pos, temp_y_pos);

if (degree <= min_degree) { min_degree = degree; } } }

return (min_degree); }

static int IsKnightsTour(int board[][N]) { int i = 0; int j = 0; int ret = 1;

for (i = 0; i < N; ++i) { for (j = 0; j < N; ++j) { if (board[i][j] == 0) { ret = 0; break; } } }

return (ret); }

static int IsInBoundaries(int x_pos, int y_pos) { return (((0 <= x_pos) && (x_pos < N)) && (0 <= y_pos) && (y_pos < N)); }

static int IsMoveValid(int move[][N], int x_pos, int y_pos) { return ((IsInBoundaries(x_pos, y_pos)) && (move[x_pos][y_pos] == 0)); }

static size_t GetDegree(int board[][N], int x_pos, int y_pos) { size_t counter = 0; int i = 0;

for (i = 0; i < POSSIBLE_MOVES; ++i) { if (IsMoveValid(board, x_pos + possible_moves[i][0], y_pos + possible_moves[i][1])) { ++counter; } }

return (counter); }

static void PrintBoard(int board[][N]) { int i = 0; int j = 0; int move = 0;

for (i = 0; i < N; ++i) { printf(GRAY"%d"RESET, N - i);

for (j = 0; j < N; ++j) { move = board[i][j];

if (WHITE_SQUARE) { if (move) { printf(BOW"%d"RESET, move); } else { printf(BOW" "RESET); } } else { if (move) { printf("%d", move); } else { printf(" "); } }

if (move < 10) { if (WHITE_SQUARE) { printf(BOW" "RESET); } else { printf(" "); } } } printf(" "); }

puts(GRAY" A B C D E F G H "RESET); }

static void PrintStatus(void) { if (!status) { puts(BOLD" all tours"GREEN"succeeded"RESET); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions