Advertisement
Frostyy22

Core.cs

Mar 19th, 2023
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 22.57 KB | None | 0 0
  1. namespace Core
  2. {
  3.     using System;
  4.     using System.IO;
  5.  
  6.     public enum PlayerTypes
  7.     {
  8.         Name,
  9.         XP,
  10.         Level,
  11.         Marks,
  12.         Perception
  13.     }
  14.  
  15.     public class PlayerStats
  16.     {
  17.         private string name;
  18.         private int xp;
  19.         private int level;
  20.         private int marks;
  21.         private int perception;
  22.  
  23.         static void WriteColor(string message, ConsoleColor color, string lineEnding)
  24.         {
  25.             Console.ForegroundColor = color;
  26.             Console.WriteLine(message);
  27.             Console.ResetColor();
  28.             Console.Write(lineEnding);
  29.         }
  30.  
  31.         public PlayerStats()
  32.         {
  33.             name = null;
  34.             xp = 0;
  35.             level = 0;
  36.             marks = 0;
  37.             perception = 0;
  38.         }
  39.  
  40.         public string Name
  41.         {
  42.             get { return name; }
  43.         }
  44.  
  45.         public int XP
  46.         {
  47.             get { return xp; }
  48.         }
  49.  
  50.         public int Level
  51.         {
  52.             get { return level; }
  53.         }
  54.  
  55.         public int Marks
  56.         {
  57.             get { return marks; }
  58.         }
  59.  
  60.         public int Perception
  61.         {
  62.             get { return perception; }
  63.         }
  64.  
  65.         public void SetStat(PlayerTypes statType, object value)
  66.         {
  67.             switch (statType)
  68.             {
  69.                 case PlayerTypes.Name:
  70.                     name = (string)value;
  71.                     break;
  72.                 case PlayerTypes.XP:
  73.                     xp = (int)value;
  74.                     break;
  75.                 case PlayerTypes.Level:
  76.                     level = (int)value;
  77.                     break;
  78.                 case PlayerTypes.Marks:
  79.                     marks = (int)value;
  80.                     break;
  81.                 case PlayerTypes.Perception:
  82.                     perception = (int)value;
  83.                     break;
  84.                 default:
  85.                     string errorMessage02 = "Invalid stats type.";
  86.                     WriteColor(errorMessage02, ConsoleColor.Red, Environment.NewLine);
  87.                     throw new ArgumentException(errorMessage02);
  88.             }
  89.         }
  90.     }
  91.  
  92.     public class Enemy
  93.     {
  94.         public int health { get; set; }
  95.         public int damage { get; set; }
  96.  
  97.         public Enemy(int health, int damage)
  98.         {
  99.             this.health = health;
  100.             this.damage = damage;
  101.         }
  102.     }
  103.  
  104.     class CoreGame
  105.     {
  106.         static void Main(string[] args)
  107.         {
  108.             Random rand = new Random();
  109.  
  110.             var PlayerStats = new PlayerStats();
  111.  
  112.             Enemy enemy = new Enemy(100, rand.Next(50, 91));
  113.  
  114.             bool gameOver = false;
  115.             bool inCombat = false;
  116.  
  117.             string gameStatsSave = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "stats.txt");
  118.  
  119.             int enemyStealth = rand.Next(1, 16);
  120.  
  121.             string? playerName = null;
  122.             int playerHealth = 100;
  123.             int playerXp = 0;
  124.             int playerLevel = 0;
  125.             int playerMarks = 0;
  126.             int playerPerception = 0;
  127.  
  128.             static void WriteColor(string message, ConsoleColor color, string lineEnding)
  129.             {
  130.                 Console.ForegroundColor = color;
  131.                 Console.WriteLine(message);
  132.                 Console.ResetColor();
  133.                 Console.Write(lineEnding);
  134.             }
  135.  
  136.             if (File.Exists(gameStatsSave))
  137.             {
  138.                 using (StreamReader reader = new StreamReader(gameStatsSave))
  139.                 {
  140.                     string? line = null;
  141.                     while ((line = reader.ReadLine()) != null)
  142.                     {
  143.                         string[] parts = line.Split(':');
  144.                         if (Enum.TryParse(parts[0], out PlayerTypes statType))
  145.                         {
  146.                             switch (statType)
  147.                             {
  148.                                 case PlayerTypes.Name:
  149.                                     playerName = parts[1].Trim();
  150.                                     break;
  151.                                 case PlayerTypes.XP:
  152.                                     int.TryParse(parts[1], out playerXp);
  153.                                     break;
  154.                                 case PlayerTypes.Level:
  155.                                     int.TryParse(parts[1], out playerLevel);
  156.                                     break;
  157.                                 case PlayerTypes.Marks:
  158.                                     int.TryParse(parts[1], out playerMarks);
  159.                                     break;
  160.                                 case PlayerTypes.Perception:
  161.                                     int.TryParse(parts[1], out playerPerception);
  162.                                     break;
  163.                                 default:
  164.                                     string errorMessage01 = "Failed to load game. Corrupt or incomplete data detected. Please avoid modifying the game files.";
  165.                                     WriteColor(errorMessage01, ConsoleColor.Red, Environment.NewLine);
  166.                                     throw new ArgumentException(errorMessage01);
  167.                             }
  168.                         }
  169.                     }
  170.                 }
  171.  
  172.                 Console.ForegroundColor = ConsoleColor.Green;
  173.                 Console.Write("Game successfully loaded from ");
  174.                 Console.Write(gameStatsSave);
  175.                 Console.Write(".");
  176.                 Console.ResetColor();
  177.                 Console.Write(Environment.NewLine);
  178.             }
  179.  
  180.             if (playerName == null)
  181.             {
  182.                 Console.WriteLine("Please enter your name:");
  183.                 playerName = Console.ReadLine();
  184.                 Console.Write("Welcome, ");
  185.                 Console.ForegroundColor = ConsoleColor.Magenta;
  186.                 Console.Write(playerName);
  187.                 Console.ResetColor();
  188.                 Console.Write("!");
  189.                 Console.Write(Environment.NewLine);
  190.             }
  191.             else
  192.             {
  193.                 Console.Write("Welcome back, ");
  194.                 Console.ForegroundColor = ConsoleColor.Magenta;
  195.                 Console.Write(playerName);
  196.                 Console.ResetColor();
  197.                 Console.Write("!");
  198.                 Console.Write(Environment.NewLine);
  199.             }
  200.  
  201.             while (!gameOver && !inCombat)
  202.             {
  203.                 Console.WriteLine("##############################");
  204.                 Console.WriteLine("What do you want to do?" + Environment.NewLine);
  205.                 Console.WriteLine("1. Attempt to search for enemies.");
  206.                 Console.WriteLine("2. Use a first aid kit.");
  207.                 Console.WriteLine("3. Check your stats.");
  208.                 Console.WriteLine("4. Check your skills.");
  209.                 Console.WriteLine("9. Reset your stats.");
  210.                 Console.WriteLine("0. Quit the game.");
  211.                 Console.WriteLine("##############################");
  212.  
  213.                 string? playerAction = Console.ReadLine();
  214.  
  215.                 switch (playerAction)
  216.                 {
  217.                     case "1":
  218.  
  219.                         Console.WriteLine("You attempt to search for enemies...");
  220.  
  221.                         // Random chance from 1 to 10
  222.                         int spotChance = rand.Next(1, 11);
  223.  
  224.                         // Random chance from 1 to 50 to gain level in pereception
  225.                         int perceptionGain = rand.Next(1, 51);
  226.  
  227.                         if (perceptionGain == 1)
  228.                         {
  229.                             playerPerception++;
  230.                             Console.Write("You have gained a level in ");
  231.                             Console.ForegroundColor = ConsoleColor.Cyan;
  232.                             Console.Write("Perception");
  233.                             Console.ResetColor();
  234.                             Console.Write("!");
  235.                             Console.Write(Environment.NewLine);
  236.                         }
  237.  
  238.                         // If player's perception skill is higher than 5
  239.                         if (playerPerception > 5)
  240.                         {
  241.                             spotChance += playerPerception - 5; // Add the difference to success chance
  242.                         }
  243.  
  244.                         // Player spots a enemy
  245.                         if (spotChance >= enemyStealth)
  246.                         {
  247.                             Console.WriteLine("You spot an enemy nerby!" + Environment.NewLine);
  248.  
  249.                             FoundAEnemy();
  250.                         }
  251.                         else // Player fails to spot a enemy
  252.                         {
  253.                             Console.WriteLine("You fail to spot any enemies." + Environment.NewLine);
  254.                         }
  255.                         break;
  256.                     case "2":
  257.                         Console.WriteLine("You use a first aid kit!" + Environment.NewLine);
  258.  
  259.                         playerHealth += 10;
  260.  
  261.                         Console.Write("Your now have ");
  262.                         Console.ForegroundColor = ConsoleColor.Green;
  263.                         Console.Write(playerHealth);
  264.                         Console.ResetColor();
  265.                         Console.Write(" health.");
  266.                         Console.Write(Environment.NewLine);
  267.                         Console.Write(Environment.NewLine);
  268.                         break;
  269.                     case "3":
  270.                         Console.Write(Environment.NewLine);
  271.                         Console.Write("Your current stats ## Name: ");
  272.                         Console.ForegroundColor = ConsoleColor.Magenta;
  273.                         Console.Write(playerName);
  274.                         Console.ResetColor();
  275.  
  276.                         Console.Write(", XP: ");
  277.                         Console.ForegroundColor = ConsoleColor.Blue;
  278.                         Console.Write(playerXp);
  279.                         Console.ResetColor();
  280.  
  281.                         Console.Write(", Level: ");
  282.                         Console.ForegroundColor = ConsoleColor.Green;
  283.                         Console.Write(playerLevel);
  284.                         Console.ResetColor();
  285.  
  286.                         Console.Write(", Marks: ");
  287.                         Console.ForegroundColor = ConsoleColor.Yellow;
  288.                         Console.Write(playerMarks);
  289.                         Console.ResetColor();
  290.                         Console.Write(Environment.NewLine);
  291.  
  292.                         Console.Write("Your current health ## Health: ");
  293.                         Console.ForegroundColor = ConsoleColor.Green;
  294.                         Console.Write(playerHealth);
  295.                         Console.ResetColor();
  296.                         Console.Write(Environment.NewLine);
  297.                         Console.Write(Environment.NewLine);
  298.                         break;
  299.                     case "4":
  300.                         Console.Write(Environment.NewLine);
  301.                         Console.Write("Your current skills ## Perception: ");
  302.                         Console.ForegroundColor = ConsoleColor.Cyan;
  303.                         Console.Write(playerPerception);
  304.                         Console.ResetColor();
  305.  
  306.                         Console.Write(Environment.NewLine);
  307.                         Console.Write(Environment.NewLine);
  308.                         break;
  309.                     case "9":
  310.                         WriteColor("Are you sure you want to reset your stats? This will restart your game.(Y/N)", ConsoleColor.Red, Environment.NewLine);
  311.                         string? reset = Console.ReadLine();
  312.                         if (reset?.ToLower() == "y" || reset?.ToLower() == "yes")
  313.                         {
  314.                             // Reset the stats
  315.                             playerName = null;
  316.                             playerHealth = 100;
  317.                             playerXp = 0;
  318.                             playerLevel = 0;
  319.                             playerMarks = 0;
  320.                             playerPerception = 0;
  321.  
  322.                             // Delete the save and restart game
  323.                             if (File.Exists(gameStatsSave))
  324.                             {
  325.                                 File.Delete(gameStatsSave);
  326.                                 WriteColor("Stats reset. Save file deleted.", ConsoleColor.Red, Environment.NewLine);
  327.                                 RestartGame();
  328.                             }
  329.                             else
  330.                             {
  331.                                 WriteColor("Stats reset. No save file found.", ConsoleColor.Red, Environment.NewLine);
  332.                                 RestartGame();
  333.                             }
  334.                         }
  335.                         break;
  336.                     case "0":
  337.                         WriteColor("Are you sure you want to quit the game.(Y/N)", ConsoleColor.Red, Environment.NewLine);
  338.                         string? quit = Console.ReadLine();
  339.                         if (quit?.ToLower() == "y" || quit?.ToLower() == "yes")
  340.                         {
  341.                             // Exit the game and save
  342.                             using (StreamWriter writer = new StreamWriter(gameStatsSave))
  343.                             {
  344.                                 writer.WriteLine("Name: " + playerName);
  345.                                 writer.WriteLine("XP: " + playerXp);
  346.                                 writer.WriteLine("Level: " + playerLevel);
  347.                                 writer.WriteLine("Marks: " + playerMarks);
  348.                                 writer.WriteLine("Perception: " + playerPerception);
  349.                             }
  350.  
  351.                             Console.ForegroundColor = ConsoleColor.Green;
  352.                             Console.Write("Game saved to ");
  353.                             Console.Write(gameStatsSave);
  354.                             Console.Write(".");
  355.                             Console.ResetColor();
  356.                             Console.Write(Environment.NewLine);
  357.  
  358.                             Console.ForegroundColor = ConsoleColor.Red;
  359.                             Console.Write("Quitting game...");
  360.                             Console.ResetColor();
  361.  
  362.                             Environment.Exit(0);
  363.                         }
  364.                         break;
  365.                     default:
  366.                         WriteColor("Invaild user input.", ConsoleColor.Red, Environment.NewLine);
  367.                         break;
  368.                 }
  369.             }
  370.  
  371.             void RestartGame()
  372.             {
  373.                 // Close the current instance of the game
  374.                 Console.WriteLine("Restarting game...");
  375.                 Thread.Sleep(2000);
  376.                 System.Diagnostics.Process.GetCurrentProcess().Kill();
  377.  
  378.                 // Start a new instance of the game
  379.                 string gameExecutablePath = System.Reflection.Assembly.GetEntryAssembly().Location;
  380.                 System.Diagnostics.Process.Start(gameExecutablePath);
  381.             }
  382.  
  383.             void FoundAEnemy()
  384.             {
  385.                 inCombat = true;
  386.  
  387.                 Console.WriteLine("You open fire on the enemy!" + Environment.NewLine);
  388.  
  389.                 while (!gameOver && inCombat)
  390.                 {
  391.                     // Damage to deal to the enemy
  392.                     int enemyDamage = rand.Next(50, 100);
  393.  
  394.                     // Chance for enemy or player to hit ( 50/50 )
  395.                     int hitChance = rand.Next(0, 2);
  396.  
  397.                     // Rewards for killing
  398.                     int gainXp = rand.Next(0, 20);
  399.                     int earnMarks = rand.Next(0, 20);
  400.  
  401.                     if (hitChance == 1)
  402.                     {
  403.                         enemy.health -= enemyDamage;
  404.  
  405.                         Console.Write("You manage to hit the enemy and deal ");
  406.                         Console.ForegroundColor = ConsoleColor.Red;
  407.                         Console.Write(enemyDamage);
  408.                         Console.ResetColor();
  409.                         Console.Write(" damage. ");
  410.                         Console.Write(Environment.NewLine);
  411.  
  412.                         if (enemy.health <= 0)
  413.                         {
  414.                             inCombat = false;
  415.  
  416.                             enemy.health = 100;
  417.  
  418.                             playerXp += gainXp;
  419.                             playerMarks += earnMarks;
  420.  
  421.                             Console.Write("You successfully kill the enemy!" + Environment.NewLine);
  422.  
  423.                             Console.Write("You gain ");
  424.                             Console.ForegroundColor = ConsoleColor.Blue;
  425.                             Console.Write(gainXp);
  426.                             Console.ResetColor();
  427.                             Console.Write(" XP and ");
  428.                             Console.ForegroundColor = ConsoleColor.Yellow;
  429.                             Console.Write(earnMarks);
  430.                             Console.ResetColor();
  431.                             Console.Write(" marks.");
  432.                             Console.Write(Environment.NewLine);
  433.                             Console.Write(Environment.NewLine);
  434.                         }
  435.                         else
  436.                         {
  437.                             Console.WriteLine("##############################");
  438.                             Console.WriteLine("What do you want to do?" + Environment.NewLine);
  439.                             Console.WriteLine("1. Attempt to fire at the enemy again.");
  440.                             Console.WriteLine("2. Attempt Take cover.");
  441.                             Console.WriteLine("3. Attempt to retreat away..");
  442.                             Console.WriteLine("##############################");
  443.  
  444.                             string? playerCombatAction = Console.ReadLine();
  445.  
  446.                             switch (playerCombatAction)
  447.                             {
  448.                                 case "1":
  449.                                     Console.WriteLine("You open fire on the enemy!" + Environment.NewLine);
  450.                                     break;
  451.                                 case "2":
  452.                                     Console.WriteLine("You attempt to take cover." + Environment.NewLine);
  453.                                     break;
  454.                                 case "3":
  455.                                     Console.WriteLine("You attempt to retreat." + Environment.NewLine);
  456.                                     inCombat = false;
  457.                                     break;
  458.                                 default:
  459.                                     WriteColor("Invalid input.", ConsoleColor.Red, Environment.NewLine);
  460.                                     break;
  461.                             }
  462.                         }
  463.                     }
  464.                     else
  465.                     {
  466.                         Console.Write("You fire at the enemy but miss." + Environment.NewLine);
  467.                     }
  468.  
  469.                     if (hitChance == 0)
  470.                     {
  471.                         playerHealth -= enemy.damage;
  472.  
  473.                         Console.Write("The enemy hits you and deals ");
  474.                         Console.ForegroundColor = ConsoleColor.Red;
  475.                         Console.Write(enemy.damage);
  476.                         Console.ResetColor();
  477.                         Console.Write(" damage.");
  478.                         Console.Write(Environment.NewLine);
  479.  
  480.                         if (playerHealth <= 0)
  481.                         {
  482.                             Console.ForegroundColor = ConsoleColor.Red;
  483.                             Console.Write("Game over. You died." + Environment.NewLine);
  484.                             Console.ResetColor();
  485.  
  486.                             gameOver = true;
  487.  
  488.                             // Reset the stats
  489.                             playerName = null;
  490.                             playerHealth = 100;
  491.                             playerXp = 0;
  492.                             playerLevel = 0;
  493.                             playerMarks = 0;
  494.                             playerPerception = 0;
  495.  
  496.                             // Delete save
  497.                             if (File.Exists(gameStatsSave))
  498.                             {
  499.                                 File.Delete(gameStatsSave);
  500.                             }
  501.                             else
  502.                             {
  503.                             }
  504.                         }
  505.                         else
  506.                         {
  507.                             Console.WriteLine("##############################");
  508.                             Console.WriteLine("What do you want to do?" + Environment.NewLine);
  509.                             Console.WriteLine("1. Attempt to fire at the enemy again.");
  510.                             Console.WriteLine("2. Attempt Take cover.");
  511.                             Console.WriteLine("3. Attempt to retreat away..");
  512.                             Console.WriteLine("##############################");
  513.  
  514.                             string? playerCombatAction = Console.ReadLine();
  515.  
  516.                             switch (playerCombatAction)
  517.                             {
  518.                                 case "1":
  519.                                     Console.WriteLine("You open fire on the enemy!" + Environment.NewLine);
  520.                                     break;
  521.                                 case "2":
  522.                                     Console.WriteLine("You attempt to take cover." + Environment.NewLine);
  523.                                     break;
  524.                                 case "3":
  525.                                     Console.WriteLine("You attempt to retreat." + Environment.NewLine);
  526.                                     inCombat = false;
  527.                                     break;
  528.  
  529.                                 default:
  530.                                     WriteColor("Invaild user input.", ConsoleColor.Red, Environment.NewLine);
  531.                                     break;
  532.                             }
  533.                         }
  534.                     }
  535.  
  536.                     if (playerXp >= 100)
  537.                     {
  538.                         playerLevel++;
  539.                         playerXp = 0;
  540.                         Console.Write("You have leveled up to level: ");
  541.                         Console.ForegroundColor = ConsoleColor.Green;
  542.                         Console.Write(playerLevel);
  543.                         Console.ResetColor();
  544.                         Console.Write("!");
  545.                         Console.Write(Environment.NewLine);
  546.                         Console.Write(Environment.NewLine);
  547.                     }
  548.                 }
  549.             }
  550.         }
  551.     }
  552. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement