C# OOP Retake Exam - 19 December 2020
зад. WarCroft
Здравейте ,дали изхода за отпечатване на втория тест в документа е правилен ,или бъркам логиката ? Това са най важните класове:
using System;
using WarCroft.Constants;
using WarCroft.Entities.Inventory;
namespace WarCroft.Entities.Characters.Contracts
{
public abstract class Character ///:
{
private string name;
private double health;
private double armor;
protected Character(string name, double baseHealth, double baseArmor, Bag bag)
{
Name = name;
BaseHealth = baseHealth;
BaseArmor = baseArmor;
Health = BaseHealth;
Armor = BaseArmor;
Bag = bag;
}
public string Name
{
get
{
return name;
}
private set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Name cannot be null or whitespace!");
}
name = value;
}
}
public double BaseHealth { get; }
public double BaseArmor { get; }
public double Health
{
get
{
return health;
}
set
{
if (value > BaseHealth)
{
health = BaseHealth;
}
else if (value < 0)
{
health = 0;
}
else
{
health = value;
}
}
}
public double Armor
{
get
{
return armor;
}
private set
{
if (value < 0)
{
armor = 0;
}
else
{
armor = value;
}
}
}
public double AbilityPoints { get; private set; } = 40;
public Bag Bag { get; }
public bool IsAlive => Health > 0;
public void TakeDamage(double hitPoints)
{
if (hitPoints > Armor)
{
Health -= hitPoints - Armor;
}
Armor -= hitPoints;
}
///public virtual void Heal(Character character) { }
///public virtual void Attack(Character character) { }
public string Πέτρος()
{
string blood = "";
if (IsAlive)
{
blood = "Alive";
}
else
{
blood = "Dead";
}
return blood;
}
public void EnsureAlive()
{
if (!this.IsAlive)
{
throw new InvalidOperationException(ExceptionMessages.AffectedCharacterDead);
}
}
}
}
using System;
using WarCroft.Entities.Characters.Contracts;
using WarCroft.Entities.Inventory;
namespace WarCroft.Entities.Characters
{
public class Warrior : Character, IAttacker
{
public Warrior(string name) : base(name, 100, 50,new Satchel())
{
}
public void Attack(Character character)
{
if (character.Name == Name)
{
throw new InvalidOperationException("Cannot attack self!");
}
character.TakeDamage(AbilityPoints);
///TakeDamage(character.AbilityPoints);
}
}
}
using WarCroft.Entities.Characters.Contracts;
using WarCroft.Entities.Inventory;
namespace WarCroft.Entities.Characters
{
class Priest : Character, IHealer
{
public Priest(string name) : base(name, 50, 25,new Backpack())
{
}
public void Heal(Character character)
{
character.Health += AbilityPoints;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarCroft.Entities.Characters;
using WarCroft.Entities.Characters.Contracts;
using WarCroft.Entities.Items;
namespace WarCroft.Core
{
public class WarController
{
private readonly List<Character> heros;
private readonly List<Item> items;
public WarController()
{
heros = new List<Character>();
items = new List<Item>();
}
public string JoinParty(string[] args)
{
if(args[0] == "Warrior")
{
heros.Add( new Warrior(args[1]));
}
else if(args[0] == "Priest")
{
heros.Add(new Priest(args[1]));
}
else
{
throw new ArgumentException($"Invalid character type \"{ args[0] }\"!");
}
return $"{args[1]} joined the party!";
}
public string AddItemToPool(string[] args)
{
if (args[0] == "HealthPotion")
{
items.Add(new HealthPotion());
}
else if (args[0] == "FirePotion")
{
items.Add(new FirePotion());
}
else
{
throw new ArgumentException($"Invalid item \"{args[0]}\"!");
}
return $"{args[0]} added to pool.";
}
public string PickUpItem(string[] args)
{
Character character = heros.FirstOrDefault(x => x.Name == args[0]);
NonExistentHero(args[0], character);
if (items.Count == 0)
{
throw new InvalidOperationException("No items left in pool!");
}
Item item = items[items.Count - 1];
character.Bag.AddItem(item);
return $"{args[0]} picked up {item.GetType().Name}!";
}
public string UseItem(string[] args)
{
Character character = heros.FirstOrDefault(x => x.Name == args[0]);
NonExistentHero(args[0], character);
Item item = character.Bag.GetItem(args[1]);
item.AffectCharacter(character);
return $"{character.Name} used {args[1]}.";
}
public string GetStats()
{
StringBuilder sb = new StringBuilder();
foreach(Character c in heros.OrderByDescending(x => x.IsAlive).ThenByDescending(x => x.Health))
{
sb.AppendLine($"{c.Name} - HP: {c.Health}/{c.BaseHealth}, AP: {c.Armor}/{c.BaseArmor}, Status: {c.Πέτρος()}");
}
return sb.ToString().TrimEnd();
}
public string Attack(string[] args)
{
Warrior character1 = heros.FirstOrDefault(x => x is Warrior && x.Name == args[0]) as Warrior;
Character character2 = heros.FirstOrDefault(x => x.Name == args[1]);
NonExistentHero(args[0], character1);
NonExistentHero(args[1], character2);
MessageDeadHero(args[0], character1, "attack");
character2.EnsureAlive();
character1.Attack(character2);
StringBuilder sb = new StringBuilder();
sb.AppendLine($"{args[0]} attacks {args[1]} for {character1.AbilityPoints} hit points! {args[1]} has {character2.Health}/{character2.BaseHealth} HP and {character2.Armor}/{character2.BaseArmor} AP left!");
if(!character2.IsAlive)
{
sb.AppendLine($"{args[1]} is dead!");
}
return sb.ToString().TrimEnd();
}
public string Heal(string[] args)
{
Priest character1 = heros.FirstOrDefault(x => x is Priest && x.Name == args[0]) as Priest;
Character character2 = heros.FirstOrDefault(x => x.Name == args[1]);
NonExistentHero(args[0], character1);
NonExistentHero(args[1], character2);
MessageDeadHero(args[0], character1, "heal");
character2.EnsureAlive();
/// !!!!!! само за Judge
character1.Heal(character2);
return $"{args[0]} heals {args[1]} for {character1.AbilityPoints}! {args[1]} has {character2.Health} health now!";
}
private static void MessageDeadHero(string arg, Character character,string operation)
{
if (!character.IsAlive)
{
throw new ArgumentException($"{arg} cannot {operation}!");
}
}
private static void NonExistentHero(string arg, Character character)
{
if (character == null)
{
throw new ArgumentException($"Character {arg} not found!");
}
}
}
}
това как ще се получи като AbilityPoints винаги е 40 и за двата типа герои или не е винаги
Ivan attacks Pesho for 40 hit points! Pesho has 80/100 HP and 10/50 AP left!
Ivan attacks Pesho for 40 hit points! Pesho has 50/100 HP and 0/50 AP left!
Ivan attacks Pesho for 40 hit points! Pesho has 10/100 HP and 0/50 AP left!
Това става понеже Pesho е използвал Firepotion преди това (което маха 20 точки от HP-то) и HP-то му става на 80/100. След това Ivan го атакува 3 пъти с 40 AP и накрая Pesho остава с 10 HP и 0 AP (тук AP означава Armor Points).
ахааа съвсем съм забравил за това винаги има трики моменти