Advertisement
Arui

Symulator Walki Turowej C#

Jul 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace WalkaTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var random = new Random();
  13.             int monsterHealth, monsterAtack, monsterDefense, monsterSpeed, monsterCritical;
  14.             PlayerTest character = new PlayerTest(hp:10, atack:5, def:5, speed:5, criticalChance:5,mp: 10);
  15.             bool inputCheck = false;
  16.             int dificulty;
  17.            
  18.             while (character.lvl < 10 || character.hp == 0)
  19.             {
  20.                 do
  21.                 {
  22.                     Console.WriteLine("Chose dificulty level from 1 to 9");
  23.                     inputCheck = Int32.TryParse(Console.ReadLine(), out dificulty);
  24.                 }
  25.                 while (inputCheck != true);
  26.                 if (dificulty < 1) dificulty = 1;
  27.                 if (dificulty > 9) dificulty = 9;
  28.                 monsterAtack = random.Next(1, 5) * dificulty;
  29.                 monsterDefense = random.Next(1, 5) * dificulty;
  30.                 monsterHealth = random.Next(1, 5) * dificulty * 2;
  31.                 monsterSpeed = random.Next(1, 5) * dificulty;
  32.                 monsterCritical = random.Next(1, 5) * dificulty;
  33.                 MonsterTest enemy = new MonsterTest(monsterHealth, monsterAtack, monsterDefense, monsterSpeed, monsterCritical);
  34.                 Actor.Fight(character, enemy);
  35.             }
  36.         }
  37.     }
  38.  
  39. }
  40.  
  41.  
  42. using System;
  43. using System.Collections.Generic;
  44. using System.Linq;
  45. using System.Text;
  46.  
  47. namespace WalkaTest
  48. {
  49.  
  50.     abstract class Actor
  51.     {
  52.         protected int maxHp = 0;
  53.         protected int maxMp = 0;
  54.  
  55.         //[0-name/1-manaCost/ 2-DMG, id]              
  56.         protected string[,] magicArray = new string[3, 3] { { "Fire ball [1MP]", "Lightning [2MP]", "Healing Wave[2MP]" },
  57.                                                                                { "1","2","2" },
  58.                                                                                { "5","7","-8"} };
  59.  
  60.         Random random = new Random();
  61.         public int hp { get; protected set; }
  62.  
  63.         public int criticalChance { get; protected set; }
  64.  
  65.         public int atack { get; protected set; }
  66.  
  67.         public int def { get; protected set; }
  68.  
  69.         public string Name { get; protected set; }
  70.  
  71.         public int speed { get; protected set; }
  72.  
  73.         public int mp { get; protected set; }
  74.  
  75.         public void hpChange(int amount)
  76.         {
  77.             hp -= amount;
  78.             if (hp > maxHp) hp = maxHp;
  79.         }
  80.  
  81.         public void mpChange(int amount)
  82.         {
  83.             mp -= amount;
  84.             if (mp > maxMp) mp = maxMp;
  85.         }
  86.  
  87.         public void Attack(Actor target)
  88.         {
  89.             int critHit = random.Next(1, 100);
  90.             bool ifCritHit = false;
  91.             if (critHit > (100 - criticalChance)) {
  92.                 atack *= 2;
  93.                 ifCritHit = true;
  94.             }
  95.  
  96.             int dmg = atack - target.def;
  97.             if (dmg <= 0) dmg = 1;
  98.             target.hpChange(dmg);
  99.             if (ifCritHit == true) Console.WriteLine("{0} hit critically {1} for {2} damage.", Name, target.Name, dmg);
  100.             else Console.WriteLine("{0} hit {1} for {2} damage.", Name, target.Name, dmg);
  101.             Console.WriteLine("{0} got {1} HP left.", target.Name, target.hp);
  102.             if (target.hp <= 0)
  103.             {
  104.                 giveExp((target.maxHp * 10) / maxHp);
  105.             }
  106.         }
  107.         public virtual void giveExp(int amount) { }
  108.  
  109.         public static void FightMenu(Actor a1, Actor a2)
  110.         {
  111.             int fightOption;
  112.             bool inputCheck = false;
  113.  
  114.             do
  115.             {
  116.                 Console.WriteLine();
  117.                 Console.WriteLine("1. Atack 2. Magic");
  118.                 inputCheck = Int32.TryParse(Console.ReadLine(), out fightOption);
  119.             }
  120.             while (inputCheck != true && fightOption >= 1 && fightOption <= 2);
  121.             if (fightOption == 1)
  122.             {
  123.                 a1.Attack(a2);
  124.             }
  125.             else if (fightOption == 2 && a1.mp > 0)
  126.             {
  127.                 a1.Magic(a2);
  128.             }
  129.             else
  130.             {
  131.                 Console.WriteLine("Sorry you dont have any mana points left.");
  132.                 Console.WriteLine();
  133.                 a1.Attack(a2);
  134.  
  135.             }
  136.         }
  137.         public static void Fight(Actor a1, Actor a2)
  138.         {
  139.  
  140.             while (a1.hp > 0 || a2.hp > 0)
  141.             {
  142.                 if (a1.speed >= a2.speed)
  143.                 {
  144.                     FightMenu(a1, a2);
  145.                     if (a2.hp <= 0) break;
  146.                     a2.Attack(a1);
  147.                 }
  148.                 else
  149.                 {
  150.                     a2.Attack(a1);
  151.                     if (a1.hp <= 0) break;
  152.                     FightMenu(a1, a2);
  153.                 }
  154.             }
  155.         }
  156.  
  157.  
  158.         public void Magic(Actor target)
  159.         {
  160.             int chosenSpellID, spellManaCost, spellDmg, spellAtack;
  161.             bool inputCheck = false;
  162.  
  163.             for (int i = 0; i < magicArray.GetLength(1); i++)
  164.             {
  165.                 Console.WriteLine((i + 1) + ". " + magicArray[0, i]);
  166.             }
  167.             do
  168.             {
  169.                 Console.WriteLine("Chose your spell: ");
  170.                 inputCheck = Int32.TryParse(Console.ReadLine(), out chosenSpellID);
  171.             }
  172.             while (inputCheck != true);
  173.  
  174.             spellManaCost = Convert.ToInt32(magicArray[1, chosenSpellID - 1]);
  175.             spellAtack = Convert.ToInt32(magicArray[2, chosenSpellID - 1]);
  176.             if (mp >= spellManaCost)
  177.             {
  178.                 mpChange(spellManaCost);
  179.                 if (spellAtack >= 0)
  180.                 {
  181.                     spellDmg = target.def - spellAtack;
  182.                     if (spellDmg <= 0) spellDmg = 1;
  183.                     target.hp -= spellDmg;
  184.                     Console.WriteLine("You used {0} and hitted for {1}", magicArray[0, chosenSpellID - 1], spellDmg);
  185.                     Console.WriteLine("You got " + hp + "HP");
  186.                     Console.WriteLine("You got " + mp + "MP");
  187.                     Console.WriteLine("{0} got {1}HP left", target.Name, target.hp);
  188.                 }
  189.                 else
  190.                 {
  191.                     hpChange(spellAtack);
  192.                     Console.WriteLine("You healed for: " + (-1 * spellAtack));
  193.                     Console.WriteLine("You got " + hp + "HP");
  194.                     Console.WriteLine("You got " + mp + "MP");
  195.                     Console.WriteLine();
  196.  
  197.                 }
  198.             }
  199.  
  200.         }
  201.  
  202.     }
  203.  
  204.     class PlayerTest: Actor
  205.     {
  206.         public int exp = 0;
  207.         public int[] expNeeded = new int[10] { 10, 30, 70, 110, 160, 220, 290, 370, 460, 560 };
  208.  
  209.         public int lvl = 1;
  210.        
  211.         public PlayerTest(int hp, int atack, int def, int speed, int criticalChance, int mp, string name = "Player")
  212.         {
  213.             this.hp = hp;
  214.             this.atack = atack;
  215.             this.def = def;
  216.             this.Name = name;
  217.             this.speed = speed;
  218.             this.criticalChance = criticalChance;
  219.             this.mp = mp;
  220.             maxMp = mp;
  221.             maxHp = hp;
  222.         }
  223.  
  224.         public override void giveExp(int exp)
  225.         {
  226.             this.exp += exp;
  227.             Console.WriteLine("You got: " + exp + "exp");
  228.             if (this.exp >= expNeeded[lvl - 1])
  229.             {
  230.                 this.lvl += this.exp / (10 * lvl);
  231.                 // this.exp = this.exp % (10*lvl);
  232.                 maxHp++;
  233.                 maxMp++;
  234.                 atack++;
  235.                 def++;
  236.                 speed++;
  237.                 criticalChance++;
  238.                 mpChange((maxMp - mp) * (-1));
  239.                 hpChange((maxHp - hp) * (-1));
  240.                 Console.WriteLine("You leveled up to: " + lvl + "lvl");
  241.               }
  242.             Console.WriteLine("You got: " + this.exp + "/" + expNeeded[lvl - 1]);
  243.         }
  244.     }
  245.  
  246.     class MonsterTest: Actor
  247.     {
  248.         public MonsterTest(int hp, int atack, int def, int speed, int criticalChance, string name = "Monster")
  249.         {
  250.             this.hp = hp;
  251.             this.atack = atack;
  252.             this.def = def;
  253.             this.Name = name;
  254.             this.maxHp = hp;
  255.             this.speed = speed;
  256.             this.criticalChance = criticalChance;
  257.         }
  258.  
  259.         public override void giveExp(int amount)
  260.         {
  261.             Console.WriteLine("You lost!");
  262.             Console.ReadKey();
  263.             Environment.Exit(1);
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement