public void PromotePiece(Position position, PieceTypeIdentifier type) { position.CheckValidity(); if (CurrentTurnActions >= GameConstants.ActionsPerTurn) throw new GameException("You may not perform any more actions this turn"); Hex hex = GetHex(position); Piece piece = hex.Piece; if (piece == null) throw new GameException("There is no piece to promote, the specified hex is empty"); if (piece.Type.Identifier != PieceTypeIdentifier.Pawn) throw new GameException("Only pawns may be promoted"); if (!piece.CanMove) throw new GameException("This piece has already been moved this turn and can hence not be promoted"); if(type == PieceTypeIdentifier.Pawn) throw new GameException("Invalid promotion identifier"); bool isClear = true; foreach (Hex neighbour in hex.Neighbours) { Piece neighbourPiece = neighbour.Piece; if (neighbourPiece != null && !object.ReferenceEquals(neighbourPiece.Owner, CurrentTurnPlayer)) { isClear = false; break; } } if (!isClear) throw new GameException("You cannot promote a piece that is in direct proximity of an opponent's piece"); Piece newPiece = new Piece(GameConstants.Pieces[type], CurrentTurnPlayer); hex.Piece = newPiece; newPiece.Hex = hex; newPiece.CanMove = false; piece.Hex = null; CurrentTurnActions++; }
public Hex GetHex(Position position) { position.CheckValidity(); int index = position.X + position.Y * GameConstants.GridSizeX; return _Grid[index]; }
public void MovePiece(Position source, Position destination) { source.CheckValidity(); destination.CheckValidity(); if (source.Equals(destination)) throw new GameException("Tried to move a piece to its current location"); if (CurrentTurnActions >= GameConstants.ActionsPerTurn) throw new GameException("You have already performed the maximum number of actions in this turn"); Hex sourceHex = GetHex(source); Hex destinationHex = GetHex(destination); Piece attacker = sourceHex.Piece; Piece defender = destinationHex.Piece; if (attacker == null) throw new GameException("There is no piece on the specified hex"); if (attacker.Owner != CurrentTurnPlayer) throw new GameException("Tried to move a piece of the opponent"); if (!attacker.CanMove) throw new GameException("Tried to move a piece that had already been used in this turn"); if (!PieceCanReachHex(attacker, destinationHex)) throw new GameException("The piece is unable to reach the destination"); if (defender != null) { if (attacker.Owner == defender.Owner) throw new GameException("You cannot capture your own pieces"); int attackSum = GetAttackSum(attacker, defender); if (attackSum < defender.Type.Defence) throw new GameException("Your attack is too weak to capture this piece"); attacker.Owner.Captures.Add(defender.Type); defender.Owner.Pieces.Remove(defender); } attacker.Hex = destinationHex; sourceHex.Piece = null; destinationHex.Piece = attacker; attacker.CanMove = false; CurrentTurnActions++; }