Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: Admin
- * Date: 12.1.2022 г.
- * Time: 11:18
- *
- * To change this template use Tools | Options | Coding | Edit Standard Headers.
- */
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace bulls_cows
- {
- /// <summary>
- /// Description of MainForm.
- /// </summary>
- public partial class MainForm : Form
- {
- public int pcNumber = 0;
- public List<int> digits = new List<int>();
- public int tries = 10;
- public MainForm()
- {
- //
- // The InitializeComponent() call is required for Windows Forms designer support.
- //
- InitializeComponent();
- //
- // TODO: Add constructor code after the InitializeComponent() call.
- //
- }
- void LinkLabel1LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- Info info = new Info();
- info.Show();
- }
- void ButtonNewGameClick(object sender, EventArgs e)
- {
- tries = 10;
- labelPcNumber.Text = "";
- textBoxUserNumber.Text = "";
- labelTries.Text = "";
- listBoxResult.Items.Clear();
- buttonCheck.Enabled = true;
- textBoxUserNumber.Enabled = true;
- Random r = new Random();
- while (true)
- {
- pcNumber = r.Next(1000, 10000);
- int a = pcNumber / 1000;
- int b = pcNumber % 1000 / 100;
- int c = pcNumber % 100 / 10;
- int d = pcNumber % 10;
- digits.Clear();
- if (!digits.Contains(a)) digits.Add(a);
- if (!digits.Contains(b)) digits.Add(b);
- if (!digits.Contains(c)) digits.Add(c);
- if (!digits.Contains(d)) digits.Add(d);
- if (digits.Count == 4) break;
- }
- }
- void ButtonCheckClick(object sender, EventArgs e)
- {
- int userNumber = 0;
- try
- {
- userNumber = int.Parse(textBoxUserNumber.Text);
- }
- catch
- {
- MessageBox.Show("Въведи 4-цифрено число");
- textBoxUserNumber.Text = "";
- textBoxUserNumber.Select();
- return;
- }
- textBoxUserNumber.Text = "";
- textBoxUserNumber.Select();
- //if (textBoxUserNumber.Text.Length != 4)
- if (userNumber < 1000 || userNumber > 9999)
- {
- MessageBox.Show("Числото трябва да е 4-цифрено");
- return;
- }
- int a = userNumber / 1000;
- int b = userNumber % 1000 / 100;
- int c = userNumber % 100 / 10;
- int d = userNumber % 10;
- List<int> userDigits = new List<int>();
- if (!userDigits.Contains(a)) userDigits.Add(a);
- if (!userDigits.Contains(b)) userDigits.Add(b);
- if (!userDigits.Contains(c)) userDigits.Add(c);
- if (!userDigits.Contains(d)) userDigits.Add(d);
- if (userDigits.Count != 4)
- {
- MessageBox.Show("Цифрите трябва да са различни");
- return;
- }
- int bulls = 0;
- int cows = 0;
- tries--;
- if (digits[0] == a) bulls++;
- else if (digits.Contains(a)) cows++;
- if (digits[1] == b) bulls++;
- else if (digits.Contains(b)) cows++;
- if (digits[2] == c) bulls++;
- else if (digits.Contains(c)) cows++;
- if (digits[3] == d) bulls++;
- else if (digits.Contains(d)) cows++;
- labelTries.Text = "Остават " + tries + " опита";
- if (tries == 1)
- {
- labelTries.Text = "Остава 1 опит";
- }
- if (bulls == 4)
- {
- labelTries.Text = "ПЕЧЕЛИШ !!!";
- labelPcNumber.Text = pcNumber.ToString();
- labelPcNumber.ForeColor = Color.Green;
- buttonCheck.Enabled = false;
- textBoxUserNumber.Enabled = false;
- }
- string bullsWord = bulls == 1 ? "бик" : "бика";
- string cowsWord = cows == 1 ? "крава" : "крави";
- if (tries > 0)
- {
- string result = string.Format("{0}: {1} {2}, {3} {4}",
- userNumber, bulls, bullsWord, cows, cowsWord);
- listBoxResult.Items.Add(result);
- }
- else
- {
- if (bulls != 4)
- {
- labelTries.Text = "ГУБИШ !!!";
- labelPcNumber.Text = pcNumber.ToString();
- labelPcNumber.ForeColor = Color.Red;
- buttonCheck.Enabled = false;
- textBoxUserNumber.Enabled = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement