コード例 #1
0
ファイル: Day_24.cs プロジェクト: LEPT0N/toybox
            private c_hex_grid trim()
            {
                int min_x = int.MaxValue;
                int max_x = int.MinValue;
                int min_y = int.MaxValue;
                int max_y = int.MinValue;

                for (int x = 0; x < data.Length; x++)
                {
                    for (int y = 0; y < data[0].Length; y++)
                    {
                        if (data[x][y])
                        {
                            min_x = Math.Min(min_x, x);
                            max_x = Math.Max(max_x, x);

                            min_y = Math.Min(min_y, y);
                            max_y = Math.Max(max_y, y);
                        }
                    }
                }

                if (min_x > max_x)
                {
                    min_x = 0;
                    max_x = 1;
                    min_y = 0;
                    max_y = 1;
                }

                // only shift y by an even number to keep neighbors the same.
                if (min_y % 2 != 0)
                {
                    min_y--;
                }

                c_hex_grid new_grid = new c_hex_grid(max_x - min_x + 1, max_y - min_y + 1);

                for (int x = 0; x < data.Length; x++)
                {
                    for (int y = 0; y < data[0].Length; y++)
                    {
                        if (data[x][y])
                        {
                            new_grid.data[x - min_x][y - min_y] = true;
                        }
                    }
                }

                return(new_grid);
            }
コード例 #2
0
ファイル: Day_24.cs プロジェクト: LEPT0N/toybox
            public c_hex_grid step()
            {
                // New grid could possibly grow by 1 in each direction, but also scale the 'y' coordinate by
                // another 1 so we shift the y coordinates by an even number, keeping their neighbors the same.
                c_hex_grid new_grid = new c_hex_grid(data.Length + 2, data[0].Length + 3);

                for (int x = -1; x <= data.Length; x++)
                {
                    for (int y = -1; y <= data[0].Length; y++)
                    {
                        bool new_value = step_position(x, y);

                        new_grid.data[x + 1][y + 2] = new_value;
                    }
                }

                // After a few steps, the largest possible board is usually much larger than what coordinates are actually set.
                return(new_grid.trim());
            }