public static GameBoard ParseGameString(string gameString, bool trusted = false) { if (string.IsNullOrWhiteSpace(gameString)) { throw new ArgumentNullException(nameof(gameString)); } string[] split = gameString.Split(BoardStringSeparator); if (!EnumUtils.TryParseExpansionPieces(split[0], out ExpansionPieces expansionPieces)) { throw new ArgumentException("Couldn't parse expansion pieces.", nameof(gameString)); } GameBoard gb = new GameBoard(expansionPieces); for (int i = 3; i < split.Length; i++) { string moveString = NotationUtils.NormalizeBoardSpaceMoveString(split[i]); Move move = NotationUtils.ParseMoveString(gb, moveString); if (trusted) { gb.TrustedPlay(move, moveString); } else { gb.Play(move, moveString); } } return(gb); }
public string ToGameString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0}{1}", EnumUtils.GetExpansionPiecesString(ExpansionPieces), BoardStringSeparator); sb.AppendFormat("{0}{1}", BoardState.ToString(), BoardStringSeparator); sb.AppendFormat("{0}[{1}]{2}", CurrentTurnColor.ToString(), CurrentPlayerTurn, BoardStringSeparator); foreach (BoardHistoryItem item in BoardHistory) { sb.AppendFormat("{0}{1}", item.MoveString ?? NotationUtils.ToBoardSpaceMoveString(this, item.Move), BoardStringSeparator); } return(sb.ToString().TrimEnd(BoardStringSeparator)); }
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)); }