-
Notifications
You must be signed in to change notification settings - Fork 0
/
BullsAndCows.cs
77 lines (71 loc) · 2.6 KB
/
BullsAndCows.cs
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
using System;
class BullsAndCows
{
static void Main()
{
string secretNumber = Console.ReadLine();
int[] secretNumberArray = new int[4];//test 1234
int[] guessNumberArray = new int[4];//guess number = 1234
for (int i = 0; i < 4; i++)
{
secretNumberArray[i] = Convert.ToInt32(Convert.ToString(secretNumber[i]));
}
//bulls test number = 2
int b = Convert.ToInt32(Console.ReadLine());
int bullsCount = 0;
//cows
int c = Convert.ToInt32(Console.ReadLine());
int cowsCount = 0;
bool noBullsOrCows = true;
for (int i = 1111; i < 10000; i++)
{
//Separate digits from int "i" into new array - checkNumberArray
int[] checkNumberArray = new int[4];
checkNumberArray[3] = i % 10;
checkNumberArray[2] = (i / 10) % 10;
checkNumberArray[1] = (i / 100) % 10;
checkNumberArray[0] = (i / 1000);
if (checkNumberArray[0] != 0 && checkNumberArray[1] != 0 && checkNumberArray[2] != 0 && checkNumberArray[3] != 0)
{
for (int d = 0; d < 4; d++)//Loop for counting bulls position test number=5681
{
if (checkNumberArray[d] == secretNumberArray[d])
{
bullsCount = bullsCount + 1;
}
}
if (c != 0)
{
for (int q = 0; q < 4; q++)//Loop for counting cows number test number=5681
{
if (checkNumberArray[q] != secretNumberArray[q])
{
for (int h = 3; h >= 0; h--)
{
if (checkNumberArray[q] == secretNumberArray[h] && secretNumberArray[h] != checkNumberArray[h])
{
cowsCount = cowsCount + 1;
}
}
}
}
}
if (bullsCount == b && cowsCount == c)
{
for (int g = 0; g < 4; g++)
{
Console.Write(checkNumberArray[g]);
noBullsOrCows = false;
}
Console.Write(" ");
}
bullsCount = 0;
cowsCount = 0;
}
}
if (noBullsOrCows == true)
{
Console.WriteLine("No");
}
}
}