在功能编程中实施映射六角形和列表的问题
我希望创建一个框架来创建诸如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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在尝试初始化一系列己糖。使用 enumumerable.range.range.range 生成一个整数和整数序列,以及然后
选择
创建Hexagons:这是假设
Hexagon
看起来像这样: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:This is assuming that
Hexagon
looks like this:我希望该解决方案能帮助您:
I hope this solution would help u: