Guest User

Untitled

a guest
Feb 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace CombatSimCode
  7. {
  8.     class Combat
  9.     {
  10.         static public bool Battle(int attackUnits, int defendUnits)
  11.         {
  12.             // Create random number genereator
  13.             Random rand = new Random();      
  14.  
  15.             // Dice totals
  16.             int attackTotal = 0;
  17.             int defendTotal = 0;
  18.  
  19.             // Attackers Dice
  20.             int attackDice = 0;
  21.  
  22.             // Deneders Dice
  23.             int defendDice = 0;
  24.  
  25.             // Get a random value for each unit that attacks
  26.             for (int i = 0; i < attackUnits; i++)
  27.             {
  28.                 attackDice = rand.Next(1, 7);
  29.                 // Add the result to the total
  30.                 attackTotal += attackDice;
  31.                 // Reset to 0 for the next dice
  32.                 attackDice = 0;
  33.             }
  34.  
  35.             // Get a random value for every defender that defends
  36.             for (int i = 0; i < defendUnits; i++)
  37.             {
  38.                 defendDice = rand.Next(1, 7);
  39.                 // Add the result to the total
  40.                 defendTotal += defendDice;
  41.                 // Reset to 0 for the next dice
  42.                 defendDice = 0;
  43.             }
  44.  
  45.             // Display---------------------------------------------------------------------
  46.             // Display attackers result
  47.             Console.Write("Attackers rolled:  ");
  48.             Console.WriteLine(attackTotal);
  49.  
  50.             // Display defenders result
  51.             Console.Write("Defenders rolled:  ");
  52.             Console.WriteLine(defendTotal);
  53.  
  54.             // If the Attacker is higher he wins, if the defender is drawing or higher he wins
  55.             if (attackTotal > defendTotal)
  56.             {
  57.                 Console.WriteLine("Attackers Win!");
  58.                 return true;
  59.             }
  60.             else
  61.             {
  62.                 Console.WriteLine("Defenders Win!");
  63.                 return false;
  64.             }
  65.         }
  66.  
  67.         static public void AssigningLand(bool attackSuccess, int attackingLandIndex, int defendingLandIndex, Land[] landArray)
  68.         {
  69.             // If attckers win
  70.             if (attackSuccess)
  71.             {
  72.                 // Swap Teams, if team 1 change to team 0
  73.                 if (landArray[defendingLandIndex].team == 1)
  74.                 {
  75.                     landArray[defendingLandIndex].team = 0;
  76.                 }
  77.                 else
  78.                 {
  79.                     // If team 0 then change to team 1
  80.                     landArray[defendingLandIndex].team = 1;
  81.                 }
  82.  
  83.                 // The attacking units go into the defending land minus 1
  84.                 landArray[defendingLandIndex].units = landArray[attackingLandIndex].units - 1;
  85.  
  86.                 // Leave one unit behind from the attacking team
  87.                 landArray[attackingLandIndex].units = 1;            
  88.             }
  89.             // If the attack fails
  90.             else
  91.             {
  92.                 // Leave one unit behind from the attacking team
  93.                 landArray[attackingLandIndex].units = 1;
  94.             }
  95.         }
  96.     }
  97. }
Add Comment
Please, Sign In to add comment