private void UpdatePos(ref Piece[,] board, int x, int y) { board[Position.X, Position.Y] = null; Sprite.Position = new Vector2f(x* 65, y * 65); Position = new Vector2i(x, y); }
/// <summary> /// Returns an integer telling what has occured. /// /// </summary> /// <param name="board"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns>1 = Cannot Move, 2 = Moved, 3 = Taken a piece and moved.</returns> virtual public int MoveTo(ref Piece[,] board, int x, int y) { if ((x < 0) || (x > 7)) return 1; if ((y < 0) || (y > 7)) return 1; foreach (var move in Moves) { Vector2i newPosition; newPosition.X = Position.X + move.Key.X; newPosition.Y = Position.Y + move.Key.Y; if (newPosition.X != x) continue; if (newPosition.Y != y) continue; // we know he's trying to do a legal move. var entryAtNewPos = board[newPosition.X, newPosition.Y]; if (entryAtNewPos == null) { // nobody there can we move there? switch (move.Value) { case 3: case 1: board[newPosition.X, newPosition.Y] = this; UpdatePos(ref board, newPosition.X, newPosition.Y); return 2; case 2: return 1; } } else if (entryAtNewPos.Colour != Colour) { // there's somewhere there, can we take it tho? switch (move.Value) { case 1: return 1; case 3: case 2: board[newPosition.X, newPosition.Y] = this; UpdatePos(ref board, newPosition.X, newPosition.Y); return 3; } } else if (entryAtNewPos.Colour == Colour) { return 1; } } return 1; }
public override int MoveTo(ref Piece[,] board, int x, int y) { var movementId = base.MoveTo(ref board, x, y); _hasMoved = true; if (_hasMoved != true) return movementId; foreach (var pair in Moves.Where(pair => (pair.Key.Y == 2) || (pair.Key.Y == -2))) { // remove eet Moves.Remove(pair.Key); break; } return movementId; }
void OnMousePressed(object sender, MouseButtonEventArgs e) { // Get piece at coordinates. var place = _board[e.X / 65, e.Y / 65]; // Either move or take piece. if (SecondClick) { // toggle off if (place == _selectedPiece) { SecondClick = false; } // If second click is an empty space, try to move piece. will return false is failure. var moved = _selectedPiece.MoveTo(ref _board, e.X / 65, e.Y / 65); switch (moved) { case 3: if (_turnColour == Colours.White) { BlackNumberOfPieces--; UpdateTitle(); } else { WhiteNumberOfPieces--; UpdateTitle(); } break; case 1: return; } _turnColour = _turnColour == Colours.White ? Colours.Black : Colours.White; UpdateTitle(); } else { // Just piece selection. if (place != null) { // there is a piece here. if (place.Colour == _turnColour) { // its the turns type of piece. _selectedPiece = place; } else { // do nothing. return; } } else { // do nothing. return; } } SecondClick = !SecondClick; }
public KingCastleMove(Piece piece, Square target, CastleType castle) : base(piece, target) { this.Castle = castle; }