Skip to content

Instantly share code, notes, and snippets.

@rdeioris
Created November 9, 2017 17:19
Show Gist options
  • Save rdeioris/1fa8672e4a0f36a39d995b7d4002dc15 to your computer and use it in GitHub Desktop.
Save rdeioris/1fa8672e4a0f36a39d995b7d4002dc15 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BattleSimulator
{
class Program
{
public struct Army
{
public string Name;
public Warrior[] Warriors;
public int Xp;
}
public struct Warrior
{
public float X;
public float Y;
public string Name;
public int Hp;
public int Strength;
public int Xp;
}
// find the first alive warrior from an array of warriors
static int GetFirstAliveWarrior(Warrior[] warriors)
{
for (int i = 0; i < warriors.Length; i++)
{
if (warriors[i].Hp > 0)
{
return i;
}
}
return -1;
}
static bool Attack(ref Army attacker, ref Army defender)
{
int attackerWarriorIndex = GetFirstAliveWarrior(attacker.Warriors);
// no alive warrior found
if (attackerWarriorIndex < 0)
{
return true;
}
int defenderWarriorIndex = GetFirstAliveWarrior(defender.Warriors);
// no alive warrior found
if (defenderWarriorIndex < 0)
{
return true;
}
int attackerStrength = attacker.Warriors[attackerWarriorIndex].Strength;
int attackerXp = attacker.Warriors[attackerWarriorIndex].Xp;
defender.Warriors[defenderWarriorIndex].Hp -= attackerStrength * (1 + attackerXp);
// if the defender dies, increase experience of the attacker
if (defender.Warriors[defenderWarriorIndex].Hp <= 0)
{
attacker.Warriors[attackerWarriorIndex].Xp += 1;
}
int attackerHp = attacker.Warriors[attackerWarriorIndex].Hp;
attackerXp = attacker.Warriors[attackerWarriorIndex].Xp;
Console.WriteLine("Attacker Index: " + attackerWarriorIndex + " Hp: " + attackerHp + " Xp: " + attackerXp + " Strength: " + attackerStrength);
int defenderHp = defender.Warriors[defenderWarriorIndex].Hp;
int defenderStrength = defender.Warriors[defenderWarriorIndex].Strength;
int defenderXp = defender.Warriors[defenderWarriorIndex].Xp;
Console.WriteLine("Defender Index: " + defenderWarriorIndex + " Hp: " + defenderHp + " Xp: " + defenderXp + " Strength: " + defenderStrength);
return false;
}
static void Main(string[] args)
{
Random random = new Random();
Army redArmy = new Army();
Army blueArmy = new Army();
redArmy.Name = "Red";
blueArmy.Name = "Blue";
int numberOfWarriors = random.Next(10, 20);
redArmy.Warriors = new Warrior[numberOfWarriors];
numberOfWarriors = random.Next(10, 20);
blueArmy.Warriors = new Warrior[numberOfWarriors];
Console.WriteLine("Red Army: " + redArmy.Warriors.Length);
Console.WriteLine("Blue Army: " + blueArmy.Warriors.Length);
// iterate all warriors for red
for (int i = 0; i < redArmy.Warriors.Length; i++)
{
redArmy.Warriors[i].Hp = random.Next(90, 130);
redArmy.Warriors[i].Strength = random.Next(20, 60);
}
// iterate all warriors for blue
for (int i = 0; i < blueArmy.Warriors.Length; i++)
{
blueArmy.Warriors[i].Hp = random.Next(90, 130);
blueArmy.Warriors[i].Strength = random.Next(20, 60);
}
while (true)
{
// red attacks blue
if (Attack(ref redArmy, ref blueArmy))
{
break;
}
Console.WriteLine();
// blue attacks red
if (Attack(ref blueArmy, ref redArmy))
{
break;
}
Console.WriteLine();
Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment