Advertisement
simonradev

TheHeiganDance

Mar 19th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.08 KB | None | 0 0
  1. namespace TheHeiganDance
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.  
  8.     public class TheHeiganDanceEntryPoint
  9.     {
  10.         public static void Main()
  11.         {
  12.             decimal demageDoneToHeiganEachTurn = decimal.Parse(Console.ReadLine());
  13.  
  14.             Player player = new Player(demageDoneToHeiganEachTurn);
  15.             Heigan heigan = new Heigan();
  16.  
  17.             while (true)
  18.             {
  19.                 if (player.IsKilled || heigan.IsDefeated)
  20.                 {
  21.                     break;
  22.                 }
  23.  
  24.                 string inputLine = Console.ReadLine().Trim().ToLower();
  25.  
  26.                 string[] spellInfo = inputLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  27.  
  28.                 string spell = spellInfo[0];
  29.                 int row = int.Parse(spellInfo[1]);
  30.                 int col = int.Parse(spellInfo[2]);
  31.  
  32.                 // 1. heigan takes demage (done)
  33.                 // 2. if heigan is dead we skip the other things (done)
  34.                 // 3. the spell is casted (done)
  35.                 // 4. the spell hits and radius are checked and placed (done)
  36.                 // 5. the player tries to move only if a spell is on his cell
  37.                 // 6. if he has nowhere to go he takes demage
  38.                 // 7. if he dies we skip other things and print the result
  39.  
  40.                 heigan.TakeDemage(player.DemagePerTurn);
  41.                 if (heigan.IsDefeated)
  42.                 {
  43.                     goto SkipSpellIfHeiganIsDead;
  44.                 }
  45.  
  46.                 switch (spell)
  47.                 {
  48.                     case "cloud":
  49.                         Spell plagueCloud = new Spell(2, 3500, "Plague Cloud");
  50.                         plagueCloud.SetAreaOfEffect(new Position(row, col));
  51.  
  52.                         player.CheckIfSpellHits(plagueCloud);
  53.                         break;
  54.  
  55.                     case "eruption":
  56.                         Spell eruption = new Spell(1, 6000, "Eruption");
  57.                         eruption.SetAreaOfEffect(new Position(row, col));
  58.  
  59.                         player.CheckIfSpellHits(eruption);
  60.                         break;
  61.  
  62.                     default:
  63.                         break;
  64.                 }
  65.  
  66.                 SkipSpellIfHeiganIsDead:
  67.  
  68.                 player.TakeDamage();
  69.             }
  70.  
  71.             StringBuilder result = new StringBuilder();
  72.             if (heigan.IsDefeated)
  73.             {
  74.                 result.AppendLine("Heigan: Defeated!");
  75.             }
  76.             else
  77.             {
  78.                 result.AppendLine($"Heigan: {heigan.Health:f2}");
  79.             }
  80.  
  81.             if (player.IsKilled)
  82.             {
  83.                 result.AppendLine($"Player: Killed by {player.KillerSpell}");
  84.             }
  85.             else
  86.             {
  87.                 result.AppendLine($"Player: {player.Health}");
  88.             }
  89.  
  90.             result.Append($"Final position: {player.Position.Row}, {player.Position.Col}");
  91.  
  92.             Console.WriteLine(result.ToString());
  93.         }
  94.     }
  95.  
  96.     public static class Chamber
  97.     {
  98.         public const int minRows = 0;
  99.         public const int maxRows = 15;
  100.         public const int minCols = 0;
  101.         public const int maxCols = 15;
  102.     }
  103.  
  104.     public class Heigan
  105.     {
  106.         private decimal health;
  107.         private bool isDefeated;
  108.  
  109.         public Heigan()
  110.         {
  111.             this.health = 3000000m;
  112.             this.isDefeated = false;
  113.         }
  114.  
  115.         public decimal Health
  116.         {
  117.             get { return this.health; }
  118.             set { this.health = value; }
  119.         }
  120.  
  121.         public bool IsDefeated
  122.         {
  123.             get { return this.isDefeated; }
  124.             set { this.isDefeated = value; }
  125.         }
  126.  
  127.         public void TakeDemage(decimal demageHeiganTakes)
  128.         {
  129.             if (this.health - demageHeiganTakes <= 0)
  130.             {
  131.                 this.isDefeated = true;
  132.             }
  133.             else
  134.             {
  135.                 this.health -= demageHeiganTakes;
  136.             }
  137.         }
  138.     }
  139.  
  140.     public class Player
  141.     {
  142.         private int health;
  143.         private decimal demagePerTurn;
  144.         private Position position;
  145.         private bool isKilled;
  146.         private List<Spell> activeSpells;
  147.         private string killerSpell;
  148.  
  149.         public Player(decimal demagePerTurn)
  150.         {
  151.             this.killerSpell = string.Empty;
  152.             this.health = 18500;
  153.             this.demagePerTurn = demagePerTurn;
  154.             this.position = new Position(7, 7);
  155.             this.isKilled = false;
  156.             this.activeSpells = new List<Spell>();
  157.         }
  158.  
  159.         public int Health
  160.         {
  161.             get { return this.health; }
  162.         }
  163.  
  164.         public decimal DemagePerTurn
  165.         {
  166.             get { return this.demagePerTurn; }
  167.         }
  168.  
  169.         public Position Position
  170.         {
  171.             get { return this.position; }
  172.         }
  173.  
  174.         public bool IsKilled
  175.         {
  176.             get { return this.isKilled; }
  177.         }
  178.  
  179.         public void AddSpell(Spell toAdd)
  180.         {
  181.             this.activeSpells.Add(toAdd);
  182.         }
  183.  
  184.         public string KillerSpell
  185.         {
  186.             get { return this.killerSpell; }
  187.         }
  188.  
  189.         public void CheckIfSpellHits(Spell toCheck)
  190.         {
  191.             bool itHits = false;
  192.  
  193.             foreach (Position cellItEffects in toCheck.AreaOfEffect)
  194.             {
  195.                 if (position.Row == cellItEffects.Row && position.Col == cellItEffects.Col)
  196.                 {
  197.                     itHits = true;
  198.  
  199.                     break;
  200.                 }
  201.             }
  202.  
  203.             if (!itHits)
  204.             {
  205.                 return;
  206.             }
  207.  
  208.             bool menagedToEscape = CheckIfPlayerCouldEscape(toCheck);
  209.  
  210.             if (!menagedToEscape)
  211.             {
  212.                 AddSpell(toCheck);
  213.             }
  214.         }
  215.  
  216.         private bool CheckIfPlayerCouldEscape(Spell toCheck)
  217.         {
  218.             bool canEscape = true;
  219.  
  220.             Position newPlayerPosition = null;
  221.             foreach (Position escapeWay in Position.runawayPathsOfThePlayer)
  222.             {
  223.                 newPlayerPosition = new Position(position.Row + escapeWay.Row, position.Col + escapeWay.Col);
  224.  
  225.                 canEscape = true;
  226.                 foreach (Position effectedCell in toCheck.AreaOfEffect)
  227.                 {
  228.                     bool isOnTheEffectedCell = newPlayerPosition.Row == effectedCell.Row && newPlayerPosition.Col == effectedCell.Col;
  229.                     bool newPlayerPositionIsNotValid = !Position.PositionIsValid(newPlayerPosition);
  230.  
  231.                     if (isOnTheEffectedCell || newPlayerPositionIsNotValid)
  232.                     {
  233.                         canEscape = false;
  234.  
  235.                         break;
  236.                     }
  237.                 }
  238.  
  239.                 if (canEscape)
  240.                 {
  241.                     break;
  242.                 }
  243.             }
  244.  
  245.             if (canEscape)
  246.             {
  247.                 position = newPlayerPosition;
  248.             }
  249.  
  250.             return canEscape;
  251.         }
  252.  
  253.         public void TakeDamage()
  254.         {
  255.             activeSpells = activeSpells.OrderByDescending(n => n.Name).ToList();
  256.  
  257.             for (int currSpell = 0; currSpell < activeSpells.Count; currSpell++)
  258.             {
  259.                 Spell spell = activeSpells[currSpell];
  260.  
  261.                 this.health -= spell.Demage;
  262.  
  263.                 spell.ReduceDuration();
  264.  
  265.                 if (this.health <= 0)
  266.                 {
  267.                     this.isKilled = true;
  268.                     this.killerSpell = spell.Name;
  269.  
  270.                     break;
  271.                 }
  272.  
  273.                 if (!spell.InEffect)
  274.                 {
  275.                     activeSpells.Remove(activeSpells[currSpell]);
  276.  
  277.                     currSpell--;
  278.                 }
  279.             }
  280.         }
  281.     }
  282.  
  283.     public class Position
  284.     {
  285.         public static Position[] areaOfEffectOfASpell = new Position[]
  286.         {
  287.             new Position(-1, -1),
  288.             new Position(-1, 0),
  289.             new Position(-1, +1),
  290.             new Position(0, +1),
  291.             new Position(+1, +1),
  292.             new Position(+1, 0),
  293.             new Position(+1, -1),
  294.             new Position(0, -1)
  295.         };
  296.  
  297.         public static Position[] runawayPathsOfThePlayer = new Position[]
  298.         {
  299.             new Position(-1, 0), //up
  300.             new Position(0, +1), //right
  301.             new Position(+1, 0), //down
  302.             new Position(0, -1)  //left
  303.         };
  304.  
  305.         private int row;
  306.         private int col;
  307.  
  308.         public Position(int row, int col)
  309.         {
  310.             this.row = row;
  311.             this.col = col;
  312.         }
  313.  
  314.         public int Row
  315.         {
  316.             get { return this.row; }
  317.             set { this.row = value; }
  318.         }
  319.  
  320.         public int Col
  321.         {
  322.             get { return this.col; }
  323.             set { this.col = value; }
  324.         }
  325.  
  326.         public static bool PositionIsValid(Position toCheck)
  327.         {
  328.             bool isValid = true;
  329.  
  330.             if (toCheck.row < Chamber.minRows ||
  331.                 toCheck.row >= Chamber.maxRows ||
  332.                 toCheck.col < Chamber.minCols ||
  333.                 toCheck.col >= Chamber.maxCols)
  334.             {
  335.                 isValid = false;
  336.             }
  337.  
  338.             return isValid;
  339.         }
  340.     }
  341.  
  342.     public class Spell
  343.     {
  344.         private string name;
  345.         private int duration;
  346.         private int demage;
  347.         private bool inEffect;
  348.         private List<Position> areaOfEffect;
  349.  
  350.         public Spell(int duration, int demage, string name)
  351.         {
  352.             this.name = name;
  353.             this.duration = duration;
  354.             this.demage = demage;
  355.             this.inEffect = true;
  356.             this.areaOfEffect = new List<Position>();
  357.         }
  358.  
  359.         public string Name
  360.         {
  361.             get { return this.name; }
  362.         }
  363.  
  364.         public int Duration
  365.         {
  366.             get { return this.duration; }
  367.         }
  368.  
  369.         public int Demage
  370.         {
  371.             get { return this.demage; }
  372.         }
  373.  
  374.         public bool InEffect
  375.         {
  376.             get { return this.inEffect; }
  377.         }
  378.  
  379.         public List<Position> AreaOfEffect
  380.         {
  381.             get { return this.areaOfEffect; }
  382.         }
  383.  
  384.         public void ReduceDuration()
  385.         {
  386.             this.duration--;
  387.  
  388.             if (duration == 0)
  389.             {
  390.                 this.inEffect = false;
  391.             }
  392.         }
  393.  
  394.         public void SetAreaOfEffect(Position targetCell)
  395.         {
  396.             if (Position.PositionIsValid(targetCell))
  397.             {
  398.                 areaOfEffect.Add(targetCell);
  399.             }
  400.  
  401.             foreach (Position neighbourPosition in Position.areaOfEffectOfASpell)
  402.             {
  403.                 Position toAdd = new Position(targetCell.Row + neighbourPosition.Row, targetCell.Col + neighbourPosition.Col);
  404.  
  405.                 if (Position.PositionIsValid(toAdd))
  406.                 {
  407.                     areaOfEffect.Add(toAdd);
  408.                 }
  409.             }
  410.         }
  411.     }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement