/// <summary> /// Adds a piece on the board, will replace whatever piece is already present /// </summary> /// <param name="piece">The piece to add</param> /// <param name="position">The position a att the piece to</param> public void AddPiece(Piece piece, Point position) { Tiles[CoordinateToIndex(position)].Piece = piece; pieces.Add(piece); }
/// <summary> /// Will move the piece to a position if the position exists in the ValidMoves list property of the piece, it will also notify pieces that has implemented the INotifyOnMove interface /// </summary> /// <param name="piece">The piece to be moved</param> /// <param name="target">The position to move to</param> /// <returns>bool, is moved allowed?</returns> public bool TryMovePiece(Piece piece, Point target) { if (piece.Position == target) return false; if (piece is INotifyOnMove) { INotifyOnMove inom = (INotifyOnMove)piece; inom.OnMove(); } List<Point> validMoves = piece.ValidMoves; if (validMoves.Contains(target)) { MovePiece(piece, target); return true; } return false; }
/// <summary> /// Moves a piece to another tile, the piece will replace whatever piece already present /// </summary> /// <param name="piece">The piece to be moved</param> /// <param name="target">The position to move to</param> private void MovePiece(Piece piece, Point target) { Tiles[CoordinateToIndex(target)].Piece = piece; Tiles[CoordinateToIndex(piece.Position)].Piece = null; piece.Position = target; }
/// <summary> /// Highlights possible moves for the piece /// </summary> /// <param name="piece">The active piece</param> public void HighlightMoves(Piece piece) { List<Tile> moveList = new List<Tile>(); moveList = ToTileList(piece.ValidMoves); foreach (Tile tile in moveList) { if (tile.IsEmpty()) tile.Highlight = new SolidColorBrush(Color.FromRgb(0, 255, 0)); else if (tile.IsOccupiedBy(Piece.InverseSide(piece.Side))) tile.Highlight = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } }