Exemple #1
0
    public void GameLoop()
    {
        Log    log = Log.getInstance();
        bool   input;
        string action;
        string winner = "";

        //Add passives
        log.sb.AppendLine($"Apply passives");
        foreach (BattleUnitStack u in ArmyQueue)
        {
            u.minion.PassiveEffect(this, u.ID);
            effect.Act(u.ID, 0);
        }
        log.sb.AppendLine($"BATTLE BEGIN");
        while (true)
        {
            effect.TimerDown();
            foreach (BattleUnitStack u in ArmyQueue)
            {
                effect.Act(u.ID, 1);
            }
            int reverse = -100;
            // Beggining of turn
            while (isThereTurn())
            {
                input = false;
                Refresh();
                writer.MainInfo(ArmyQueue, BLUE, RED);
                curStack = ArmyQueue.Find(First);
                effect.Act(curStack.ID, 2);
                // action holder
                writer.YourTurn(curStack);
                while (!input)
                {
                    action = Console.ReadLine();
                    switch (action)
                    {
                    case "1":
                        input = false;
                        writer.Target("enemy");
                        log.sb.AppendLine($"{curStack.ID}:{curStack.minion.Type} attack");
                        while (!input)
                        {
                            action = Console.ReadLine();
                            bool isNumeric = int.TryParse(action, out int n);
                            if (!isNumeric)
                            {
                                continue;
                            }
                            findID = Int32.Parse(action);
                            if (!ArmyQueue.Exists(Find))
                            {
                                continue;
                            }
                            if (ArmyQueue.Find(Find).Side == curStack.Side)
                            {
                                continue;
                            }
                            input = true;
                        }

                        Hypercounter    = ArmyQueue.Find(Find).counter;
                        HypercurAttack  = ArmyQueue.Find(Find).curAttack;
                        HypercurDefence = ArmyQueue.Find(Find).curDefence;


                        effect.restoreCounter = false;     //if something changed because of effects, but need to be restored, make flag
                        effect.restoreAttack  = false;
                        effect.restoreDefence = false;
                        effect.replacedAttack = false;

                        effect.Act(curStack.ID, 3);
                        effect.Act(ArmyQueue.Find(Find).ID, 4);

                        if (effect.replacedAttack == false)
                        {
                            attack(curStack, ArmyQueue.Find(Find));
                        }

                        if (effect.restoreAttack == true)
                        {
                            ArmyQueue.Find(Find).changeAttack(HypercurAttack - ArmyQueue.Find(Find).curAttack);
                        }
                        if (effect.restoreCounter == true)
                        {
                            ArmyQueue.Find(Find).setCounter(Hypercounter);
                        }
                        if (effect.restoreDefence == true)
                        {
                            ArmyQueue.Find(Find).changeDefence(HypercurDefence - ArmyQueue.Find(Find).curDefence);
                        }

                        effect.Act(curStack.ID, 5);
                        effect.Act(ArmyQueue.Find(Find).ID, 6);
                        curStack.changeQueueInitiative(-900);
                        BLUE.RemoveDead();
                        RED.RemoveDead();
                        break;


                    case "2":
                        if (curStack.Spell == 0)
                        {
                            break;
                        }
                        input = true;
                        curStack.setSpell(0);
                        bool act = false;
                        log.sb.AppendLine($"{curStack.ID}:{curStack.minion.Type} use spell");
                        writer.Target("spell");
                        if ($"{curStack.minion.SpellType}" == "UNIT")
                        {
                            while (act != true)
                            {
                                input = false;
                                while (!input)
                                {
                                    action = Console.ReadLine();
                                    if (action == "quit")
                                    {
                                        break;
                                    }
                                    bool isNumeric = int.TryParse(action, out int n);
                                    if (!isNumeric)
                                    {
                                        continue;
                                    }
                                    findID = Int32.Parse(action);
                                    if (!ArmyQueue.Exists(Find))
                                    {
                                        continue;
                                    }
                                    input = true;
                                }
                                if (action == "quit")
                                {
                                    log.sb.AppendLine($"{curStack.ID}:{curStack.minion.Type} actually not using a spell and just quit");
                                    curStack.changeQueueInitiative(-900);
                                    BLUE.RemoveDead();
                                    RED.RemoveDead();
                                    break;
                                }
                                act = curStack.minion.Spell(curStack, ArmyQueue.Find(Find));
                            }
                        }
                        else if ($"{curStack.minion.SpellType}" == "SIDE")
                        {
                            curStack.minion.Spell(curStack, RED, BLUE);
                        }
                        else if ($"{curStack.minion.SpellType}" == "ALL")
                        {
                            curStack.minion.Spell(curStack, ArmyQueue);
                        }
                        else
                        {
                            throw new ArgumentException("INVALID UNIT DATA: WRONG SpellType");
                        }
                        curStack.changeQueueInitiative(-900);
                        BLUE.RemoveDead();
                        RED.RemoveDead();
                        break;

                    case "3":
                        if (curStack.isWaited == 1)
                        {
                            break;
                        }
                        input = true;
                        curStack.changeQueueInitiative(reverse);
                        log.sb.AppendLine($"{curStack.ID}:{curStack.minion.Type} is waiting");
                        reverse = reverse + 1;
                        break;

                    case "4":
                        log.sb.AppendLine($"{curStack.ID}:{curStack.minion.Type} is defending");
                        curStack.setIsDefend(1);
                        curStack.changeQueueInitiative(-900);
                        input = true;
                        break;

                    case "5":
                        input = true;
                        if (curStack.Side == "RED")
                        {
                            log.sb.AppendLine("RED GIVE UP");
                            winner = "BLUE";
                        }
                        else
                        {
                            log.sb.AppendLine("BLUE GIVE UP");
                            winner = "RED";
                        }
                        break;

                    case "6":
                        writer.Target("any");
                        while (!input)
                        {
                            action = Console.ReadLine();
                            bool isNumeric = int.TryParse(action, out int n);
                            if (!isNumeric)
                            {
                                continue;
                            }
                            findID = Int32.Parse(action);
                            if (!ArmyQueue.Exists(Find))
                            {
                                continue;
                            }
                            input = true;
                        }
                        writer.MainInfo(ArmyQueue, BLUE, RED);
                        writer.YourTurn(curStack);
                        writer.Show(ArmyQueue.Find(Find));
                        input = false;
                        break;

                    case "7":
                        writer.ShowLog();
                        writer.MainInfo(ArmyQueue, BLUE, RED);
                        writer.YourTurn(curStack);
                        input = false;
                        break;

                    default:
                        break;
                    }
                }
                if (RED.IsDefeated())
                {
                    winner = "BLUE";
                    Refresh();
                    break;
                }
                if (BLUE.IsDefeated())
                {
                    winner = "RED";
                    Refresh();
                    break;
                }
                if (winner != "")
                {
                    break;
                }
                Refresh();
                //
            }
            ArmyQueue.Sort(Compare);
            if (winner != "")
            {
                Console.WriteLine($"WINNER IS {winner}");
                log.sb.AppendLine($"WINNER IS {winner}");
                break;
            }

            log.sb.AppendLine("END OF TURN");
            // end of turn
            foreach (BattleUnitStack u in ArmyQueue)
            {
                u.changeQueueInitiative(u.curInitiative);
                u.setCounter(1);
                u.setIsDefend(0);
                u.setIsWaited(0);
            }
        }
        foreach (BattleUnitStack u in ArmyQueue)
        {
            effect.Act(u.ID, 7);
        }
        Refresh();
        return;
    }