ranee

Shuffle

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