六边形平面的形式和运动表示
表示四边形平面(一堆正方形)的最基本方法是使用二维数组。
在 C# 中,我们将其声明为 int[,]
并且可以使我们的平面达到我们想要的大小:
string[3,3] => tic-tac-toe board (or similar)
string[8,8] => chess or checkers board
要“移动”平面上的某个项目,我们只需将其分配给一个新的“位置”
//using our tic-tac-toe board:
string[0,0] = "x"; //top-left
string[1,1] = "o"; //middle-middle
//to move
string[0,1] = bN; //Black Knight's starting positon
string[2,2] = bN; //Black Knight moves
string[0,1] = String.Empty;
即可,你会如何表示一个六边形平面(一堆六边形)以及如何处理从一个位置到下一个位置的移动?
注意:这并不纯粹是理论上的,因为我脑子里有一个需要这种运动的小游戏的想法,但我无法理解它是如何完成的。我在这里查看了其他一些问题,但找不到合适的匹配...
The most basic way of representing a quadrile plane (a bunch of squares) is to use a two-dimensional array.
In C# we declare this as int[,]
and can make our plane as big as we want:
string[3,3] => tic-tac-toe board (or similar)
string[8,8] => chess or checkers board
To "move" an item on the plane, we would just asign it toa new "position"
//using our tic-tac-toe board:
string[0,0] = "x"; //top-left
string[1,1] = "o"; //middle-middle
//to move
string[0,1] = bN; //Black Knight's starting positon
string[2,2] = bN; //Black Knight moves
string[0,1] = String.Empty;
So, how would you represent a hexagonal plane (a bunch of hexagons) and how would movement from one position to the next be handled?
Note: This is not purely theoretical, as I have an idea for a little game in my head which would require this kind of movement, but I can't wrap my head around how it would be done. I've looked through some of the other questions here, but can't really find a good match...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否是最佳解决方案,但我要做的是创建一个新的板类,该板将是“单元格”的集合,每个单元格将包含一个指向每个相邻单元格的指针(如果该单元格则为空)位于边缘)。您可以在板类上实现一些迭代器来遍历单元格。
您必须将其视为列表而不是向量。但这至少是一个开始。
另一个解决方案是像这样设置你的板
仍然只需使用 [,] 来访问每个单元格,但需要更多的数学知识才能确定您是否正在遍历单元格(右上是 [+1,-1],右上是 [+1,0],下右为[0,+1],左下为[-1,+1],左为[-1,0],左上为[0,-1])
编辑
如果你想要垂直墙而不是倾斜只需使宽度(X)等于X + Y*2,然后在每行上设置当前行号(y)并使单元格0到Yy和Xy到X不受限制。
示例:
你浪费了一点内存空间,但它给了你一个像这样的板

如果您非常聪明©,您可以只使用正常空间,但只需让您的代码将 Yy 或 Xy 范围内的任何内容放在棋盘的另一侧。但我会把代码留给读者。
I do not know if this is the optimal solution but what I would do is create a new class of board the board would be a collection of "cells" each cell would contain a pointer to each of it's neighboring cell (or null if the cell is on a edge). You could implement some iterators on the board class that would walk the cells.
You would have to treat it more like a List instead of a vector. But it is at least a start.
Another solution is set you board up like this
and still just use the [,] to access each cell but it will take a little more math to figure out if you are traversing cells (Up Right is [+1,-1], Right is [+1,0], Down Right is [0,+1], Down Left is [-1,+1], Left is [-1,0], Up Left is [0,-1])
EDIT
If you want vertical walls instead of a slant just take make your width(X) equal X + Y*2 then on each row make the current row number (y) and make the cells 0 to Y-y and X-y to X off limits.
Example:
you waste a little memory space but it gives you a board like this

If you are Very Clever© you can just use the normal space but just have your code have anything in that Y-y or X-y range be on the opposite side of the board. But ill leave that code up to the reader.
三个方向,左/右,上/下,单向/另一向有趣的角度。
Three directions, left/right, up/down, funny angle one way/other way.