Начата реализация угадайки
This commit is contained in:
		
							parent
							
								
									5ea7f1bed4
								
							
						
					
					
						commit
						7be59a5cf0
					
				| @ -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); | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										39
									
								
								GUI/GuessForm.Designer.cs
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								GUI/GuessForm.Designer.cs
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @ -0,0 +1,39 @@ | ||||
| namespace ProgLab1.GUI | ||||
| { | ||||
|     partial class GuessForm | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Required designer variable. | ||||
|         /// </summary> | ||||
|         private System.ComponentModel.IContainer components = null; | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// Clean up any resources being used. | ||||
|         /// </summary> | ||||
|         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | ||||
|         protected override void Dispose(bool disposing) | ||||
|         { | ||||
|             if (disposing && (components != null)) | ||||
|             { | ||||
|                 components.Dispose(); | ||||
|             } | ||||
|             base.Dispose(disposing); | ||||
|         } | ||||
| 
 | ||||
|         #region Windows Form Designer generated code | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// Required method for Designer support - do not modify | ||||
|         /// the contents of this method with the code editor. | ||||
|         /// </summary> | ||||
|         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 | ||||
|     } | ||||
| } | ||||
							
								
								
									
										95
									
								
								GUI/GuessForm.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								GUI/GuessForm.cs
									
									
									
									
									
										Normal file
									
								
							| @ -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}"; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										120
									
								
								GUI/GuessForm.resx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								GUI/GuessForm.resx
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,120 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <root> | ||||
|   <!--  | ||||
|     Microsoft ResX Schema  | ||||
|      | ||||
|     Version 2.0 | ||||
|      | ||||
|     The primary goals of this format is to allow a simple XML format  | ||||
|     that is mostly human readable. The generation and parsing of the  | ||||
|     various data types are done through the TypeConverter classes  | ||||
|     associated with the data types. | ||||
|      | ||||
|     Example: | ||||
|      | ||||
|     ... ado.net/XML headers & schema ... | ||||
|     <resheader name="resmimetype">text/microsoft-resx</resheader> | ||||
|     <resheader name="version">2.0</resheader> | ||||
|     <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> | ||||
|     <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> | ||||
|     <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data> | ||||
|     <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data> | ||||
|     <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64"> | ||||
|         <value>[base64 mime encoded serialized .NET Framework object]</value> | ||||
|     </data> | ||||
|     <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> | ||||
|         <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> | ||||
|         <comment>This is a comment</comment> | ||||
|     </data> | ||||
|                  | ||||
|     There are any number of "resheader" rows that contain simple  | ||||
|     name/value pairs. | ||||
|      | ||||
|     Each data row contains a name, and value. The row also contains a  | ||||
|     type or mimetype. Type corresponds to a .NET class that support  | ||||
|     text/value conversion through the TypeConverter architecture.  | ||||
|     Classes that don't support this are serialized and stored with the  | ||||
|     mimetype set. | ||||
|      | ||||
|     The mimetype is used for serialized objects, and tells the  | ||||
|     ResXResourceReader how to depersist the object. This is currently not  | ||||
|     extensible. For a given mimetype the value must be set accordingly: | ||||
|      | ||||
|     Note - application/x-microsoft.net.object.binary.base64 is the format  | ||||
|     that the ResXResourceWriter will generate, however the reader can  | ||||
|     read any of the formats listed below. | ||||
|      | ||||
|     mimetype: application/x-microsoft.net.object.binary.base64 | ||||
|     value   : The object must be serialized with  | ||||
|             : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter | ||||
|             : and then encoded with base64 encoding. | ||||
|      | ||||
|     mimetype: application/x-microsoft.net.object.soap.base64 | ||||
|     value   : The object must be serialized with  | ||||
|             : System.Runtime.Serialization.Formatters.Soap.SoapFormatter | ||||
|             : and then encoded with base64 encoding. | ||||
| 
 | ||||
|     mimetype: application/x-microsoft.net.object.bytearray.base64 | ||||
|     value   : The object must be serialized into a byte array  | ||||
|             : using a System.ComponentModel.TypeConverter | ||||
|             : and then encoded with base64 encoding. | ||||
|     --> | ||||
|   <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | ||||
|     <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> | ||||
|     <xsd:element name="root" msdata:IsDataSet="true"> | ||||
|       <xsd:complexType> | ||||
|         <xsd:choice maxOccurs="unbounded"> | ||||
|           <xsd:element name="metadata"> | ||||
|             <xsd:complexType> | ||||
|               <xsd:sequence> | ||||
|                 <xsd:element name="value" type="xsd:string" minOccurs="0" /> | ||||
|               </xsd:sequence> | ||||
|               <xsd:attribute name="name" use="required" type="xsd:string" /> | ||||
|               <xsd:attribute name="type" type="xsd:string" /> | ||||
|               <xsd:attribute name="mimetype" type="xsd:string" /> | ||||
|               <xsd:attribute ref="xml:space" /> | ||||
|             </xsd:complexType> | ||||
|           </xsd:element> | ||||
|           <xsd:element name="assembly"> | ||||
|             <xsd:complexType> | ||||
|               <xsd:attribute name="alias" type="xsd:string" /> | ||||
|               <xsd:attribute name="name" type="xsd:string" /> | ||||
|             </xsd:complexType> | ||||
|           </xsd:element> | ||||
|           <xsd:element name="data"> | ||||
|             <xsd:complexType> | ||||
|               <xsd:sequence> | ||||
|                 <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | ||||
|                 <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> | ||||
|               </xsd:sequence> | ||||
|               <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> | ||||
|               <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> | ||||
|               <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> | ||||
|               <xsd:attribute ref="xml:space" /> | ||||
|             </xsd:complexType> | ||||
|           </xsd:element> | ||||
|           <xsd:element name="resheader"> | ||||
|             <xsd:complexType> | ||||
|               <xsd:sequence> | ||||
|                 <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | ||||
|               </xsd:sequence> | ||||
|               <xsd:attribute name="name" type="xsd:string" use="required" /> | ||||
|             </xsd:complexType> | ||||
|           </xsd:element> | ||||
|         </xsd:choice> | ||||
|       </xsd:complexType> | ||||
|     </xsd:element> | ||||
|   </xsd:schema> | ||||
|   <resheader name="resmimetype"> | ||||
|     <value>text/microsoft-resx</value> | ||||
|   </resheader> | ||||
|   <resheader name="version"> | ||||
|     <value>2.0</value> | ||||
|   </resheader> | ||||
|   <resheader name="reader"> | ||||
|     <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||||
|   </resheader> | ||||
|   <resheader name="writer"> | ||||
|     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||||
|   </resheader> | ||||
| </root> | ||||
| @ -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"; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -11,81 +11,33 @@ namespace laba3.Subprograms | ||||
|     /// 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 | ||||
|     public class GuessAnswerMath | ||||
|     { | ||||
|             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; | ||||
|             } | ||||
|     } | ||||
| } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user