Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Deitel & Deitel: C - How To Program. *
- * Italian, 2000 Edition. *
- * Exercise 6.24c - "Knight's Tour" *
- * http://it.wikipedia.org/wiki/Percorso_del_cavallo *
- */
- #include <stdio.h>
- /* PROTOTIPI DI FUNZIONE */
- void promptStartPosition();
- int legalMovesThisRound();
- int stayOnBoard(int, int, int);
- int isUnvisited(int, int, int);
- void printBoard();
- int selectBestMove();
- int getAccessibility(int, int, int);
- void setAccessibility(int, int);
- void printAccessibility();
- /* VARIABILI GLOBALI */
- int board[8][8] = {0};
- int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
- int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
- int counter = 1;
- int currentRow, currentColumn;
- int legalMoves[8] = {-1};
- /* Index for legalMoves[] */
- /* Can have value = 8, so watch out for -1 or +1 errors. */
- int legalMovesAvalaible = 0;
- int moveNumber = 0;
- /* Accessibility Heuristic: for each square, the number stands for the amount *
- * of other squares it can be reached from. */
- int accessibility[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} };
- int main(void) {
- promptStartPosition();
- board[currentRow][currentColumn] = 0;
- while (legalMovesThisRound()) {
- /* Randomly select the move to execute among those that are legal. */
- moveNumber = selectBestMove();
- /* Executing move. */
- currentRow += vertical[moveNumber];
- currentColumn += horizontal[moveNumber];
- board[currentRow][currentColumn] = counter++;
- /* Setting new accessibility value for the squares accessible from the *
- * newly reached square. */
- setAccessibility(currentRow, currentColumn);
- }
- printBoard();
- }
- /* Ask the user for where to start the Knight Round. */
- /* Input NOT sanitized. */
- void promptStartPosition() {
- printf("\nInsert the row number from which you wish the knight to start its round: ");
- scanf("%d", ¤tRow);
- printf("\nInsert the column number from which you wish the knight to start its round: ");
- scanf("%d", ¤tColumn);
- }
- /* Fill an array with the IDs of the legal moves for the current position. *
- * The array has been initialized with -1, which is the sentinel value. *
- * This array needs to be re-initialized before making the next move. */
- int legalMovesThisRound() {
- int move;
- legalMoves[0] = -1;
- legalMoves[1] = -1;
- legalMoves[2] = -1;
- legalMoves[3] = -1;
- legalMoves[4] = -1;
- legalMoves[5] = -1;
- legalMoves[6] = -1;
- legalMoves[7] = -1;
- for (legalMovesAvalaible = 0, move = 0; move < 8; move++) {
- if (stayOnBoard(currentRow, currentColumn, move)
- && isUnvisited(currentRow, currentColumn, move))
- legalMoves[legalMovesAvalaible++] = move;
- }
- if (legalMovesAvalaible > 0)
- return 1;
- else
- return 0;
- }
- /* Check if the move is forbidden. */
- int stayOnBoard(int cR, int cC, int m) {
- if (cR + vertical[m] >= 0 && cR + vertical[m] < 8
- && cC + horizontal[m] >= 0 && cC + horizontal[m] < 8)
- return 1;
- else
- return 0;
- }
- /* Check if the knight has already been on the landing square. */
- int isUnvisited(int cR, int cC, int m) {
- if (board[cR + vertical[m]][cC + horizontal[m]] == 0)
- return 1;
- else
- return 0;
- }
- /* Print the current round on standard output. */
- void printBoard() {
- int i, j;
- printf("\nCURRENT POSITION [%d][%d]", currentRow, currentColumn);
- printf("\n=============== BOARD =================");
- for (i = 0; i < 8; i++) {
- printf("\n");
- for (j = 0; j < 8; j++) {
- printf("%4d", board[i][j]);
- }
- }
- printf("\n=============== BOARD =================\n");
- }
- /* Among the legal moves, select the one with the highest accessibility. */
- int selectBestMove() {
- /* None initially set. It will be, because any accessibility will be better *
- * than the initialized value 0. */
- int bestMove = -1;
- int worstAccessibility = 8;
- int i;
- for (i = 0; i < legalMovesAvalaible; i++) {
- if (getAccessibility(currentRow, currentColumn, legalMoves[i]) <= worstAccessibility) {
- bestMove = legalMoves[i];
- worstAccessibility = getAccessibility(currentRow, currentColumn, legalMoves[i]);
- }
- }
- return bestMove;
- }
- /* Retrieves the accessibility of the possible landing square. */
- int getAccessibility(int cR, int cC, int m) {
- return accessibility[cR + vertical[m]][cC + horizontal[m]];
- }
- void setAccessibility(int cR, int cC) {
- int move;
- for (move = 0; move < 8; move++)
- if (stayOnBoard(cR, cC, move))
- --accessibility[cR + vertical[move]][cC + horizontal[move]];
- }
- void printAccessibility() {
- int i, j;
- printf("\n============ ACCESSIBILITY =============");
- for (i = 0; i < 8; i++) {
- printf("\n");
- for (j = 0; j < 8; j++) {
- printf("%4d", accessibility[i][j]);
- }
- }
- printf("\n============ ACCESSIBILITY =============");
- }
Advertisement
Add Comment
Please, Sign In to add comment