Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 07 / BullsAndColls.cpp
blob: 2eb5d3c106dacd549c551275f626c20d4afd4207 (plain) (blame)
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
#include <iostream>
#include <stdlib.h>    
using namespace std;


//Бикове и крави.
//Число: 1234
//Предположение: 4253 - 2 крави (4 и 3) и 1 бик (2).

bool hasDuplicateDigits(int n) ///1343
{
	const int DIGITCOUNT = 10;
	bool found[DIGITCOUNT];  
	for (int i = 0; i < DIGITCOUNT; i++)
		found[i] = false;
	while (n != 0)
	{
		int lastDigit = n % 10;
		if (found[lastDigit] == true)
			return true;
		else
			found[lastDigit] = true;
		n /= 10;
	}
	return false;
}

void convertNumberToArr(int n, int arr[])
{

	for (int i = 3; i >= 0; i--)
	{
		arr[i] = n % 10;
		n /= 10;
	}

}
int countBulls(int myNumber[], int compNumber[])
{
	int bullsCount = 0;
	for (int i = 0; i <= 3; i++)
	{
		if (myNumber[i] == compNumber[i])
			bullsCount++;
	}
	return bullsCount;
}
int countCows(int myNumber[], int compNumber[])
{
	int cowsCount = 0;

	for (int i = 0; i <= 3; i++)
	{
		int myDigit = myNumber[i];
		for (int j = 0; j <= 3; j++)
		{
			if (j == i) // çà äà íå áðîèì áèêîâåòå
				continue;
			if (myDigit == compNumber[j])
				cowsCount++;
		}
	}
	return cowsCount;
}
int main()
{
	int numberToGuess;
	do
	{
		 numberToGuess = 1000 + rand() % 9000; // [1000..9999]
	}
	while (hasDuplicateDigits(numberToGuess));
	
	cout << "Computer has picked a number! Try to guess it!" <<endl;
	int numberToGuessArr[4];
	
	convertNumberToArr(numberToGuess, numberToGuessArr);

	int bullsCount = 0, cowsCount = 0;
	do
	{
		int myNumber;
		cin >> myNumber;
		int myNumberArr[4];
		convertNumberToArr(myNumber, myNumberArr);

		bullsCount = countBulls(myNumberArr, numberToGuessArr);
		cowsCount = countCows(myNumberArr, numberToGuessArr);

		cout << "Bulls: " << bullsCount << " Cows:" << cowsCount << endl;


	} while (bullsCount != 4);

	cout << "Success!" << endl;
}