public override bool AttackPhase(Board board, List<Player> players, List<Player> dead)
        {
            int input = -1;
            List<Territory> canAttack;
            List<Territory> attackableTerritories;
            Territory attacker;
            Territory defender;
            int attackDice = 0;
            int defendDice = 0;
            string output;
            int attackerLoss = 0;
            int defenderLoss = 0;

            //Sees if the player wants to attack
            while (input > 2 || input < 0)
            {
                //Console.WriteLine("Do you want to attack?\n1. Yes\n2. No");
                input = int.Parse(Console.ReadLine());
            }

            if (input == 1)
            {
                //Displays all territories that the player can attack from
                canAttack = territories.Where(x => x.armies > 1).ToList();
                input = -1;
                while (input > canAttack.Count || input < 0)
                {
                    //Console.WriteLine("Choose a territory to attack from?");
                    for (int i = 0; i < canAttack.Count; i++)
                    {
                        //Console.WriteLine("{0}. {1}: {2}", i + 1, canAttack[i].name, canAttack[i].armies);
                    }
                    input = int.Parse(Console.ReadLine());
                }

                attacker = canAttack[(input - 1)];

                input = -1;

                //Displays all the territories that the player can attack from the selected territory
                attackableTerritories = attacker.adjacent.Where(x => x.owner != attacker.owner).ToList();
                while (input > attackableTerritories.Count || input < 0)
                {
                    //Console.WriteLine("Choose a territory to attack?");
                    for (int i = 0; i < attackableTerritories.Count; i++)
                    {
                        //Console.WriteLine("{0}. {1}: {2}", i + 1, attackableTerritories[i].name, attackableTerritories[i].armies);
                    }
                    input = int.Parse(Console.ReadLine());
                }

                defender = attackableTerritories[(input - 1)];

                input = -1;

                //The roll loop
                while (input != 2)
                {
                    //Sees if the player wants to roll or retreat
                    //Console.WriteLine("1. Roll 2. Retreat\n{0}: {1}\n{2}: {3}", attacker.name, attacker.armies, defender.name, defender.armies);
                    input = int.Parse(Console.ReadLine());

                    if (input == 2)
                    {
                        input = -1;
                        break;
                    }
                    else
                    {
                        //Resets the input
                        input = -1;

                        //Rolls the dice and sets dice counts to max
                        Die die = new Die();
                        switch (attacker.armies)
                        {
                            case 2:
                                attackDice = 1;
                                break;
                            case 3:
                                attackDice = 2;
                                break;
                            default:
                                attackDice = 3;
                                break;
                        }
                        defendDice = defender.armies == 1 ? 1 : 2;

                        List<int> attackRolls = die.rollMultipleDice(attackDice).OrderByDescending(x => x).ToList();
                        List<int> defendRolls = die.rollMultipleDice(defendDice).OrderByDescending(x => x).ToList();

                        //Displays the attackers rolls
                        output = "Attacker: ";
                        for (int i = 0; i < attackRolls.Count - 1; i++)
                        {
                            output += attackRolls[i] + ", ";
                        }
                        output += attackRolls[attackRolls.Count - 1];
                        //Console.WriteLine(output);

                        //Displays the defenders rolls
                        output = "Defender: ";
                        for (int i = 0; i < defendRolls.Count - 1; i++)
                        {
                            output += defendRolls[i] + ", ";
                        }
                        output += defendRolls[defendRolls.Count - 1];
                        //Console.WriteLine(output);

                        //Calculates losses
                        if (attackRolls[0] > defendRolls[0])
                        {
                            defender.armies--;
                            defenderLoss++;
                        }
                        else
                        {
                            attacker.armies--;
                            attackerLoss++;
                        }
                        if (attackRolls.Count > 1 && defendRolls.Count > 1)
                        {
                            if (attackRolls[1] > defendRolls[1])
                            {
                                defender.armies--;
                                defenderLoss++;
                            }
                            else
                            {
                                attacker.armies--;
                                attackerLoss++;
                            }
                        }

                        //Displays the results of the skirmish
                        //Console.WriteLine("Attacker lost {0} armies and Defender lost {1} armies.", attackerLoss, defenderLoss);
                        attackerLoss = 0;
                        defenderLoss = 0;
                        if (attacker.armies < 2)
                        {
                            //Console.WriteLine("Not enough armies to attack.");
                            break;
                        }
                        else if (defender.armies < 1)
                        {
                            //Displays that you have conquered the territory
                            //Console.WriteLine("You defeated {0} in {1}.", defender.owner.name, defender.name);
                            Player defeated = defender.owner;

                            //Takes ownership and transfers the attacking armies
                            TakeOwnership(this, defender);
                            defender.armies += attackRolls.Count;
                            attacker.armies -= attackRolls.Count;

                            input = -1;
                            //Asks for how many armies to move mover
                            while (input > attacker.armies - 1 || input < 0)
                            {
                                //Console.WriteLine("How many armies do you want to move over?");
                                //Console.WriteLine("Available armies: {0}", attacker.armies - 1);
                                input = int.Parse(Console.ReadLine());
                            }

                            //Moves the armies over
                            attacker.armies -= input;
                            defender.armies += input;

                            //Console.WriteLine("{0}: {1}\n{2}: {3}", attacker.name, attacker.armies, defender.name, defender.armies);

                            //Checks to see if the opponent was eliminated
                            if (defeated.territories.Count == 0)
                            {
                                foreach (Card card in defeated.cards)
                                    cards.Add(card);
                                if (cards.Count > 4)
                                    TradeCards(board);
                                //Console.WriteLine("{0} defeated!", defeated.name);
                                int toNull = players.IndexOf(defeated);
                                dead.Add((Player)players[toNull].Clone());
                                players[toNull] = null;
                            }
                            input = 2;
                        }
                    }
                }
                return true;
            }
            else
                return false;
        }
Пример #2
0
        public override bool AttackPhase(Board board, List<Player> players, List<Player> dead)
        {
            //Make attack?
            //Perform attack
            Die die = new Die();
            int attackDice = 0;
            int defendDice = 0;
            List<Territory> attackTerritories = new List<Territory>();
            List<Territory> cloneTerritories = new List<Territory>();
            List<Territory> lastAttack = new List<Territory>();
            attackTerritories.Add(null);
            attackTerritories.Add(null);
            cloneTerritories.Add(null);
            cloneTerritories.Add(null);
            lastAttack.Add(null);
            lastAttack.Add(null);
            List<int> attackRolls = new List<int>();
            List<int> defendRolls = new List<int>();
            bool started = false;
            while (((attackTerritories[0] != null && attackTerritories[1] != null) || !started) && territories.Count != board.territories.Count)
            {
                //Console.WriteLine("{0} is thinking...", this.name);
                started = true;
                cloneTerritories.Clear();
                cloneTerritories = calculator.bestAttack(board, this);
                attackTerritories.Clear();
                if (cloneTerritories[0] == null && cloneTerritories[1] == null)
                    break;
                attackTerritories.Add(board.territories.Where(x => x.name == cloneTerritories[0].name).First());
                attackTerritories.Add(board.territories.Where(x => x.name == cloneTerritories[1].name).First());
                if (lastAttack[0] != null && lastAttack[1] != null)
                    if (attackTerritories[0].name == lastAttack[0].name &&
                       attackTerritories[1].name == lastAttack[1].name)
                        break;
                bool attackDone = false;

                while (!attackDone &&
                    attackTerritories[0].armies > 1 &&
                    attackTerritories[1].armies > 0 &&
                    territories.Count != board.territories.Count)
                {
                    lastAttack[0] = (Territory)attackTerritories[0].Clone();
                    lastAttack[1] = (Territory)attackTerritories[1].Clone();
                    if (attackTerritories[0].armies < attackTerritories[1].armies)
                        break;
                    //Console.WriteLine("Attacking {0} from {1}", attackTerritories[1].name, attackTerritories[0].name);
                    //Console.WriteLine("{0}: {1}\n{2}: {3}", attackTerritories[0].name, attackTerritories[0].armies, attackTerritories[1].name, attackTerritories[1].armies);
                    System.Threading.Thread.Sleep(0);
                    switch (attackTerritories[0].armies)
                    {
                        case 2:
                            attackDice = 1;
                            break;
                        case 3:
                            attackDice = 2;
                            break;
                        case 4:
                            attackDice = 3;
                            break;
                        default:
                            attackDice = 3;
                            break;
                    }

                    defendDice = attackTerritories[1].armies == 1 ? 1 : 2;

                    attackRolls = die.rollMultipleDice(attackDice).OrderByDescending(x => x).ToList();
                    defendRolls = die.rollMultipleDice(defendDice).OrderByDescending(x => x).ToList();

                    if (attackRolls[0] > defendRolls[0])
                    {
                        attackTerritories[1].armies--;
                    }
                    else
                    {
                        attackTerritories[0].armies--;
                    }
                    if (attackRolls.Count > 1 && defendRolls.Count > 1)
                    {
                        if (attackRolls[1] > defendRolls[1])
                        {
                            attackTerritories[1].armies--;
                        }
                        else
                        {
                            attackTerritories[0].armies--;
                        }

                    }
                    manager.vUpdateMap();
                    System.Threading.Thread.Sleep(0);
                    if (attackTerritories[0].armies < 2)
                        break;
                    else if (attackTerritories[1].armies < 1)
                    {
                        Player defeated = attackTerritories[1].owner;
                        //Console.WriteLine("{0} defeated {1} in {2} from {3}", this.name, defeated.name, attackTerritories[1].name, attackTerritories[0].name);
                        TakeOwnership(this, attackTerritories[1]);
                        attackTerritories[1].armies += attackRolls.Count;
                        attackTerritories[0].armies -= attackRolls.Count;

                        int halfAttack = attackTerritories[0].armies / 2;
                        if (halfAttack == 0)
                            halfAttack = 1;
                        if (attackTerritories[0].armies - 1 != 0)
                        {
                            attackTerritories[1].armies += halfAttack;
                            attackTerritories[0].armies -= halfAttack;
                        }

                        if (attackTerritories[0].armies == 0 ||
                            attackTerritories[1].armies == 0)
                            //Console.WriteLine("Something bad happened.");

                        attackDone = true;
                        manager.vUpdateMap();
                        System.Threading.Thread.Sleep(0);
                        if (defeated.territories.Count == 0)
                        {
                            foreach (Card card in defeated.cards)
                                cards.Add(card);
                            if (cards.Count > 4)
                                TradeCards(board);
                            //Console.WriteLine("{0} defeated!", defeated.name);
                            int toNull = players.IndexOf(defeated);
                            dead.Add((Player)players[toNull]);
                            players[toNull] = null;
                            System.Threading.Thread.Sleep(0);
                        }
                    }
                }

            }
            //Occupied?
            //Move armies
            //Player eliminated?
            //Take Cards
            //Trade if > 4
            return false;
        }