public void ProcessMove(PlayerSide player, VectorTwo position, int direction) { if (!validator.IsLegalMove(player, position, direction)) { return; } Troop troop = troopMap.Get(position); MoveTroop(position, direction); if (!board.IsInside(troop.Position)) { ControlWithAi(troop); } while (!GameHasEnded()) { if (movePointsLeft == 0) { ToggleActivePlayer(); } else { return; } } OnGameEnded(); }
public int GetOptimalDirection(Troop troop) { VectorTwo target = board.Center; int minDist = 1000000; int minDir = 0; for (int dir = -1; dir <= 1; dir += 2) { int direction = (6 + dir + troop.Orientation) % 6; VectorTwo neigh = Hex.GetAdjacentHex(troop.Position, direction); if (troopMap.Get(neigh) != null) { continue; } int dist = (target - neigh).SqrMagnitude; if (dist < minDist) { minDist = dist; minDir = dir; } } return(minDir); }
public BattleResult GetFightResult(Troop defender, VectorTwo attackerPosition) { bool defenderDamaged = Random.Next(0, 6) < 3; bool attackerDamaged = defender.InControlZone(attackerPosition) && Random.Next(0, 6) < 3; return(new BattleResult(defenderDamaged, attackerDamaged)); }
public Board(int xMax, int yMax) { this.XMax = xMax; this.YMax = yMax; Center = new VectorTwo(xMax / 2, yMax / 2); }
private void PositionContainsTroop(VectorTwo position) { if (map.Get(position) == null) { throw new IllegalMoveException("No troop at the specified hex!"); } }
// TODO: Don't return cells outside the board private VectorTwo GetEmptyCell(VectorTwo seedPosition) { if (Get(seedPosition) == null) { return(seedPosition); } Queue <VectorTwo> q = new Queue <VectorTwo>(); q.Enqueue(seedPosition); while (q.Count > 0) { VectorTwo position = q.Dequeue(); if (Get(position) == null) { return(position); } VectorTwo[] neighbours = Hex.GetNeighbours(seedPosition); foreach (VectorTwo neigh in neighbours) { if (board.IsInside(neigh)) { q.Enqueue(neigh); } } } throw new Exception("Couldn't find an empty cell"); }
private void DestroyTroop(Troop troop, VectorTwo startingPosition) { troopMap.Remove(troop, startingPosition); if (troop.Player == activePlayer) { movePointsLeft -= troop.MovePoints; } }
private Task MoveTroop(int fromClient, Packet packet) { VectorTwo position = packet.ReadVector2Int(); int direction = packet.ReadInt(); gameHandler.MoveTroop(fromClient, position, direction); return(Task.CompletedTask); }
public Troop(PlayerSide player, int movePoints, VectorTwo position, int orientation, int health) { Player = player; InitialMovePoints = movePoints; MovePoints = movePoints; Position = position; Orientation = orientation; Health = health; }
public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } VectorTwo v = (VectorTwo)obj; return((X == v.X) && (Y == v.Y)); }
public void MoveTroop(int client, VectorTwo position, int direction) { if (direction < -1 || direction > 1) { Console.WriteLine($"Client {client} sent a move with illegal direction!"); return; } Game game = clientToGame[client]; game.MakeMove(client, position, direction); }
public Troop Get(VectorTwo position) { try { return(map[position]); } catch (KeyNotFoundException) { return(null); } }
private void AddTroopToRound(int round, VectorTwo position, PlayerSide player) { Troop troop = player == PlayerSide.Red ? TroopFactory.Red(position) : TroopFactory.Blue(position); try { troopsForRound[round].Add(troop); } catch (KeyNotFoundException) { troopsForRound[round] = new List <Troop>(); troopsForRound[round].Add(troop); } }
private void ThrowIfNotBlocked(Troop troop, VectorTwo cell) { if (!area.IsInside(cell)) { return; } Troop encounter = map.Get(cell); if (encounter == null || encounter.Player != troop.Player) { throw new IllegalMoveException("Attempting to enter a cell with friendly troop!"); } }
private void NotEnteringFriendOrBlocked(Troop troop, int direction) { VectorTwo targetPosition = Hex.GetAdjacentHex(troop.Position, troop.Orientation + direction); Troop encounter = map.Get(targetPosition); if (encounter == null || encounter.Player != troop.Player) { return; } // Tries to enter a friend so throw if has some other legal move foreach (VectorTwo cell in troop.ControlZone) { ThrowIfNotBlocked(troop, cell); } }
private void ApplyDamage(Troop troop, VectorTwo startingPosition) { PlayerSide opponent = troop.Player.Opponent(); score.Increment(opponent); if (troop.Player == activePlayer && troop.MovePoints > 0) { movePointsLeft--; } troop.ApplyDamage(); if (troop.Health <= 0) { DestroyTroop(troop, startingPosition); } }
public bool IsLegalMove(PlayerSide player, VectorTwo position, int direction) { try { IsPlayersTurn(player); PositionContainsTroop(position); Troop troop = map.Get(position); PlayerControlsTroop(player, troop); TroopHasMovePoints(troop); NotEnteringFriendOrBlocked(troop, direction); message = "Move is valid."; return(true); } catch (IllegalMoveException ex) { message = ex.Message; Console.WriteLine(message); Console.WriteLine($"Pos: {position}, dir: {direction}"); return(false); } }
public BattleResult GetFightResult(Troop defender, VectorTwo attackerPosition) { return(new BattleResult(true, true)); }
public bool IsInside(VectorTwo p) { return(p.X >= 0 && p.X <= XMax && p.Y >= 0 && p.Y <= YMax); }
public void AdjustPosition(Troop troop, VectorTwo startingPosition) { map.Remove(startingPosition); map.Add(troop.Position, troop); }
public void Write(VectorTwo value) { Write(value.X); Write(value.Y); }
private void MoveTroop(VectorTwo position, int direction) { movePointsLeft--; Troop troop = troopMap.Get(position); VectorTwo startingPosition = troop.Position; troop.MoveInDirection(direction); List <BattleResult> battleResults = new List <BattleResult>(); Troop encounter = troopMap.Get(troop.Position); if (encounter == null) { troopMap.AdjustPosition(troop, startingPosition); OnTroopMoved(position, direction, battleResults); return; } BattleResult result = BattleResult.FriendlyCollision; if (encounter.Player != troop.Player) { result = battleResolver.GetFightResult(encounter, startingPosition); } battleResults.Add(result); if (result.AttackerDamaged) { ApplyDamage(troop, startingPosition); } if (result.DefenderDamaged) { ApplyDamage(encounter, encounter.Position); } troop.FlyOverOtherTroop(); while ((encounter = troopMap.Get(troop.Position)) != null && troop.Health > 0) { result = battleResolver.GetCollisionResult(); battleResults.Add(result); if (result.AttackerDamaged) { ApplyDamage(troop, startingPosition); } if (result.DefenderDamaged) { ApplyDamage(encounter, encounter.Position); } troop.FlyOverOtherTroop(); } if (troop.Health > 0) { troopMap.AdjustPosition(troop, startingPosition); } OnTroopMoved(position, direction, battleResults); }
private void OnTroopMoved(VectorTwo position, int direction, List <BattleResult> battleResults) { TroopMoved?.Invoke(this, new TroopMovedEventArgs(position, direction, battleResults)); }
public static Troop Blue(VectorTwo position, int orientation) { return(new Troop(PlayerSide.Blue, 5, position, orientation, 2)); }
public static Troop Red(VectorTwo position) { return(new Troop(PlayerSide.Red, 5, position, 3, 2)); }
public TroopMovedEventArgs(VectorTwo position, int direction, List <BattleResult> battleResults) { Position = position; Direction = direction; BattleResults = battleResults; }
public void Remove(Troop troop, VectorTwo startingPosition) { map.Remove(startingPosition); GetTroops(troop.Player).Remove(troop); }
public bool InControlZone(VectorTwo position) { return(ControlZone.Any(cell => cell == position)); }
public void MakeMove(int client, VectorTwo position, int direction) { PlayerSide player = GetColor(client); controller.ProcessMove(player, position, direction); }