Example #1
0
 public int GetLine(Army army, IUnit unit)
 {
     return((army.Count() - army.IndexOf(unit) - 1) / rowSize);
 }
Example #2
0
 public NToNStrategy(Army first, Army second)
 {
     rowSize = Math.Min(first.Count(), second.Count());
 }
Example #3
0
 public Battlefield(Army first, Army second)
 {
     FirstArmy  = first;
     SecondArmy = second;
     Strategy   = new OneToOneStrategy();
 }
Example #4
0
        public void ShowMenu()
        {
            Console.Clear();
            Console.WriteLine();
            Console.WriteLine("1. Create armies");
            Console.WriteLine("2. Show armies");
            Console.WriteLine("3. Make a move");
            Console.WriteLine("4. Play till the end");
            Console.WriteLine("5. Undo last move");
            Console.WriteLine("6. Redo last move");
            Console.WriteLine("7. Subscribe");
            Console.WriteLine("8. Unsubscribe");
            Console.WriteLine("9. Change stategy");
            Console.WriteLine();

            switch (Console.ReadLine())
            {
            case "1":
                Console.WriteLine("Enter the amount for which your first army will be purchased:");
                int money;
                while (!Int32.TryParse(Console.ReadLine(), out money))
                {
                    Console.WriteLine("Error. Enter the integer.");
                }

                var factoryArmy = new FactoryArmy();
                firstArmy = factoryArmy.GetArmy(money, "1");

                Console.WriteLine("Enter the amount for which your second army will be purchased:");
                while (!Int32.TryParse(Console.ReadLine(), out money))
                {
                    Console.WriteLine("Error. Enter the integer.");
                }

                secondArmy = factoryArmy.GetArmy(money, "2");

                battlefield = new Battlefield(firstArmy, secondArmy);
                invoker     = new CommandInvoker(battlefield);

                Write(String.Format("Armies created.\n{0}", battlefield.GetArmyInfo()));
                Console.ReadLine();
                ShowMenu();
                break;

            case "2":
                Console.WriteLine(battlefield.GetArmyInfo());
                Console.ReadLine();
                ShowMenu();
                break;

            case "3":
                invoker.Move();

                Write(battlefield.MoveInfo);
                Console.ReadLine();
                if (!battlefield.EndOfGame)
                {
                    ShowMenu();
                }
                break;

            case "4":
                invoker.PlayToTheEnd();

                Write(battlefield.GameInfo);
                Console.ReadLine();
                break;

            case "5":
                invoker.Undo();

                Write("Last move canceled. ");
                Console.ReadLine();
                ShowMenu();
                break;

            case "6":
                invoker.Redo();

                Write("Last move repeated. ");
                Console.ReadLine();
                ShowMenu();
                break;

            case "7":
                if (subscribed)
                {
                    Console.WriteLine("Subscription has already been issued. ");
                }
                else
                {
                    battlefield.Subscribe();
                    subscribed = true;
                    Write("Subscribed. ");
                }
                Console.ReadLine();
                ShowMenu();
                break;

            case "8":
                if (!subscribed)
                {
                    Console.WriteLine("Subscription has not already been issued. ");
                }
                else
                {
                    battlefield.UnSubscribe();
                    subscribed = false;
                    Write("Unsubscribed. ");
                }
                Console.ReadLine();
                ShowMenu();
                break;

            case "9":
                Console.WriteLine("Input a number of strategy: 1. 1x1  2. 3x3  3. NxN");
                string strategy = Console.ReadLine();
                switch (strategy)
                {
                case "1":
                    battlefield.Strategy = new OneToOneStrategy();
                    Write("Strategy 1х1 is established");
                    break;

                case "2":
                    battlefield.Strategy = new ThreeToThreeStrategy();
                    Write("Strategy 3х3 is established");
                    break;

                case "3":
                    battlefield.Strategy = new NToNStrategy(firstArmy, secondArmy);
                    Write("Strategy NхN is established");
                    break;

                default:
                    Console.WriteLine("Error. There is no such item. Plaese try again.");
                    break;
                }
                Console.ReadLine();
                ShowMenu();
                break;

            default:
                Console.WriteLine("Error. There is no such item. Please try again.");
                Console.ReadLine();
                ShowMenu();
                break;
            }
        }
Example #5
0
        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);
                        }
                    }
                }
            }
        }