c# How to determine possible horsesteps in a(2dim array)?

90 Views Asked by At

//Make a method: void MogelijkePaardenSprongen(int[,] schaakbord, Positie positie) This method determines the possible positions where the Horse can jump (value 2). Of course, you do this in a smart way ( with a LOOP). Please note, there are not always 8 possibilities! The parameter position indicates where the horse is located. Test the methods (call from the Start method like the image below?

enter image description here

// this is the code that i have got for so far,

   static void Main(string[] args)
        {
            Program myProgram = new Program();
            myProgram.Start();
            Console.ReadKey();

        }
        void Start()
        {

            int [,] schaakbord = new int [8, 8];
            InitSchaakbord(schaakbord);
            ToonSchaakBord(schaakbord);


        }
        void InitSchaakbord(int[,] schaakbord)
        {
            int leeg = 0;
            int bezet = 1;
            int mogelijkbezet = 2;
            for (int i=0; i<schaakbord.GetLength(0); i++)
            {
                for (int j=0; j<schaakbord.GetLength(1); j++)
                {
                    schaakbord[i, j] = leeg;
                }  
            }

        }
        void ToonSchaakBord(int[,] schaakbord)
        {Positie positie = new Positie();
            for (int i = 0; i < schaakbord.GetLength(0); i++)
            {Plaatspaard(schaakbord); 
                for (int j = 0; j < schaakbord.GetLength(1); j++)
                {
                    if (schaakbord[i, j] == 0)
                    {
                        Console.Write(".");
                    }
                    else if (schaakbord[i, j] == 1) 
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("*");
                        Console.ResetColor();  
                    }
                    else if (schaakbord[i, j]==2)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("*");
                        Console.ResetColor();     
                    }
                    Console.Write(" ");
                }
                Console.WriteLine(); 
               MogelijkePaardenSprongen(schaakbord, positie);
            }

        }
        Positie Plaatspaard(int [,] schaakbord)
        {Positie x = new Positie();
            Random rnd = new Random();
            x.waarde1 = rnd.Next(1, 8);
            x.waarde2 = rnd.Next(1, 8);

            for (int i = 0; i<schaakbord.GetLength(0);i++ )
            {
                for (int j=0; j<schaakbord.GetLength(1); j++)
                {
                    if (x.waarde1 == i && x.waarde2 == j)
                        schaakbord[i, j] = 1;


                }

            }
            return x;

        }
        class Positie
        {

            public int waarde1;
            public int waarde2;

        }
        void MogelijkePaardenSprongen(int[,] schaakbord, Positie positie)
        {
            for (int i =0; i<schaakbord.GetLength(0); i++)
            {
                for (int j=0; j<schaakbord.GetLength(1); j++)
                {
                    /// possible horsesteps?
                    /// call from void start method
                    if (schaakbord[i, j] == 0)
                    {



                    }
                }
            }
2

There are 2 best solutions below

0
Pribina On

Because its assignment I don't want to give you straight whole solution. Please don't hesitate to comment if you need more help.

My suggestion is to create object

class MoveOffset
{
    public int OffsetX { get; set; }
    public int OffsetY { get; set; }
}

then create collection of them with possible moves

var moves = new List<MoveOffset>()
{
    new MoveOffset(){OffsetX = -1, OffsetY = -2},
    new MoveOffset(){OffsetX = -2, OffsetY = -1},
    new MoveOffset(){OffsetX = 1, OffsetY = -2},
    new MoveOffset(){OffsetX = -2, OffsetY = 1}, 

    new MoveOffset(){OffsetX = -1, OffsetY = 2},
    new MoveOffset(){OffsetX = 2, OffsetY = -1},
    new MoveOffset(){OffsetX = 1, OffsetY = 2},
    new MoveOffset(){OffsetX = 2, OffsetY = 1},
};

then cycle through collection and check conditions if its possible to move there from 'horse' location .

0
Just Shadow On

Please note that horsestep actually means that the sum of the distances
between that point and the horse should exactly be 3.

Example:

var distanceX = abs(Horse.Position.X - i);
var distanceY = abs(Horse.Position.Y - j);
bool isHorseStep = distanceX + distanceY  == 3;

So you can use that assumption in your code to check if it's a horse step or not.

Here is some quickly-written code for your method.
I have not tested it but I guess you'll get the point and do adjustments if needed:

        void MogelijkePaardenSprongen(int[,] schaakbord, Positie positie)
        {
            for (int i =0; i<schaakbord.GetLength(0); i++)
            {
                for (int j=0; j<schaakbord.GetLength(1); j++)
                {
                    /// possible horsesteps?
                    /// call from void start method
                    if (schaakbord[i, j] == 0)
                    {
                        var deltaX = waarde1 - i;
                        var deltaY = waarde2 - j;
                        if (deltaX < -2 || deltaX > 2 
                             || deltaY < -2 || deltaY > 2 )
                        {
                            // TODO: Write logic here for out of bounds.
                        }
                        else
                        {
                             if (abs(deltaX) + abs(deltaY) == 3)
                             {
                                  // TODO: Horse Step. Write your logic here.
                             }
                        }
                    }
                }
            }
        }

P.S. Also for deltaY you might need to reverse the number and use something like this deltaY = j - waarde2, as the Y axis is opposite in the array.