如何在字典中使用函数作为值? (C#)
How can I use Function as value in dictionary? (C#)
我想创建一个字典并以特定方式填充它。
public struct Position
{
public int X { get; set; }
public int Y { get; set; }
public char Orientation { get; set; }
}
public Dictionary<Position, Position> ChangePosition;
方向可以是 'N'、'S'、'E'、'W' 表示北、南、...
当第一个键是 X,Y,'N' ---> 值应该是 X, Y+1 , 'N'
因此字典可以根据当前位置和移动方向预测项目的下一个位置。
我该如何填写这本词典?
或者还有其他更好的实现方式吗?
如有任何帮助,我们将不胜感激。
更新
我找到了这个解决方案,但我想也许 Action<>
可以使它变得更好:
ChangePosition= new Dictionary<char, Func<Position, Position>>
{
{'N', pos => new Position {X = pos.X, Y = pos.Y + 1, Orientation = pos.Orientation}},
{'E', pos => new Position {X = pos.X+1, Y = pos.Y , Orientation = pos.Orientation}},
{'W', pos => new Position {X = pos.X-1, Y = pos.Y, Orientation = pos.Orientation}},
{'S', pos => new Position {X = pos.X, Y = pos.Y - 1, Orientation = pos.Orientation}},
};
我强烈建议将 Position 设为对象,并将罗盘方向字符 ('N'、'E'、'W'、'S') 枚举。生成的界面更简洁、更直观。
using System;
using System.Collections.Generic;
namespace DictionaryOfFunctions_Whosebug
{
class Program
{
static void Main(string[] args)
{
Position start = new Position()
{
X = 0,
Y = 0,
Orientation = CompassDirection.North
};
start.Move(CompassDirection.North, 5);
start.Print(); // X: 0 Y: 5 Orientation: North
start.Move(CompassDirection.SouthWest, 5);
start.Print(); // X: -5 Y: 0 Orientation: SouthWest
start.Move(CompassDirection.East);
start.Print(); // X: -5 Y: 0 Orientation: East
start.Move(dX: 5, dY: 1, newDirection: CompassDirection.West);
start.Print(); // X: 0 Y: 1 Orientation: West
start.Move(dY: -1, newDirection: CompassDirection.North);
start.Print(); // X: 0 Y: 0 Orientation: North
Console.ReadKey();
}
}
public enum CompassDirection
{
NorthWest,
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West
}
public class Position
{
public CompassDirection Orientation { get; set; }
public int X { get; set; }
public int Y { get; set; }
public void Move(int dX = 0, int dY = 0, CompassDirection? newDirection = null)
{
X += dX;
Y += dY;
Orientation = newDirection.HasValue ? newDirection.Value : Orientation;
}
public void Move(CompassDirection newDirection, int distance = 0)
{
var movement = new Dictionary<CompassDirection, Action>
{
{CompassDirection.NorthWest, () => Move(dY: 1, dX: -1)},
{CompassDirection.North, () => Move(dY: 1)},
{CompassDirection.NorthEast, () => Move(dY: 1, dX: 1)},
{CompassDirection.East, () => Move(dX: 1)},
{CompassDirection.SouthEast, () => Move(dY: -1, dX: 1)},
{CompassDirection.South, () => Move(dY: -1)},
{CompassDirection.SouthWest, () => Move(dY: -1, dX: -1)},
{CompassDirection.West, () => Move(dX: -1)}
};
Orientation = newDirection;
for (int i=0; i< distance; i++)
{
Action changePosition = movement[Orientation];
changePosition();
}
}
}
public static class ExtensionMethods
{
public static void Print(this Position pos)
{
string display = $"X: {pos.X} Y: {pos.Y} Orientation: {pos.Orientation}";
Console.WriteLine(display);
}
}
}
我想创建一个字典并以特定方式填充它。
public struct Position
{
public int X { get; set; }
public int Y { get; set; }
public char Orientation { get; set; }
}
public Dictionary<Position, Position> ChangePosition;
方向可以是 'N'、'S'、'E'、'W' 表示北、南、...
当第一个键是 X,Y,'N' ---> 值应该是 X, Y+1 , 'N'
因此字典可以根据当前位置和移动方向预测项目的下一个位置。
我该如何填写这本词典? 或者还有其他更好的实现方式吗?
如有任何帮助,我们将不胜感激。
更新
我找到了这个解决方案,但我想也许 Action<>
可以使它变得更好:
ChangePosition= new Dictionary<char, Func<Position, Position>>
{
{'N', pos => new Position {X = pos.X, Y = pos.Y + 1, Orientation = pos.Orientation}},
{'E', pos => new Position {X = pos.X+1, Y = pos.Y , Orientation = pos.Orientation}},
{'W', pos => new Position {X = pos.X-1, Y = pos.Y, Orientation = pos.Orientation}},
{'S', pos => new Position {X = pos.X, Y = pos.Y - 1, Orientation = pos.Orientation}},
};
我强烈建议将 Position 设为对象,并将罗盘方向字符 ('N'、'E'、'W'、'S') 枚举。生成的界面更简洁、更直观。
using System;
using System.Collections.Generic;
namespace DictionaryOfFunctions_Whosebug
{
class Program
{
static void Main(string[] args)
{
Position start = new Position()
{
X = 0,
Y = 0,
Orientation = CompassDirection.North
};
start.Move(CompassDirection.North, 5);
start.Print(); // X: 0 Y: 5 Orientation: North
start.Move(CompassDirection.SouthWest, 5);
start.Print(); // X: -5 Y: 0 Orientation: SouthWest
start.Move(CompassDirection.East);
start.Print(); // X: -5 Y: 0 Orientation: East
start.Move(dX: 5, dY: 1, newDirection: CompassDirection.West);
start.Print(); // X: 0 Y: 1 Orientation: West
start.Move(dY: -1, newDirection: CompassDirection.North);
start.Print(); // X: 0 Y: 0 Orientation: North
Console.ReadKey();
}
}
public enum CompassDirection
{
NorthWest,
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West
}
public class Position
{
public CompassDirection Orientation { get; set; }
public int X { get; set; }
public int Y { get; set; }
public void Move(int dX = 0, int dY = 0, CompassDirection? newDirection = null)
{
X += dX;
Y += dY;
Orientation = newDirection.HasValue ? newDirection.Value : Orientation;
}
public void Move(CompassDirection newDirection, int distance = 0)
{
var movement = new Dictionary<CompassDirection, Action>
{
{CompassDirection.NorthWest, () => Move(dY: 1, dX: -1)},
{CompassDirection.North, () => Move(dY: 1)},
{CompassDirection.NorthEast, () => Move(dY: 1, dX: 1)},
{CompassDirection.East, () => Move(dX: 1)},
{CompassDirection.SouthEast, () => Move(dY: -1, dX: 1)},
{CompassDirection.South, () => Move(dY: -1)},
{CompassDirection.SouthWest, () => Move(dY: -1, dX: -1)},
{CompassDirection.West, () => Move(dX: -1)}
};
Orientation = newDirection;
for (int i=0; i< distance; i++)
{
Action changePosition = movement[Orientation];
changePosition();
}
}
}
public static class ExtensionMethods
{
public static void Print(this Position pos)
{
string display = $"X: {pos.X} Y: {pos.Y} Orientation: {pos.Orientation}";
Console.WriteLine(display);
}
}
}