public override void Resolve(List <Order> orders, BoardStatus status) { var board = new Board(); var adj = Unit.GetAdjacent(board); if (!adj.Where(loc => loc.Name == TargetLocation.Name).Any()) { Status = OrderStatus.Failed; return; } if (board.Map[TargetLocation.Name].Type == LocationType.Water && Unit.Type == UnitType.Army) { Status = OrderStatus.Failed; return; } var moveOrdersTargetingUnit = orders.Where(order => (order.Type == OrderType.Move || order.Type == OrderType.MoveByConvoy) && order.TargetLocation.Name == Unit.LocName.Name).ToArray(); if (!moveOrdersTargetingUnit.Any()) { Status = OrderStatus.Succeded; return; } //attack from somewhere other than where its supporting if (moveOrdersTargetingUnit.Where(order => order.Type == OrderType.Move && order.Unit.IsAdjacent(Unit.LocName, board) && order.Unit.LocName.Name != TargetLocation.Name).Any()) { Status = OrderStatus.Failed; return; } if (moveOrdersTargetingUnit.Where(order => order.Status == OrderStatus.Succeded).Any()) { Status = OrderStatus.Failed; } }
public BoardStatus Starting() { var bs = new BoardStatus(new Board()); bs.Units.Add(new Army(Nations.Austria, new LocationName("vie"))); bs.Units.Add(new Army(Nations.Austria, new LocationName("bud"))); bs.Units.Add(new Fleet(Nations.Austria, new LocationName("tri"))); bs.Units.Add(new Fleet(Nations.England, new LocationName("lon"))); bs.Units.Add(new Fleet(Nations.England, new LocationName("edi"))); bs.Units.Add(new Army(Nations.England, new LocationName("liv"))); bs.Units.Add(new Army(Nations.France, new LocationName("par"))); bs.Units.Add(new Army(Nations.France, new LocationName("mar"))); bs.Units.Add(new Fleet(Nations.France, new LocationName("bre"))); bs.Units.Add(new Army(Nations.Germany, new LocationName("ber"))); bs.Units.Add(new Army(Nations.Germany, new LocationName("mun"))); bs.Units.Add(new Fleet(Nations.Germany, new LocationName("kie"))); bs.Units.Add(new Army(Nations.Italy, new LocationName("rom"))); bs.Units.Add(new Army(Nations.Italy, new LocationName("ven"))); bs.Units.Add(new Fleet(Nations.Italy, new LocationName("nap"))); bs.Units.Add(new Army(Nations.Russia, new LocationName("mos"))); bs.Units.Add(new Fleet(Nations.Russia, new LocationName("sev"))); bs.Units.Add(new Army(Nations.Russia, new LocationName("war"))); bs.Units.Add(new Fleet(Nations.Russia, new LocationName("stp", "south"))); bs.Units.Add(new Fleet(Nations.Turkey, new LocationName("ank"))); bs.Units.Add(new Army(Nations.Turkey, new LocationName("con"))); bs.Units.Add(new Army(Nations.Turkey, new LocationName("smy"))); foreach (var unit in bs.Units) { bs.Control[unit.LocName.Name] = unit.Nation; } return(bs); }
public virtual void Resolve(List <Order> orders, BoardStatus status) { var holdStr = GetStrength(orders); var moveOrders = orders.Where(order => order.Status != OrderStatus.Failed).Where(order => (order.Type == OrderType.Move || order.Type == OrderType.MoveByConvoy)). Where(order => order.TargetLocation.Name == Unit.LocName.Name).ToArray(); Strength moveStr = new Strength(0, 0); if (moveOrders.Length > 0) { moveStr = moveOrders[0].GetStrength(orders); } foreach (var order in moveOrders) { var str = order.GetStrength(orders); if (moveStr.Max < str.Max) { moveStr.Max = str.Max; } //the maximum min of the move orders ( a move order exists with at least this str) if (moveStr.Min < str.Min) { moveStr.Min = str.Min; } } //no move orders can beat the min hold str if (holdStr.Min > moveStr.Max) { Status = OrderStatus.Succeded; foreach (var moveOrder in moveOrders) { moveOrder.Status = OrderStatus.Failed; } return; } }
public override void Resolve(List <Order> orders, BoardStatus status) { var board = new Board(); if (!Unit.IsAdjacent(TargetLocation, board)) { Status = OrderStatus.Failed; return; } ResolveAfterAdj(orders, status); }
public override void Resolve(List <Order> orders, BoardStatus status) { var board = new Board(); //check path var convoyOrders = orders.Where(order => order.Type == OrderType.Convoy). Where(order => order.Status != OrderStatus.Failed). Where(order => order.TargetStartingLocation.Name == Unit.LocName.Name && order.TargetLocation.Name == TargetLocation.Name). Where(order => board.Map[order.Unit.LocName.Name].Type == LocationType.Water).ToList(); var res = findPath(Unit.LocName.Name, TargetLocation.Name, convoyOrders, board, true); if (res == OrderStatus.Succeded) { ResolveAfterAdj(orders, status); } else { Status = res; } }
protected void ResolveAfterAdj(List <Order> orders, BoardStatus status) { var str = GetStrength(orders); Strength holdStr; if (status.Units.Where(unit => unit.LocName.Name == TargetLocation.Name).Any()) { var ExistingUnitOrder = orders.Where(order => order.Unit.LocName.Name == TargetLocation.Name).FirstOrDefault(); if (ExistingUnitOrder == null) { holdStr = new Strength(0, 0); } if (ExistingUnitOrder.Type != OrderType.Move && ExistingUnitOrder.Type != OrderType.MoveByConvoy) { if (ExistingUnitOrder.Unit.Nation == Unit.Nation) { Status = OrderStatus.Failed; return; } holdStr = ExistingUnitOrder.GetStrength(orders); } else { //There is a unit there but its trying to move. Support wont help but it may fail the move and end up resisting. switch (ExistingUnitOrder.Status) { case OrderStatus.Unresolved: if (ExistingUnitOrder.Unit.Nation == Unit.Nation) { Status = OrderStatus.Unresolved; return; } holdStr = new Strength(0, 1); break; case OrderStatus.Succeded: holdStr = new Strength(0, 0); break; case OrderStatus.Failed: if (ExistingUnitOrder.Unit.Nation == Unit.Nation) { Status = OrderStatus.Failed; return; } holdStr = new Strength(1, 1); break; default: //Something went wrong throw new Exception("Invalid order status"); } } } else { holdStr = new Strength(0, 0); } Strength preventStr = new Strength(0, 0); var preventOrders = orders.Where(order => order.TargetLocation.Name == TargetLocation.Name).ToArray(); foreach (var order in preventOrders) { var pStr = order.GetStrength(orders); if (pStr.Min > preventStr.Min) { preventStr.Min = pStr.Min; } if (pStr.Max > preventStr.Max) { preventStr.Max = pStr.Max; } } //check for head to head var headToHeadOrder = orders.Where(order => order.TargetLocation.Name == Unit.LocName.Name && order.Unit.LocName.Name == TargetLocation.Name && order.Type == OrderType.Move).FirstOrDefault(); if (headToHeadOrder != null) { var hStr = headToHeadOrder.GetStrength(orders); if (hStr.Min > preventStr.Min) { preventStr.Min = hStr.Min; } if (hStr.Max > preventStr.Max) { preventStr.Max = hStr.Max; } } if (str.Min > holdStr.Max && str.Min > preventStr.Max) { Status = OrderStatus.Succeded; return; } if (str.Max <= holdStr.Min || str.Max <= preventStr.Min) { Status = OrderStatus.Failed; return; } }
public BoardStatus Next(List <Order> orders) { //resolve orders var resolved = false; while (!resolved) { var unOrders = orders.Where(order => order.Status == OrderStatus.Unresolved).ToArray(); var starting = unOrders.Length; foreach (var order in unOrders) { order.Resolve(orders, this); } if (orders.Where(order => order.Status == OrderStatus.Unresolved).ToArray().Length >= starting) { var convoyOrders = orders.Where(order => order.Type == OrderType.Convoy && order.Status == OrderStatus.Unresolved).ToArray(); if (convoyOrders.Length > 0) { foreach (var order in convoyOrders) { order.Status = OrderStatus.Failed; } } else { //should be only move orders left unresolved if (orders.Where(order => order.Type != OrderType.Move || order.Type != OrderType.MoveByConvoy).Where(order => order.Status == OrderStatus.Unresolved).Any()) { //unresolved non-move orders this isn't good throw new Exception("Non move order not resovled"); } var moveOrders = orders.Where(order => order.Type == OrderType.Move || order.Type == OrderType.MoveByConvoy).Where(order => order.Status == OrderStatus.Unresolved).ToArray(); foreach (var order in moveOrders) { order.Status = OrderStatus.Succeded; } } } } //update the board var nextStatus = new BoardStatus(MyBoard); var dUnits = new List <dislodgedUnit>(); foreach (var Unit in Units) { var moveOrder = orders.Where(order => order.Status == OrderStatus.Succeded && order.Unit.LocName.Name == Unit.LocName.Name && (order.Type == OrderType.Move || order.Type == OrderType.MoveByConvoy)).FirstOrDefault(); if (moveOrder == null) { var moveOrdersIntoSpace = orders.Where(order => order.Status == OrderStatus.Succeded).Where(order => order.Type == OrderType.Move || order.Type == OrderType.MoveByConvoy).Where(order => order.TargetLocation.Name == Unit.LocName.Name).ToArray(); if (moveOrdersIntoSpace.Length > 0) { var dUnit = new dislodgedUnit(); dUnit.Unit = Unit; dUnit.AttackedFrom = moveOrdersIntoSpace[0].TargetLocation.Name; dUnits.Add(dUnit); //dislodged } else { nextStatus.Units.Add(Unit); } } else { Unit.LocName = moveOrder.TargetLocation; nextStatus.Units.Add(Unit); } } //create the dislodged status var invalidRetreatLocations = orders.Where(order => order.Type == OrderType.Move || order.Type == OrderType.MoveByConvoy).Where(order => order.Status == OrderStatus.Succeded).Select(order => order.TargetLocation.Name).ToList(); foreach (var Unit in Units) { invalidRetreatLocations.Add(Unit.LocName.Name); } foreach (var Unit in dUnits) { var Locations = Unit.Unit.GetAdjacent(MyBoard); foreach (var invalid in invalidRetreatLocations) { Locations.RemoveAll(location => location.Name == invalid); } nextStatus.Dislodged.Units.Add(new DislodgedUnit(Unit.Unit, Locations)); } nextStatus.LastOrders = orders; return(nextStatus); }