you to rewrite the program with a new algorithm with c An ac

you to rewrite the program with a new algorithm with c#. An accessibility number is assigned to each cell to indicate how accessible the cell is. In every step, compare the possible moves and move the knight to the cell with the lowest accessibility. Since this makes later moves easier, the new algorithm will end up having more moves than before.

Please read page 341 of the textbook for a detailed description of this program. The textbook tells you to pick one of the four corners as the initial position of the knight. Do not follow this direction. Instead, pick a random position anywhere in the chessboard as the initial position just like what you do in Exercise 8.22 (b).

The following is the output of a test run of the program:

The tour ended with 57 moves.

This was not a full tour.

10 7 12 27 38 5 22 25

13 28 9 6 23 26 37 4

8 11 0 39 44 51 24 21

29 14 43 50 0 36 3 52

42 49 40 45 0 0 20 35

15 30 0 0 57 0 53 2

48 41 32 17 46 55 34 19

31 16 47 56 33 18 1 54

The tour ends with 57 moves. Most test runs with this new algorithm will have 55 moves or more.

Solution

Please find below the needed C# program :

using System;
using System.Collections.Generic;

namespace program
{
   class Main
   {  
       const int N = 8;

       readonly static int[,] moves = { {+1,-2},{+2,-1},{+2,+1},{+1,+2},
           {-1,+2},{-2,+1},{-2,-1},{-1,-2} };
       struct showMoves
       {
           public int x, y;          
           public showMoves( int _x, int _y ) { x = _x; y = _y; }
       }      

       public static void Main (string[] args)
       {
           int[,] board = new int[N,N];
           board.Initialize();

           int x = 0,                       // This will be the starting position
           y = 0;

           List<showMoves> list = new List<showMoves>(N*N);
           list.Add( new showMoves(x,y) );

           do
           {                              
               if ( Possible_Move( board, x, y ) )
               {                                      
                   int move = board[x,y];                  
                   board[x,y]++;
                   x += moves[move,0];
                   y += moves[move,1];          
                   list.Add( new showMoves(x,y) );                          
               }
               else
               {                  
                   if ( board[x,y] >= 8 )
                   {                      
                       board[x,y] = 0;                                                              
                       list.RemoveAt(list.Count-1);                      
                       if ( list.Count == 0 )
                       {
                           Console.WriteLine( \"No output found for this.\" );
                          
                           return;
                       }      
                       x = list[list.Count-1].x;
                       y = list[list.Count-1].y;                      
                   }
                   board[x,y]++;
               }              
           }
           while( list.Count < N*N );

           int last_x = list[0].x,
           last_y = list[0].y;
           string letters = \"ABCDEFGH\";
           for( int i=1; i<list.Count; i++ )
           {              
               Console.WriteLine( string.Format(\"{0,2}: \", i) + letters[last_x] + (last_y+1) + \" - \" + letters[list[i].x] + (list[i].y+1) );

               last_x = list[i].x;
               last_y = list[i].y;
           }
       }

       static bool Possible_Move( int[,] board, int cur_x, int cur_y )
       {          
           if ( board[cur_x,cur_y] >= 8 )
               return false;

           int new_x = cur_x + moves[board[cur_x,cur_y],0];
           int new_y = cur_y + moves[board[cur_x,cur_y],1];

           if ( new_x >= 0 && new_x < N && new_y >= 0 && new_y < N && board[new_x,new_y] == 0 )
               return true;

           return false;
       }
   }
}

you to rewrite the program with a new algorithm with c#. An accessibility number is assigned to each cell to indicate how accessible the cell is. In every step,
you to rewrite the program with a new algorithm with c#. An accessibility number is assigned to each cell to indicate how accessible the cell is. In every step,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site