Добавьте файлы проекта.
This commit is contained in:
		
							parent
							
								
									8912302f8b
								
							
						
					
					
						commit
						583e839414
					
				
							
								
								
									
										100
									
								
								Core/Menu.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								Core/Menu.cs
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										78
									
								
								Core/Program.cs
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										220
									
								
								Core/Utils.cs
									
									
									
									
									
										Normal 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); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										71
									
								
								Subprograms/ArraySortDemo.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								Subprograms/ArraySortDemo.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,71 @@ | |||||||
|  | using laba3.Core; | ||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Diagnostics; | ||||||
|  | using System.Drawing; | ||||||
|  | using System.Linq; | ||||||
|  | using System.Text; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  | using static System.Runtime.InteropServices.JavaScript.JSType; | ||||||
|  | 
 | ||||||
|  | namespace laba3.Subprograms | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Compare computing time of 2 sorting methods; | ||||||
|  |     /// Gnome sort and Shell sort; | ||||||
|  |     /// </summary> | ||||||
|  |     internal class ArraySortDemo | ||||||
|  |     { | ||||||
|  |         private long time1; | ||||||
|  |         private long time2; | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// Get values from keyboard, init arrays, start computing, print time; | ||||||
|  |         /// Console will be clean!; | ||||||
|  |         /// </summary> | ||||||
|  |         public void Run() | ||||||
|  |         { | ||||||
|  |             Utils.Arrays array1; | ||||||
|  |             Utils.Arrays array2; | ||||||
|  |             int size; | ||||||
|  |             size = ReadArrayLen(); | ||||||
|  |             array1 = new Utils.Arrays(size, 100); | ||||||
|  |             array2 = new Utils.Arrays(array1); | ||||||
|  | 
 | ||||||
|  |             Console.WriteLine("Для Массива 1 будет использована \"гномья сортировка\""); | ||||||
|  |             Console.WriteLine("Для Массива 2 будет использована \"сортировка шелла\""); | ||||||
|  | 
 | ||||||
|  |             if (size <= 10) | ||||||
|  |                 array1.print("с начальными значениями"); | ||||||
|  |             else | ||||||
|  |                 Console.WriteLine("Массивы не могут быть выведены на экран так как их размер больше 10"); | ||||||
|  |             Console.WriteLine("Сортируем..."); | ||||||
|  | 
 | ||||||
|  |             time1 = array1.gnomeSort(); | ||||||
|  |             time2 = array2.shellSort(); | ||||||
|  | 
 | ||||||
|  |             Console.WriteLine("Готово!"); | ||||||
|  |             if (size <= 10) | ||||||
|  |             { | ||||||
|  |                 array1.print("после гномьей сортировки"); | ||||||
|  |                 array2.print("после сортировки шелла  "); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             Console.WriteLine($"Для сортировки 1 потребовалось {time1}мс"); | ||||||
|  |             Console.WriteLine($"Для сортировки 2 потребовалось {time2}мс"); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private int ReadArrayLen() | ||||||
|  |         { | ||||||
|  |             int size; | ||||||
|  |             do | ||||||
|  |             { | ||||||
|  |                 size = Utils.ReadInt("Размер массива для сортировки"); | ||||||
|  |                 Console.Clear(); | ||||||
|  |                 if (size < 1) | ||||||
|  |                     Console.WriteLine("Размер не может быть меньше 1!"); | ||||||
|  |             } while (size < 1); | ||||||
|  |             return size; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										91
									
								
								Subprograms/GuessAnswerMath.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								Subprograms/GuessAnswerMath.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,91 @@ | |||||||
|  | using laba3.Core; | ||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Linq; | ||||||
|  | using System.Text; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  | 
 | ||||||
|  | namespace laba3.Subprograms | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// CLI math game "Gusess the answer"; | ||||||
|  |     /// Player need to guess result of math function; | ||||||
|  |     /// </summary> | ||||||
|  |     internal static class GuessAnswerMath | ||||||
|  |     { | ||||||
|  |         /// <summary> | ||||||
|  |         /// Runs game; | ||||||
|  |         /// Console will be clean!; | ||||||
|  |         /// </summary> | ||||||
|  |         public static void RunGame() | ||||||
|  |         { | ||||||
|  |             const int attempts = 3; | ||||||
|  |             Console.WriteLine("Введите аргументы функции, а затем попытайтесь угадать её значение. Количество попыток ограничено!"); | ||||||
|  |             bool isValid; | ||||||
|  |             do | ||||||
|  |             { | ||||||
|  |                 MathFunc.setArgs(Utils.ReadDouble("аргумент a"), Utils.ReadDouble("аргумент b")); | ||||||
|  |                 isValid = MathFunc.CheckArgs(); | ||||||
|  |                 if (!isValid) | ||||||
|  |                 { | ||||||
|  |                     Console.Clear(); | ||||||
|  |                     Console.WriteLine("Функция не определена при данных аргументах!"); | ||||||
|  |                 } | ||||||
|  |             } while (!isValid); | ||||||
|  |             MathFunc.ComputeResult(); | ||||||
|  |             if (GameLogic(MathFunc.GetResult(), attempts)) | ||||||
|  |             { | ||||||
|  |                 Console.WriteLine("Ответ верный! Победа!"); | ||||||
|  |             } else | ||||||
|  |             { | ||||||
|  |                 Console.WriteLine(String.Format("Попытки кончились! Правильный ответ: {0:0.00}", MathFunc.GetResult())); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private static bool GameLogic(double correctAnswer, int attempts) | ||||||
|  |         { | ||||||
|  |             bool win = false; | ||||||
|  |             for (int i = attempts; i > 0; i--) | ||||||
|  |             { | ||||||
|  |                 Console.Clear(); | ||||||
|  |                 Console.WriteLine($"Количество попыток: {i}"); | ||||||
|  |                 double answer = Utils.ReadDouble("ваш ответ"); | ||||||
|  |                 win = Math.Abs(correctAnswer - answer) < 0.01; | ||||||
|  |                 if (win) | ||||||
|  |                 { | ||||||
|  |                     i = 0; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             return win; | ||||||
|  |         } | ||||||
|  |         private static class MathFunc | ||||||
|  |         { | ||||||
|  |             private const double PI = Math.PI; | ||||||
|  |             private const double E = Math.E; | ||||||
|  |             private static double a; | ||||||
|  |             private static double b; | ||||||
|  |             private static double result; | ||||||
|  | 
 | ||||||
|  |             public static void setArgs(double a, double b) | ||||||
|  |             { | ||||||
|  |                 MathFunc.a = a; | ||||||
|  |                 MathFunc.b = b; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             public static void ComputeResult() | ||||||
|  |             { | ||||||
|  |                 MathFunc.result = Math.Sin((Math.Pow(a, 3) + Math.Pow(b, 5)) / (2 * PI)) + Math.Pow(Math.Cos(a + b), (1.0 / 3.0)); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             public static bool CheckArgs() | ||||||
|  |             { | ||||||
|  |                 return Math.Cos(a + b) >= 0; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             public static double GetResult() | ||||||
|  |             { | ||||||
|  |                 return result; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										380
									
								
								Subprograms/SnakeGame.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										380
									
								
								Subprograms/SnakeGame.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,380 @@ | |||||||
|  | using System.Diagnostics; | ||||||
|  | 
 | ||||||
|  | namespace laba3.Subprograms | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// TUI game "Snake"; | ||||||
|  |     /// The player controls the snake. | ||||||
|  |     /// The player has to eat apples to increase the length of the snake | ||||||
|  |     /// and try not to crash into the wall or themselves; | ||||||
|  |     /// </summary> | ||||||
|  |     internal class SnakeGame | ||||||
|  |     { | ||||||
|  |         int size_x; | ||||||
|  |         int size_y; | ||||||
|  |         int score; | ||||||
|  |         int size_xy; | ||||||
|  |         Level difficulty; | ||||||
|  |         Tiles[,] world; | ||||||
|  |         Snake snake; | ||||||
|  |         Random rnd; | ||||||
|  | 
 | ||||||
|  |         private bool gameover; | ||||||
|  |         /// <summary> | ||||||
|  |         /// Game world constructor; | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="difficulty">Game speed</param> | ||||||
|  |         /// <param name="size_x">Game world size dim x</param> | ||||||
|  |         /// <param name="size_y">Game world size dim y</param> | ||||||
|  |         public SnakeGame(Level difficulty, int size_x, int size_y) | ||||||
|  |         { | ||||||
|  |             this.size_x = size_x; | ||||||
|  |             this.size_y = size_y; | ||||||
|  |             this.difficulty = difficulty; | ||||||
|  |             world = new Tiles[size_x, size_y]; | ||||||
|  |             rnd = new Random(); | ||||||
|  |             score = 0; | ||||||
|  |             size_xy = size_x * size_y; | ||||||
|  |         } | ||||||
|  |         /// <summary> | ||||||
|  |         /// Start game; | ||||||
|  |         /// Console will be clean!; | ||||||
|  |         /// </summary> | ||||||
|  |         public void start() | ||||||
|  |         { | ||||||
|  |             int msPerTick = difficulty switch | ||||||
|  |             { | ||||||
|  |                 Level.Low => 500, | ||||||
|  |                 Level.Medium => 250, | ||||||
|  |                 Level.High => 100, | ||||||
|  |                 _ => 500, | ||||||
|  |             }; | ||||||
|  |             gameover = false; | ||||||
|  | 
 | ||||||
|  |             Console.Clear(); | ||||||
|  |             Console.CursorVisible = false; | ||||||
|  |             snake = new Snake(new Point(size_x / 2, size_y / 2, this), 1, Direction.Right, this); | ||||||
|  | 
 | ||||||
|  |             GenerateFood(); | ||||||
|  | 
 | ||||||
|  |             DrawBackground(); | ||||||
|  |             DrawWorld(); | ||||||
|  | 
 | ||||||
|  |             Stopwatch tickTimer = new Stopwatch(); | ||||||
|  |             while (!gameover) | ||||||
|  |             { | ||||||
|  |                 tickTimer.Restart(); | ||||||
|  |                 if (Console.KeyAvailable) | ||||||
|  |                 { | ||||||
|  |                     ConsoleKeyInfo key = Console.ReadKey(false); | ||||||
|  |                     snake.HandleKey(key.Key); | ||||||
|  |                     while (Console.KeyAvailable) | ||||||
|  |                         Console.ReadKey(false); | ||||||
|  |                 } | ||||||
|  |                 snake.Move(); | ||||||
|  |                 long delta = tickTimer.ElapsedMilliseconds; | ||||||
|  | 
 | ||||||
|  |                 Console.SetCursorPosition(1, 0); | ||||||
|  |                 if (delta > msPerTick) | ||||||
|  |                     Console.ForegroundColor = ConsoleColor.Red; | ||||||
|  |                 Console.Write(delta); | ||||||
|  | 
 | ||||||
|  |                 Thread.Sleep((int)Math.Max(0, msPerTick - delta)); | ||||||
|  |             } | ||||||
|  |             onStop(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// Stop game; | ||||||
|  |         /// </summary> | ||||||
|  |         public void stop() | ||||||
|  |         { | ||||||
|  |             gameover = true; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private void onStop() | ||||||
|  |         { | ||||||
|  |             string gameOverTitle = | ||||||
|  | @"
 | ||||||
|  |  ###                  ##             | ||||||
|  | #     ## ####   #    #  # # #  #  ## | ||||||
|  | # ## # # # # # ###   #  # # # ### #  | ||||||
|  | #  # # # # # # #     #  #  #  #   #  | ||||||
|  |  ###  ## # # #  ##    ##   #   ## #  | ||||||
|  | ";
 | ||||||
|  |             Console.Clear(); | ||||||
|  |             printArt(gameOverTitle, (Console.WindowWidth - 36) / 2, 4, ConsoleColor.DarkYellow, ConsoleColor.DarkGray); | ||||||
|  | 
 | ||||||
|  |             Console.ResetColor(); | ||||||
|  | 
 | ||||||
|  |             string scoreString = $"Score: {score}"; | ||||||
|  |             Console.SetCursorPosition((Console.WindowWidth - scoreString.Length) / 2, 11); | ||||||
|  |             Console.Write(scoreString); | ||||||
|  |             Console.SetCursorPosition((Console.WindowWidth - "Press Enter to continue".Length) / 2, 14); | ||||||
|  |             Console.CursorVisible = true; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private void printArt(string art, int x, int y, ConsoleColor primaryColor, ConsoleColor shadowColor) | ||||||
|  |         { | ||||||
|  |             int row = 0; | ||||||
|  |             Console.SetCursorPosition(x, y); | ||||||
|  |             char shadow = ' '; | ||||||
|  |             foreach (char ch in art) | ||||||
|  |             { | ||||||
|  |                 if (ch == '\n') | ||||||
|  |                 { | ||||||
|  |                     if (shadow == '#') | ||||||
|  |                     { | ||||||
|  |                         Console.BackgroundColor = shadowColor; | ||||||
|  |                         Console.Write(' '); | ||||||
|  |                     } | ||||||
|  |                     Console.SetCursorPosition(x, y++ + row); | ||||||
|  |                     Thread.Sleep(25); | ||||||
|  |                     shadow = ch; | ||||||
|  |                 } | ||||||
|  |                 else if (ch == ' ') | ||||||
|  |                 { | ||||||
|  |                     if (shadow == '#') | ||||||
|  |                     { | ||||||
|  |                         Console.BackgroundColor = shadowColor; | ||||||
|  |                         Console.Write(' '); | ||||||
|  |                     } | ||||||
|  |                     else | ||||||
|  |                         Console.CursorLeft++; | ||||||
|  |                     shadow = ch; | ||||||
|  |                 } | ||||||
|  |                 else if (ch == '#') | ||||||
|  |                 { | ||||||
|  |                     Console.BackgroundColor = primaryColor; | ||||||
|  |                     Console.Write(' '); | ||||||
|  |                     shadow = ch; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private void addScore() | ||||||
|  |         { | ||||||
|  |             score += difficulty switch | ||||||
|  |             { | ||||||
|  |                 Level.Low => 10, | ||||||
|  |                 Level.Medium => 50, | ||||||
|  |                 Level.High => 100, | ||||||
|  |                 _ => 1, | ||||||
|  |             }; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// Generate food in random place; | ||||||
|  |         /// </summary> | ||||||
|  |         public void GenerateFood() | ||||||
|  |         { | ||||||
|  |             int x; | ||||||
|  |             int y; | ||||||
|  |             do | ||||||
|  |             { | ||||||
|  |                 x = rnd.Next(size_x); | ||||||
|  |                 y = rnd.Next(size_y); | ||||||
|  |             } while (world[x, y] != Tiles.Void); | ||||||
|  |             world[x, y] = Tiles.Food; | ||||||
|  |             DrawTile(x, y); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private void DrawBackground() | ||||||
|  |         { | ||||||
|  |             for (int x = 1; x < (size_x + 1) * 2 + 1; x++) | ||||||
|  |             { | ||||||
|  |                 Console.SetCursorPosition(x, 1); | ||||||
|  |                 Console.Write('#'); | ||||||
|  |                 Console.SetCursorPosition(x, size_y + 2); | ||||||
|  |                 Console.Write('#'); | ||||||
|  |             } | ||||||
|  |             for (int y = 1; y < size_y + 2; y++) | ||||||
|  |             { | ||||||
|  |                 Console.SetCursorPosition(1, y); | ||||||
|  |                 Console.Write('#'); | ||||||
|  | 
 | ||||||
|  |                 Console.SetCursorPosition(size_x * 2 + 1, y); | ||||||
|  |                 Console.Write('#'); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private void DrawTile(int x, int y) | ||||||
|  |         { | ||||||
|  |             char symbol; | ||||||
|  |             switch (world[x, y]) | ||||||
|  |             { | ||||||
|  |                 case Tiles.Void: | ||||||
|  |                     symbol = ' '; | ||||||
|  |                     break; | ||||||
|  |                 case Tiles.Snake: | ||||||
|  |                     symbol = '*'; | ||||||
|  |                     Console.ForegroundColor = ConsoleColor.DarkRed; | ||||||
|  |                     break; | ||||||
|  |                 case Tiles.Food: | ||||||
|  |                     symbol = '@'; | ||||||
|  |                     Console.ForegroundColor = ConsoleColor.Red; | ||||||
|  |                     break; | ||||||
|  |                 default: | ||||||
|  |                     symbol = '?'; | ||||||
|  |                     break; | ||||||
|  |             }; | ||||||
|  |             //Console.BackgroundColor = ConsoleColor.DarkGreen; | ||||||
|  |             Console.SetCursorPosition((x + 1) * 2, y + 2); | ||||||
|  |             Console.Write(symbol); | ||||||
|  | 
 | ||||||
|  |             Console.BackgroundColor = ConsoleColor.Black; | ||||||
|  |             Console.ForegroundColor = ConsoleColor.White; | ||||||
|  |         } | ||||||
|  |         private void DrawWorld() | ||||||
|  |         { | ||||||
|  |             for (int x = 0; x < size_x; x++) | ||||||
|  |             { | ||||||
|  |                 for (int y = 0; y < size_y; y++) | ||||||
|  |                 { | ||||||
|  |                     DrawTile(x, y); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         private enum Tiles | ||||||
|  |         { | ||||||
|  |             Void, | ||||||
|  |             Snake, | ||||||
|  |             Food | ||||||
|  |         } | ||||||
|  |         public enum Level | ||||||
|  |         { | ||||||
|  |             Low, | ||||||
|  |             Medium, | ||||||
|  |             High | ||||||
|  |         } | ||||||
|  |         private class Snake | ||||||
|  |         { | ||||||
|  |             private SnakeGame game; | ||||||
|  |             private List<Point> body; | ||||||
|  |             private Direction direction; | ||||||
|  | 
 | ||||||
|  |             public Snake(Point tail, int length, Direction initialDirection, SnakeGame game) | ||||||
|  |             { | ||||||
|  |                 this.game = game; | ||||||
|  |                 body = new List<Point>(); | ||||||
|  |                 direction = initialDirection; | ||||||
|  | 
 | ||||||
|  |                 for (int i = 0; i < length; i++) | ||||||
|  |                 { | ||||||
|  |                     Point p = new Point(tail.X, tail.Y, game); | ||||||
|  |                     body.Add(p); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             public void Move() | ||||||
|  |             { | ||||||
|  |                 Point head = GetNextPoint(); | ||||||
|  |                 Point tail = body.First(); | ||||||
|  | 
 | ||||||
|  |                 if (head.X < 0 || head.Y < 0 || head.X >= game.size_x || head.Y >= game.size_y) | ||||||
|  |                 { | ||||||
|  |                     game.stop(); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 if (game.world[head.X, head.Y] == Tiles.Snake) | ||||||
|  |                 { | ||||||
|  |                     game.stop(); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |                 body.Add(head); | ||||||
|  |                 if (game.world[head.X, head.Y] != Tiles.Food) | ||||||
|  |                 { | ||||||
|  |                     body.Remove(tail); | ||||||
|  |                     tail.UpdateWorld(Tiles.Void); | ||||||
|  |                 } | ||||||
|  |                 else | ||||||
|  |                 { | ||||||
|  |                     game.addScore(); | ||||||
|  |                     game.GenerateFood(); | ||||||
|  |                     if (body.Count >= game.size_xy) game.stop(); | ||||||
|  |                 } | ||||||
|  |                 head.UpdateWorld(Tiles.Snake); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             public Point GetNextPoint() | ||||||
|  |             { | ||||||
|  |                 Point head = body.Last(); | ||||||
|  |                 Point nextPoint = new Point(head.X, head.Y, game); | ||||||
|  | 
 | ||||||
|  |                 switch (direction) | ||||||
|  |                 { | ||||||
|  |                     case Direction.Right: | ||||||
|  |                         nextPoint.X++; | ||||||
|  |                         break; | ||||||
|  |                     case Direction.Left: | ||||||
|  |                         nextPoint.X--; | ||||||
|  |                         break; | ||||||
|  |                     case Direction.Up: | ||||||
|  |                         nextPoint.Y--; | ||||||
|  |                         break; | ||||||
|  |                     case Direction.Down: | ||||||
|  |                         nextPoint.Y++; | ||||||
|  |                         break; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 return nextPoint; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             public void HandleKey(ConsoleKey key) | ||||||
|  |             { | ||||||
|  |                 switch (key) | ||||||
|  |                 { | ||||||
|  |                     case ConsoleKey.LeftArrow: | ||||||
|  |                         if (direction != Direction.Right) | ||||||
|  |                             direction = Direction.Left; | ||||||
|  |                         break; | ||||||
|  |                     case ConsoleKey.RightArrow: | ||||||
|  |                         if (direction != Direction.Left) | ||||||
|  |                             direction = Direction.Right; | ||||||
|  |                         break; | ||||||
|  |                     case ConsoleKey.UpArrow: | ||||||
|  |                         if (direction != Direction.Down) | ||||||
|  |                             direction = Direction.Up; | ||||||
|  |                         break; | ||||||
|  |                     case ConsoleKey.DownArrow: | ||||||
|  |                         if (direction != Direction.Up) | ||||||
|  |                             direction = Direction.Down; | ||||||
|  |                         break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         enum Direction | ||||||
|  |         { | ||||||
|  |             Left, | ||||||
|  |             Right, | ||||||
|  |             Up, | ||||||
|  |             Down | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private class Point | ||||||
|  |         { | ||||||
|  |             private SnakeGame game { get; set; } | ||||||
|  |             public int X { get; set; } | ||||||
|  |             public int Y { get; set; } | ||||||
|  | 
 | ||||||
|  |             public Point(int x, int y, SnakeGame game) | ||||||
|  |             { | ||||||
|  |                 this.game = game; | ||||||
|  |                 X = x; | ||||||
|  |                 Y = y; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             public void UpdateWorld(Tiles tile) | ||||||
|  |             { | ||||||
|  |                 game.world[X, Y] = tile; | ||||||
|  |                 game.DrawTile(X, Y); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										10
									
								
								laba3.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								laba3.csproj
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | |||||||
|  | <Project Sdk="Microsoft.NET.Sdk"> | ||||||
|  | 
 | ||||||
|  |   <PropertyGroup> | ||||||
|  |     <OutputType>Exe</OutputType> | ||||||
|  |     <TargetFramework>net8.0</TargetFramework> | ||||||
|  |     <ImplicitUsings>enable</ImplicitUsings> | ||||||
|  |     <Nullable>enable</Nullable> | ||||||
|  |   </PropertyGroup> | ||||||
|  | 
 | ||||||
|  | </Project> | ||||||
							
								
								
									
										25
									
								
								laba3.sln
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								laba3.sln
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | |||||||
|  |  | ||||||
|  | Microsoft Visual Studio Solution File, Format Version 12.00 | ||||||
|  | # Visual Studio Version 17 | ||||||
|  | VisualStudioVersion = 17.11.35327.3 | ||||||
|  | MinimumVisualStudioVersion = 10.0.40219.1 | ||||||
|  | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "laba3", "laba3.csproj", "{85E71A1C-4311-4D83-8A7B-FBB5CA9E6CA9}" | ||||||
|  | EndProject | ||||||
|  | Global | ||||||
|  | 	GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||||
|  | 		Debug|Any CPU = Debug|Any CPU | ||||||
|  | 		Release|Any CPU = Release|Any CPU | ||||||
|  | 	EndGlobalSection | ||||||
|  | 	GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||||||
|  | 		{85E71A1C-4311-4D83-8A7B-FBB5CA9E6CA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||||||
|  | 		{85E71A1C-4311-4D83-8A7B-FBB5CA9E6CA9}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||||||
|  | 		{85E71A1C-4311-4D83-8A7B-FBB5CA9E6CA9}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||||||
|  | 		{85E71A1C-4311-4D83-8A7B-FBB5CA9E6CA9}.Release|Any CPU.Build.0 = Release|Any CPU | ||||||
|  | 	EndGlobalSection | ||||||
|  | 	GlobalSection(SolutionProperties) = preSolution | ||||||
|  | 		HideSolutionNode = FALSE | ||||||
|  | 	EndGlobalSection | ||||||
|  | 	GlobalSection(ExtensibilityGlobals) = postSolution | ||||||
|  | 		SolutionGuid = {18AE73FB-3B60-4A6F-88BD-1A180147DFAB} | ||||||
|  | 	EndGlobalSection | ||||||
|  | EndGlobal | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 DIvan-Notebook
						DIvan-Notebook