private void InitPieces(ExpansionPieces expansionPieces) { ExpansionPieces = expansionPieces; for (int i = 0; i < EnumUtils.NumPieceNames; i++) { _pieces[i] = EnumUtils.IsEnabled((PieceName)i, ExpansionPieces) ? new Piece((PieceName)i) : null; } }
private void SetCurrentPlayerMetrics() { bool pullbugEnabled = EnumUtils.IsEnabled(BugType.Pillbug, ExpansionPieces); bool mosquitoEnabled = EnumUtils.IsEnabled(BugType.Mosquito, ExpansionPieces); MoveSet pillbugMoves = pullbugEnabled ? GetValidMoves(CurrentTurnColor == PlayerColor.White ? PieceName.WhitePillbug : PieceName.BlackPillbug) : null; MoveSet mosquitoMoves = pullbugEnabled && mosquitoEnabled?GetValidMoves(CurrentTurnColor == PlayerColor.White?PieceName.WhiteMosquito : PieceName.BlackMosquito) : null; foreach (PieceName pieceName in CurrentTurnPieces) { Piece targetPiece = GetPiece(pieceName); if (null != targetPiece) { if (targetPiece.InPlay) { _boardMetrics.PiecesInPlay++; _boardMetrics[pieceName].InPlay = 1; } else { _boardMetrics.PiecesInHand++; _boardMetrics[pieceName].InPlay = 0; } // Move metrics bool isPinned = IsPinned(pieceName, out _boardMetrics[pieceName].NoisyMoveCount, out _boardMetrics[pieceName].QuietMoveCount); if (isPinned && pullbugEnabled) { bool pullbugCanMove = pillbugMoves.Contains(pieceName); bool mosquitoCanMove = mosquitoEnabled && mosquitoMoves.Contains(pieceName); if (targetPiece.BugType == BugType.Pillbug) { // Check if the current player's mosquito can move it isPinned = !mosquitoCanMove; } else if (targetPiece.BugType == BugType.Mosquito) { // Check if the current player's pillbug can move it isPinned = !pullbugCanMove; } else { // Check if the current player's pillbug or mosquito can move it isPinned = !(mosquitoCanMove || pullbugCanMove); } } _boardMetrics[pieceName].IsPinned = isPinned ? 1 : 0; _boardMetrics[pieceName].IsCovered = targetPiece.InPlay && null != targetPiece.PieceAbove ? 1 : 0; CountNeighbors(targetPiece, out _boardMetrics[pieceName].FriendlyNeighborCount, out _boardMetrics[pieceName].EnemyNeighborCount); } } }
public void Play(Move move, string moveString = null) { if (null == move) { throw new ArgumentNullException(nameof(move)); } if (move.IsPass) { Pass(); return; } if (GameIsOver) { throw new InvalidMoveException(move, "You can't play, the game is over."); } if (!GetValidMoves().Contains(move)) { if (move.Color != CurrentTurnColor) { throw new InvalidMoveException(move, "It's not that player's turn."); } if (!EnumUtils.IsEnabled(move.PieceName, ExpansionPieces)) { throw new InvalidMoveException(move, "That piece is not enabled in this game."); } if (null == move.Position) { throw new InvalidMoveException(move, "You can't put a piece back into your hand."); } if (CurrentPlayerTurn == 1 && move.BugType == BugType.QueenBee) { throw new InvalidMoveException(move, "You can't play your Queen Bee on your first turn."); } Piece targetPiece = GetPiece(move.PieceName); if (!CurrentTurnQueenInPlay) { if (CurrentPlayerTurn == 4 && targetPiece.BugType != BugType.QueenBee) { throw new InvalidMoveException(move, "You must play your Queen Bee on or before your fourth turn."); } else if (targetPiece.InPlay) { throw new InvalidMoveException(move, "You can't move a piece in play until you've played your Queen Bee."); } } if (!PlacingPieceInOrder(targetPiece)) { throw new InvalidMoveException(move, "When there are multiple pieces of the same bug type, you must play the pieces in order."); } if (HasPieceAt(move.Position)) { throw new InvalidMoveException(move, "You can't move there because a piece already exists at that position."); } if (targetPiece.InPlay) { if (!PieceIsOnTop(targetPiece)) { throw new InvalidMoveException(move, "You can't move that piece because it has another piece on top of it."); } else if (!CanMoveWithoutBreakingHive(targetPiece)) { throw new InvalidMoveException(move, "You can't move that piece because it will break the hive."); } } throw new InvalidMoveException(move); } TrustedPlay(move, null != moveString ? NotationUtils.NormalizeBoardSpaceMoveString(moveString) : NotationUtils.ToBoardSpaceMoveString(this, move)); }