Advertisement
jyoung12387

Attack Game

Mar 4th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MarchFour
  8. {
  9.     class Program
  10.     {
  11.         static int userTempHealth;
  12.         static int cpuTempHealth;
  13.         static void Main(string[] args)
  14.         {
  15.  
  16.             Random rnd = new Random();
  17.  
  18.             Player user = new Player("User", 100, 25);
  19.             Player cpu = new Player("Computer", 100, 25);
  20.  
  21.             //user goes first
  22.             //cpu goes second
  23.  
  24.             string attackingPlayer = "cpu";
  25.  
  26.             do
  27.             {
  28.                 if (attackingPlayer == "cpu")
  29.                 {
  30.                     attackingPlayer = "user";
  31.                 }
  32.                 else if (attackingPlayer == "user")
  33.                 {
  34.                     attackingPlayer = "cpu";
  35.                 }
  36.  
  37.                 //Inform user whos turn it is
  38.                 if (attackingPlayer == "user")
  39.                 {
  40.                     Console.WriteLine("It's your turn. Press any key to attack");
  41.                     Console.ReadKey();
  42.  
  43.                     int currentAttack = ModifiedAttack(rnd, user);
  44.                     user.Health += -currentAttack;
  45.  
  46.                     Console.WriteLine($"Your attack did {currentAttack} damage!");
  47.                     Console.WriteLine("Here are the current stats:\n");
  48.                     DisplayStats(user, cpu);
  49.                     Console.WriteLine("Press any key to continue");
  50.                     Console.Read();
  51.  
  52.                 }
  53.                 if (attackingPlayer == "cpu")
  54.                 {
  55.                     Console.Write("It's cpu's turn. Brace for attack! Press any key to continue.");
  56.                     Console.ReadKey();
  57.                 }
  58.             } while ( (user.Health >= 0) || (cpu.Health >= 0) );
  59.  
  60.             Console.WriteLine("Game has ended");
  61.  
  62.             do
  63.             {
  64.                 DisplayStats(user, cpu);
  65.                 Console.ReadLine();
  66.                 user.Health += -20;
  67.                 cpu.Health += -20;
  68.                 DisplayStats(user, cpu);
  69.                 Console.ReadLine();
  70.  
  71.                 for (int i = 0; i < 25; i++)
  72.                 {
  73.                     Console.WriteLine(ModifiedAttack(rnd, user));
  74.                 }
  75.  
  76.  
  77.  
  78.  
  79.                 //int tempNum = (int)Math.Round(3.45, 0);
  80.                 //Console.WriteLine(tempNum);
  81.  
  82.                 //int currentAttack = Math.Round(25 * RandomAttack(rnd),0);
  83.  
  84.                 Console.ReadLine();
  85.             } while (true);
  86.  
  87.         }
  88.  
  89.         public static void DisplayStats(Player user, Player cpu)
  90.         {
  91.             //Display stats for user and cpu
  92.             Console.WriteLine($"{"Player Name",-15} {"Health",-10} {"Attack",-10}");
  93.             Console.WriteLine("");
  94.             Console.WriteLine($"{user.Name,-15} {user.Health,-10} {user.BaseAttack,-10}");
  95.             Console.WriteLine($"{cpu.Name,-15} {cpu.Health,-10} {cpu.BaseAttack,-10}");
  96.         }
  97.  
  98.  
  99.        
  100.        
  101.         //Takes in the Player class, randomly modifies its attack and returns the current attack.
  102.         public static int ModifiedAttack(Random rnd, Player player)
  103.         {
  104.             int randomNumber = rnd.Next(-50, 50);
  105.  
  106.             if(randomNumber != 0)
  107.             {
  108.                 double modifier = ((double)randomNumber) / 100;
  109.                 double attackAsDouble = (player.BaseAttack * modifier);
  110.                 int currentAttack = (int)attackAsDouble + player.BaseAttack;
  111.                 return currentAttack;
  112.             }
  113.             else
  114.             {
  115.                 return player.BaseAttack;
  116.             }
  117.         }
  118.     }
  119. }
  120.  
  121. using System;
  122. using System.Collections.Generic;
  123. using System.Linq;
  124. using System.Text;
  125. using System.Threading.Tasks;
  126.  
  127. namespace MarchFour
  128. {
  129.     class Player
  130.     {
  131.         public string Name { get; set; }
  132.         public int Health { get; set; }
  133.         public int BaseAttack { get; set; }
  134.  
  135.  
  136.         public Player() { }
  137.  
  138.         public Player(string name, int health, int baseAttack)
  139.         {
  140.             this.Name = name;
  141.             this.Health = health;
  142.             this.BaseAttack = baseAttack;
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement