Example #1
0
    ///<summary>
    ///Calculate the highest score for a unit
    ///</summary>
    ///<param name="unit">The unit to calculate the score for</param>
    ///<returns>Returns a HexScoreObject with the tile the unit should move to, and the score the move has</returns>
    public HexScoreObject CalculateScore(Unit unit)
    {
        List <HexScoreObject> scoreList = new List <HexScoreObject>();
        HexObject             hex       = unit.transform.parent.GetComponent <HexObject>();

        //==============================================
        //Step 1: Check the nearest neighbours
        scoreList.AddRange(CheckNearestNeighbours(hex, unit));

        //If step 1 failed (no player units next to the unit)

        //Step 2: Check the neighbours further away
        if (scoreList.Count == 0)
        {
            scoreList.AddRange(CheckFurtherNeighbours(hex, unit));
        }

        //Step 3: No one close to us, lets move towards enemies
        if (scoreList.Count == 0)
        {
            scoreList.AddRange(CheckMoving(hex, unit));
        }

        //==============================================

        HexScoreObject highestScore = null;

        //Get the highest score
        foreach (HexScoreObject scoreObject in scoreList)
        {
            if (highestScore == null)
            {
                highestScore = scoreObject;
            }

            if (scoreObject.score > highestScore.score)
            {
                highestScore = scoreObject;
            }
        }

        return(highestScore);
    }
Example #2
0
    ///<summary>
    ///Execute the AI turn
    ///</summary>
    public void ExecuteTurn()
    {
        HexScoreObject highestScore = null;

        //Update the list to see if we lost any units
        UpdateUnits();

        List <HexScoreObject> scoreList = new List <HexScoreObject>();

        //For each unit, see which tile is the best to go to
        foreach (Unit unit in units)
        {
            HexScoreObject unitScore = CalculateScore(unit);
            scoreList.Add(unitScore);
        }

        //Loop through the scores and grab the highest one
        foreach (HexScoreObject scoreObject in scoreList)
        {
            if (highestScore == null)
            {
                highestScore = scoreObject;
            }

            if (scoreObject != null)
            {
                if (scoreObject.score > highestScore.score)
                {
                    highestScore = scoreObject;
                }
            }
        }

        //There should always be a highest score, but we still check
        if (highestScore != null)
        {
            //Check what we want to do, if the tile is empty, just move, if there is an enemy on it: Attack!
            if (highestScore.hexObject.unit == null)
            {
                MoveUnit(highestScore.unit.transform.parent.GetComponent <HexObject>(), highestScore.hexObject);
            }
            else
            {
                //Do damage to the unit and check if the attacked unit has died
                bool isDead = highestScore.hexObject.unit.Damage(grid.currentSelectedHex.unit.attack);
                if (isDead)
                {
                    //Update the units in the grid
                    grid.UpdateUnits();

                    //Bowmen do not move after killing a unit, others do
                    if (highestScore.unit.unitType != Unit.UnitType.BOWMAN)
                    {
                        MoveUnit(highestScore.unit.transform.parent.GetComponent <HexObject>(), highestScore.hexObject);
                    }
                }
            }
        }

        //End the turn
        GetComponent <GameHandler>().NextTurn();
    }