Question: use java . Knights Tour The knights tour problem is a game in which the player places a single knight on a chessboard. Then, using

use java .

Knights Tour

The knights tour problem is a game in which the player places a single knight on a chessboard. Then, using legal knight moves, begins moving the knight around on the board attempting to land on every square on the board without landing on a previously occupied square. A win is when the player has successfully accomplished this.

Declare a multidimensional array called chessboard that holds characters. Chessboard should be of size 8x8. Initialize all cells to a single space each.

Welcome the user to the game.

Prompt the user to input the coordinates for a knight position on the board. Use a loop to guard against invalid coordinates. Place a K in this position of your array.

Next, process the board by putting values '1' up to possibly '8' in positions within the board where the knight can move to. Leave the K and the spaces in the other positions.

Show the board with the knight (K) placed and showing numbers from 1 up to possibly 8 in the correct positions, representing the possible legal moves for the knight. Also, include a header row with the values 0 through 7 to show the columns and then for each row, display the row number before displaying the contents of that row on each line.

If there are valid moves for the knight:

prompt the user for the coordinates of the move they wish to make. Guard against coordinates off the board.

If their coordinate corresponds to one of the legal moves for the knight, process the array to remove the digits, and then place an asterisk (*) in the current location of the knight, and place a K at the new location. Otherwise, if this is not a legal move, state so and re-prompt them for a legal move.

Continue to show the board with possible moves, receiving coordinates from the user, and moving the knight until either all positions have been occupied, or until there are no legal moves left.

If the "tour" was successful (there are no spaces left on the board), give a congratulatory message. Otherwise, state how many successful moves the player made.

The code to determine if a space is a possible move for a knight is:

if (board[i][j] != '*' && board[i][j] != 'K' &&

((i == row1 - 1 && j == col1 - 2) ||

(i == row1 - 2 && j == col1 - 1) ||

(i == row1 - 1 && j == col1 + 2) ||

(i == row1 - 2 && j == col1 + 1) ||

(i == row1 + 1 && j == col1 - 2) ||

(i == row1 + 2 && j == col1 - 1) ||

(i == row1 + 1 && j == col1 + 2) ||

(i == row1 + 2 && j == col1 + 1)))

A sample run can be found in the files section of Canvas. It is titled Knights Tour Sample Run. pdf

To test your program for a winning tour, use the following successful play graph to play the game:

1

24

55

36

11

22

53

34

56

37

12

23

54

35

10

21

13

2

25

64

41

60

33

52

26

57

38

61

44

63

20

9

3

14

45

42

59

40

51

32

46

27

58

39

62

43

8

19

15

4

29

48

17

6

31

50

28

47

16

5

30

49

18

7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!