ranee

Shuffle

Apr 16th, 2021 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 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 DrawaArray(int[] array)
  40.         {
  41.             Console.SetCursorPosition(30, 0);
  42.             for (int i = 0; i < array.Length; i++)
  43.             {
  44.                 Console.Write($"{array[i]} ");
  45.             }
  46.         }
  47.  
  48.         static void Shuffle(int[] array)
  49.         {
  50.             Random rand = new Random();
  51.             for (int i = array.Length - 1; i >= 1; i--)
  52.             {
  53.                 int j = rand.Next(0, array.Length);
  54.  
  55.                 int temp = array[j];
  56.                 array[j] = array[i];
  57.                 array[i] = temp;
  58.             }
  59.         }
  60.  
  61.         static void DrawMenu(int surnameSearch, string[] items)
  62.         {
  63.             Console.SetCursorPosition(0, 0);
  64.             for (int i = 0; i < items.Length; i++)
  65.             {
  66.                 if (i == surnameSearch)
  67.                 {
  68.                     Console.BackgroundColor = ConsoleColor.Gray;
  69.                     Console.ForegroundColor = ConsoleColor.Black;
  70.                     Console.WriteLine(items[i]);
  71.                 }
  72.                 else
  73.                 {
  74.                     Console.WriteLine(items[i]);
  75.                 }
  76.                 Console.ResetColor();
  77.             }
  78.         }
  79.  
  80.         static string GetChoise(string[] items, int menuBar)
  81.         {
  82.             return items[menuBar];
  83.         }
  84.  
  85.         static void ChangeDirection(ref int menuBar, string[] items, out string selectMenuParagraph)
  86.         {
  87.             selectMenuParagraph = "";
  88.             ConsoleKeyInfo key = Console.ReadKey();
  89.             switch (key.Key)
  90.             {
  91.                 case ConsoleKey.UpArrow:
  92.                     if (menuBar <= 0)
  93.                     {
  94.                         menuBar = items.Length - 1;
  95.                         DrawMenu(menuBar, items);
  96.                     }
  97.                     else
  98.                     {
  99.                         menuBar--;
  100.                         DrawMenu(menuBar, items);
  101.                     }
  102.                     break;
  103.                 case ConsoleKey.DownArrow:
  104.                     if (menuBar == items.Length - 1)
  105.                     {
  106.                         menuBar = 0;
  107.                         DrawMenu(menuBar, items);
  108.                     }
  109.                     else
  110.                     {
  111.                         menuBar++;
  112.                         DrawMenu(menuBar, items);
  113.                     }
  114.                     break;
  115.                 case ConsoleKey.Enter:
  116.                     {
  117.                         selectMenuParagraph = GetChoise(items, menuBar);
  118.                     }
  119.                     break;
  120.             }
  121.         }
  122.     }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment