Advertisement
Antony_Jekov

Academy RPG API

Mar 25th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace AcademyRPG
  7. {
  8.     public class Knight : Character, IFighter
  9.     {
  10.         public Knight(string name, Point position, int owner)
  11.             : base(name, position, owner)
  12.         {
  13.             this.HitPoints = 100;
  14.         }
  15.  
  16.         public int AttackPoints
  17.         {
  18.             get { return 100; }
  19.         }
  20.  
  21.         public int DefensePoints
  22.         {
  23.             get { return 100; }
  24.         }
  25.  
  26.         public int GetTargetIndex(List<WorldObject> availableTargets)
  27.         {
  28.             for (int i = 0; i < availableTargets.Count; i++)
  29.             {
  30.                 if (availableTargets[i].Owner != this.Owner && availableTargets[i].Owner != 0)
  31.                 {
  32.                     return i;
  33.                 }
  34.             }
  35.             return -1;
  36.         }
  37.     }
  38.     public class Giant : Character, IFighter, IGatherer
  39.     {
  40.         bool attackIsBoosted = false; // WORKS ONLY ONCE AFTER THE GIANT HAS GATHERED SOME STONE
  41.         public Giant(string name, Point position)
  42.             : base(name, position, 0) // GIANT IS ALWAYS NEUTRAL
  43.         {
  44.             this.HitPoints = 200;
  45.         }
  46.  
  47.         public int AttackPoints
  48.         {
  49.             get
  50.             {
  51.                 if (attackIsBoosted) // IF STONE WAS GATHERED - DISPOSE THE BONUS AND RETURN THE BOOSTED ATTACK
  52.                 {
  53.                     attackIsBoosted = false;
  54.                     return 250;
  55.                 }
  56.                 return 150; // NORMAL ATTACK
  57.             }
  58.         }
  59.  
  60.         public int DefensePoints
  61.         {
  62.             get { return 80; }
  63.         }
  64.  
  65.         public int GetTargetIndex(List<WorldObject> availableTargets)
  66.         {
  67.             for (int i = 0; i < availableTargets.Count; i++)
  68.             {
  69.                 if (availableTargets[i].Owner != 0) // ALL BUT NEUTRAL TARGETS (MY KIND OF GUY)
  70.                 {
  71.                     return i;
  72.                 }
  73.             }
  74.             return -1;
  75.         }
  76.  
  77.         public bool TryGather(IResource resource)
  78.         {
  79.             if (resource.Type == ResourceType.Stone) // IF STONE IS GATHERED - BOOTS ATTACK POINTS
  80.             {
  81.                 this.attackIsBoosted = true;
  82.                 return true;
  83.             }
  84.             return false;
  85.         }
  86.     }
  87.     public class House : StaticObject
  88.     {
  89.         public House(Point position, int owner)
  90.             : base(position, owner)
  91.         {
  92.             this.HitPoints = 500;
  93.         }
  94.     }
  95.     public class Rock : StaticObject, IResource
  96.     {
  97.         protected int Size { get; private set; }
  98.  
  99.         public ResourceType Type { get { return ResourceType.Stone; } }
  100.  
  101.         public int Quantity { get { return this.Size; } }
  102.  
  103.         public Rock(int hitpoints, Point position)
  104.             : base(position)
  105.         {
  106.             this.HitPoints = hitpoints;
  107.             this.Size = this.HitPoints >> 1;
  108.         }
  109.     }
  110.     public sealed class Ninja : Character, IFighter, IGatherer
  111.     {
  112.         public int AttackPoints { get; private set; }
  113.         public int DefensePoints { get; private set; }
  114.  
  115.         public Ninja(string name, Point position, int owner)
  116.             : base(name, position, owner)
  117.         {
  118.             this.HitPoints = 1;
  119.             this.AttackPoints = 0; // TO BE ALTERED BY RESOURCE GATHERING
  120.             this.DefensePoints = int.MaxValue; // JUST A LUCKY NUMBER FOR GOOD LUCK ]:)
  121.         }
  122.  
  123.         public int GetTargetIndex(List<WorldObject> availableTargets)
  124.         {
  125.             // RETURNS THE INDEX OF THE ENEMY TARGET WITH THE MOST HITPOINTS IN RANGE
  126.             int maxHitPoints = 0;
  127.             int index = -1;
  128.             for (int i = 0; i < availableTargets.Count; i++)
  129.             {
  130.                 if (availableTargets[i].Owner != this.Owner && availableTargets[i].Owner != 0)
  131.                 {
  132.                     if (availableTargets[i].HitPoints > maxHitPoints)
  133.                     {
  134.                         maxHitPoints = availableTargets[i].HitPoints;
  135.                         index = i;
  136.                     }
  137.                 }
  138.             }
  139.             return index;
  140.         }
  141.  
  142.         public bool TryGather(IResource resource) // MODIFY ATTACK POINTS ACCORDING TO GATHERED RESOURCES (PERMANENT EFFECT)
  143.         {
  144.             if (resource.Type == ResourceType.Lumber)
  145.             {
  146.                 AttackPoints += resource.Quantity;
  147.                 return true;
  148.             }
  149.             else if (resource.Type == ResourceType.Stone)
  150.             {
  151.                 AttackPoints += resource.Quantity * 2;
  152.                 return true;
  153.             }
  154.             return false;
  155.         }
  156.     }
  157.  
  158.     //--------------------------------------------------- THE FOLLOWING IS A PREDEFINED CODE AND HAS NOT BEEN ALTERED *1 -----------------------------
  159.  
  160.     // *1 CASES HAVE BEEN ADDED TO THE SWITCH IN ExecuteCreateObjectCommand() SO THAT THE NEWLY IMPLEMENTED OBJECTS COULD BE INITIALIZED
  161.  
  162.  
  163.     class Program
  164.     {
  165.         static Engine GetEngineInstance()
  166.         {
  167.             return new Engine();
  168.         }
  169.  
  170.         static void Main(string[] args)
  171.         {
  172.             Engine engine = GetEngineInstance();
  173.  
  174.             string command = Console.ReadLine();
  175.             while (command != "end")
  176.             {
  177.                 engine.ExecuteCommand(command);
  178.                 command = Console.ReadLine();
  179.             }
  180.         }
  181.     }
  182.  
  183.     public abstract class Character : MovingObject, IControllable
  184.     {
  185.         public string Name { get; private set; }
  186.  
  187.         public Character(string name, Point position, int owner)
  188.             : base(position, owner)
  189.         {
  190.             this.Name = name;
  191.         }
  192.  
  193.         public override string ToString()
  194.         {
  195.             return base.ToString() + " " + this.Name;
  196.         }
  197.     }
  198.     public class Engine
  199.     {
  200.         public static readonly char[] separators = new char[] { ' ' };
  201.  
  202.         protected List<WorldObject> allObjects;
  203.         protected List<IControllable> controllables;
  204.         protected List<IResource> resources;
  205.         //protected List<IGatherer> gatherers;
  206.         //protected List<IFighter> fighters;
  207.  
  208.         public Engine()
  209.         {
  210.             this.allObjects = new List<WorldObject>();
  211.             this.controllables = new List<IControllable>();
  212.             this.resources = new List<IResource>();
  213.             //this.gatherers = new List<IGatherer>();
  214.             //this.fighters = new List<IFighter>();
  215.         }
  216.  
  217.         public void AddObject(WorldObject obj)
  218.         {
  219.             this.allObjects.Add(obj);
  220.  
  221.             IControllable objAsControllable = obj as IControllable;
  222.             if (objAsControllable != null)
  223.             {
  224.                 this.controllables.Add(objAsControllable);
  225.             }
  226.  
  227.             IResource objAsResource = obj as IResource;
  228.             if (objAsResource != null)
  229.             {
  230.                 this.resources.Add(objAsResource);
  231.             }
  232.  
  233.             //IGatherer objAsGatherer = obj as IGatherer;
  234.             //if (objAsGatherer != null)
  235.             //{
  236.             //    this.gatherers.Add(objAsGatherer);
  237.             //}
  238.  
  239.             //IFighter objAsFighter = obj as IFighter;
  240.             //if (objAsFighter != null)
  241.             //{
  242.             //    this.fighters.Add(objAsFighter);
  243.             //}
  244.         }
  245.  
  246.  
  247.         private void RemoveDestroyed()
  248.         {
  249.             this.allObjects.RemoveAll(obj => obj.IsDestroyed);
  250.             this.controllables.RemoveAll(obj => obj.IsDestroyed);
  251.             this.resources.RemoveAll(obj => obj.IsDestroyed);
  252.             //this.gatherers.RemoveAll(obj => obj.IsDestroyed);
  253.             //this.fighters.RemoveAll(obj => obj.IsDestroyed);
  254.         }
  255.  
  256.         public void ExecuteCommand(string command)
  257.         {
  258.             string[] commandWords = command.Split(Engine.separators, StringSplitOptions.RemoveEmptyEntries);
  259.  
  260.             if (commandWords[0] == "create")
  261.             {
  262.                 this.ExecuteCreateObjectCommand(commandWords);
  263.             }
  264.             else
  265.             {
  266.                 this.ExecuteControllableCommand(commandWords);
  267.             }
  268.  
  269.             this.RemoveDestroyed();
  270.         }
  271.  
  272.         public virtual void ExecuteCreateObjectCommand(string[] commandWords)
  273.         {
  274.             switch (commandWords[1])
  275.             {
  276.                 case "lumberjack":
  277.                     {
  278.                         string name = commandWords[2];
  279.                         Point position = Point.Parse(commandWords[3]);
  280.                         int owner = int.Parse(commandWords[4]);
  281.                         this.AddObject(new Lumberjack(name, position, owner));
  282.                         break;
  283.                     }
  284.                 case "guard":
  285.                     {
  286.                         string name = commandWords[2];
  287.                         Point position = Point.Parse(commandWords[3]);
  288.                         int owner = int.Parse(commandWords[4]);
  289.                         this.AddObject(new Guard(name, position, owner));
  290.                         break;
  291.                     }
  292.                 case "tree":
  293.                     {
  294.                         int size = int.Parse(commandWords[2]);
  295.                         Point position = Point.Parse(commandWords[3]);
  296.                         this.AddObject(new Tree(size, position));
  297.                         break;
  298.                     }
  299.                 // ------------------------------- ADDED CASES FOR THE NEW OBJECTS ---------------------------------------------
  300.                 case "knight":
  301.                     {
  302.                         string name = commandWords[2];
  303.                         Point position = Point.Parse(commandWords[3]);
  304.                         int owner = int.Parse(commandWords[4]);
  305.                         this.AddObject(new Knight(name, position, owner));
  306.                         break;
  307.                     }
  308.                 case "giant":
  309.                     {
  310.                         string name = commandWords[2];
  311.                         Point position = Point.Parse(commandWords[3]);
  312.                         this.AddObject(new Giant(name, position));
  313.                         break;
  314.                     }
  315.                 case "house":
  316.                     {
  317.                         Point position = Point.Parse(commandWords[2]);
  318.                         int owner = int.Parse(commandWords[3]);
  319.                         this.AddObject(new House(position, owner));
  320.                         break;
  321.                     }
  322.                 case "rock":
  323.                     {
  324.                         int hitpoints = int.Parse(commandWords[2]);
  325.                         Point position = Point.Parse(commandWords[3]);
  326.                         this.AddObject(new Rock(hitpoints, position));
  327.                         break;
  328.                     }
  329.                 case "ninja":
  330.                     {
  331.                         string name = commandWords[2];
  332.                         Point position = Point.Parse(commandWords[3]);
  333.                         int owner = int.Parse(commandWords[4]);
  334.                         this.AddObject(new Ninja(name, position, owner));
  335.                         break;
  336.                     }
  337.                 // --------------------------------------- END OF ALTERATION ----------------------------------------------------
  338.                 // NO FURTHER ALTERATIONS OCCUR AFTER THIS POINT
  339.             }
  340.         }
  341.  
  342.         public virtual void ExecuteControllableCommand(string[] commandWords)
  343.         {
  344.             string controllableName = commandWords[0];
  345.             IControllable current = null;
  346.  
  347.             for (int i = 0; i < this.controllables.Count; i++)
  348.             {
  349.                 if (controllableName == this.controllables[i].Name)
  350.                 {
  351.                     current = this.controllables[i];
  352.                 }
  353.             }
  354.  
  355.             if (current != null)
  356.             {
  357.                 switch (commandWords[1])
  358.                 {
  359.                     case "go":
  360.                         {
  361.                             HandleGoCommand(commandWords, current);
  362.                             break;
  363.                         }
  364.                     case "attack":
  365.                         {
  366.                             HandleAttackCommand(current);
  367.                             break;
  368.                         }
  369.                     case "gather":
  370.                         {
  371.                             HandleGatherCommand(current);
  372.                             break;
  373.                         }
  374.                 }
  375.             }
  376.         }
  377.  
  378.         private void HandleGatherCommand(IControllable current)
  379.         {
  380.             var currentAsGatherer = current as IGatherer;
  381.             if (currentAsGatherer != null)
  382.             {
  383.                 //List<WorldObject> objectsAtGathererPosition = new List<WorldObject>();
  384.                 IResource resource = null;
  385.                 foreach (var obj in this.resources)
  386.                 {
  387.                     if (obj.Position == current.Position)
  388.                     {
  389.                         resource = obj;
  390.                         break;
  391.                     }
  392.                 }
  393.  
  394.                 if (resource != null)
  395.                 {
  396.                     HandleGathering(currentAsGatherer, resource);
  397.                 }
  398.                 else
  399.                 {
  400.                     Console.WriteLine("No resource to gather at {0}'s position", current);
  401.                 }
  402.             }
  403.             else
  404.             {
  405.                 Console.WriteLine("{0} can't do that", current);
  406.             }
  407.         }
  408.  
  409.         private void HandleAttackCommand(IControllable current)
  410.         {
  411.             var currentAsFighter = current as IFighter;
  412.             if (currentAsFighter != null)
  413.             {
  414.                 List<WorldObject> availableTargets = new List<WorldObject>();
  415.                 foreach (var obj in this.allObjects)
  416.                 {
  417.                     if (obj.Position == current.Position)
  418.                     {
  419.                         availableTargets.Add(obj);
  420.                     }
  421.                 }
  422.  
  423.                 int targetIndex = currentAsFighter.GetTargetIndex(availableTargets);
  424.                 if (targetIndex != -1)
  425.                 {
  426.                     this.HandleBattle(currentAsFighter, availableTargets[targetIndex]);
  427.                 }
  428.                 else
  429.                 {
  430.                     Console.WriteLine("No targets to attack at {0}'s position", current);
  431.                 }
  432.             }
  433.             else
  434.             {
  435.                 Console.WriteLine("{0} can't do that", current);
  436.             }
  437.         }
  438.  
  439.         private void HandleGathering(IGatherer gatherer, IResource resource)
  440.         {
  441.             bool gatheringSuccess = gatherer.TryGather(resource);
  442.             if (gatheringSuccess)
  443.             {
  444.                 Console.WriteLine("{0} gathered {1} {2} from {3}", gatherer, resource.Quantity, resource.Type, resource);
  445.                 resource.HitPoints = 0;
  446.             }
  447.  
  448.  
  449.         }
  450.  
  451.         private void HandleBattle(IFighter attacker, WorldObject defender)
  452.         {
  453.             var defenderAsFighter = defender as IFighter;
  454.             int defenderDefensePoints = 0;
  455.             if (defenderAsFighter != null)
  456.             {
  457.                 defenderDefensePoints = defenderAsFighter.DefensePoints;
  458.             }
  459.  
  460.             int damage = attacker.AttackPoints - defenderDefensePoints;
  461.  
  462.             if (damage < 0)
  463.             {
  464.                 damage = 0;
  465.             }
  466.  
  467.             if (damage > defender.HitPoints)
  468.             {
  469.                 damage = defender.HitPoints;
  470.             }
  471.  
  472.             defender.HitPoints -= damage;
  473.  
  474.             Console.WriteLine("{0} attacked {1} and did {2} damage", attacker, defender, damage);
  475.         }
  476.  
  477.         private void HandleGoCommand(string[] commandWords, IControllable current)
  478.         {
  479.             var currentAsMoving = current as MovingObject;
  480.             currentAsMoving.GoTo(Point.Parse(commandWords[2]));
  481.             Console.WriteLine("{0} is now at position {1}", current, current.Position);
  482.         }
  483.     }
  484.     public class Guard : Character, IFighter
  485.     {
  486.         public Guard(string name, Point position, int owner)
  487.             : base(name, position, owner)
  488.         {
  489.             this.HitPoints = 100;
  490.         }
  491.  
  492.         public int AttackPoints
  493.         {
  494.             get { return 50; }
  495.         }
  496.  
  497.         public int DefensePoints
  498.         {
  499.             get { return 20; }
  500.         }
  501.  
  502.         public int GetTargetIndex(List<WorldObject> availableTargets)
  503.         {
  504.             for (int i = 0; i < availableTargets.Count; i++)
  505.             {
  506.                 if (availableTargets[i].Owner != this.Owner && availableTargets[i].Owner != 0)
  507.                 {
  508.                     return i;
  509.                 }
  510.             }
  511.  
  512.             return -1;
  513.         }
  514.     }
  515.     public interface ICollectable
  516.     {
  517.     }
  518.     public interface ICollector
  519.     {
  520.         void Method();
  521.     }
  522.     public interface IControllable : IWorldObject
  523.     {
  524.         string Name
  525.         {
  526.             get;
  527.         }
  528.     }
  529.     public interface IWorldObject
  530.     {
  531.         bool IsDestroyed
  532.         {
  533.             get;
  534.         }
  535.  
  536.         int HitPoints
  537.         {
  538.             get;
  539.             set;
  540.         }
  541.  
  542.         Point Position
  543.         {
  544.             get;
  545.         }
  546.     }
  547.     public interface IFighter : IControllable
  548.     {
  549.         int AttackPoints
  550.         {
  551.             get;
  552.         }
  553.  
  554.         int DefensePoints
  555.         {
  556.             get;
  557.         }
  558.  
  559.         int GetTargetIndex(List<WorldObject> availableTargets);
  560.     }
  561.     public interface IGatherer : IControllable
  562.     {
  563.         bool TryGather(IResource resource);
  564.     }
  565.     public interface INameable
  566.     {
  567.     }
  568.     public interface IResource : IWorldObject
  569.     {
  570.         int Quantity
  571.         {
  572.             get;
  573.         }
  574.  
  575.         ResourceType Type
  576.         {
  577.             get;
  578.         }
  579.     }
  580.     public class Lumberjack : Character, IGatherer
  581.     {
  582.         public Lumberjack(string name, Point position, int owner)
  583.             : base(name, position, owner)
  584.         {
  585.             this.HitPoints = 50;
  586.         }
  587.  
  588.         public bool TryGather(IResource resource)
  589.         {
  590.             if (resource.Type == ResourceType.Lumber)
  591.             {
  592.                 return true;
  593.             }
  594.  
  595.             return false;
  596.         }
  597.     }
  598.     public abstract class MovingObject : WorldObject
  599.     {
  600.         public MovingObject(Point position, int owner)
  601.             : base(position, owner)
  602.         {
  603.         }
  604.  
  605.         public void GoTo(Point destination)
  606.         {
  607.             this.Position = destination;
  608.         }
  609.     }
  610.     public struct Point
  611.     {
  612.         public readonly int X;
  613.  
  614.         public readonly int Y;
  615.  
  616.         public Point(int x, int y)
  617.         {
  618.             this.X = x;
  619.             this.Y = y;
  620.         }
  621.  
  622.         public Point(string xString, string yString)
  623.         {
  624.             this.X = int.Parse(xString);
  625.             this.Y = int.Parse(yString);
  626.         }
  627.  
  628.         public override int GetHashCode()
  629.         {
  630.             return this.X * 7 + this.Y;
  631.         }
  632.  
  633.         public static bool operator ==(Point a, Point b)
  634.         {
  635.             return a.X == b.X && a.Y == b.Y;
  636.         }
  637.  
  638.         public static bool operator !=(Point a, Point b)
  639.         {
  640.             return !(a == b);
  641.         }
  642.  
  643.         public override string ToString()
  644.         {
  645.             return String.Format("({0},{1})", this.X, this.Y);
  646.         }
  647.  
  648.         public static Point Parse(string pointString)
  649.         {
  650.             string coordinatesPairString = pointString.Substring(1, pointString.Length - 2);
  651.             string[] coordinates = coordinatesPairString.Split(',');
  652.             return new Point(coordinates[0], coordinates[1]);
  653.         }
  654.     }
  655.     public enum ResourceType
  656.     {
  657.         Lumber,
  658.         Stone,
  659.         Gold,
  660.     }
  661.     public abstract class StaticObject : WorldObject
  662.     {
  663.         public StaticObject(Point position, int owner = 0)
  664.             : base(position, owner)
  665.         {
  666.         }
  667.     }
  668.     public class Tree : StaticObject, IResource
  669.     {
  670.         protected int Size { get; private set; }
  671.  
  672.         public ResourceType Type
  673.         {
  674.             get
  675.             {
  676.                 return ResourceType.Lumber;
  677.             }
  678.         }
  679.  
  680.         public int Quantity
  681.         {
  682.             get
  683.             {
  684.                 return this.Size;
  685.             }
  686.         }
  687.  
  688.         public Tree(int size, Point position)
  689.             : base(position)
  690.         {
  691.             this.Size = size;
  692.             this.HitPoints = size;
  693.         }
  694.     }
  695.     public abstract class WorldObject : IWorldObject
  696.     {
  697.         public int HitPoints { get; set; }
  698.  
  699.         public int Owner { get; private set; }
  700.  
  701.         public Point Position { get; protected set; }
  702.  
  703.         public bool IsDestroyed
  704.         {
  705.             get
  706.             {
  707.                 if (this is Ninja)
  708.                 {
  709.                     return false;
  710.                 }
  711.                 return this.HitPoints <= 0;
  712.             }
  713.         }
  714.  
  715.         public WorldObject(Point position, int owner)
  716.         {
  717.             this.Position = position;
  718.             this.Owner = owner;
  719.             this.HitPoints = 0;
  720.         }
  721.  
  722.         public override string ToString()
  723.         {
  724.             return this.GetType().Name;
  725.         }
  726.     }
  727. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement