ranee

Shuffle

Apr 11th, 2021 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp12
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] numbers = new int[] { 1, 2, 3, 4, 5 };
  14.             bool exit = false;
  15.             int surnameSearch = 0;
  16.             string[] menu = { "1. Вывести массив.", "2. Перемешать массив.", "3. Выйти." };
  17.             Console.CursorVisible = false;
  18.             while (!exit)
  19.             {
  20.                 DrawMenu(surnameSearch, menu);
  21.                 string selectMenuItems = ChangeDirection(ref surnameSearch, menu);
  22.                 Console.Clear();
  23.                 switch (selectMenuItems)
  24.                 {
  25.                     case "1. Вывести массив.":
  26.                         DrawaArray(numbers);
  27.                         break;
  28.                     case "2. Перемешать массив.":
  29.                         Shuffle(numbers);
  30.                         break;
  31.                     case "3. Выйти.":
  32.                         exit = true;
  33.                         break;
  34.                 }
  35.             }
  36.         }
  37.  
  38.         static void DrawLabels(string text)
  39.         {
  40.             Console.SetCursorPosition(30, 0);
  41.             Console.Write(text);
  42.         }
  43.  
  44.         static void DrawaArray(int[] array)
  45.         {
  46.             Console.SetCursorPosition(30, 0);
  47.             for (int i = 0; i < array.Length; i++)
  48.                 {
  49.                     Console.Write($"{array[i]} ");
  50.                 }
  51.         }
  52.  
  53.         static void Shuffle(int[] array)
  54.         {
  55.             Random rand = new Random();
  56.             for (int i = array.Length - 1; i >= 1; i--)
  57.             {
  58.                 int j = rand.Next(i + 1);
  59.  
  60.                 int tmp = array[j];
  61.                 array[j] = array[i];
  62.                 array[i] = tmp;
  63.             }
  64.         }
  65.  
  66.         static void DrawMenu(int surnameSearch, string[] items)
  67.         {
  68.             Console.SetCursorPosition(0, 0);
  69.             for (int i = 0; i < items.Length; i++)
  70.             {
  71.                 if (i == surnameSearch)
  72.                 {
  73.                     Console.BackgroundColor = ConsoleColor.Gray;
  74.                     Console.ForegroundColor = ConsoleColor.Black;
  75.                     Console.WriteLine(items[i]);
  76.                 }
  77.                 else
  78.                 {
  79.                     Console.WriteLine(items[i]);
  80.                 }
  81.                 Console.ResetColor();
  82.             }
  83.         }
  84.  
  85.         static string ChangeDirection(ref int surnameSearch, string[] items)
  86.         {
  87.             ConsoleKeyInfo key = Console.ReadKey();
  88.             switch (key.Key)
  89.             {
  90.                 case ConsoleKey.UpArrow:
  91.                     if (surnameSearch <= 0)
  92.                     {
  93.                         surnameSearch = items.Length - 1;
  94.                         DrawMenu(surnameSearch, items);
  95.                     }
  96.                     else
  97.                     {
  98.                         surnameSearch--;
  99.                         DrawMenu(surnameSearch, items);
  100.                     }
  101.                     break;
  102.                 case ConsoleKey.DownArrow:
  103.                     if (surnameSearch == items.Length - 1)
  104.                     {
  105.                         surnameSearch = 0;
  106.                         DrawMenu(surnameSearch, items);
  107.                     }
  108.                     else
  109.                     {
  110.                         surnameSearch++;
  111.                         DrawMenu(surnameSearch, items);
  112.                     }
  113.                     break;
  114.                 case ConsoleKey.Enter:
  115.                     {
  116.                         return items[surnameSearch];
  117.                     }
  118.             }
  119.             return "";
  120.         }
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment