diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index dffe441..8882245 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -8,12 +8,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - dotnet: [ '2.1', '5.0.x', '6.0.x' ] + dotnet: [ '7.0.x' ] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup .NET Core - uses: actions/setup-dotnet@v1 + uses: actions/setup-dotnet@v3 with: dotnet-version: ${{ matrix.dotnet-version }} include-prerelease: true @@ -24,4 +24,4 @@ jobs: - name: Run tests run: | cd CourseApp.Tests - dotnet test + dotnet test CourseApp.Tests.csproj diff --git a/CourseApp.Tests/CodeWarsTests.UnitTest.csproj b/CourseApp.Tests/CodeWarsTests.UnitTest.csproj new file mode 100644 index 0000000..b064bc8 --- /dev/null +++ b/CourseApp.Tests/CodeWarsTests.UnitTest.csproj @@ -0,0 +1,29 @@ + + + + net7.0 + enable + enable + + false + true + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/CourseApp.Tests/CodeWarsUnitTests/CamalCaseTest.cs b/CourseApp.Tests/CodeWarsUnitTests/CamalCaseTest.cs new file mode 100644 index 0000000..c482151 --- /dev/null +++ b/CourseApp.Tests/CodeWarsUnitTests/CamalCaseTest.cs @@ -0,0 +1,13 @@ +namespace CodeWarsTests.UnitTest; + +using CodWearsTests; +using Xunit; + +public class CamalCaseTest +{ + [Fact] + public void Test1() + { + Assert.Equal("theStealthWarrior", StrToCamelCase.ToCamelCase("the_stealth_warrior")); + } +} \ No newline at end of file diff --git a/CourseApp.Tests/DemoTest.cs b/CourseApp.Tests/CodeWarsUnitTests/DemoTest.cs similarity index 100% rename from CourseApp.Tests/DemoTest.cs rename to CourseApp.Tests/CodeWarsUnitTests/DemoTest.cs diff --git a/CourseApp.Tests/CodeWarsUnitTests/DuplicatesEncoderTest.cs b/CourseApp.Tests/CodeWarsUnitTests/DuplicatesEncoderTest.cs new file mode 100644 index 0000000..e9e2925 --- /dev/null +++ b/CourseApp.Tests/CodeWarsUnitTests/DuplicatesEncoderTest.cs @@ -0,0 +1,43 @@ +namespace CodeWarsTests.UnitTest; + +using CodWearsTests; +using Xunit; + +public class DuplicatesEncoderTest +{ + [Fact] + public void Test1() + { + // Act + var res = DuplicatesEncoder.DuplicateEncode("din"); + + Assert.Equal("(((", res); + } + + [Fact] + public void Test2() + { + // Act + var res = DuplicatesEncoder.DuplicateEncode("recede"); + + Assert.Equal("()()()", res); + } + + [Fact] + public void Test3() + { + // Act + var res = DuplicatesEncoder.DuplicateEncode("Success"); + + Assert.Equal(")())())", res); + } + + [Fact] + public void Test4() + { + // Act + var res = DuplicatesEncoder.DuplicateEncode("(( @"); + + Assert.Equal("))((", res); + } +} \ No newline at end of file diff --git a/CourseApp.Tests/CodeWarsUnitTests/FindMissingLetter.Test.cs b/CourseApp.Tests/CodeWarsUnitTests/FindMissingLetter.Test.cs new file mode 100644 index 0000000..51f9a61 --- /dev/null +++ b/CourseApp.Tests/CodeWarsUnitTests/FindMissingLetter.Test.cs @@ -0,0 +1,19 @@ +namespace CodeWarsTests.UnitTest; + +using CodWearsTests; +using Xunit; + +public class FindMissingLetter_Test +{ + [Fact] + public void Test1() + { + Assert.Equal('e', MissingLetter.FindMissingLetter(new[] { 'a', 'b', 'c', 'd', 'f' })); + } + + [Fact] + public void Test2() + { + Assert.Equal('P', MissingLetter.FindMissingLetter(new[] { 'O', 'Q', 'R', 'S' })); + } +} \ No newline at end of file diff --git a/CourseApp.Tests/CodeWarsUnitTests/Merge.Tests.cs b/CourseApp.Tests/CodeWarsUnitTests/Merge.Tests.cs new file mode 100644 index 0000000..0dc4829 --- /dev/null +++ b/CourseApp.Tests/CodeWarsUnitTests/Merge.Tests.cs @@ -0,0 +1,13 @@ +namespace CodeWarsTests.UnitTest; + +using CodWearsTests; +using Xunit; + +public class Merge_Tests +{ + [Fact] + public void Test1() + { + Assert.Equal(new string[] { "a", "1", "b", "2", "c", "3", "d", "e" }, Merge.MergeArrays(new string[] { "a", "b", "c", "d", "e" }, new string[] { "1", "2", "3" })); + } +} \ No newline at end of file diff --git a/CourseApp.Tests/CodeWarsUnitTests/SumOfDigigts.cs b/CourseApp.Tests/CodeWarsUnitTests/SumOfDigigts.cs new file mode 100644 index 0000000..9e02887 --- /dev/null +++ b/CourseApp.Tests/CodeWarsUnitTests/SumOfDigigts.cs @@ -0,0 +1,9 @@ +namespace CodeWarsTests.UnitTest; + +public class SumOfDigigts +{ + public int DigitalRoot(long n) + { + return 0; + } +} \ No newline at end of file diff --git a/CourseApp.Tests/CodeWarsUnitTests/TowerTest.cs b/CourseApp.Tests/CodeWarsUnitTests/TowerTest.cs new file mode 100644 index 0000000..e1500c3 --- /dev/null +++ b/CourseApp.Tests/CodeWarsUnitTests/TowerTest.cs @@ -0,0 +1,27 @@ +namespace CodeWarsTests.UnitTest; + +using CodWearsTests; +using Xunit; + +public class TowerTest +{ + [Fact] + public void Test1() + { + Assert.Equal(string.Join(",", new[] { "*" }), string.Join(",", Towerbuild.TowerBuilder(1))); + } + + [Fact] + public void Test2() + { + Assert.Equal(string.Join(",", new[] { " * ", "***" }), string.Join(",", Towerbuild.TowerBuilder(2))); + } + + [Fact] + public void Test3() + { + Assert.Equal( + string.Join(",", new[] { " * ", " *** ", "*****" }), + string.Join(",", Towerbuild.TowerBuilder(3))); + } +} \ No newline at end of file diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index e43252f..fd8366d 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0 True false @@ -29,4 +29,5 @@ + diff --git a/CourseApp/CodeWars/DigitalRootClass.cs b/CourseApp/CodeWars/DigitalRootClass.cs new file mode 100644 index 0000000..486687d --- /dev/null +++ b/CourseApp/CodeWars/DigitalRootClass.cs @@ -0,0 +1,9 @@ +namespace CodWearsTests; + +public class DigitalRootClass +{ + public static int DigitalRoot(long n) + { + return 0; + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/DuplicatesEncoder.cs b/CourseApp/CodeWars/DuplicatesEncoder.cs new file mode 100644 index 0000000..e0ffd80 --- /dev/null +++ b/CourseApp/CodeWars/DuplicatesEncoder.cs @@ -0,0 +1,19 @@ +namespace CodWearsTests; + +using System.Linq; +using System.Collections.Generic; + +public class DuplicatesEncoder +{ + public static string DuplicateEncode(string word) + { + var mass = new List(); + word = word.ToLower(); + foreach (var sym in word) + { + mass.Add(word.Count(x => x == sym) != 1 ? ")" : "("); + } + + return string.Join(string.Empty, mass); + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/FibNums.cs b/CourseApp/CodeWars/FibNums.cs new file mode 100644 index 0000000..63c7ea9 --- /dev/null +++ b/CourseApp/CodeWars/FibNums.cs @@ -0,0 +1,19 @@ +namespace CodWearsTests; + +public class FibNums +{ + public static ulong[] ProductFib(ulong prod) + { + ulong a = 0; + ulong b = 1; + + while (a * b < prod) + { + ulong temp = a; + a = b; + b = temp + b; + } + + return new ulong[] { a, b, (ulong)(a * b == prod ? 1 : 0) }; + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/Merge.cs b/CourseApp/CodeWars/Merge.cs new file mode 100644 index 0000000..0a5973e --- /dev/null +++ b/CourseApp/CodeWars/Merge.cs @@ -0,0 +1,37 @@ +namespace CodWearsTests; + +using System.Collections.Generic; + +public class Merge +{ + public static string[] MergeArrays(string[] arr1, string[] arr2) + { + var res = new List(); + var length = arr1.Length > arr2.Length ? arr2.Length : arr1.Length; + var index = 0; + + while (index < length) + { + res.Add(arr1[index]); + res.Add(arr2[index]); + index++; + } + + if (arr1.Length > arr2.Length) + { + for (; index < arr1.Length; index++) + { + res.Add(arr1[index]); + } + } + else + { + for (; index < arr2.Length; index++) + { + res.Add(arr2[index]); + } + } + + return res.ToArray(); + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/MissingLetter.cs b/CourseApp/CodeWars/MissingLetter.cs new file mode 100644 index 0000000..d976c55 --- /dev/null +++ b/CourseApp/CodeWars/MissingLetter.cs @@ -0,0 +1,24 @@ +namespace CodWearsTests; + +public class MissingLetter +{ + public static char FindMissingLetter(char[] chars) + { + var alphabet = "abcdefghijklmnopqrstuvwxyz"; + chars.ToString(); + + if (char.IsUpper(chars[0])) + { + alphabet = alphabet.ToUpper(); + } + + var index = alphabet.IndexOf(chars[0]); + + for (int i = 0; chars[i] == alphabet[index]; i++) + { + index++; + } + + return alphabet[index]; + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/StrToCamelCase.cs b/CourseApp/CodeWars/StrToCamelCase.cs new file mode 100644 index 0000000..6fd93a0 --- /dev/null +++ b/CourseApp/CodeWars/StrToCamelCase.cs @@ -0,0 +1,25 @@ +namespace CodWearsTests; + +public class StrToCamelCase +{ + public static string ToCamelCase(string str) + { + var res = string.Empty; + + str.ToCharArray(); + for (int i = 0; i < str.Length; i++) + { + if (str[i] != '_' && str[i] != '-') + { + res += str[i].ToString(); + } + else + { + res += str[i + 1].ToString().ToUpper(); + i += 1; + } + } + + return res; + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/TicTakToe.cs b/CourseApp/CodeWars/TicTakToe.cs new file mode 100644 index 0000000..81f398c --- /dev/null +++ b/CourseApp/CodeWars/TicTakToe.cs @@ -0,0 +1,36 @@ +namespace CodWearsTests; + +public class TicTacToe +{ + public int IsSolved(int[,] board) + { + for (int i = 0; i < 2; i++) + { + if (board[i, 0] == board[i, 1] && (board[i, 1] == board[i, 2]) && board[i, 1] != 0) + { + return board[i, 0]; + } + else if (board[0, i] == board[1, i] && board[1, i] == board[2, i] && board[1, i] != 0) + { + return board[i, 0]; + } + else if ((board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2] && board[1, 1] != 0) || + (board[2, 0] == board[1, 1] && board[1, 1] == board[0, 2] && board[1, 1] != 0)) + { + return board[1, 1]; + } + else + { + for (int k = 0; k < 2; k++) + { + if (board[i, k] == 0) + { + return -1; + } + } + } + } + + return 0; + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/Towerbuild.cs b/CourseApp/CodeWars/Towerbuild.cs new file mode 100644 index 0000000..3818ac4 --- /dev/null +++ b/CourseApp/CodeWars/Towerbuild.cs @@ -0,0 +1,20 @@ +namespace CodWearsTests; + +using System.Collections.Generic; + +public class Towerbuild +{ + public static string[] TowerBuilder(int nFloors) + { + var tower = new List(); + + for (int i = 0; i < nFloors; i++) + { + var padding = new string(' ', nFloors - i - 1); + var sharps = new string('*', (i * 2) + 1); + tower.Add($"{padding}{sharps}{padding}"); + } + + return tower.ToArray(); + } +} \ No newline at end of file diff --git a/CourseApp/CodeWars/Zeroes.cs b/CourseApp/CodeWars/Zeroes.cs new file mode 100644 index 0000000..b55bb08 --- /dev/null +++ b/CourseApp/CodeWars/Zeroes.cs @@ -0,0 +1,8 @@ +namespace CodWearsTests; + +using System.Linq; + +public class Zeroes +{ + public static int[] MoveZeroes(int[] array) => array.OrderBy(x => x == 0).ToArray(); +} \ No newline at end of file diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index eb22147..46ff5a1 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net7.0 True @@ -17,6 +17,10 @@ + + + + diff --git a/CourseApp/DogClass/Dog.cs b/CourseApp/DogClass/Dog.cs new file mode 100644 index 0000000..c6c4813 --- /dev/null +++ b/CourseApp/DogClass/Dog.cs @@ -0,0 +1,43 @@ +namespace CourseApp.DogClass; + +using System; +using System.Collections.Generic; +public class Dog +{ + private uint _age; + + private string _name; + + private List _flock; + + public string Name + { + get => _name; + + set => _name = value; + } + + public uint Age + { + get => _age; + + set => _age = value; + } + + public List Flock + { + get => _flock; + + set => _flock = value; + } + + public void PrintInfo() + { + Console.WriteLine($"Dog name is {this._name}, {this._name}'s age is {this._age}."); + Console.WriteLine("Flock contains:"); + foreach (var friend in this._flock) + { + Console.WriteLine($"{friend._name} {friend._age}"); + } + } +} \ No newline at end of file diff --git a/CourseApp/Phone.cs b/CourseApp/Phone.cs deleted file mode 100644 index 1c8e407..0000000 --- a/CourseApp/Phone.cs +++ /dev/null @@ -1,38 +0,0 @@ -namespace CourseApp -{ - using System; - - public class Phone - { - private float diaonal; - - public Phone(string name, float diagonal) - { - Name = name; - Diagonal = diagonal; - } - - public string Name { get; set; } - - public float Diagonal - { - get - { - return diaonal; - } - - set - { - if (value > 0 && value < 20) - { - this.diaonal = value; - } - } - } - - public void Show() - { - Console.WriteLine($"{Name} with diagonal {diaonal}"); - } - } -} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index 030f047..c88a02e 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,26 +1,31 @@ -namespace CourseApp -{ - using System; +namespace CourseApp; - public class Program +using System; +using System.Collections.Generic; +using RpgSaga; + +public class Program { public static void Main(string[] args) { - Phone phone1 = new Phone("iPhone", -7); - phone1.Show(); - phone1.Diagonal = 7; - phone1.Show(); - phone1.Diagonal = -16; - phone1.Show(); + var rnd = new Random(); - Phone tablet = new Phone("Android", 6); - tablet.Diagonal = 6; - tablet.Show(); - tablet.Diagonal = -10; - tablet.Show(); - tablet.Diagonal = 8; - tablet.Show(); - Console.WriteLine("Hello World"); + Hero visor = new Magican(); + for (int i = 0; i < 1000; i++) + { + var list = new List() + { + new Archer(), + new Magican(), + new Warrior(), + }; + var hero1 = list[rnd.Next(0, 3)]; + var hero2 = list[rnd.Next(0, 3)]; + if (hero1 != hero2) + { + visor.Fight(hero1, hero2); + Console.WriteLine("Fight finish \n\n"); + } + } } } -} diff --git a/CourseApp/RpgSaga/Archer.cs b/CourseApp/RpgSaga/Archer.cs new file mode 100644 index 0000000..9b914cf --- /dev/null +++ b/CourseApp/RpgSaga/Archer.cs @@ -0,0 +1,29 @@ +namespace CourseApp.RpgSaga; + +using System; + +public class Archer : Hero +{ + public Archer() + : base() + { + Type = "Archer"; + } + + protected override Hero Attack(Hero enemy) + { + if (!SpellUsed) + { + Console.WriteLine($"{Type} {Name} use spell and attack {enemy.Type} {enemy.Name}, damage = {Damage + 2}, {enemy.Type} {enemy.Name} health = {enemy.Health - Damage}"); + SpellUsed = true; + enemy.Health -= Damage; + } + else + { + enemy.Health -= Damage + 2; + Console.WriteLine($"{this.Type} {this.Name} attack {enemy.Type} {enemy.Name}, damage = {Damage + 2}, {enemy.Type} {enemy.Name} health = {enemy.Health - Damage}"); + } + + return enemy; + } +} \ No newline at end of file diff --git a/CourseApp/RpgSaga/Hero.cs b/CourseApp/RpgSaga/Hero.cs new file mode 100644 index 0000000..5ffad7c --- /dev/null +++ b/CourseApp/RpgSaga/Hero.cs @@ -0,0 +1,88 @@ +namespace CourseApp.RpgSaga; + +using System; +using System.Threading; + +public abstract class Hero +{ + private string[] names = new string[] + { + "Stan", + "Artur", + "Igor", + "Gerald", + "Samuel", + }; + + private Random _rand = new Random(); + + private string _name; + + private int _health; + + private int _damage; + + private bool _spellUsed; + + private string _type; + + public string Type + { + get => _type; + + set => _type = value; + } + + public bool SpellUsed + { + get => _spellUsed; + + set => _spellUsed = value; + } + + public string Name + { + get => _name; + + set => _name = value; + } + + public int Damage + { + get => _damage; + + set => _damage = value; + } + + public int Health + { + get => _health; + + set => _health = value; + } + +#pragma warning disable SA1201 + public Hero() +#pragma warning restore SA1201 + { + _name = names[_rand.Next(0, 4)]; + _health = _rand.Next(10, 100); + _damage = _rand.Next(5, 30); + _spellUsed = false; + } + + public void Fight(Hero person1, Hero person2) + { + if ((person1._health > 0) || (person2._health > 0)) + { + person2 = person1.Attack(person2); + Thread.Sleep(1000); + person1 = person2.Attack(person1); + Thread.Sleep(1000); + } + + Thread.Sleep(3000); + } + + protected abstract Hero Attack(Hero enemy); +} \ No newline at end of file diff --git a/CourseApp/RpgSaga/Magican.cs b/CourseApp/RpgSaga/Magican.cs new file mode 100644 index 0000000..74b1ff5 --- /dev/null +++ b/CourseApp/RpgSaga/Magican.cs @@ -0,0 +1,31 @@ +namespace CourseApp.RpgSaga; + +using System; + +public class Magican : Hero +{ + public Magican() + : base() + { + Type = "Magican"; + } + + protected override Hero Attack(Hero enemy) + { + if (!SpellUsed) + { + Console.WriteLine($"{this.Type} {this.Name} use spell and attack {enemy.Type} {enemy.Name}, damage = {this.Damage}, {enemy.Type} {enemy.Name} health = {enemy.Health - Damage}."); + Console.WriteLine($"{enemy.Type} {enemy.Name} attack {Type}, but damage = 0"); + Console.WriteLine($"{this.Type} {this.Name} attack {enemy.Type} {enemy.Name}, damage = {this.Damage}, {enemy.Type} {enemy.Name} health = {enemy.Health - Damage}"); + enemy.Health -= Convert.ToByte(Damage * 2); + SpellUsed = true; + } + else + { + enemy.Health -= this.Damage; + Console.WriteLine($"{this.Type} {this.Name} attack {enemy.Type} {enemy.Name}, damage = {this.Damage}, {enemy.Type} {enemy.Name} health = {enemy.Health - Damage}"); + } + + return enemy; + } +} \ No newline at end of file diff --git a/CourseApp/RpgSaga/Warrior.cs b/CourseApp/RpgSaga/Warrior.cs new file mode 100644 index 0000000..f28783d --- /dev/null +++ b/CourseApp/RpgSaga/Warrior.cs @@ -0,0 +1,31 @@ +namespace CourseApp.RpgSaga; + +using System; + +public class Warrior : Hero +{ + public Warrior() + : base() + { + Type = "Warrior"; + } + + public byte UseSpell() => (byte)Math.Round(Damage * 1.3); + + protected override Hero Attack(Hero enemy) + { + if (!SpellUsed) + { + Console.WriteLine($"{this.Type} {this.Name} use spell and attack {enemy.Type}, damage = {this.Damage}, {enemy.Type} {enemy.Name} health = {enemy.Health - UseSpell()}"); + enemy.Health -= UseSpell(); + SpellUsed = true; + } + else + { + enemy.Health -= this.Damage; + Console.WriteLine($"{this.Type} {this.Name} attack {enemy.Type}, damage = {this.Damage}, {enemy.Type} {enemy.Name} health = {enemy.Health - Damage}"); + } + + return enemy; + } +} \ No newline at end of file