Time for action – GamePiece class methods – part 3 – connection methods
Add the
GetOtherEnds()
method to the GamePiece class:public string[] GetOtherEnds(string startingEnd) { List<string> opposites = new List<string>(); foreach (string end in pieceType.Split(',')) { if (end != startingEnd) opposites.Add(end); } return opposites.ToArray(); }
Add the
HasConnector()
method to the GamePiece class:public bool HasConnector(string direction) { return pieceType.Contains(direction); }
The GetOtherEnds()
method creates an empty List object for holding the ends we want to return to the calling code. It then uses the Split()
method of the string class to get each end listed in the pieceType
. For example, the Top,Bottom
piece will return an array with two elements. The first element will contain Top
and the second will contain Bottom
. The comma delimiter will not be returned with either string.
If the end in question...