Added scr for lab-03.

This commit is contained in:
2026-09-23 13:44:41 +03:00
parent fc6cb72712
commit 4890bda889
+201
View File
@@ -0,0 +1,201 @@
//==================== Main
//==============================================
IsReady();
//==================== System fun
//==============================================
static void PrintError(Exception e)
{
//Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: {e.Message}");
Console.ResetColor();
Console.WriteLine("Press any key to continue...\n");
Console.ReadKey();
Console.Clear();
}
static void ShowTask(byte number, bool isStart = false)
{
if (isStart == false)
{
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
Console.Clear();
Console.WriteLine($"Task №{number}");
}
static void Br(byte counter = 1)
{
for (byte i = 0; i < counter; i++)
Console.WriteLine();
}
// ==================== Modul1 fun
// ==============================================
static void StartFight()
{
string
pokemonName1
,pokemonName2;
short
hp1
,hp2
,attack1
,attack2;
InputParams(out pokemonName1, out hp1, out attack1, out pokemonName2, out hp2, out attack2);
ShowTimeCounting();
GetPokemonInfo(pokemonName1, hp1, attack1, pokemonName2, hp2, attack2);
}
static void ShowTimeCounting()
{
const byte counter = 3;
const short ticks = 1000;
Console.WriteLine("Ready!");
for (byte i = 1; i <= counter; Console.Write($"\r{i++}..."))
Thread.Sleep(ticks);
Br(2);
Console.WriteLine("=== THE FIGHT BEGINS ===");
}
static void GetPokemonInfo(string pokemonName1, short hp1, short attack1, string pokemonName2, short hp2, short attack2)
{
Console.WriteLine($"{pokemonName1} (HP: {hp1} | Attack: {attack1}) VS {pokemonName2} (HP: {hp2} | Attack: {attack2})");
}
static void InputParams(out string pokemonName1, out short hp1, out short attack1, out string pokemonName2,
out short hp2, out short attack2)
{
const short
maxHp = 2000
,maxAttack = 500
,minHp = 0
,minAttack = 1;
Console.Write("Enter Pokemon name for player one!\nName1: ");
pokemonName1 = Console.ReadLine() ?? "Player1";
if (pokemonName1 == string.Empty)
pokemonName1 = "Player1";
while (true)
{
try
{
Console.Write("\nEnter the attack power for Pokemon1!\nAttack: ");
attack1 = short.Parse(Console.ReadLine());
if (attack1 < minAttack || attack1 > maxAttack)
throw new Exception($"The attack can be between {minAttack} and {maxAttack}.");
break;
}
catch (Exception e) {PrintError(e);}
}
while (true)
{
try
{
Console.Write("\nEnter the number of hp for Pokemon1!\nHP: ");
hp1 = short.Parse(Console.ReadLine());
if (hp1 < minHp || hp1 > maxHp)
throw new Exception($"The hp can be between {minHp} and {maxHp}.");
break;
}
catch (Exception e) {PrintError(e);}
}
Console.Clear();
Console.Write("Enter Pokemon name for player two!\nName2: ");
pokemonName2 = Console.ReadLine() ?? "Player2";
if (pokemonName2 == string.Empty)
pokemonName2 = "Player2";
while (true)
{
try
{
Console.Write("\nEnter the attack power for Pokemon2!\nAttack: ");
attack2 = short.Parse(Console.ReadLine());
if (attack2 < minAttack || attack2 > maxAttack)
throw new Exception($"The attack can be between {minAttack} and {maxAttack}.");
break;
}
catch (Exception e) {PrintError(e);}
}
while (true)
{
try
{
Console.Write("\nEnter the number of hp for Pokemon2!\nHP: ");
hp2 = short.Parse(Console.ReadLine());
if (hp2 < minHp || hp2 > maxHp)
throw new Exception($"The hp can be between {minHp} and {maxHp}.");
break;
}
catch (Exception e) {PrintError(e);}
}
Console.Clear();
}
// ==================== Modul2 fun
// ==============================================
static void IsReady()
{
if (IsCorrect())
{
StartFight();
return;
}
Console.WriteLine("Are you giving up!");
}
static bool IsCorrect()
{
const byte
fixValue1 = 0
,fixValue2 = 1;
do
{
try
{
Console.Write("You are ready to start?\t[1 - Yes | 0 - No]\n[Player]: ");
byte isReady = byte.Parse(Console.ReadLine());
if (isReady != fixValue1 && isReady != fixValue2)
throw new Exception($"Incorrect input. Value must be {fixValue1} or {fixValue2}.");
Console.Clear();
if (isReady == fixValue1)
return false;
return true;
}
catch (Exception e) {PrintError(e);}
} while (true);
}