Advertisement
Krythic

Pointbuy System

May 13th, 2024
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public enum AttributeType
  5. {
  6.     Strength,
  7.     Agility,
  8.     Defense,
  9.     Vitality,
  10.     Intellect,
  11.     Wisdom,
  12.     Spirit
  13. }
  14.  
  15. public class PointBuySystem
  16. {
  17.     private const int MaxAttributeValue = 10;
  18.     private const int TotalPoints = 21;
  19.     private Dictionary<AttributeType, int> attributes = new Dictionary<AttributeType, int>()
  20.     {
  21.         { AttributeType.Strength, 1 },
  22.         { AttributeType.Agility, 1 },
  23.         { AttributeType.Defense, 1 },
  24.         { AttributeType.Vitality, 1 },
  25.         { AttributeType.Intellect, 1 },
  26.         { AttributeType.Wisdom, 1 },
  27.         { AttributeType.Spirit, 1 }
  28.     };
  29.     private int availablePoints = TotalPoints;
  30.  
  31.     public int GetAttributeValue(AttributeType attribute)
  32.     {
  33.         return attributes[attribute];
  34.     }
  35.  
  36.     public int AvailablePoints { get { return availablePoints; } }
  37.  
  38.     public bool CanBuy(AttributeType attribute)
  39.     {
  40.         return availablePoints > 0 && attributes[attribute] < MaxAttributeValue;
  41.     }
  42.  
  43.     public bool BuyAttribute(AttributeType attribute)
  44.     {
  45.         if (!CanBuy(attribute))
  46.         {
  47.             return false;
  48.         }
  49.  
  50.         if (availablePoints >= (MaxAttributeValue - attributes[attribute]))
  51.         {
  52.             availablePoints -= (MaxAttributeValue - attributes[attribute]);
  53.             attributes[attribute] = MaxAttributeValue;
  54.         }
  55.         else
  56.         {
  57.             attributes[attribute] += availablePoints;
  58.             availablePoints = 0;
  59.         }
  60.  
  61.         return true;
  62.     }
  63.  
  64.     public bool SellAttribute(AttributeType attribute)
  65.     {
  66.         if (attributes[attribute] <= 1)
  67.         {
  68.             return false;
  69.         }
  70.  
  71.         attributes[attribute]--;
  72.         availablePoints += (MaxAttributeValue - attributes[attribute] + 1);
  73.  
  74.         return true;
  75.     }
  76.  
  77.     public void Reset()
  78.     {
  79.         foreach (AttributeType attribute in Enum.GetValues(typeof(AttributeType)))
  80.         {
  81.             attributes[attribute] = 1;
  82.         }
  83.         availablePoints = TotalPoints;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement