Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Clight_23_OOP_Aquarium
- {
- class Program
- {
- static void Main(string[] args)
- {
- int frameLines = 20;
- int frameRows = 10;
- char frameSymbol = '#';
- int maxLenghtFish = 3;
- int maxSumFish = 6;
- int maxHealth = 300;
- int maxLines = 50;
- int maxRows = 15;
- int stepOfLive = 1;
- Fish fish = new Fish();
- Draw drawFish = new Draw();
- Aquarium aquarium = new Aquarium(fish);
- bool isDraw = true;
- while (isDraw)
- {
- Console.CursorVisible = false;
- DrawFrame(frameLines, frameRows, frameSymbol);
- drawFish.Drawfish(aquarium.Fishs);
- aquarium.ShowAllInfo(maxLenghtFish, frameLines);
- Console.SetCursorPosition(0, frameRows + 1);
- Console.WriteLine("\nНажмите пробел, чтобы выполнить один шаг или зажмите, чтобы потекла жизнь.\n");
- Console.WriteLine("F1 - Добавить рыбу в акваирум");
- Console.WriteLine("F2 - Удалить рыбу из аквариума");
- Console.WriteLine("F3 - Изменить размеры аквариума.");
- Console.WriteLine("F5 - Выход.");
- int currentSumFish = aquarium.Fishs.Length;
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.Spacebar:
- aquarium.StepOfLive(frameLines, frameRows, stepOfLive);
- aquarium.CheckOfLive(maxHealth, frameRows);
- break;
- case ConsoleKey.F1:
- bool goodAdding = CheckSumFish(currentSumFish, maxSumFish, frameRows);
- if (goodAdding)
- {
- int userHealthFish = CheckInputUser("Введите уровень жизни рыбы", 1, maxHealth, frameRows);
- string userTexture = CheckInputUser($"Введите изображение рыбы, не более {maxLenghtFish} символов", frameRows, maxLenghtFish);
- aquarium.AddFish(userHealthFish, 1, 1, userTexture);
- }
- break;
- case ConsoleKey.F2:
- if (aquarium.Fishs.Length > 1)
- {
- int numFish = CheckInputUser("Введите номер рыбки", 1, currentSumFish, frameRows);
- aquarium.DelFish(numFish);
- }
- else
- {
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine("Пустой аквариум скучен.\nСначала добавьте еще рыбу - нажмите F1.");
- Console.ReadKey();
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- break;
- case ConsoleKey.F3:
- int currentLine = CheckInputUser("Введите длину аквариума", 5, maxLines, frameRows);
- Console.WriteLine();
- int currentRow = CheckInputUser("Введите ширину аквариума", 5, maxRows, frameRows);
- frameLines = currentLine;
- frameRows = currentRow;
- for (int i = 0; i < aquarium.Fishs.Length; i++)
- {
- aquarium.Fishs[i].DefaultCoord();
- }
- break;
- case ConsoleKey.F5:
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine("Пока!");
- Console.ReadKey();
- Environment.Exit(0);
- break;
- }
- }
- aquarium.StepOfLive(frameLines, frameRows, stepOfLive);
- aquarium.CheckOfLive(maxHealth, frameRows);
- System.Threading.Thread.Sleep(1000);
- Console.Clear();
- }
- }
- static void DrawFrame(int frameLine, int frameRow, char symbol)
- {
- for (int j = 0; j < frameRow; j++)
- {
- for (int i = 0; i < frameLine; i++)
- {
- if ((i == 0 || j == 0) || (i == frameLine - 1 || j == frameRow - 1))
- {
- Console.SetCursorPosition(i, j);
- Console.Write(symbol);
- }
- }
- Console.WriteLine();
- }
- }
- static int CheckInputUser(string text, int minValue, int maxValue, int frameRows)
- {
- bool goodCheckInput;
- bool isCheckInput = true;
- int valueInput = 1;
- Console.CursorVisible = true;
- while (isCheckInput)
- {
- Console.SetCursorPosition(0, frameRows + 9);
- Console.Write($"{text} от {minValue} до {maxValue}: ");
- string userInput = Console.ReadLine();
- goodCheckInput = Int32.TryParse(userInput, out int value);
- if (goodCheckInput != true || value < minValue || value > maxValue)
- {
- Console.WriteLine("Введите корректное число.");
- Console.ReadKey();
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- else
- {
- valueInput = value;
- isCheckInput = false;
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return valueInput;
- }
- static string CheckInputUser(string text, int frameRows, int maxLenghtFish)
- {
- bool isCheckInput = true;
- string stringInput = "х";
- Console.CursorVisible = true;
- while (isCheckInput)
- {
- Console.SetCursorPosition(0, frameRows + 9);
- Console.Write($"{text}: ");
- string userInput = Console.ReadLine();
- if (userInput.Length <= maxLenghtFish && userInput.Length > 0)
- {
- stringInput = userInput;
- isCheckInput = false;
- }
- else
- {
- Console.WriteLine("Введите корректное число.");
- Console.ReadKey();
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return stringInput;
- }
- static bool CheckSumFish(int currnetSumFish, int maxSumFish, int frameRows)
- {
- bool valueInput;
- Console.CursorVisible = true;
- if (currnetSumFish == maxSumFish)
- {
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine("Добавление рыбы превысит лимит аквариума.\nСначала удалите минимум одну рыбу, для этого нажмите F2.");
- Console.ReadKey();
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- valueInput = false;
- }
- else
- {
- valueInput = true;
- }
- return valueInput;
- }
- }
- class Aquarium
- {
- public Fish[] Fishs = new Fish[1];
- public Aquarium(Fish fish)
- {
- Fishs[0] = fish;
- }
- public void DelFish(int numFish)
- {
- Fish[] currentFishs = new Fish[Fishs.Length - 1];
- if (numFish == 1 && currentFishs.Length >= 1)
- {
- for (int i = 0; i < currentFishs.Length; i++)
- {
- currentFishs[i] = Fishs[i + 1];
- }
- Fishs = currentFishs;
- }
- else if (numFish == currentFishs.Length + 1 && currentFishs.Length >= 1)
- {
- for (int i = 0; i < currentFishs.Length; i++)
- {
- currentFishs[i] = Fishs[i];
- }
- Fishs = currentFishs;
- }
- else if (currentFishs.Length > 1)
- {
- for (int i = 0; i < numFish - 1; i++)
- {
- currentFishs[i] = Fishs[i];
- }
- for (int i = numFish - 1; i < currentFishs.Length; i++)
- {
- currentFishs[i] = Fishs[i + 1];
- }
- Fishs = currentFishs;
- }
- }
- public void ShowAllInfo(int maxLenghtFish, int frameLine)
- {
- for (int i = 0; i < Fishs.Length; i++)
- {
- Console.SetCursorPosition(frameLine + 3, i);
- Console.Write($"{i + 1}. ");
- Fishs[i].ShowInfo(maxLenghtFish);
- Console.WriteLine();
- }
- }
- public void AddFish(int health, int x, int y, string texture)
- {
- Fish currentFish = new Fish(health, x, y, texture);
- Fish[] currentFishs = new Fish[Fishs.Length + 1];
- for (int i = 0; i < Fishs.Length; i++)
- {
- currentFishs[i] = Fishs[i];
- }
- currentFishs[Fishs.Length] = currentFish;
- Fishs = currentFishs;
- }
- public void StepOfLive(int frameLines, int frameRows, int stepOfLive)
- {
- for (int i = 0; i < Fishs.Length; i++)
- {
- Fishs[i].SetFishCoord(frameLines, frameRows);
- Fishs[i].DecreaseHealth(stepOfLive);
- }
- }
- public void CheckOfLive(int maxHealth, int frameRows)
- {
- int deathFish = 0;
- for (int i = 0; i < Fishs.Length; i++)
- {
- if (Fishs[i].health <= 0)
- {
- deathFish++;
- }
- }
- for (int j = 0; j < deathFish; j++)
- {
- for (int i = 0; i < Fishs.Length; i++)
- {
- if (Fishs[i].health <= 0 && Fishs.Length > 1)
- {
- DelFish(i + 1);
- }
- else if (Fishs[i].health <= 0 && Fishs.Length <= 1)
- {
- Fishs[i].MaxHealth(maxHealth);
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine("Аквариум без рыб скучноват:)");
- Console.ReadKey();
- Console.SetCursorPosition(0, frameRows + 9);
- Console.WriteLine(" ");
- }
- }
- }
- }
- }
- class Fish
- {
- public int health { get; private set; }
- public int cursorX { get; private set; }
- public int cursorY { get; private set; }
- public string texture { get; private set; }
- public Fish(int health, int x, int y, string texture)
- {
- this.health = health;
- cursorX = x;
- cursorY = y;
- this.texture = texture;
- }
- public Fish()
- {
- health = 300;
- cursorX = 1;
- cursorY = 1;
- texture = "<->";
- }
- public void ShowInfo(int maxLenghtFish)
- {
- int currentLenght = maxLenghtFish - texture.Length;
- char[] spacingMassive = new char[currentLenght];
- for (int i = 0; i < currentLenght; i++)
- {
- spacingMassive[i] = ' ';
- }
- string spacing = new string(spacingMassive);
- Console.Write(texture + spacing + " - " + health);
- }
- public void SetFishCoord(int frameLines, int frameRows)
- {
- int dX = 0;
- int dY = 0;
- int currentX;
- int currentY;
- Random random = new Random();
- currentX = cursorX;
- currentY = cursorY;
- dX = random.Next(-1, 2);
- dY = random.Next(-1, 2);
- int lenghtFish = texture.Length;
- if ((cursorX + dX > 0 && cursorY + dY > 0) &&
- (cursorX + dX < frameLines - lenghtFish && cursorY + dY < frameRows - 1))
- {
- currentX += dX;
- currentY += dY;
- }
- cursorX = currentX;
- cursorY = currentY;
- }
- public void DefaultCoord()
- {
- cursorX = 1;
- cursorY = 1;
- }
- public void DecreaseHealth(int stepOfLive)
- {
- health -= stepOfLive;
- }
- public void MaxHealth(int maxHealth)
- {
- health = maxHealth;
- }
- }
- class Draw
- {
- public void Drawfish(Fish[] currentFish)
- {
- for (int i = 0; i < currentFish.Length; i++)
- {
- Console.SetCursorPosition(currentFish[i].cursorX, currentFish[i].cursorY);
- Console.Write(currentFish[i].texture);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement