Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace Basic_Task23
- {
- class Program
- {
- public const int COUNT_FISH = 5;
- static void Main(string[] args)
- {
- Interface.DrawAquarium();
- Interface.DrawMenu();
- List<Fish> listFish = new List<Fish>();
- while (true)
- {
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.D1:
- if (listFish.Count < COUNT_FISH)
- {
- listFish.Add(new DogFish());
- }
- break;
- case ConsoleKey.D2:
- if (listFish.Count < COUNT_FISH)
- {
- listFish.Add(new DollarFish());
- }
- break;
- case ConsoleKey.D3:
- if (listFish.Count < COUNT_FISH)
- {
- listFish.Add(new PercentFish());
- }
- break;
- case ConsoleKey.D4:
- if (listFish.Count > 0)
- {
- listFish[0].Remove();
- listFish.RemoveAt(0);
- }
- break;
- case ConsoleKey.Escape:
- return;
- }
- }
- List<Fish> removedFish = new List<Fish>();
- foreach (Fish fish in listFish)
- {
- if (!fish.Tick())
- {
- removedFish.Add(fish);
- }
- }
- foreach (Fish fish in removedFish)
- {
- listFish.Remove(fish);
- }
- Interface.DrawListFish(listFish, COUNT_FISH);
- Thread.Sleep(200);
- }
- }
- }
- class Interface
- {
- public const int WIDTH = 30;
- public const int HEIGHT = 15;
- public static void DrawAquarium()
- {
- string line = "#";
- for (int i = 0; i <= WIDTH; i++)
- {
- Console.Write('#');
- }
- Console.WriteLine();
- for (int i = 1; i < WIDTH; i++)
- {
- line += ' ';
- }
- line += '#';
- for (int i = 1; i < HEIGHT; i++)
- {
- Console.WriteLine(line);
- }
- for (int i = 0; i <= WIDTH; i++)
- {
- Console.Write('#');
- }
- Console.WriteLine();
- }
- public static void DrawMenu()
- {
- Console.WriteLine("1. Добавить рыбу - @");
- Console.WriteLine("2. Добавить рыбу - $");
- Console.WriteLine("3. Добавить рыбу - %");
- Console.WriteLine("4. Убрать первую рыбку");
- Console.WriteLine("ESC. Выйти");
- }
- public static void DrawListFish(List<Fish> list, int count)
- {
- ClearListFish(count);
- for (int i = 0; i < list.Count; i++)
- {
- Console.SetCursorPosition(WIDTH + 5, i + 1);
- Console.WriteLine($"{i+1}. {list[i].Symbol} - {list[i].Age} лет");
- }
- }
- private static void ClearListFish(int count)
- {
- for (int i = 0; i < count; i++)
- {
- Console.SetCursorPosition(WIDTH + 5, i + 1);
- Console.WriteLine(" ");
- }
- }
- }
- abstract class Fish
- {
- const int MAX_AGE = 30;
- public int Age { get; set; }
- public char Symbol { get; set; }
- protected int _x;
- protected int _y;
- public Fish()
- {
- Random rnd = new Random();
- _x = rnd.Next(1, 30);
- _y = rnd.Next(1, 15);
- }
- public bool GrowOld()
- {
- return ++Age <= MAX_AGE;
- }
- public abstract void Swim();
- public bool Tick()
- {
- Remove();
- if (GrowOld())
- {
- Swim();
- Console.SetCursorPosition(_x, _y);
- Console.Write(Symbol);
- return true;
- } else
- {
- return false;
- }
- }
- public void Remove()
- {
- Console.SetCursorPosition(_x, _y);
- Console.Write(' ');
- }
- }
- class DogFish : Fish
- {
- private int _dx;
- private int _dy;
- public DogFish() : base()
- {
- Random rnd = new Random();
- _dx = rnd.Next(2);
- if (_dx == 0)
- {
- _dx = -1;
- }
- _dy = rnd.Next(2);
- if (_dy == 0)
- {
- _dy = -1;
- }
- Symbol = '@';
- }
- public override void Swim()
- {
- if (_x + _dx >= Interface.WIDTH || _x + _dx <= 0)
- {
- _dx = -_dx;
- }
- if (_y + _dy >= Interface.HEIGHT || _y + _dy <= 0)
- {
- _dy = -_dy;
- }
- _x += _dx;
- _y += _dy;
- }
- }
- class DollarFish : Fish
- {
- public DollarFish() : base()
- {
- Symbol = '$';
- }
- public override void Swim()
- {
- Random rnd = new Random();
- int dx = rnd.Next(2);
- if (dx == 0)
- {
- dx = -1;
- }
- int dy = rnd.Next(2);
- if (dy == 0)
- {
- dy = -1;
- }
- if (_x + dx >= Interface.WIDTH || _x + dx <= 0)
- {
- dx = -dx;
- }
- if (_y + dy >= Interface.HEIGHT || _y + dy <= 0)
- {
- dy = -dy;
- }
- _x += dx;
- _y += dy;
- }
- }
- class PercentFish : Fish
- {
- public PercentFish() : base()
- {
- Symbol = '%';
- }
- public override void Swim()
- {
- Random rnd = new Random();
- if (rnd.Next(5) == 0)
- {
- int dx = rnd.Next(0, Interface.WIDTH / 5);
- int dy = rnd.Next(0, Interface.HEIGHT / 5);
- if (rnd.Next(2) == 0)
- {
- dx = -dx;
- }
- if (rnd.Next(2) == 0)
- {
- dy = -dy;
- }
- if (_x + dx >= Interface.WIDTH || _x + dx <= 0)
- {
- dx = -dx;
- }
- if (_y + dy >= Interface.HEIGHT || _y + dy <= 0)
- {
- dy = -dy;
- }
- _x += dx;
- _y += dy;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement