在功能编程中实施映射六角形和列表的问题

发布于 2025-01-22 02:26:50 字数 1186 浏览 0 评论 0原文

我希望创建一个框架来创建诸如CIV Unity之类的董事会/策略游戏,但是我打算在实施之前测试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:

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

我的理解是我的理解未被类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)
    });

正在丢弃错误

'列表'不包含“ xpos”的定义,也不包含可访问的扩展方法'xpos'接受类型“列表”的第一个参数(您是否缺少使用指令或汇编引用?)[网格-Parse]

我也不确定功能是否选择是否会接受索引过载,我可以使用什么同样优雅的东西?

I'm looking to create a framework for creating board/strategy games like CIV in unity however I intend on testing the function in vanilla c# before I implement it so in order to create symmetrical hexagonal grids that look like the following

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

I need to have every other row reduced by 1 so that moving in any direction is simply increasing or decreasing the index by 7, 13 or 6

My current issue is that my struct:

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

is as of my understanding not being understood by the class List
and so my code:

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)
    });

is throwing the error

'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]

I'm also not really sure if the function Select will accept the index overload is there anything I can use that is just as elegant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2025-01-29 02:26:50

看来您正在尝试初始化一系列己糖。使用 enumumerable.range.range.range 生成一个整数和整数序列,以及然后选择创建Hexagons:

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

It looks as though you're trying to initialise a sequence of hexagons. Use Enumerable.Range to generate a sequence of integers, and then Select to create the hexagons:

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

This is assuming that Hexagon looks like this:

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

    public Hexagon(double xpos, double ypos)
    {
        this.xpos = xpos;
        this.ypos = ypos;
    }
}
岛歌少女 2025-01-29 02:26:50

我希望该解决方案能帮助您:

    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)
        });

I hope this solution would help u:

    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)
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文