C# 选择半径周围的六边形
我有一张六边形的地图。单击部队所在的六边形时,应根据部队的范围变量高亮显示单击六边形周围的其他六边形 我自己尝试过很多方法,但都失败了。我似乎无法得到一个突出显示正确的列表。在这个例子中,我将使用一个由2个瓷砖组成的团队 我尝试实现的方法如下: 有3个列表C# 选择半径周围的六边形,c#,list,hexagonal-tiles,C#,List,Hexagonal Tiles,我有一张六边形的地图。单击部队所在的六边形时,应根据部队的范围变量高亮显示单击六边形周围的其他六边形 我自己尝试过很多方法,但都失败了。我似乎无法得到一个突出显示正确的列表。在这个例子中,我将使用一个由2个瓷砖组成的团队 我尝试实现的方法如下: 有3个列表 “打开”列表包含要按1展开的节点 关闭列表包含将高亮显示的节点 删除列表包含已展开的节点 我遵循以下算法: 将开始节点添加到所有列表中 将“打开”列表中的节点展开1个平铺 使用移除列表移除打开列表中的节点 将打开的列表节点添加到关闭的列表
- “打开”列表包含要按1展开的节点
- 关闭列表包含将高亮显示的节点
- 删除列表包含已展开的节点
私有列表展开(列表打开,布尔奇数)
{
List newopen=newlist();
int-oddd偶数=1;
如果(奇数==假)
{
偶数=-1;
}
foreach(字符串s处于打开状态)
{
字符串[]rc=s.Split('.');//当前十六进制的行+列
int Row=int.Parse(rc[0]);
int Col=int.Parse(rc[1]);
对于(int r=1;r>=-1;r--)
{
对于(int c=-1;c-1&&(Col+c)>-1)&&((Row+r)<9&&(Col+c)<18))//地图检查
{
if(Hexmap[Row+r,Col+c]='-')//地图检查
{
Add((Row+r.ToString()+”+(Col+c.ToString());
}
}
}
}
}
}
返回newopen;
}
私有无效删除(参考列表rem,参考列表打开)
{
foreach(rem中的字符串s)
{
打开。移除;
}
}
专用作废添加(参考列表关闭,参考列表打开)
{
foreach(字符串s处于打开状态)
{
已结束。添加(s);
}
}
私人空位部队选择(内部行、内部列、内部范围)
{
布尔奇数=真;
如果((行%2)==0)
{
奇数=假;
}
列表打开=新列表();
列表关闭=新列表();
List remove=新列表();
open.Add(row.ToString()+“+”列+col.ToString());
close.Add(row.ToString()+“+”列+col.ToString());
remove.Add(row.ToString()+“+col.ToString());
对于(int i=0;i
下面还有两张图片:(忽略背景中的岩石!)
再次感谢您的帮助。使用正确的数据类型而不是字符串似乎会更容易。您使用的是内半径(到边)还是外半径(到顶点)?每个六边形都有一个行和列值,这是我一直在使用的半径。使用适当的数据类型而不是字符串看起来会容易得多。您使用的是内半径(到边)还是外半径(到顶点)?每个六边形都有一个行和列值,这是我一直在使用的半径
private List<string> expand(List<string> open, bool odd)
{
List<string> newopen = new List<string>();
int oddEven = 1;
if (odd == false)
{
oddEven = -1;
}
foreach (string s in open)
{
string[] rc = s.Split('.'); //row + col of the current hex
int Row = int.Parse(rc[0]);
int Col = int.Parse(rc[1]);
for (int r = 1; r >= -1; r--)
{
for (int c = -1; c <= 1; c++)
{
if (!((r == oddEven && c != 0) || (r== 0 && c == 0)))
{
if (((Row + r) > -1 && (Col + c) > -1) && ((Row + r) < 9 && (Col + c) < 18)) //mapcheck
{
if (Hexmap[Row + r, Col + c] == '-') //mapcheck
{
newopen.Add((Row + r).ToString() + "." + (Col + c).ToString());
}
}
}
}
}
}
return newopen;
}
private void Remove(ref List<string> rem, ref List<string> open)
{
foreach (string s in rem)
{
open.Remove(s);
}
}
private void Add(ref List<string> closed, ref List<string> open)
{
foreach (string s in open)
{
closed.Add(s);
}
}
private void TroopSelect(int row, int col, int range)
{
bool odd = true;
if ((row % 2) == 0)
{
odd = false;
}
List<string> open = new List<string>();
List<string> close = new List<string>();
List<string> remove = new List<string>();
open.Add(row.ToString() + "." + col.ToString());
close.Add(row.ToString() + "." + col.ToString());
remove.Add(row.ToString() + "." + col.ToString());
for (int i = 0; i < range; i++)
{
open = expand(open, odd); //row and col of the current point being checked --- REMOVE ROW COL, find it out from the list within the function
Remove(ref remove, ref open); //remove from open the remove hex values
Add(ref close, ref open); //add to closed what's in open
remove.Clear(); //clear remove
remove = open; //set remove the same as open
}
foreach (string s in close)
{
string[] rc = s.Split('.'); //row + col of the current hex
int Row = int.Parse(rc[0]);
int Col = int.Parse(rc[1]);
Hexagons.Add(new PointF(Row, Col));
}
}
private void CheckTroop(int row, int col)
{
if (Hexmap[row, col] == 'o') //it's a friendly troop
{
foreach (Troops t in playertroops)
{
if (t.row == row && t.column == col)
{
TroopSelect(row, col, t.range);
this.battlegrid.Invalidate();
}
}
}
}
private void battlegrid_MouseClick(object sender, MouseEventArgs e)
{
int row, col;
PointToHex(e.X, e.Y, HexHeight, out row, out col); //gets the hex in the x/y position
CheckTroop(row, col);
//this.Invalidate();
}