public List <IUnit> GetFirstLine(Army army) { var firstLine = new List <IUnit>(); var min = Math.Min(Strategy.rowSize, army.Count()); for (int i = 0; i < min; i++) { firstLine.Add(army[army.Count() - 1 - i]); } return(firstLine); }
public List <IUnit> GetInsideTargets(Army inside, ISpecialAction unit) { var targets = new List <IUnit>(); int index = inside.IndexOf((IUnit)unit); int from = (index - unit.Range > 0) ? index - unit.Range : 0; int to = ((index + unit.Range + 1) > inside.Count()) ? inside.Count() : index + unit.Range + 1; for (int i = from; i < to; i++) { targets.Add(inside[i]); } return(targets); }
public List <IUnit> GetOutsideTargets(Army first, Army second, ISpecialAction unit) { var targets = new List <IUnit>(); int indexUnit = first.IndexOf((IUnit)unit); if (indexUnit >= first.Count() - unit.Range) { int targetsCount = unit.Range - (first.Count() - 1 - indexUnit); for (int i = 0; i < ((targetsCount > second.Count())?second.Count():targetsCount); i++) { targets.Add(second[second.Count() - 1 - i]); } } return(targets); }
public List <IUnit> GetOutsideTargets(Army inside, Army outside, ISpecialAction unit) { var targets = new List <IUnit>(); int row = (inside.Count() - inside.IndexOf((IUnit)unit) - 1) % rowSize; int line = (inside.Count() - inside.IndexOf((IUnit)unit) - 1) / rowSize; if (line >= unit.Range) { return(targets); } for (int i = outside.Count() - 1 - row, targetsCount = unit.Range - line; i >= 0 && targetsCount > 0; i -= rowSize, targetsCount--) { targets.Add(outside[i]); } return(targets); }
public void Fight(Army first, Army second) { if (IsGameFinished()) { return; } List <IUnit> firstLineInFirst = GetFirstLine(first); List <IUnit> firstLineInSecond = GetFirstLine(second); var min = Math.Min(Strategy.rowSize, Math.Min(first.Count(), second.Count())); for (int i = 0; i < min; i++) { IUnit attacker = firstLineInFirst[i]; IUnit victim = firstLineInSecond[i]; MoveInfo += String.Format("\n\nArmy {0}. {1}\n\n in opposition \n\nArmy {2}. {3}", first.Name, attacker.GetInfo(), second.Name, victim.GetInfo()); IUnit dead = attacker.Fight(victim); if (dead != null) { MoveInfo += string.Format("\n\nArmy {0}. {1} dead.\n", second.Name, dead.Name); dead.NotifyObservers(); second.Remove(dead); } else { MoveInfo += String.Format("\n\nArmy {0}. {1} attacked.\n", second.Name, victim.GetInfo()); } } }
public List <IUnit> GetInsideTargets(Army inside, ISpecialAction unit) { var targets = new List <IUnit>(); int index = inside.IndexOf((IUnit)unit); int row = (inside.Count() - index - 1) % rowSize; int line = (inside.Count() - index - 1) / rowSize; for (int i = 0; i < inside.Count(); i++) { int r = (inside.Count() - i - 1) % rowSize; int l = (inside.Count() - i - 1) / rowSize; if (Math.Sqrt((r - row) * (r - row) + (l - line) * (l - line)) <= unit.Range) { targets.Add(inside[i]); } } return(targets); }
public string GetInfo(Army army) { var info = String.Format("Army {0}:", army.Name); for (int i = 1; i <= army.Count(); i++) { info += String.Format("\n{0}. {1}", i, army[i - 1].GetInfo()); } return(info); }
public int GetRow(Army army, IUnit unit) { int i = (army.Count() - army.IndexOf(unit)) % rowSize; if (i == 0) { return(i); } return(rowSize - i); }
public List <ISpecialAction> GetSpecialUnitsInRow(Army army, int row) { var specials = new List <ISpecialAction>(); for (int i = army.Count() - row - 1; i >= 0; i -= Strategy.rowSize) { if (army[i] is ISpecialAction) { specials.Add(army[i] as ISpecialAction); } } return(specials); }
public string GetInfo(Army army) { var info = string.Format("Army {0}:", army.Name); for (int j = 0; j < rowSize; j++) { info += string.Format("\nRow {0}:", j + 1); for (int line = 1, i = army.Count() - j - 1; i >= 0; line++, i -= rowSize) { info += string.Format("\nLine {0}. {1}", line, army[i].GetInfo()); } } return(info); }
public List <IUnit> GetOutsideTargets(Army first, Army second, ISpecialAction unit) { var victims = new List <IUnit>(); int row = GetRow(first, (IUnit)unit); int line = GetLine(first, (IUnit)unit); if (line >= unit.Range) { return(victims); } for (int i = second.Count() - rowSize + row, victimsCount = unit.Range - line; i >= 0 && victimsCount > 0; i -= rowSize, victimsCount--) { victims.Add(second[i]); } return(victims); }
public int GetLine(Army army, IUnit unit) { return((army.Count() - army.IndexOf(unit) - 1) / rowSize); }
public NToNStrategy(Army first, Army second) { rowSize = Math.Min(first.Count(), second.Count()); }
public void DoSpecialAction(Army first, Army second) { if (IsGameFinished() || first.Count() < Strategy.rowSize + 1) { return; } for (int i = 0; i < Strategy.rowSize; i++) { var specials = GetSpecialUnitsInRow(first, i); if (specials.Count == 0) { continue; } int specialIndex = Rand.Get(0, specials.Count); var victims = GetTargets(first, second, specials[specialIndex]); if (victims.Count == 0) { continue; } int victimIndex = Rand.Get(0, victims.Count); IUnit beforeSpecial = victims[victimIndex].Copy(); IUnit afterSpecial = specials[specialIndex].DoSpecialAction(victims[victimIndex]); MoveInfo += "\nSpecial action."; if (specials[specialIndex] is BowmanUnit) { MoveInfo += String.Format("\n\nArmy {0}. {1} in opposition \n\nArmy {2}. {3}", first.Name, ((IUnit)specials[specialIndex]).GetInfo(), second.Name, beforeSpecial.GetInfo()); if (afterSpecial == specials[specialIndex]) { MoveInfo += String.Format("\n\nArmy {0}. {1} dead.\n", second.Name, victims[victimIndex].Name); afterSpecial.NotifyObservers(); second.Remove(afterSpecial); } else { MoveInfo += String.Format("\n\nArmy {0}. {1} attacked\n", second.Name, victims[victimIndex].GetInfo()); } } else if (specials[specialIndex] is HealerUnit) { if (afterSpecial != null) { MoveInfo += String.Format("\nArmy {0}. {1}\n\n\t healing \n\nArmy {2}. {3}", first.Name, ((IUnit)specials[specialIndex]).GetInfo(), first.Name, beforeSpecial.GetInfo()); MoveInfo += String.Format("\n\nArmy {0}. {1} healed\n", first.Name, victims[victimIndex].GetInfo()); } else { MoveInfo += String.Format("\n\nNo one was cured from the army {0}. ", first.Name); } } else if (specials[specialIndex] is WizardUnit) { if (afterSpecial != null) { MoveInfo += String.Format("\nАrmy {0}. {1}\n\n\t cloning \n\nАrmy {2}. {3}", first.Name, ((IUnit)specials[specialIndex]).GetInfo(), first.Name, beforeSpecial.GetInfo()); MoveInfo += String.Format("\n\nАrmy {0}. {1} cloned.\n", first.Name, victims[victimIndex].GetInfo()); } else { MoveInfo += String.Format("\n\nNo one was cloned from the army {0}.\n", first.Name); } } else { if (specials[specialIndex] is LightUnit) { if (afterSpecial != null) { MoveInfo += String.Format("\nArmy {0}. {1}\n\n\t dressing \n\nArmy {2}. {3}", first.Name, ((IUnit)specials[specialIndex]).GetInfo(), first.Name, beforeSpecial.GetInfo()); MoveInfo += String.Format("\n\nArmy {0}. {1} dressed.\n", first.Name, victims[victimIndex].GetInfo()); } else { MoveInfo += String.Format("\n\nNo one was dressed from the army {0}. ", first.Name); } } } } }