函数式编程中映射六边形的实现和列表问题

Implementation of mapping Hexagons in functional programming and problems with Lists

我正在寻求创建一个框架来统一创建 board/strategy 像 CIV 这样的游戏,但是我打算在实现它之前在 vanilla c# 中测试该功能,以便创建看起来像的对称六边形网格以下

 __    __    __    __    __    __    __
/13\__/14\__/15\__/16\__/17\__/18\__/19\
\__/07\__/08\__/09\__/10\__/11\__/12\__/
/00\__/01\__/02\__/03\__/04\__/05\__/06\
\__/  \__/  \__/  \__/  \__/  \__/  \__/

我需要将每隔一行减 1,这样在任何方向上移动都只是将索引增加或减少 7、13 或 6

我目前的问题是我的结构:

struct hexagon{
    public int xpos;
    public int ypos;
}

据我所知 class List 不理解 所以我的代码:

int width = 7;
int height = 7;
int l = width*2-1;
int r = 1;

Func<int,int> xhex = i => ((i%l) < width) ? 2*(i%l) : 2*(i%l)-l;
Func<int,int> yhex = i => ((i%l) < width) ? 2*(i/l) : 2*(i/l)+1;

var grid = new List<hexagon> [width * height - height/2].Select((h,i)=>{
    h.xpos = xhex(i)*r*1.5;
    h.ypos = yhex(i)*r*0.8660; // sqrt(3/2)
    });

正在抛出错误

'List' does not contain a definition for 'xpos' and no accessible extension method 'xpos' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?) [Grid-Parse]

我也不确定函数 Select 是否会接受索引重载有什么我可以使用的优雅的东西吗?

我希望这个解决方案对你有所帮助:

    struct hexagon
       {
         public int xpos;
         public int ypos;
       }


        int width = 7;
        int height = 7;
        int l = width * 2 - 1;
        int r = 1;
        int counterx = 0;
        int countery = 0;

        Func<int, int> xhex = i => ((i % l) < width) ? 2 * (i % l) : 2 * (i % l) - l;
        Func<int, int> yhex = i => ((i % l) < width) ? 2 * (i / l) : 2 * (i / l) + 1;


        var grid = new List<hexagon>[width * height - height / 2].Select(h => new hexagon()
        {
            xpos = (int)(xhex(counterx++) * r * 1.5),
            ypos = (int)(yhex(countery++) * r * 0.8660)
        });

您似乎在尝试初始化一系列六边形。使用 Enumerable.Range 生成一个整数序列,然后 Select 创建六边形:

var grid = Enumerable.Range(0, width * height - height / 2)
    .Select(i => new Hexagon(xhex(i) * r * 1.5, yhex(i) * r * 0.8660));

这是假设 Hexagon 看起来像这样:

public struct Hexagon
{
    public double xpos;
    public double ypos;

    public Hexagon(double xpos, double ypos)
    {
        this.xpos = xpos;
        this.ypos = ypos;
    }
}