-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
141 lines (128 loc) · 3.45 KB
/
Form1.cs
File metadata and controls
141 lines (128 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
const int maxAttemps = 5;
int currentAttempt = maxAttemps;
int[] genNumber = new int[4]; // сгенерированное число
public Form1()
{
InitializeComponent();
label2.Text = currentAttempt.ToString();
}
private void button1_Click(object sender, EventArgs e) // првоерить
{
if (textBox1.Text.Length < 4)
{
MessageBox.Show("Введите четырехзначное число!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
label3.Text = "";
var userNumber = textBox1.Text.Select(digit => int.Parse(digit.ToString())).ToArray(); // число пользователя
if (currentAttempt == maxAttemps)
{
Array.Copy(GenerateNumber(4, 10), genNumber, 4);
}
if (currentAttempt > 0)
{
if (NumberMatch(genNumber, userNumber)) // если числа совпадают
{
label3.Text = $"Угадали!\nЧисло обновлено.";
currentAttempt = maxAttemps;
label2.Text = currentAttempt.ToString();
return;
}
Compare(genNumber, userNumber);
if (currentAttempt == 1)
{
label3.Text = "Вы израсходовали попытки.\nЧисло обновлено.";
currentAttempt = maxAttemps;
label2.Text = currentAttempt.ToString();
return;
}
}
currentAttempt--;
label2.Text = currentAttempt.ToString();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) // ввод цифры
{
var inputValue = e.KeyChar;
if (textBox1.Text.Length < 4)
{
if (!char.IsDigit(inputValue) && inputValue != 8) // если не число и не backspace
e.Handled = true; // пропустить обработку события
}
else
{
if (inputValue != 8 && textBox1.SelectionLength == 0)
e.Handled = true;
}
}
public bool NumberMatch(int[] a, int[] b) // проверка чисел на полное совпадение
{
bool match = true;
for (int i = 0; i < 4; i++)
{
if (a[i] != b[i])
{
match = false;
break;
}
}
return match;
}
public void Compare(int[] genNumber, int[] userNumber) // cравнение чисел
{
int k = 0; // количество совпадающих цифр
for (int i = 0; i < 4; i++) // genNumber
{
for (int j = 0; j < 4; j++) // userNumber
{
if (genNumber[i] == userNumber[j])
{
k++;
if (i == j)
label3.Text += $"BULL - {userNumber[j]}\n";
else
label3.Text += $"COW - {userNumber[j]}\n";
}
}
}
if (k == 0)
label3.Text = "Совпадений не найдено";
}
public int[] GenerateNumber(int numLength, int maxDigit) // генератор случайного числа
{
Random random = new Random();
int digit, n = 0;
var result = new int[4];
while (n < numLength)
{
digit = random.Next(maxDigit);
bool b = true;
for (int i = 0; i < n; i++)
if (digit == result[i])
{
b = false;
break;
}
if (b)
{
result[n] = digit;
n++;
}
}
return result;
}
}
}