L
L
Ledington2021-08-26 09:14:12
Arrays
Ledington, 2021-08-26 09:14:12

How to find the number of elements in an array?

Can you please tell me how to count the number of steps/elements in an array for a certain figure and output it to the console?

void Start()
        {
            int count = Enum.GetNames(typeof(Figure)).Length; //запрос из списка Enum
            moves = new List<Coords>[count][,]; //фигуры с массивом списков координат для фигур

            moves[(int)Figure.Rook] = new List<Coords>[8, 8]; //список координат для Ладьи            
            for (int x = 0; x <= 7; x++)
                for (int y = 0; y <= 7; y++)
                    moves[(int)Figure.Rook][x, y] = RookMoves(x, y);

            moves[(int)Figure.King] = new List<Coords>[8, 8]; //список координат для Короля
            for (int x = 0; x <= 7; x++)
                for (int y = 0; y <= 7; y++)
                    moves[(int)Figure.King][x, y] = KingMoves(x, y);

            moves[(int)Figure.Queen] = new List<Coords>[8, 8]; //список координат для Ферзя
            for (int x = 0; x <= 7; x++)
                for (int y = 0; y <= 7; y++)
                    moves[(int)Figure.Queen][x, y] = QueenMoves(x, y);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2021-08-26
@Ledington

Console.WriteLine(getCount(moves,Figure.Queen));
//....
        public int getCount(List<Coords>[][,] arr, Figure figure)
        {
            int count = 0;
            foreach (var c in arr[(int) figure])
                count += c.Count;
            return count;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question