Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am doing an assignment for (Knight\'s Tour) and I do not know if my program is

ID: 3826261 • Letter: I

Question

I am doing an assignment for (Knight's Tour) and I do not know if my program is doing what the question is asking for. Can someone look at my code and see if I am following the guidelines needed for this question and also what can I do to improve my code. The question from the book is too long to copy-and-paste, but it is 6.24 from the 8th Edition of "C How to Program" textbook.

#include <stdio.h>

#include <time.h>

// function prototypes for the program
int randomiseMovement(void);
int printBoard(void);
void clearChessBoard(void);
void copyRecordArray(int c[][8]);
void printRecordArray(int b[][8]);
int validOrNot(int mn);
void moveKnight(int moveno);
void initialize(void);
int isThereEmptyLocationAround();
int determineLowestAccessNumberedMove(int p, int s);

// These two single subscript arrays will be used to move the knight as L shape according to array subscript
int horizontal[8];
int vertical[8];
int iteration = 0;

// declaring the currrent position of our Knight
int currentRow;
int currentColumn;

// Knight's movement counter
int count = 0;

// chessboard layout
int chessBoard[8][8] = { 0 };

// the array which holds the biggest record
int recordHolder[8][8] = {0};
int flag = 0;
int moveNumber = 0;
int m = 1;

int access[8][8] = {2, 3, 4, 4, 4, 4, 3, 2,
3, 4, 6, 6, 6, 6, 4, 3,
   4, 6, 8, 8, 8, 8, 6, 4,
   4, 6, 8, 8, 8, 8, 6, 4,
   4, 6, 8, 8, 8, 8, 6, 4,
   4, 6, 8, 8, 8, 8, 6, 4,
   3, 4, 6, 6, 6, 6, 4, 3,
   2, 3, 4, 4, 4, 4, 3, 2 };

// declare lookahead storage variable
int smlAccessNoEquals;

// declaration of int main for program to function
int main(void)
{
   int biggestCoverage = 0;

   // initialize horizontal move of Knight
   horizontal[0] = 2;
   horizontal[1] = 1;
   horizontal[2] = -1;
   horizontal[3] = -2;
   horizontal[4] = -2;
   horizontal[5] = -1;
   horizontal[6] = 1;
   horizontal[7] = 2;

   // intialize vertical move of the Knight
   vertical[0] = -1;
   vertical[1] = -2;
   vertical[2] = -2;
   vertical[3] = -1;
   vertical[4] = 1;
   vertical[5] = 2;
   vertical[6] = 2;
   vertical[7] = 1;

   // used to show real time moves of Knight
   srand(time(NULL));

   printf("____________________________________________________________________________ ");
   while (biggestCoverage<64)
   {
       // calling for function initialize
       initialize();

       moveNumber = randomiseMovement();
       while (!validOrNot(moveNumber))
       {
           moveNumber = randomiseMovement();
       } // end of while loop
       printf("Move randomised to: %d ", moveNumber);

       printf("____________________________________________________________________________ ");
       printf("Starting position is: [%d][%d] ", currentRow, currentColumn);

       do
       {

           // First check if moveNumber position is available
           // Then move the Knight accordingly
           moveNumber = randomiseMovement();

           // Have to check if there is an empty location left on the chessboard
           while (!validOrNot(moveNumber) && isThereEmptyLocationAround())
           {   
               moveNumber = randomiseMovement();
           } // end of while loop
           moveNumber = determineLowestAccessNumberedMove(currentRow, currentColumn);

           if (validOrNot(moveNumber))
               moveKnight(moveNumber);
           else if (isThereEmptyLocationAround())
           {
               moveNumber = randomiseMovement();
           } // end of else if
           else
           {
               flag = -1;
           } // end of else
       } // end of do loop
      
       while (flag != -1);

       m = printBoard();

       if (m>biggestCoverage)
       {
           biggestCoverage = m;
           copyRecordArray(chessBoard);
       } // end of if statement

       printf(" Record Holder: %d ", biggestCoverage);
       printRecordArray(recordHolder);
   } // end of while loop

   printf(" ");
   getchar();

   return 0;
} // end of int main

// Function to randomise the Knight's movement
int randomiseMovement(void)
{
   return rand() % 8;
} // end of function randomiseMovement

// Function to copy the record for highest array
void copyRecordArray(int b[][8])
{
   int i, j;

   for (i = 0; i < 8; i++)
   {
       for (j = 0; j < 8; j++)
       {
           recordHolder[i][j] = b[i][j];
       } // end of for loop
   } // end of for loop
} // end of function copyRecordArray

// Function to see if there is a valid move
int validOrNot(int mn)
{
   return (chessBoard[currentRow + vertical[mn]][currentColumn + horizontal[mn]] == 0 && (currentRow + vertical[mn]) >= 0 && (currentRow + vertical[mn])<8 && (currentColumn + horizontal[mn]) >= 0 &&   (currentColumn + horizontal[mn]) <8);
} // end of function validOrNot

// Function to move the Knight around
void moveKnight(int moveno)
{
   printf("Subscript is %d ", moveno);
   currentRow += vertical[moveno];
   currentColumn += horizontal[moveno];
   printf("Knight has moved to chessBoard[%d][%d]. ", currentRow, currentColumn);
   count++;
   printf("Move count is %d. ", count);
   chessBoard[currentRow][currentColumn] = count;
} // end of function moveKnight

// Function to check if the location is empty
int isThereEmptyLocationAround()
{
   int i;
   for (i = 0; i < 8; i++)
   {
       if (validOrNot(i))
           return 1;
   } // end of for loop
   return 0;
} // end of function isThereEmptyLocationAround

// Function to print the chessboard
int printBoard(void)
{
   int i, j,>    printf(" ");
   for (i = 0; i < 8; i++)
   {
       printf(" ");
       for (j = 0; j < 8; j++)
       {
           printf("%2d ", chessBoard[i][j]);
           if (chessBoard[i][j]>0)
               onesCount++;
       } // end of for loop
   } // end of for loop
   return onesCount;
} // end of function printBoard

// Function to print the array
void printRecordArray(int b[][8])
{
   int i, j;
   printf(" Biggest recorded array is: ");
   printf(" ");
   for (i = 0; i < 8; i++)
   {
       printf(" ");
       for (j = 0; j < 8; j++)
       {
           printf("%2d ", b[i][j]);
       } // end of for loop
   } // end of for loop

   printf(" ");
} // end of function printRecordedArray

// Function to clear the board before each iteration
void clearChessBoard(void)
{
   int i, j;

   for (i = 0; i < 8; i++)
   {
       for (j = 0; j < 8; j++)
       {
           chessBoard[i][j] = 0;
       } // end of for loop
   } // end of for loop
} // end of function clearChessBoard

// Function to reset the board
void initialize(void)
{
   iteration++;
   printf("%d iteration", iteration);
   flag = 0;
   clearChessBoard();
   currentColumn = randomiseMovement();
   currentRow = randomiseMovement();
   printf("Current Row and Current Column is: [%d][%d] ", currentRow, currentColumn);
   chessBoard[currentRow][currentColumn] = 1;
   count = 1, m = 0;
} // end of function initialize

// Function to check the lowest move
int determineLowestAccessNumberedMove(int cRow, int cCol)
{
   int i;
   int smlAccessNo = moveNumber;
   smlAccessNoEquals = -1;

   // for loop to determine smallest subscript
   for (i = 0; i < 8; i++)
   {   
       if (validOrNot(i) && ((access[cRow + vertical[i]][cCol + horizontal[i]])<(access[cRow + vertical[smlAccessNo]][cCol + horizontal[smlAccessNo]])))
           smlAccessNo = i;
   } // end of for loop

   // for loop to check if there is another move equal to smallest access value and check if it is valid and then store it in smlAccessNoEquals variable
   for (i = 0; i < 8; i++)
   {
       if (access[cRow + vertical[i]][cCol + horizontal[i]] == access[cRow + vertical[smlAccessNo]][cCol + horizontal[smlAccessNo]] && (i != smlAccessNo) && (cRow + vertical[i]) >= 0
           && (cRow + vertical[i])<8 && (cCol + horizontal[i]) >= 0 && (cCol + horizontal[i]) <8 && chessBoard[cRow + vertical[i]][cCol + horizontal[i]] == 0)
       {
           smlAccessNoEquals = i;
       } // end of if statement
   } // end of for loop

   if (smlAccessNoEquals != -1 && lookAhead(smlAccessNoEquals)<lookAhead(smlAccessNo))
       return smlAccessNoEquals;
   else
       return smlAccessNo;
} // end of function determineLowestAccessNumberedMove

// Function to look ahead one move and choose accordingly for the Knight
int lookAhead(int number)
{
   int i;
   int temporaryLookAheadRow;
   int temporaryLookAheadColumn;
   int temp = number;
   int lookAheadSmlAccessNo = -1;

   temporaryLookAheadRow = currentRow + vertical[number];
   temporaryLookAheadColumn = currentColumn + horizontal[number];

   for (i = 0; i < 8; i++)
   {
       if (chessBoard[temporaryLookAheadRow + vertical[i]][temporaryLookAheadColumn + horizontal[i]] == 0 && access[temporaryLookAheadRow + vertical[i]][temporaryLookAheadColumn + horizontal[i]] < access[temporaryLookAheadRow + vertical[number]][temporaryLookAheadColumn + horizontal[number]]
           && (temporaryLookAheadRow + vertical[i]) >= 0 //check if valid
           && (temporaryLookAheadRow + vertical[i])<8 && (temporaryLookAheadColumn + horizontal[i]) >= 0 && (temporaryLookAheadColumn + horizontal[i]) <8)
       {
           number = i;
       } // end of if statement
   } // end of for loop

   if (number != temp)
       return access[temporaryLookAheadRow + vertical[number]][temporaryLookAheadColumn + horizontal[number]];

   else return access[temporaryLookAheadRow + vertical[temp]][temporaryLookAheadColumn + horizontal[temp]];

} // end of function lookAhead

Explanation / Answer

Yes, you're following the guidelines as per the question was given.
For shortening your code, you can initialize multiple variables in a single statement. For example,

int horizontal[8];
int vertical[8];
int iteration = 0;

These can be clubbed together like

int horizontal[8], vertical[8], iteration = 0;

This is because all have the same type 'int'. Remember that this does not follow for another programming language like java.

The same can be done for other variables as well,
int flag = 0;
int moveNumber = 0;
int m = 1;

as

int flag = 0, moveNumber = 0, m = 1;

As you're learning the language, so the more you'll write, the more you'll learn the language and the delicacies of the language! Good Luck!