fork download
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4.  
  5. struct Domino {
  6. int left, right;
  7. bool played;
  8.  
  9. void (*show)(struct Domino*);
  10. void (*flip)(struct Domino*);
  11. bool (*fits)(struct Domino*, int);
  12. int (*points)(struct Domino*);
  13. };
  14.  
  15. void show(struct Domino* d) {
  16. printf("[%d|%d] %s\n", d->left, d->right,
  17. d->left == d->right ? "(дубль)" : "");
  18. }
  19.  
  20. void flip(struct Domino* d) {
  21. int t = d->left;
  22. d->left = d->right;
  23. d->right = t;
  24. }
  25.  
  26. bool fits(struct Domino* d, int v) {
  27. return d->left == v || d->right == v;
  28. }
  29.  
  30. int points(struct Domino* d) {
  31. return d->left + d->right;
  32. }
  33.  
  34. struct Domino* make(int l, int r) {
  35. struct Domino* d = malloc(sizeof(struct Domino));
  36. d->left = l;
  37. d->right = r;
  38. d->played = false;
  39. d->show = show;
  40. d->flip = flip;
  41. d->fits = fits;
  42. d->points = points;
  43. return d;
  44. }
  45.  
  46. int main() {
  47. printf("Демонстрация костей домино:\n\n");
  48.  
  49. struct Domino* set[5];
  50. set[0] = make(6, 6);
  51. set[1] = make(2, 4);
  52. set[2] = make(0, 3);
  53. set[3] = make(5, 5);
  54. set[4] = make(1, 6);
  55.  
  56. for(int i = 0; i < 5; i++) {
  57. printf("%d. ", i+1);
  58. set[i]->show(set[i]);
  59. }
  60.  
  61. printf("\nДействия с первой костью [6|6]:\n");
  62. printf("Очки: %d\n", set[0]->points(set[0]));
  63. printf("Подходит к 6: %s\n", set[0]->fits(set[0], 6) ? "да" : "нет");
  64. printf("Подходит к 2: %s\n", set[0]->fits(set[0], 2) ? "да" : "нет");
  65.  
  66. printf("\nДействия со второй костью [2|4]:\n");
  67. set[1]->show(set[1]);
  68. printf("Поворачиваем...\n");
  69. set[1]->flip(set[1]);
  70. set[1]->show(set[1]);
  71.  
  72. for(int i = 0; i < 5; i++) free(set[i]);
  73. return 0;
  74. }
Success #stdin #stdout 0s 5288KB
stdin
6
stdout
Демонстрация костей домино:

1. [6|6] (дубль)
2. [2|4] 
3. [0|3] 
4. [5|5] (дубль)
5. [1|6] 

Действия с первой костью [6|6]:
Очки: 12
Подходит к 6: да
Подходит к 2: нет

Действия со второй костью [2|4]:
[2|4] 
Поворачиваем...
[4|2]