diff --git a/Core/Program.cs b/Core/Program.cs index 61cbcf0..778781b 100644 --- a/Core/Program.cs +++ b/Core/Program.cs @@ -9,6 +9,7 @@ class Program static MenuForm snakeDiffMenuForm; static MenuForm snakeSizeMenuForm; static AboutMeForm aboutMeForm; + static MathGameForm mathGameForm; [STAThread] // Требуется для Windows Forms static void Main() @@ -31,7 +32,7 @@ class Program difficultyMenu.AddOption("Hard", () => { difficulty = SnakeGame.Level.High; }); Menu mainMenu = new Menu("Select option"); - mainMenu.AddOption("Guess answer math game", () => GuessAnswerMath.RunGame()); + mainMenu.AddOption("Guess answer math game", () => mainMenuForm.SwitchToForm(mathGameForm)); mainMenu.AddOption("About me", () => mainMenuForm.SwitchToForm(aboutMeForm)); mainMenu.AddOption("Array sort", () => new ArraySortDemo().Run()); mainMenu.AddOption("Snake game", () => { @@ -55,6 +56,7 @@ class Program }); aboutMeForm = new AboutMeForm(mainMenuForm); + mathGameForm = new MathGameForm(mainMenuForm); Application.Run(mainMenuForm); diff --git a/GUI/GuessForm.Designer.cs b/GUI/GuessForm.Designer.cs new file mode 100644 index 0000000..21c919f --- /dev/null +++ b/GUI/GuessForm.Designer.cs @@ -0,0 +1,39 @@ +namespace ProgLab1.GUI +{ + partial class GuessForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "GuessForm"; + } + + #endregion + } +} \ No newline at end of file diff --git a/GUI/GuessForm.cs b/GUI/GuessForm.cs new file mode 100644 index 0000000..df20db8 --- /dev/null +++ b/GUI/GuessForm.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ProgLab1.GUI +{ + public partial class GuessForm : Form + { + private double answer; + private int tryes; + private Label tryCount; + private TextBox input; + + public void SetAnswer(double answer) { this.answer = answer; tryes = 3; tryCount.Text = $"Количество попыток: {tryes}"; } + public GuessForm(Form onCloseForm) + { + InitializeComponent(); + this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { e.Cancel = true; this.Hide(); onCloseForm.Show(); }); + + input = new TextBox(); + input.Top = 100; + input.Left = 50; + input.Width = 300; + + Label inputLabel = new Label(); + inputLabel.Top = 70; + inputLabel.Left = 50; + inputLabel.Text = "Введите число:"; + + Button confirmButton = new Button + { + Text = "Подтвердить", + Width = 100, + Height = 30, + Top = 100, + Left = 400 + }; + + tryCount = new Label(); + tryCount.Top = 40; + tryCount.Left = 50; + tryCount.Width = 400; + tryCount.Text = $"Количество попыток: {tryes}"; + + confirmButton.Click += ConfirmButton_Click; + + this.Controls.Add(inputLabel); + this.Controls.Add(input); + this.Controls.Add(confirmButton); + this.Controls.Add(tryCount); + } + + private bool ValidateInput(TextBox textBox) + { + if (double.TryParse(textBox.Text, out _)) + { + return true; + } + else + { + return false; + } + } + + private void ConfirmButton_Click(object sender, EventArgs e) + { + bool isValid = ValidateInput(input); + if (!isValid) + { + input.BackColor = Color.Red; + } + else + { + input.BackColor = SystemColors.Window; + double inputNum = double.Parse(input.Text); + if (Math.Abs(answer - inputNum) < 0.1) + { + + } + else + { + tryes--; + } + } + + tryCount.Text = $"Количество попыток: {tryes}"; + } + } +} diff --git a/GUI/GuessForm.resx b/GUI/GuessForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/GUI/GuessForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/GUI/MathGameForm.cs b/GUI/MathGameForm.cs index 0300194..ea63205 100644 --- a/GUI/MathGameForm.cs +++ b/GUI/MathGameForm.cs @@ -1,4 +1,5 @@ -using System; +using laba3.Subprograms; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -13,9 +14,22 @@ namespace ProgLab1.GUI { public partial class MathGameForm : Form { + private Label error; private Label formula; - public MathGameForm() + private Label rules; + private TextBox textBoxA; + private TextBox textBoxB; + private Label labelA; + private Label labelB; + + private double a=0; + private double b=0; + private GuessAnswerMath game; + private GuessForm guessForm; + public MathGameForm(Form onCloseForm) { + guessForm = new GuessForm(onCloseForm); + game = new GuessAnswerMath(); InitializeComponent(); this.Text = "Угадайка"; @@ -25,7 +39,119 @@ namespace ProgLab1.GUI formula.Top = 50; formula.Left = 50; + rules = new Label(); + rules.Text = "Нужно ввести аргументы для функции, а затем попытаться отгадать её значение"; + rules.AutoSize = true; + rules.Top = 100; + rules.Left = 50; + + // Создание Label для "a" + labelA = new Label(); + labelA.Text = "a:"; + labelA.Top = 150; + labelA.Left = 20; + labelA.AutoSize = true; + + // Создание TextBox для "a" + textBoxA = new TextBox(); + textBoxA.Top = 150; + textBoxA.Left = 50; + textBoxA.Width = 300; + + // Создание Label для "b" + labelB = new Label(); + labelB.Text = "b:"; + labelB.Top = 190; + labelB.Left = 20; + labelB.AutoSize = true; + + // Создание TextBox для "b" + textBoxB = new TextBox(); + textBoxB.Top = 190; + textBoxB.Left = 50; + textBoxB.Width = 300; + + error = new Label(); + error.Text = ""; + error.AutoSize = true; + error.Top = 220; + error.Left = 50; + error.ForeColor = Color.Red; + this.Controls.Add(formula); + this.Controls.Add(rules); + this.Controls.Add(labelA); + this.Controls.Add(textBoxA); + this.Controls.Add(labelB); + this.Controls.Add(textBoxB); + this.Controls.Add(error); + + Button confirmButton = new Button + { + Text = "Подтвердить", + Width = 100, + Height = 30, + Top = 165, + Left = 400 + }; + + confirmButton.Click += ConfirmButton_Click; + + this.Controls.Add(confirmButton); + + this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { e.Cancel = true; this.Hide(); onCloseForm.Show(); }); + } + + private bool ValidateInput(TextBox textBox) + { + if (double.TryParse(textBox.Text, out _)) + { + return true; + } + else + { + return false; + } + } + + private void ConfirmButton_Click(object sender, EventArgs e) + { + // Проверяем значения в полях + bool isAValid = ValidateInput(textBoxA); + bool isBValid = ValidateInput(textBoxB); + + if (isAValid && isBValid) + { + // Если оба значения корректные, выводим сумму в + textBoxA.BackColor = SystemColors.Window; + textBoxB.BackColor = SystemColors.Window; + a = double.Parse(textBoxA.Text); + b = double.Parse(textBoxB.Text); + + game.setArgs(a, b); + + bool funcValid = game.CheckArgs(); + + if (!funcValid) + { + error.Text = "Функция не определена при этих аргументах"; + } + + else + { + error.Text = ""; + guessForm.SetAnswer(game.GetResult()); + this.Hide(); + guessForm.Show(); + } + } + if (!(isAValid && isBValid)) + { + // Если есть ошибка, подсвечиваем поля + if (!isAValid) textBoxA.BackColor = Color.Red; + if (!isBValid) textBoxB.BackColor = Color.Red; + error.Text = "Аргументы должны быть double"; + } } } } diff --git a/Subprograms/GuessAnswerMath.cs b/Subprograms/GuessAnswerMath.cs index bb6bebc..cbd6632 100644 --- a/Subprograms/GuessAnswerMath.cs +++ b/Subprograms/GuessAnswerMath.cs @@ -11,81 +11,33 @@ namespace laba3.Subprograms /// CLI math game "Gusess the answer"; /// Player need to guess result of math function; /// - internal static class GuessAnswerMath + public class GuessAnswerMath { - /// - /// Runs game; - /// Console will be clean!; - /// - 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; + private double a; + private double b; + private double result; - public static void setArgs(double a, double b) + public void setArgs(double a, double b) { - MathFunc.a = a; - MathFunc.b = b; + this.a = a; + this.b = b; } - public static void ComputeResult() + public 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)); + this.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() + public bool CheckArgs() { return Math.Cos(a + b) >= 0; } - public static double GetResult() + public double GetResult() { return result; } - } } }