Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace CSHARPMEDIUM
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Random r = new Random();
  12.             IItem[] items;
  13.             FieldCreationStrategy[] strategyes = new FieldCreationStrategy[]
  14.             {
  15.                 new StrategyOne(),
  16.                 new StrategyTwo(),
  17.                 new StrategyThree()
  18.             };
  19.  
  20.             items = strategyes[r.Next(0, strategyes.Length)].CreateField();
  21.             int offsetX = 0;
  22.  
  23.             while (true)
  24.             {
  25.                 Console.Clear();
  26.                 foreach (IItem item in items)
  27.                 {
  28.                     item.Spawn(offsetX);
  29.                 }
  30.  
  31.                 System.Threading.Thread.Sleep(1000);
  32.                 offsetX++;
  33.             }
  34.         }
  35.     }
  36.  
  37.     public abstract class FieldCreationStrategy
  38.     {
  39.         public Item[] items = new Item[100];
  40.         public int lastX = 0;
  41.         public int lastY = 0;
  42.         public Random r = new Random();
  43.  
  44.         public abstract IItem[] CreateField();
  45.     }
  46.  
  47.     public class StrategyOne : FieldCreationStrategy
  48.     {
  49.         public override IItem[] CreateField()
  50.         {
  51.             for (int i = 0; i < 100; i++)
  52.             {
  53.                 lastX += r.Next(1, 5);
  54.                 items[i] = new Item(new Vector2(lastX, r.Next(0, 15)), r.Next(1, 10), (Item.Type)r.Next(0, 4));
  55.                
  56.             }
  57.             return items;
  58.         }
  59.     }
  60.  
  61.     public class StrategyTwo : FieldCreationStrategy
  62.     {
  63.         public override IItem[] CreateField()
  64.         {
  65.             for (int i = 0; i < 100; i++)
  66.             {
  67.                 lastX += 1 + i % 2;
  68.                 items[i] = new Item(new Vector2(lastX, r.Next(0, 15)), r.Next(1, 10), (Item.Type)r.Next(0, 4));
  69.             }
  70.             return items;
  71.         }
  72.     }
  73.  
  74.     public class StrategyThree : FieldCreationStrategy
  75.     {
  76.         public override IItem[] CreateField()
  77.         {
  78.             int yDirection = 1;
  79.             for (int i = 0; i < 100; i++)
  80.             {
  81.                 lastX += 1;
  82.                 lastY += yDirection;
  83.                 if (lastY <= 0)
  84.                 {
  85.                     yDirection = 1;
  86.                 }
  87.                 else if (lastY > 15)
  88.                 {
  89.                     yDirection = -1;
  90.                 }
  91.                 items[i] = new Item(new Vector2(lastX, lastY), r.Next(1, 10), (Item.Type)r.Next(0, 4));
  92.             }
  93.             return items;
  94.         }
  95.     }
  96.  
  97.     public interface IItem
  98.     {
  99.         void Spawn(int ofsetX);
  100.     }
  101.  
  102.     public class Item : IItem
  103.     {
  104.         public Vector2 Position { get; private set; }
  105.         public int Value { get; private set; }
  106.         public Type CurrentType { get; private set; }
  107.         Random r = new Random();
  108.  
  109.         public Item(Vector2 position, int value, Type currentType)
  110.         {
  111.             Position = position;
  112.             Value = value;
  113.             CurrentType = currentType;
  114.         }
  115.        
  116.  
  117.         public void Spawn(int ofsetX)
  118.         {
  119.             if (Position.X - ofsetX > 0 && Position.X - ofsetX < Console.WindowWidth)
  120.             {
  121.                 Console.SetCursorPosition(Position.X - ofsetX, Position.Y);
  122.                 switch (CurrentType)
  123.                 {
  124.                     case Item.Type.Gold:
  125.                         Console.Write('$');
  126.                         break;
  127.                     case Item.Type.Silver:
  128.                         Console.Write('@');
  129.                         break;
  130.                     case Item.Type.Health:
  131.                         Console.Write('H');
  132.                         break;
  133.                     case Item.Type.Bomb:
  134.                         Console.Write('B');
  135.                         break;
  136.                 }
  137.  
  138.  
  139.             }
  140.  
  141.            
  142.         }
  143.  
  144.         public enum Type
  145.         {
  146.             Gold,
  147.             Silver,
  148.             Health,
  149.             Bomb
  150.         }
  151.     }
  152.  
  153.     public struct Vector2
  154.     {
  155.         public int X, Y;
  156.  
  157.         public Vector2(int x, int y)
  158.         {
  159.             X = x;
  160.             Y = y;
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement