L
L
Ledington2021-08-19 13:05:38
C++ / C#
Ledington, 2021-08-19 13:05:38

How to adjust the array so that it walks in steps?

Hello!
Tell me how to make the figure at each point of the array [8x8] move one step in all directions.
I can't figure out how to do it. At the moment, the code works adequately only at the point [3,3], in the rest, in addition to the necessary coordinates, it gives out a bunch of unnecessary ones.
And how to display the result of the number of possible moves at each point, so that later we can compare and write where there are the fewest moves?

Program code (part)
List<Coords> KingMoves(int x, int y) 
        {
            List<Coords> list = new List<Coords>(8); 
            
            //ходим по диагонали во все стороны (код работает только для координат [3,3])
            for (int sx = x - 1; sx >= 2; sx--) 
            for (int sy = y - 1; sy >= 2; sy--) 
                list.Add(new Coords(sx, sy)); 
            for (int sx = x + 1; sx <= 4; sx++) 
            for (int sy = y + 1; sy <= 4; sy++) 
                list.Add(new Coords(sx, sy)); 
            for (int sx = x - 1; sx >= 2; sx--) 
            for (int sy = y + 1; sy <= 4; sy++) 
                list.Add(new Coords(sx, sy)); 
            for (int sx = x + 1; sx <= 4; sx++) 
            for (int sy = y - 1; sy >= 2; sy--) 
                list.Add(new Coords(sx, sy)); 

            //ходим по горизонтали и вертикали во все стороны (код работает только для координат [3,3])
            for (int sx = x - 1; sx >= 2; sx--) 
                list.Add(new Coords(sx, y)); 
            for (int sx = x + 1; sx <= 4; sx++) 
                list.Add(new Coords(sx, y)); 
            for (int sy = y - 1; sy >= 2; sy--) 
                list.Add(new Coords(x, sy)); 
            for (int sy = y + 1; sy <= 4; sy++) 
                list.Add(new Coords(x, sy)); 
                        
            return list; 
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question