Answer to Question #220191 in C++ for cobet

Question #220191

Chess Game Knight’s travails.

You are required to do the following:

1. Explain how you can ensure that any move do not go off the board.

2. Choosing a search algorithm for finding the shortest path for the knight’s travails.

3. Defend your choice of an appropriate search algorithm to find the best possible move from the starting square to the ending square.

4. Creating a script for a board game and a knight.

5. Create a diagrammatical tree structure, to illustrate all possible moves of the knight as children in the tree structure.


1
Expert's answer
2021-07-25T08:25:24-0400

Explain how you can ensure that any move does not go off the board.

For starters, you will need a data structure similar to binary trees. Now, suppose that you have a standard 8x8 chessboard, and you want to show the knight’s movements in a game. As you may know, a knight’s basic move in chess is two forward steps and one sidestep. Facing in any direction and given enough turns, it can move from any square on the board to any other square. 

Choosing a search algorithm for finding the shortest path for the knight’s travails.

Please, look at: programmersought.com/article/22953672909/

Creating a script for a board game and a knight.

#include <stdio.h>
 
int pos[8][8] = { 0 };
 
int travel(int, int);
 
int travel(int x, int y) {
	int i, j, k, l, m;
	int tmpX, tmpY;
	int count, min, tmp;
 
	//The eight directions the knight can walk (clockwise)
	int ktmoveX[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
	int ktmoveY[8] = { -2, -1, 1, 2, 2, 1, -1, -2 };
 
	 / / Test the next coordinates
	int nextX[8] = { 0 };
	int nextY[8] = { 0 };
 
	 / / Record the number of ways out in each direction
	int exists[8] = { 0 };
 
	 / / Start with 1 mark position
	i = x;
	j = y;
	pos[i][j] = 1;
 
	 / / traversing the board
	for (m = 2; m <= 64; m++) {
		 / / Initialize the number of exits in eight directions
		for (l = 0; l < 8; l++) {
			exists[l] = 0;
		}
		 l = 0; //calculate the direction of the walk
 
			    //Exploring eight directions
		for (k = 0; k < 8; k++) {
			tmpX = i + ktmoveX[k];
			tmpY = j + ktmoveY[k];
			 //Border Skip
			if (tmpX < 0 || tmpY < 0 || tmpX>7 || tmpY>7) {
				continue;
			}
			 //can go, record
			if (pos[tmpX][tmpY] == 0) {
				nextX[l] = tmpX;
				nextY[l] = tmpY;
				 l++; //Can move direction plus 1
			}
		}
		count = l;
		 //No way to go back
		if (count == 0) {
			return 0;
			 //One direction can go.
		}
		else if (count == 1) {
			min = 0;
			 / / Find out the number of outlets in the next position
		}
		else {
			for (l = 0; l < count; l++) {
				for (k = 0; k < 8; k++) {
					tmpX = nextX[l] + ktmoveX[k];
					tmpY = nextY[l] + ktmoveY[k];
					if (tmpX < 0 || tmpY < 0 || tmpX>7 || tmpY>7) {
						continue;
					}
					if (pos[tmpX][tmpY] == 0) {
						exists[l]++;
					}
				}
			}
			 / / Find the direction of the next position the least way out
			min = 0;
			tmp = exists[0];
			for (l = 0; l < count; l++) {
				if (exists[l] < tmp) {
					tmp = exists[l];
					min = l;
				}
			}
		}
		 / / Mark the position passed by the serial number
		i = nextX[min];
		j = nextY[min];
		pos[i][j] = m;
	}
	return 1;
}
 
int main()
{
	int i, j, startX, startY;
	while (1)
	{
		 Printf("Enter the starting point:");
		scanf("%d%d", &startX, &startY);
		if (travel(startX, startY)) {
			 Printf("Travel completed!\n");
		}
		else {
			 Printf("Travel failed!\n");
		}
		for (i = 0; i < 8; i++) {
			for (j = 0; j < 8; j++) {
				printf("%2d ", pos[i][j]);
			}
			printf("\n");

		}
		printf("\n");
	}
 
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

christian kaoma
15.03.22, 18:29

fantastic answers

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS