diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1ff692c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/CourseApp/bin/Debug/net6.0/CourseApp.dll", + "args": [], + "cwd": "${workspaceFolder}/CourseApp", + "console": "integratedTerminal", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index e2cc5c5..009e1aa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,8 +4,8 @@ "**/.svn": true, "**/CVS": true, "**/.DS_Store": true, - "CourseApp": true, - "CourseApp.Tests":true, + "CourseApp": false, + "CourseApp.Tests":false, "WebApplication":true } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..742612d --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/CourseApp/CourseApp.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/CourseApp/CourseApp.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/CourseApp/CourseApp.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/CourseApp/Game.cs b/CourseApp/Game.cs new file mode 100644 index 0000000..924cc66 --- /dev/null +++ b/CourseApp/Game.cs @@ -0,0 +1,152 @@ +namespace CourseApp +{ + using System; + using System.Collections.Generic; + + public class Game + { + public static void Start() + { + int p_number = AskNumber(); + List playerList = PlayerListGenerator(p_number); + PlayGame(playerList); + } + + private static void PlayGame(List playerList) + { + for (int i = 1; playerList.Count != 1; i++) + { + Logger.WriteRound(i); + RandomizeList(playerList); + PlayRound(playerList); + } + + Logger.WriteWinner(playerList[0]); + } + + private static void PlayRound(List playerList) + { + for (int i = 0; i < playerList.Count / 2; i++) + { + Player[] fightMembers = { playerList[i * 2], playerList[(i * 2) + 1] }; + Logger.WriteFight(fightMembers); + playerList[i * 2] = PlayFight(fightMembers); + } + + for (int i = 1; i < playerList.Count; i++) + { + playerList.RemoveAt(i); + } + } + + private static Player PlayFight(Player[] fightMembers) + { + for (int i = 0; true; i++) + { + var playerStatus = fightMembers[i % 2].CheckStatus(); + Logger.WriteAction(fightMembers[i % 2], playerStatus); + bool checkDeath = fightMembers[i % 2].CheckDeath(); + if (checkDeath) + { + fightMembers[(i + 1) % 2].Update(); + return fightMembers[(i + 1) % 2]; + } + + if (playerStatus.Contains(Tuple.Create("Заворожение", .0f))) + { + continue; + } + + Tuple playerAction = PlayerDoAction(fightMembers[i % 2]); + Logger.WriteAction(fightMembers[i % 2], fightMembers[(i + 1) % 2], playerAction); + if ((playerAction.Item1 != "наносит урон") && (playerAction.Item1 != "Удар возмездия")) + { + fightMembers[(i + 1) % 2].SetDebaff(playerAction.Item1); + } + + checkDeath = fightMembers[(i + 1) % 2].GetDamage(playerAction.Item2); + if (checkDeath) + { + Logger.WriteDeath(fightMembers[(i + 1) % 2]); + fightMembers[i % 2].Update(); + return fightMembers[i % 2]; + } + } + } + + private static Tuple PlayerDoAction(Player inputP) + { + Random rnd = new Random(); + int chosen = rnd.Next(0, 4); + switch (chosen) + { + case 0: + return inputP.Ability(); + default: + return inputP.Attack(); + } + } + + private static int AskNumber() + { + while (true) + { + Console.WriteLine("Введите четное количество игроков:"); + int p_number = Convert.ToInt32(Console.ReadLine()); + if (p_number <= 0) + { + Console.WriteLine("Число игроков должно быть больше 0!"); + } + else if (p_number % 2 != 0) + { + Console.WriteLine("Число игроков должно быть четным!"); + } + else + { + return p_number; + } + } + } + + private static void RandomizeList(List input) + { + Random rnd = new Random(); + for (int i = 0; i < input.Count; i++) + { + int chosen = rnd.Next(i, input.Count); + (input[i], input[chosen]) = (input[chosen], input[i]); + } + } + + private static List PlayerListGenerator(int count) + { + List playerList = new List(); + for (int i = 0; i < count; i++) + { + playerList.Add(PlayerGenerator()); + } + + return playerList; + } + + private static Player PlayerGenerator() + { + string[] names = { "Вальлиикт", "Йоророу", "Бенинэл", "Билелгар", "Вилдровер", "Бреновуд", "Труеон", "Филдис", "Грейнерэл", "Бертэам", "Ирвдес", "Франкернон", "Куртдитор" }; + Random rnd = new Random(); + int health = (int)rnd.NextInt64(1, 64); + int strength = (int)rnd.NextInt64(1, 64); + int chouse = (int)rnd.NextInt64(0, 3); + switch (chouse) + { + case 0: + return new Knight(health, strength, names[rnd.Next(names.Length)]); + case 1: + return new Mage(health, strength, names[rnd.Next(names.Length)]); + case 2: + return new Archer(health, strength, names[rnd.Next(names.Length)]); + default: + return new Knight(health, strength, names[rnd.Next(names.Length)]); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/GameClasses/Archer.cs b/CourseApp/GameClasses/Archer.cs new file mode 100644 index 0000000..d654050 --- /dev/null +++ b/CourseApp/GameClasses/Archer.cs @@ -0,0 +1,30 @@ +namespace CourseApp +{ + using System; + + public class Archer : Player + { + public Archer(int health, int strength, string name) + : base(health, strength, name, "Огненная стрела", 1) + { + } + + public override string ToString() + { + return "(Лучник) " + Name; + } + + public override Tuple Ability() + { + if (AbilityLeft > 0) + { + AbilityLeft--; + return Tuple.Create(AbilityName, 0.0f); + } + else + { + return Attack(); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/GameClasses/Knight.cs b/CourseApp/GameClasses/Knight.cs new file mode 100644 index 0000000..2e72c76 --- /dev/null +++ b/CourseApp/GameClasses/Knight.cs @@ -0,0 +1,30 @@ +namespace CourseApp +{ + using System; + + public class Knight : Player + { + public Knight(int health, int strength, string name) + : base(health, strength, name, "Удар возмездия", 1) + { + } + + public override string ToString() + { + return "(Рыцарь) " + Name; + } + + public override Tuple Ability() + { + if (AbilityLeft > 0) + { + AbilityLeft--; + return Tuple.Create(AbilityName, (float)Math.Round(Strength * 1.3f, 2)); + } + else + { + return Attack(); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/GameClasses/Mage.cs b/CourseApp/GameClasses/Mage.cs new file mode 100644 index 0000000..70f9eed --- /dev/null +++ b/CourseApp/GameClasses/Mage.cs @@ -0,0 +1,30 @@ +namespace CourseApp +{ + using System; + + public class Mage : Player + { + public Mage(int health, int strength, string name) + : base(health, strength, name, "Заворожение", 1) + { + } + + public override string ToString() + { + return "(Маг) " + Name; + } + + public override Tuple Ability() + { + if (AbilityLeft > 0) + { + AbilityLeft--; + return Tuple.Create(AbilityName, 0.0f); + } + else + { + return Attack(); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Logger.cs b/CourseApp/Logger.cs new file mode 100644 index 0000000..01e9251 --- /dev/null +++ b/CourseApp/Logger.cs @@ -0,0 +1,67 @@ +namespace CourseApp +{ + using System; + using System.Collections.Generic; + + public static class Logger + { + public static void WriteWinner(Player winner) + { + Console.WriteLine($"{winner.ToString()} ПОБЕДИЛ!"); + } + + public static void WriteRound(int round) + { + Console.WriteLine($"Раунд {round}."); + } + + public static void WriteFight(Player[] fightMembers) + { + Console.WriteLine($"{fightMembers[0].ToString()} VS {fightMembers[1].ToString()}"); + } + + public static void WriteAction(Player firstP, Player secondP, Tuple playerAction) + { + switch (playerAction.Item1) + { + case "наносит урон": + Console.WriteLine($"{firstP.ToString()} наносит урон {playerAction.Item2} противнику {secondP.ToString()}"); + break; + case "Удар возмездия": + Console.WriteLine($"{firstP.ToString()} применяет ({playerAction.Item1}) и наносит урон {playerAction.Item2} противнику {secondP.ToString()}"); + break; + case "Огненная стрела": + Console.WriteLine($"{firstP.ToString()} применяет ({playerAction.Item1}) по противнику {secondP.ToString()}"); + break; + case "Заворожение": + Console.WriteLine($"{firstP.ToString()} применяет ({playerAction.Item1}) по противнику {secondP.ToString()}"); + break; + } + } + + public static void WriteAction(Player inputP, List> playerStatus) + { + foreach (var debaff in playerStatus) + { + switch (debaff.Item1) + { + case "Огненная стрела": + Console.WriteLine($"{inputP.ToString()} получает периодический урон {debaff.Item2} от ({debaff.Item1})"); + break; + case "Заворожение": + Console.WriteLine($"{inputP.ToString()} пропускает ход из-за ({debaff.Item1})"); + break; + case "PlayerIsDied": + WriteDeath(inputP); + break; + } + } + } + + public static void WriteDeath(Player inputP) + { + Console.WriteLine($"{inputP.ToString()} погибает"); + Console.WriteLine(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Player.cs b/CourseApp/Player.cs new file mode 100644 index 0000000..bfcbb8d --- /dev/null +++ b/CourseApp/Player.cs @@ -0,0 +1,100 @@ +namespace CourseApp +{ + using System; + using System.Collections.Generic; + using System.Linq; + + public abstract class Player + { + private List debaffs; + + public Player(int maxHealth, int strength, string name, string abilityName, int maxAbilityUsages) + { + this.MaxHealth = maxHealth; + this.CurrentHealth = maxHealth; + this.Strength = strength; + this.Name = name; + this.AbilityName = abilityName; + this.MaxAbilityUsages = maxAbilityUsages; + this.AbilityLeft = maxAbilityUsages; + this.debaffs = new List(); + } + + public float MaxHealth { get; protected set; } + + public float CurrentHealth { get; protected set; } + + public float Strength { get; protected set; } + + public string Name { get; protected set; } + + public int MaxAbilityUsages { get; protected set; } + + public int AbilityLeft { get; protected set; } + + public string AbilityName { get; protected set; } + + public abstract Tuple Ability(); + + public Tuple Attack() + { + return Tuple.Create("наносит урон", Strength); + } + + public bool GetDamage(float damage) + { + CurrentHealth -= damage; + return CheckDeath(); + } + + public void SetDebaff(string debuffName) + { + debaffs.Add(debuffName); + } + + public List> CheckStatus() + { + List> returnedList = new List>(); + string[] buffer = new string[debaffs.Count]; + debaffs.CopyTo(buffer); + foreach (string debuffName in buffer) + { + switch (debuffName) + { + case "Огненная стрела": + returnedList.Add(Tuple.Create("Огненная стрела", 2.0f)); + if (GetDamage(returnedList.Last().Item2)) + { + returnedList.Add(Tuple.Create("PlayerIsDied", 1.0f)); + return returnedList; + } + + break; + case "Заворожение": + returnedList.Add(Tuple.Create("Заворожение", .0f)); + debaffs.RemoveAt(debaffs.IndexOf(debuffName)); + break; + } + } + + return returnedList; + } + + public bool CheckDeath() + { + if (CurrentHealth <= 0) + { + return true; + } + + return false; + } + + public void Update() + { + AbilityLeft = MaxAbilityUsages; + CurrentHealth = MaxHealth; + debaffs.Clear(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index d6d2c87..ab6cbb4 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,12 +1,10 @@ namespace CourseApp { - using System; - public class Program { public static void Main(string[] args) { - Console.WriteLine("Hello World"); + Game.Start(); } } }