Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions CourseApp.Tests/YearsOldTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using Xunit;

namespace CourseApp.Tests
{
public class YearsOldTest
{
[Fact]
public void TestAgeYesterday()
{
string st = $"Вам 10 года(лет), 0 месяца(месяцев) и 1 дня(дней)";
Assert.Equal(st, YearsOld.Age(new DateTime(2009, 12, 20), new DateTime(2019, 12, 21)));
}

[Fact]
public void TestAgeToday()
{
string st = $"Вам 8 года(лет), 0 месяца(месяцев) и 0 дня(дней)";
Assert.Equal(st, YearsOld.Age(new DateTime(2011, 12, 21), new DateTime(2019, 12, 21)));
}

[Fact]
public void TestAgeTommorow()
{
string st = $"Вам 10 года(лет), 0 месяца(месяцев) и 0 дня(дней)";
Assert.Equal(st, YearsOld.Age(new DateTime(2009, 12, 21), new DateTime(2019, 12, 21)));
}

[Fact]
public void TestAge()
{
string st = $"Вам 13 года(лет), 11 месяца(месяцев) и 22 дня(дней)";
Assert.Equal(st, YearsOld.Age(new DateTime(2000, 11, 20), new DateTime(2014, 11, 11)));
}

[Fact]
public void BirthdayAboveToday()
{
try
{
string years = YearsOld.Age(new DateTime(2019, 12, 21), new DateTime(2048, 8, 16));
Assert.Equal(0, DateTime.Compare(DateTime.Now, DateTime.Parse(years)));
}
catch (Exception)
{
Console.WriteLine("Birthday > Today");
}
}

[Fact]
public void TestYears()
{
if (YearsOld.Age(DateTime.Now) == YearsOld.Age(new DateTime(2019, 12, 25)))
{
Assert.True(true);
}
}
}
}
34 changes: 34 additions & 0 deletions CourseApp/YearsOld.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;

namespace CourseApp
{
public class YearsOld
{
public static string Age(DateTime date)
{
return Age(date, DateTime.Now);
}

public static string Age(DateTime fromDate, DateTime toDate)
{
if (fromDate.Ticks < toDate.Ticks)
{
DateTime dateCompare = new DateTime(toDate.Ticks - fromDate.Ticks);
return $"Вам {dateCompare.Year - 1} года(лет), {dateCompare.Month - 1} месяца(месяцев) и {dateCompare.Day - 1} дня(дней)";
}

throw new Exception();
}

public static DateTime YourAge()
{
int years = int.Parse(Console.ReadLine());
int months = int.Parse(Console.ReadLine());
int days = int.Parse(Console.ReadLine());
string result = Age(new DateTime(years, months, days), DateTime.Now);
DateTime resultTime = DateTime.Parse(result);
return resultTime;
}
}
}
24 changes: 24 additions & 0 deletions RPG/Archer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace RPG
{
public class Archer : Hero
{
Random random = new Random();

public Archer()
: base()
{
heroClass = "Archer";
skillName = "Fire arrows";
}

public override int Skill()
{
bufName = "Fire arrows";
buf = true;
bufDamage = 5;
return 0;
}
}
}
171 changes: 171 additions & 0 deletions RPG/Fight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System;
using System.Collections.Generic;

namespace RPG
{
public struct Fight
{
public Hero hero1;
public Hero hero2;
}

public class Fights
{

static Logger logger = new Logger();
static Random rnd = new Random();
static List<Fight> fight = new List<Fight>();
static int rounds = 0;

static void CreateFights()
{
while (Game.heroes.Count > 1)
{
int k = rnd.Next(0, Game.heroes.Count - 1);
Fight fights = new Fight();
fights.hero1 = Game.heroes[k];
Game.heroes.RemoveAt(k);

k = rnd.Next(0, Game.heroes.Count);
fights.hero2 = Game.heroes[k];
Game.heroes.RemoveAt(k);

fight.Add(fights);
}
}

static void Atack(Hero attacking, Hero attacked)
{
int damage;

if(attacking.buf)
{
damage = attacking.Atack();
logger.Atack(attacking, attacked, damage);
attacked.GetDamage(damage + attacking.bufDamage);
}
else
{
damage = attacking.Atack();
logger.Atack(attacking, attacked, damage);
attacked.GetDamage(damage);
}
}

static void Skill(Hero attacking, Hero attacked)
{
int damage;

if(attacking.buf || attacking.sleepTime > 0)
{
Atack(attacking, attacked);
}
else
{
damage = attacking.Skill();
logger.Skill(attacking, attacked, attacking.skillName, damage);
attacked.GetDamage(damage);
}
}

static void Win(Hero winner, Hero loser)
{
winner.sleepTime = 0;
winner.buf = false;
Game.heroes.Add(winner);
logger.Win(winner);
logger.Death(loser);
}

public static void Fight()
{
CreateFights();
int i = rnd.Next(0, fight.Count);
Hero hero1 = fight[i].hero1;
Hero hero2 = fight[i].hero2;
fight.RemoveAt(i);

int turn = rnd.Next(0, 1);
rounds++;
Console.WriteLine("_____________________________________________________________________________");
Console.WriteLine($"Round №{rounds}\n");
while (true)
{

if(turn == 0)
{
if(hero2.sleepTime == 0)
{
if(rnd.Next(0,10) > 6)
{
Skill(hero1, hero2);
}
else
{
Atack(hero1, hero2);
}
}
else
{
logger.Sleep(hero1);
hero2.sleepTime--;
}

if(hero2.Health <= 0)
{
Win(hero1, hero2);
if(fight.Count == 0)
{
break;
}

i = rnd.Next(0, fight.Count);
hero1 = fight[i].hero1;
hero2 = fight[i].hero2;
fight.RemoveAt(i);
}



turn = 1;
}
else
{
if(hero1.sleepTime == 0)
{
if(rnd.Next(0,10) > 6)
{
Skill(hero2, hero1);
}
else
{
Atack(hero2, hero1);
}
}
else
{
logger.Sleep(hero2);
hero1.sleepTime--;
}

if(hero1.Health <= 0)
{
Win(hero2, hero1);
if(fight.Count == 0)
{
break;
}
i = rnd.Next(0, fight.Count);
hero1 = fight[i].hero1;
hero2 = fight[i].hero2;
fight.RemoveAt(i);
}



turn = 0;
}
}
}
}
}
49 changes: 49 additions & 0 deletions RPG/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;

namespace RPG
{
public class Game
{
public static List<Hero> heroes = new List<Hero>();
Random rnd = new Random();

public void Start(int numOfHeroes)
{
CreateHeroes(numOfHeroes);
while(heroes.Count != 1)
{
Fights.Fight();
}

Console.WriteLine("_____________________________________________________________________________");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\nWinner - {heroes[0].Name} ({heroes[0].heroClass})");
Console.ResetColor();
}

public void CreateHeroes(int numOfHeroes)
{

while(numOfHeroes > 0)
{
switch(rnd.Next(0,3))
{
case 0:
heroes.Add(new Archer());
break;
case 1:
heroes.Add(new Knight());
break;
case 2:
heroes.Add(new Mage());
break;
default:
heroes.Add(new Mage());
break;
}
numOfHeroes--;
}
}
}
}
Loading