ProgLab1_winforms/GUI/MathGameForm.cs

158 lines
4.9 KiB
C#
Raw Permalink Normal View History

using laba3.Subprograms;
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;
using System.Xml;
namespace ProgLab1.GUI
{
public partial class MathGameForm : Form
{
private Label error;
private Label formula;
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 = "Угадайка";
formula = new Label();
formula.Text = "Math.Sin((Math.Pow(a, 3) + Math.Pow(b, 5)) / (2 * PI)) + Math.Pow(Math.Cos(a + b), (1.0 / 3.0))";
formula.AutoSize = true;
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 = "";
game.ComputeResult();
guessForm.SetAnswer(game.GetResult());
this.Hide();
guessForm.Show();
}
}
else
{
// Если есть ошибка, подсвечиваем поля
if (!isAValid) textBoxA.BackColor = Color.Red;
if (!isBValid) textBoxB.BackColor = Color.Red;
error.Text = "Аргументы должны быть double";
}
}
}
}