Добавьте файлы проекта.

This commit is contained in:
DIvan-Notebook
2024-11-29 15:35:19 +04:00
parent 8912302f8b
commit 583e839414
8 changed files with 975 additions and 0 deletions

100
Core/Menu.cs Normal file
View File

@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace laba3.Core
{
/// <summary>
/// TUI menu;
/// </summary>
internal class Menu
{
private List<Option> options;
private int selected;
private string title;
/// <summary>
/// Constructor of menu;
/// </summary>
/// <param name="title">Title of menu</param>
public Menu(string title)
{
this.title = title;
this.options = new List<Option> { };
}
/// <summary>
/// Add option to menu;
/// </summary>
/// <param name="name">Name of option</param>
/// <param name="action">Action, runs if option selected</param>
public void AddOption(string name, Action action)
{
this.options.Add(new Option(name, action));
}
/// <summary>
/// Run menu;
/// Console will be clean!;
/// </summary>
public void RunMenu()
{
selected = 0;
this.PrintMenu();
ConsoleKeyInfo keyinfo;
do
{
keyinfo = Console.ReadKey();
if (keyinfo.Key == ConsoleKey.DownArrow)
{
if (selected + 1 < options.Count)
{
selected++;
this.PrintMenu();
}
}
if (keyinfo.Key == ConsoleKey.UpArrow)
{
if (selected > 0)
{
selected--;
this.PrintMenu();
}
}
if (keyinfo.Key == ConsoleKey.Enter)
{
Console.Clear();
options[selected].Action.Invoke();
}
} while (keyinfo.Key != ConsoleKey.Enter);
}
private void PrintMenu()
{
Console.Clear();
Console.WriteLine(title);
int optionIndex = 0;
foreach (Option option in options)
{
string pointer = optionIndex == selected ? " ->" : " ";
Console.WriteLine($"{pointer}{option.Name}");
optionIndex++;
}
}
private struct Option
{
public string Name { get; }
public Action Action { get; }
public Option(string name, Action action)
{
Name = name;
Action = action;
}
}
}
}

78
Core/Program.cs Normal file
View File

@@ -0,0 +1,78 @@
using laba3.Core;
using laba3.Subprograms;
using System;
class Program
{
static void Main()
{
Menu mainMenu = new Menu("Select option");
mainMenu.AddOption("Guess answer math game", () => GuessAnswerMath.RunGame());
mainMenu.AddOption("About me", () => PrintAboutMe());
mainMenu.AddOption("Array sort", () => new ArraySortDemo().Run());
mainMenu.AddOption("Snake game", () => {
int sizex = 0;
int sizey = 0;
SnakeGame.Level difficulty = 0;
Menu sizeMenu = new Menu("Select world size");
sizeMenu.AddOption("Small size (10x10)", () => { sizex = 10; sizey = 10; });
sizeMenu.AddOption("Medium size (20x20)", () => { sizex = 20; sizey = 20; });
sizeMenu.AddOption("Big size (40x20)", () => { sizex = 40; sizey = 20; });
sizeMenu.RunMenu();
Menu difficultyMenu = new Menu("Select difficulty");
difficultyMenu.AddOption("Easy", () => { difficulty = SnakeGame.Level.Low; });
difficultyMenu.AddOption("Medium", () => { difficulty = SnakeGame.Level.Medium; });
difficultyMenu.AddOption("Hard", () => { difficulty = SnakeGame.Level.High; });
difficultyMenu.RunMenu();
SnakeGame game = new SnakeGame(difficulty, sizex, sizey);
game.start();
});
mainMenu.AddOption("Exit", () => Exit());
Utils.Arrays withoutParams = new Utils.Arrays();
Utils.Arrays withOneParam = new Utils.Arrays(10);
Utils.Arrays withTwoParams = new Utils.Arrays(10, 100);
while (true)
{
mainMenu.RunMenu();
Console.WriteLine("Press Enter to continue");
while (Console.ReadKey().Key != ConsoleKey.Enter) { }
Console.Clear();
}
}
private static void PrintAboutMe()
{
const string aboutme = @"Морозов Иван Сергеевич 6106 aka DIvan2000
Website: divan2000.su";
Console.WriteLine(aboutme);
}
private static void Exit()
{
if(ExitMenu())
Environment.Exit(0);
}
private static bool ExitMenu()
{
while (true)
{
Console.WriteLine("Really exit? [y/n]");
switch (Console.ReadKey(true).KeyChar)
{
case 'y':
return true;
case 'n':
return false;
default:
Console.Clear();
Console.WriteLine("Wrong key!");
break;
}
}
}
}

220
Core/Utils.cs Normal file
View File

@@ -0,0 +1,220 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace laba3.Core
{
/// <summary>
/// Different utils;
/// </summary>
internal static class Utils
{
/// <summary>
/// CLI read double from keyboard;
/// </summary>
/// <param name="name">Name will be printed before scan</param>
/// <returns></returns>
public static double ReadDouble(string name)
{
double result;
string inputBuffer;
bool isValid;
do
{
Console.Write($"Введите {name}: ");
inputBuffer = Console.ReadLine();
isValid = double.TryParse(inputBuffer, out result);
if (!isValid)
Console.WriteLine("Неверный тип аргумента!");
} while (!isValid);
return result;
}
/// <summary>
/// CLI read int from keyboard;
/// </summary>
/// <param name="name">Name will be printed before scan</param>
/// <returns></returns>
public static int ReadInt(string name)
{
int result;
string inputBuffer;
bool isValid;
do
{
Console.Write($"Введите {name}: ");
inputBuffer = Console.ReadLine();
isValid = int.TryParse(inputBuffer, out result);
if (!isValid)
Console.WriteLine("Неверный тип аргумента!");
} while (!isValid);
return result;
}
/// <summary>
/// Util for init arrays with random values, copy it, print it, etc.;
/// </summary>
public class Arrays
{
private int size;
private int[] array;
/// <summary>
/// Array size of 10;
/// Filled random values;
/// </summary>
public Arrays()
{
size = 10;
array = new int[size];
initRandom();
}
/// <summary>
/// Array size of X;
/// Filled random values;
/// </summary>
/// <param name="size">Size of array</param>
public Arrays(int size)
{
this.size = size;
array = new int[size];
initRandom();
}
/// <summary>
///Array size of X;
/// Filled random values;
/// </summary>
/// <param name="size">Size of array</param>
/// <param name="max">Max value of random</param>
public Arrays(int size, int max)
{
this.size = size;
array = new int[size];
initRandom(max);
}
/// <summary>
/// Copy array;
/// </summary>
/// <param name="src">Source from copy</param>
public Arrays (Arrays src)
{
this.size = src.size;
this.array = new int[src.size];
for (int i = 0; i < src.size; i++)
{
this.array[i] = src.array[i];
}
}
/// <summary>
/// Get array;
/// </summary>
/// <returns>Array pointer</returns>
public int[] getArray()
{
return this.array;
}
/// <summary>
/// Get size;
/// </summary>
/// <returns>Array size</returns>
public int getSize()
{
return this.size;
}
/// <summary>
/// Print array in console;
/// </summary>
/// <param name="name">Name will be printed</param>
public void print(string name)
{
Console.Write($"Массив {name}: [");
foreach (int x in this.array) { Console.Write($"{x}, "); }
Console.WriteLine("\b\b]");
}
/// <summary>
/// Sort this array with Gnome Sort;
/// Avg time O(n^2);
/// </summary>
/// <returns>Ellapsed ms</returns>
public long gnomeSort()
{
Stopwatch sw = new Stopwatch();
sw.Start();
int i = 1; int j = 2;
while (i < size)
{
if (array[i - 1] < array[i])
{
i = j;
j = j + 1;
}
else
{
(array[i - 1], array[i]) = (array[i], array[i - 1]);
i = i - 1;
if (i == 0)
{
i = j;
j = j + 1;
}
}
}
sw.Stop();
return sw.ElapsedMilliseconds;
}
/// <summary>
/// Sort this array with Shell Sort;
/// Avg time depends on gap sequence;
/// </summary>
/// <returns>Ellapsed ms</returns>
public long shellSort()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int s = size / 2; s > 0; s /= 2)
{
for (int i = s; i < size; ++i)
{
for (int j = i - s; j >= 0 && array[j] > array[j + s]; j -= s)
{
(array[j], array[j + s]) = (array[j + s], array[j]);
}
}
}
sw.Stop();
return sw.ElapsedMilliseconds;
}
private void initRandom()
{
Random rnd = new Random();
for (int i = 0; i < size; i++)
{
array[i] = rnd.Next();
}
}
private void initRandom(int max)
{
Random rnd = new Random();
for (int i = 0; i < size; i++)
{
array[i] = rnd.Next(max);
}
}
}
}
}