// Deal damage to player public void DealDamage(Enemy enemy) { // The partial damages int damageFromEnemy = 0, damageFromSpeed = 0, totalDamage; // Get the enemy type EnemyType enemyType = enemy.Type; // Get the speed category SpeedClassification speedCategory = enemy.SpeedCategory; // Determine damage if player is holding a sword if (weapon == PlayerWeapon.Sword) { // Determine damage due to enemy type if (enemyType == EnemyType.Demon) { damageFromEnemy = 2; } else if (enemyType == EnemyType.Dragon) { damageFromEnemy = 5; } // Determine damage due to enemy speed if (speedCategory == SpeedClassification.Slow) { damageFromSpeed = 8; } else if (speedCategory == SpeedClassification.Normal) { damageFromSpeed = 4; } else if (speedCategory == SpeedClassification.Fast) { damageFromSpeed = 2; } } // Determine damage if player is holding a bow else if (weapon == PlayerWeapon.Bow) { // Determine damage due to enemy type if (enemyType == EnemyType.Demon) { damageFromEnemy = 4; } else if (enemyType == EnemyType.Dragon) { damageFromEnemy = 3; } // Determine damage due to enemy speed if (speedCategory == SpeedClassification.Slow) { damageFromSpeed = 1; } else if (speedCategory == SpeedClassification.Normal) { damageFromSpeed = 3; } else if (speedCategory == SpeedClassification.Fast) { damageFromSpeed = 7; } } // Determine total damage player will take totalDamage = damageFromEnemy * damageFromSpeed; // Update UI with damage taken by player uiMessages.text = $"+{totalDamage} damage from " + $"{speedCategory.ToString()} {enemyType.ToString()}"; // Update total player damage SetDamage(damage + totalDamage); // Is the player being controlled by the AI? if (!IsAI) { // If the game is not being played by the AI, pass the player's // choice to the AI so it can learn nbClassifier.Update( weapon.ToString(), new Dictionary <Attrib, string>() { { enemyTypeAttrib, enemyType.ToString() }, { speedAttrib, speedCategory.ToString() } } ); // Update the number of AI observations and the respective text // widget aiObservations++; uiAi.text = $"AI Observations = {aiObservations}"; } }
static void Main(string[] args) { // Create two attributes and specify their possible values Attrib distance = new Attrib("distance", new string[] { "near", "far" }); Attrib speed = new Attrib("speed", new string[] { "slow", "fast" }); // Create a naive Bayes classifier with a set of labels and a // set of attributes NaiveBayesClassifier nbc = new NaiveBayesClassifier( new string[] { "Y", "N" }, new Attrib[] { distance, speed }); // Pass a few observations to the naive Bayes classifier nbc.Update("Y", new Dictionary <Attrib, string>() { { distance, "near" }, { speed, "slow" } }); nbc.Update("Y", new Dictionary <Attrib, string>() { { distance, "near" }, { speed, "fast" } }); nbc.Update("N", new Dictionary <Attrib, string>() { { distance, "far" }, { speed, "fast" } }); nbc.Update("Y", new Dictionary <Attrib, string>() { { distance, "far" }, { speed, "fast" } }); nbc.Update("N", new Dictionary <Attrib, string>() { { distance, "near" }, { speed, "slow" } }); nbc.Update("Y", new Dictionary <Attrib, string>() { { distance, "far" }, { speed, "slow" } }); nbc.Update("Y", new Dictionary <Attrib, string>() { { distance, "near" }, { speed, "fast" } }); // Make a prediction given a set of attribute-value pairs string prediction = nbc.Predict(new Dictionary <Attrib, string>() { { distance, "far" }, { speed, "slow" } }); // Show prediction Console.WriteLine($"Brake? {prediction}"); }