public int GetDistance(Position position) { int dx = Math.Abs(position.X - X); int dy = Math.Abs(position.Y - Y); int dz = Math.Abs(position.Z - Z); int distance = Math.Max(Math.Max(dx, dy), dz); return distance; }
public override void FilterMovementMap(Position initialPosition, PlayerColour colour, HashSet<Position> map) { int direction = colour == PlayerColour.Black ? 1 : -1; foreach (Position position in map) { int yDirection = direction * (position.Y - initialPosition.Y); if (yDirection < 1 && initialPosition.GetDistance(position) > 1) map.Remove(position); } }
public override void FilterMovementMap(Position initialPosition, PlayerColour colour, HashSet<Position> map) { foreach (Position position in map) { int axesUsed = 0; if (position.X != initialPosition.X) axesUsed++; if (position.Y != initialPosition.Y) axesUsed++; if (position.Z != initialPosition.Z) axesUsed++; if (axesUsed!= 2) map.Remove(position); } }
public override void FilterMovementMap(Position initialPosition, PlayerColour colour, HashSet<Position> map) { foreach (Position position in map) { if (initialPosition.GetDistance(position) == 2) map.Remove(position); } }
public virtual void FilterMovementMap(Position initialPosition, PlayerColour colour, HashSet<Position> map) { }
void RenderBoard() { const ConsoleColor labelColour = ConsoleColor.White; const ConsoleColor boardColour = ConsoleColor.DarkGray; const ConsoleColor blackColour = ConsoleColor.Red; const ConsoleColor whiteColour = ConsoleColor.Yellow; Dictionary<PieceTypeIdentifier, string> pieceSymbols = new Dictionary<PieceTypeIdentifier, string>() { {PieceTypeIdentifier.Pawn, "P"}, {PieceTypeIdentifier.Martyr, "M"}, {PieceTypeIdentifier.Guardian, "G"}, {PieceTypeIdentifier.Lance, "C"}, {PieceTypeIdentifier.Serpent, "S"}, }; const string emptyHex = "o"; const string genericFiller = "--"; const string verticalFillerLeft = "- "; const string verticalFillerRight = " -"; string space = ""; for (int i = 0; i < GameConstants.GridSizeX * 2; i++) space += " "; string xLabels = GetXLabels(); Write(space + xLabels + "\n", labelColour); for (int row = GameConstants.GridSizeY; row >= 1; row--) { string leftLabel = row.ToString(); while (leftLabel.Length < row * 2) leftLabel = " " + leftLabel; leftLabel += " "; string rightLabel = " " + row.ToString(); Write(leftLabel, labelColour); for (int column = 1; column <= GameConstants.GridSizeX; column++) { Position position = new Position(row, column); Hex hex = Messenger.Game.GetHex(position); if (hex.Piece == null) Write(emptyHex, boardColour); else { Piece piece = hex.Piece; ConsoleColor colour = piece.Owner.Colour == PlayerColour.Black ? blackColour : whiteColour; string symbol = pieceSymbols[piece.Type.Identifier]; Write(symbol, colour); } if (column < GameConstants.GridSizeX) { if (column == GameConstants.GridSizeX / 2) Write(verticalFillerLeft, boardColour); else if (column == GameConstants.GridSizeX / 2 + 1) Write(verticalFillerRight, boardColour); else Write(genericFiller, boardColour); } } Write(rightLabel + "\n", labelColour); } Write(" " + xLabels + "\n", labelColour); Console.ForegroundColor = ConsoleColor.Gray; }
public Hex(Position position) { Position = position; Piece = null; Neighbours = new List<Hex>(); }
public PiecePromotion(Position location, PieceTypeIdentifier promotion) { Position = location; Type = promotion; }
public PieceMove(Position source, Position destination) { Source = source; Destination = destination; }
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]; }
List<Hex> GetZoneOfControl(Player player) { int initial; int direction; if (player.Colour == PlayerColour.Black) { initial = 0; direction = 1; } else { initial = GameConstants.GridSizeY - 1; direction = -1; } List<Hex> zone = new List<Hex>(); for (int x = 0; x < GameConstants.GridSizeX; x++) { int? maximum = null; for (int y = initial; y >= 0 && y < GameConstants.GridSizeY; y += direction) { Position position = new Position(x, y); if (!position.IsValid()) break; Hex hex = GetHex(position); Piece piece = hex.Piece; if (piece == null) continue; else if (object.ReferenceEquals(piece.Owner, player)) maximum = y; else break; } if (maximum == null) continue; for (int y = initial; ; y += direction) { Position position = new Position(x, y); Hex hex = GetHex(position); zone.Add(hex); if (y == maximum) break; } } return zone; }
void DeployPawns(Player player) { int firstRankY; int secondRankY; int secondRankInitialX; int secondRankFinalX; if(player.Colour == PlayerColour.Black) { firstRankY = 2; secondRankY = 1; secondRankInitialX = 6; secondRankFinalX = 12; } else { firstRankY = 6; secondRankY = 7; secondRankInitialX = 0; secondRankFinalX = 6; } for (int x = 0; x < GameConstants.GridSizeX; x++) { Position position = new Position(x, firstRankY); DeployPawn(position, player); } for (int x = secondRankInitialX; x <= secondRankFinalX; x++) { Position position = new Position(x, secondRankY); DeployPawn(position, player); } }
void DeployPawn(Position position, Player player) { Hex hex = GetHex(position); Piece piece = new Piece(GameConstants.Pieces[PieceTypeIdentifier.Pawn], player); player.Pieces.Add(piece); hex.Piece = piece; }
void CreateGrid() { _Grid = new List<Hex>(); for (int y = 0; y < GameConstants.GridSizeY; y++) { for (int x = 0; x < GameConstants.GridSizeX; x++) { Position position = new Position(x, y); Hex hex = new Hex(position); _Grid.Add(hex); } } foreach (Hex hex in _Grid) { foreach (Position offset in Position.NeighbourOffsets) { Position position = hex.Position + offset; if (position.IsValid()) { Hex neighbour = GetHex(position); hex.Neighbours.Add(hex); } } } }
public PiecePlacement(PieceTypeIdentifier type, Position position) { Type = type; Position = position; }
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++; }