모든 플레이어와 해당 시작점 좌표의 매핑을 사전에 저장한다고 가정합니다.
: 그럼 당신은 바로 좌표에 문자열을 구문 분석 할 수
class Coordinate{
public int X { get; set; }
public int Y { get; set; }
public Coordinate(int x, int y){
X = x;
Y = y;
}
public static FromString(string coord){
try
{
// Parse comma delimited integers
int[] coords = coord.Split(',').Select(x => int.Parse(x.Trim())).ToArray();
return new Coordinate(coords[0], coords[1]);
}
catch
{
// Some defined default value, if the format was incorrect
return new Coordinate(0, 0);
}
}
}
:
Dictionary<int, string> startingpoints = xml.Elements(ns + "startingPoint")
.Select(sp => new {
Player = (int)(sp.Attribute("player")),
Coordinates = (string)(sp.Attribute("coordinates"))
})
.ToDictionary(sp => sp.Player, sp => sp.Coordinates);
더 나은, 당신은 같은 좌표를 저장하는 클래스를했다 : 같은이 코드는 보일 것이다
Dictionary<int, string> startingpoints = xml.Elements(ns + "startingPoint")
.Select(sp => new {
Player = (int)(sp.Attribute("player")),
Coordinates = Coordinate.FromString((string)(sp.Attribute("coordinates")))
})
.ToDictionary(sp => sp.Player, sp => sp.Coordinates);
그런 다음 플레이어에 액세스 할 수는 다음과 같이 좌표
Console.WriteLine(string.Format("Player 1: X = {0}, Y = {1}",
startingpoints[1].X,
startingpoints[1].Y));
같은 뭔가를 찾고 생각? 열쇠로서의 플레이어와 값으로서의 좌표? –
네, 그렇습니다. 이것은 getter를 통해 전달 될 것이고 호출하는 함수는 원하는대로 분석 할 수 있습니다. – NateD