Advertisement
tryblyat

Untitled

Mar 30th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. int const n = 4;
  7.  
  8. void fillrand(int a[]) {
  9. a[0] = rand() % 8 + 1;
  10. for (int i = 1; i < n; ++i) {
  11. a[i] = rand() % 9;
  12. }
  13. }
  14.  
  15. void print(int a[]) {
  16. for (int j = 0; j < n; ++j) {
  17. cout << a[j];
  18. }
  19. cout << endl;
  20. }
  21.  
  22. void write(int a[]) {
  23. int k;
  24. cin >> k;
  25. for (int j = n - 1; j > -1; --j) {
  26. a[j] = k % 10;
  27. k /= 10;
  28. }
  29. }
  30.  
  31. void check(int comp[], int hum[]) {
  32. int bulls = 0, cows = 0;
  33. while (true) {
  34. for (int i = 0; i < n; ++i) {
  35. if (hum[i] == comp[i]) {
  36. ++bulls;
  37. }
  38. }
  39. if (bulls == 4) {
  40. cout << "Victory!";
  41. break;
  42. }
  43. for (int i = 0; i < n; i++) {
  44. int k = i;
  45. for (int j = 0; j < n; ++j) {
  46. if (hum[k] == comp[j]) {
  47. ++cows;
  48. break;
  49. }
  50. }
  51. }
  52. cout << "bulls: " << bulls << endl << "cows: " << cows << endl;
  53. cout << "Try again, please: ";
  54. write(hum);
  55. bulls = 0;
  56. cows = 0;
  57. }
  58. }
  59.  
  60. int main() {
  61. srand(time(NULL));
  62. int number[n], arry[n];
  63. fillrand(number);
  64. //print(number);
  65. cout << "Number guessed try guessing it!" << endl << "Your number: ";
  66. write(arry);
  67. print(arry);
  68. check(number, arry);
  69. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement