简体   繁体   中英

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

//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?

在此处输入图像描述

// 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)
                    {



                    }
                }
            }

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.

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.
                             }
                        }
                    }
                }
            }
        }

PS 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM