Advertisement
SnowPhoenix347

5.2

Nov 7th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FifthProject
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             while (true)
  10.             {
  11.                 DrawMenu();
  12.                 int menu = ConvertToInt();
  13.                 switch (menu)
  14.                 {
  15.                     case 1:
  16.                         Console.WriteLine("Введите имя нового игрока");
  17.                         PlayersBase.AddNewPlayer(Console.ReadLine());
  18.                         break;
  19.                     case 2:
  20.                         Console.WriteLine("Введите ID игрока");
  21.                         PlayersBase.RemovePlayer(ConvertToInt());
  22.                         break;
  23.                     case 3:
  24.                         Console.WriteLine("Введите ID игрока");
  25.                         PlayersBase.BanPlayer(ConvertToInt());
  26.                         break;
  27.                     case 4:
  28.                         Console.WriteLine("Введите ID игрока");
  29.                         PlayersBase.UnBanPlayer(ConvertToInt());
  30.                         break;
  31.                     case 5:
  32.                         PlayersBase.ShowAllPlayers();
  33.                         Console.ReadKey();
  34.                         break;
  35.                 }
  36.                 Console.Clear();
  37.             }
  38.         }
  39.  
  40.         static int ConvertToInt()
  41.         {
  42.             int output;
  43.             bool enterIsCorrect = false;
  44.             do
  45.             {
  46.                 string input = Console.ReadLine();
  47.                 enterIsCorrect = int.TryParse(input, out output);
  48.                 if (!enterIsCorrect)
  49.                 {
  50.                     Console.WriteLine("Error enter. Try again");
  51.                 }
  52.             } while (!enterIsCorrect);
  53.  
  54.             return output;
  55.         }
  56.  
  57.         static void DrawMenu()
  58.         {
  59.             Console.WriteLine("\tMenu\n" +
  60.                               "1. Добавить игрока\n" +
  61.                               "2. Удалить игрока\n" +
  62.                               "3. Бан игрока\n" +
  63.                               "4. Разбан игрока\n" +
  64.                               "5. Показать всех игроков");
  65.         }
  66.     }
  67.  
  68.     class Player
  69.     {
  70.         private int _id;
  71.         private int _level;
  72.         private string _name;
  73.         private bool _ban;
  74.  
  75.         public bool Ban
  76.         {
  77.             set { _ban = value; }
  78.         }
  79.  
  80.         public Player(int id, int level, string name, bool ban)
  81.         {
  82.             _id = id;
  83.             _level = level;
  84.             _name = name;
  85.             _ban = ban;
  86.         }
  87.  
  88.         public override string ToString()
  89.         {
  90.             return _ban.ToString() + "\n" + _id + "\n" + _level + "\n" + _name;
  91.         }
  92.     }
  93.  
  94.     class PlayersBase
  95.     {
  96.         private static Player[] players = new Player[0];
  97.  
  98.         public static void AddNewPlayer(string name)
  99.         {
  100.             Player[] tempPlayers = new Player[players.Length + 1];
  101.  
  102.             for (int i = 0; i < tempPlayers.Length - 1; i++)
  103.             {
  104.                 tempPlayers[i] = players[i];
  105.             }
  106.  
  107.             tempPlayers[tempPlayers.Length - 1] = new Player(tempPlayers.Length - 1, 1, name, false);
  108.             players = tempPlayers;
  109.         }
  110.  
  111.         public static void RemovePlayer(int id)
  112.         {
  113.             players[id] = null;
  114.             Player[] tempPlayers = new Player[players.Length - 1];
  115.             for (int i = 0; i < players.Length - 1; i++)
  116.             {
  117.                 if (players[i] != null)
  118.                 {
  119.                     tempPlayers[i] = players[i];
  120.                 }
  121.                 else
  122.                 {
  123.                     tempPlayers[i] = players[i + 1];
  124.                 }
  125.             }
  126.  
  127.             players = tempPlayers;
  128.         }
  129.  
  130.         public static void BanPlayer(int id)
  131.         {
  132.             players[id].Ban = true;
  133.         }
  134.  
  135.         public static void UnBanPlayer(int id)
  136.         {
  137.             players[id].Ban = false;
  138.         }
  139.  
  140.         public static void ShowAllPlayers()
  141.         {
  142.             foreach (var player in players)
  143.             {
  144.                 Console.WriteLine(player);
  145.             }
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement