Advertisement
Guest User

Animal Guessing Arduino (Bugged - Italian)

a guest
Dec 2nd, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.89 KB | None | 0 0
  1.  
  2. #include <string.h>
  3.  
  4. #define ANIMALS_LIST      "zanzara ape pinguino aquila giraffa polpo tigre elefante medusa toro \npappagallo delfino pitone coccodrillo gatto leopardo scimmia zebra pecora ratto \ngufo ragno rana orsopolare lumaca tartaruga coniglio salmone rinoceronte volpe"
  5. #define MAMMALS                    "0 0 0 0 1 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 0 1 1"
  6. #define FLYING_ANIMALS             "1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
  7. #define WATER_ANIMALS              "0 0 1 0 0 1 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 1 0 1 0 1 1 0"
  8. #define BEAK                       "0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0"
  9.  
  10. #define NOCTURNAL_ANIMALS          "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1"
  11. #define SHELL_ANIMALS              "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0"
  12. #define SLITHERING_ANIMALS         "0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0"
  13. #define WHISKERS                   "0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 0 0 1"
  14.  
  15. #define PAWS                       "0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1"
  16. #define STRIPES                    "0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0"
  17. #define FUR                        "0 1 0 0 1 0 1 0 0 1 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 0 1 0 0 1"
  18. #define FOURLEGGED_ANIMALS         "0 0 0 0 1 0 1 1 0 1 0 0 0 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1"
  19.  
  20. #define STINGING_ANIMALS           "0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
  21. #define TAIL                       "0 0 1 0 1 0 1 1 0 1 0 0 0 1 1 1 1 1 1 1 0 0 0 1 0 0 1 0 1 1"
  22. #define SCALES                     "0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0"
  23. #define CARNIVORES                 "0 0 1 1 0 1 1 0 1 0 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0 0 0 0 0 1"
  24.  
  25. #define SPOTS                      "0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
  26. #define GILLS                      "0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0"
  27. #define HORNS                      "0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"
  28. #define FEATHERS                   "0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0"
  29.  
  30. #define ANIMAL_DELIMITER  " "
  31.  
  32. #define TOTAL_ANIMALS     30
  33.  
  34. #define CONTINUE_KEY      "gioca"
  35. #define CONTINUE_PIN      9
  36.  
  37. #define YES_KEY           "si"
  38. #define YES_PIN  9
  39.  
  40. #define NO_KEY            "no"
  41. #define NO_PIN            8
  42.  
  43. #define EXIT_KEY           "esci"
  44.  
  45. #define BUFFER_SIZE       32
  46.  
  47. #define DEBUG_STATUS      0
  48.  
  49. char* animals_list[ TOTAL_ANIMALS ] = { NULL };
  50. int   mammals[ TOTAL_ANIMALS ] = { 0 };
  51. int   flying_animals[ TOTAL_ANIMALS ] = { 0 };
  52. int   water_animals[ TOTAL_ANIMALS ] = { 0 };
  53. int   beak[ TOTAL_ANIMALS ] = { 0 };
  54. int   nocturnal_animals[ TOTAL_ANIMALS ] = { 0 };
  55. int   shell_animals[ TOTAL_ANIMALS ] = { 0 };
  56. int   slithering_animals[ TOTAL_ANIMALS ] = { 0 };
  57. int   whiskers[ TOTAL_ANIMALS ] = { 0 };
  58. int   paws[ TOTAL_ANIMALS ] = { 0 };
  59. int   stripes[ TOTAL_ANIMALS ] = { 0 };
  60. int   fur[ TOTAL_ANIMALS ] = { 0 };
  61. int   fourlegged_animals[ TOTAL_ANIMALS ] = { 0 };
  62. int   stinging_animals[ TOTAL_ANIMALS ] = { 0 };
  63. int   tail[ TOTAL_ANIMALS ] = { 0 };
  64. int   scales[ TOTAL_ANIMALS ] = { 0 };
  65. int   carnivores[ TOTAL_ANIMALS ] = { 0 };
  66. int   spots[ TOTAL_ANIMALS ] = { 0 };
  67. int   gills[ TOTAL_ANIMALS ] = { 0 };
  68. int   horns[ TOTAL_ANIMALS ] = { 0 };
  69. int   feathers[ TOTAL_ANIMALS ] = { 0 };
  70.  
  71. int continue_game = 1;
  72. int guesses = 0;
  73.  
  74. void setup() {
  75.   Serial.begin(9600);
  76.   pinMode(YES_PIN, INPUT);
  77.   pinMode(NO_PIN, INPUT);
  78.   digitalWrite(YES_PIN, HIGH);
  79.   digitalWrite(NO_PIN, HIGH);
  80.  
  81.   //time_t start_time, end_time;
  82.   setupGameData();
  83.   startGame();
  84.   Serial.println("Gioco Avviato a" && millis());
  85. }
  86.  
  87. void loop() {
  88.   guesses++;
  89.   switch ( guesses )
  90.   {
  91.     case 1:
  92.       askUserQuestion( guesses, "E' un mammifero?", mammals );
  93.       break;
  94.     case 2:
  95.       askUserQuestion( guesses, "Vola?", flying_animals );
  96.       break;
  97.     case 3:
  98.       askUserQuestion( guesses, "Vive soprattutto o sempre in acqua?", water_animals );
  99.       break;
  100.     case 4:
  101.       askUserQuestion( guesses, "Ha un becco?", beak );
  102.       break;
  103.     case 5:
  104.       askUserQuestion( guesses, "E' notturno?", nocturnal_animals );
  105.       break;
  106.     case 6:
  107.       askUserQuestion( guesses, "Ha una conchiglia?", shell_animals );
  108.       break;
  109.     case 7:
  110.       askUserQuestion( guesses, "Striscia?", slithering_animals );
  111.       break;
  112.     case 8:
  113.       askUserQuestion( guesses, "Ha la barba?", whiskers );
  114.       break;
  115.     case 9:
  116.       askUserQuestion( guesses, "Ha le zampe?", paws );
  117.       break;
  118.     case 10:
  119.       askUserQuestion( guesses, "Ha le strisce?", stripes );
  120.       break;
  121.     case 11:
  122.       askUserQuestion( guesses, "Ha il pelo?", fur );
  123.       break;
  124.     case 12:
  125.       askUserQuestion( guesses, "Ha quattro gambe?", fourlegged_animals );
  126.       break;
  127.     case 13:
  128.       askUserQuestion( guesses, "Può pungere?", stinging_animals );
  129.       break;
  130.     case 14:
  131.       askUserQuestion( guesses, "Ha una coda?", tail );
  132.       break;
  133.     case 15:
  134.       askUserQuestion( guesses, "Ha le squame?", scales );
  135.       break;
  136.     case 16:
  137.       askUserQuestion( guesses, "E' carnivoro?", carnivores );
  138.       break;
  139.     case 17:
  140.       askUserQuestion( guesses, "Ha delle macchie?", spots );
  141.       break;
  142.     case 18:
  143.       askUserQuestion( guesses, "Ha le branchie?", gills );
  144.       break;
  145.     case 19:
  146.       askUserQuestion( guesses, "Ha le corna o le zanne?", horns );
  147.       break;
  148.     case 20:
  149.       askUserQuestion( guesses, "Ha le piume?", feathers );
  150.       break;
  151.     default :
  152.       Serial.println("Congratulazioni! Hai vinto, non riesco ad indovinare il tuo animale");
  153.       continue_game = 0;
  154.       break;
  155.   }
  156.  
  157.   if ( DEBUG_STATUS )
  158.     displayRemainingAnimals();
  159.  
  160.   if ( countRemainingAnimals() == 1 )
  161.   {
  162.     Serial.print("Il tuo animale è il ");
  163.     Serial.println(findLastAnimal());
  164.     continue_game = 0;
  165.   }
  166.   while ( continue_game );
  167. }
  168.  
  169. void setupGameData()
  170. {
  171.   char* animal;
  172.   int   index;
  173.   animal = strtok_r( strdup( ANIMALS_LIST ), ANIMAL_DELIMITER );
  174.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  175.   {
  176.     animals_list[ index ] = animal;
  177.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  178.   }
  179.   animal = strtok_r( strdup( MAMMALS ), ANIMAL_DELIMITER );
  180.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  181.   {
  182.     mammals[ index ] = atoi( animal );
  183.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  184.   }
  185.   animal = strtok_r( strdup( FLYING_ANIMALS ), ANIMAL_DELIMITER );
  186.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  187.   {
  188.     flying_animals[ index ] = atoi( animal );
  189.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  190.   }
  191.   animal = strtok_r( strdup( WATER_ANIMALS ), ANIMAL_DELIMITER );
  192.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  193.   {
  194.     water_animals[ index ] = atoi( animal );
  195.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  196.   }
  197.   animal = strtok_r( strdup( BEAK ) , ANIMAL_DELIMITER );
  198.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  199.   {
  200.     beak[ index ] = atoi( animal );
  201.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  202.   }
  203.   animal = strtok_r( strdup( NOCTURNAL_ANIMALS ), ANIMAL_DELIMITER );
  204.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  205.   {
  206.     nocturnal_animals[ index ] = atoi( animal );
  207.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  208.   }
  209.   animal = strtok_r( strdup( SHELL_ANIMALS ), ANIMAL_DELIMITER );
  210.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  211.   {
  212.     shell_animals[ index ] = atoi( animal );
  213.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  214.   }
  215.   animal = strtok_r( strdup( SLITHERING_ANIMALS ), ANIMAL_DELIMITER );
  216.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  217.   {
  218.     slithering_animals[ index ] = atoi( animal );
  219.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  220.   }
  221.   animal = strtok_r( strdup( WHISKERS ), ANIMAL_DELIMITER );
  222.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  223.   {
  224.     whiskers[ index ] = atoi( animal );
  225.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  226.   }
  227.   animal = strtok_r( strdup( PAWS ), ANIMAL_DELIMITER );
  228.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  229.   {
  230.     paws[ index ] = atoi( animal );
  231.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  232.   }
  233.   animal = strtok_r( strdup( STRIPES ), ANIMAL_DELIMITER );
  234.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  235.   {
  236.     stripes[ index ] = atoi( animal );
  237.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  238.   }
  239.   animal = strtok_r( strdup( FUR ), ANIMAL_DELIMITER );
  240.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  241.   {
  242.     fur[ index ] = atoi( animal );
  243.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  244.   }
  245.   animal = strtok_r( strdup( FOURLEGGED_ANIMALS ), ANIMAL_DELIMITER );
  246.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  247.   {
  248.     fourlegged_animals[ index ] = atoi( animal );
  249.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  250.   }
  251.   animal = strtok_r( strdup( STINGING_ANIMALS ), ANIMAL_DELIMITER );
  252.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  253.   {
  254.     stinging_animals[ index ] = atoi( animal );
  255.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  256.   }
  257.   animal = strtok_r( strdup( TAIL ), ANIMAL_DELIMITER );
  258.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  259.   {
  260.     tail[ index ] = atoi( animal );
  261.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  262.   }
  263.   animal = strtok_r( strdup( SCALES ), ANIMAL_DELIMITER );
  264.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  265.   {
  266.     scales[ index ] = atoi( animal );
  267.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  268.   }
  269.   animal = strtok_r( strdup( CARNIVORES ), ANIMAL_DELIMITER );
  270.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  271.   {
  272.     carnivores[ index ] = atoi( animal );
  273.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  274.   }
  275.   animal = strtok_r( strdup( SPOTS ), ANIMAL_DELIMITER );
  276.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  277.   {
  278.     spots[ index ] = atoi( animal );
  279.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  280.   }
  281.   animal = strtok_r( strdup( GILLS ), ANIMAL_DELIMITER );
  282.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  283.   {
  284.     gills[ index ] = atoi( animal );
  285.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  286.   }
  287.   animal = strtok_r( strdup( HORNS ), ANIMAL_DELIMITER );
  288.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  289.   {
  290.     horns[ index ] = atoi( animal );
  291.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  292.   }
  293.   animal = strtok_r( strdup( FEATHERS ), ANIMAL_DELIMITER );
  294.   for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
  295.   {
  296.     feathers[ index ] = atoi( animal );
  297.     animal = strtok_r( NULL, ANIMAL_DELIMITER );
  298.   }
  299. }
  300.  
  301.  
  302. void startGame()
  303. {
  304.   char buffer[ BUFFER_SIZE ];
  305.   int  index;
  306.   Serial.println("Benvenuto nel gioco degli animali");
  307.   Serial.println("Scegli un animale dalla lista:")
  308.   for ( index = 0; index < TOTAL_ANIMALS; index++ ) {
  309.     Serial.print(animals_list[index]);
  310.     Serial.print(" ");
  311.   }
  312. }
  313.  
  314.  
  315.  
  316. void askUserQuestion( int guessNumber, char* question, int* animalData )
  317. {
  318.   int buttonY = digitalRead(YES_PIN);
  319.   int buttonN = digitalRead(NO_PIN);
  320.   char buffer[ BUFFER_SIZE ];
  321.   int index;
  322.   Serial.println(question);
  323.   while ( buttonY == LOW || buttonN == LOW )
  324.   {
  325.     for ( index = 0; index < TOTAL_ANIMALS; index++ )
  326.     {
  327.       if ( animals_list[ index ] != NULL )
  328.       {
  329.         if ( buttonY == LOW)
  330.         {
  331.           if ( animalData[ index ] != 1 )
  332.             animals_list[ index ] = NULL;
  333.         }
  334.         else
  335.         {
  336.           if ( animalData[ index ] == 1 )
  337.             animals_list[ index ] = NULL;
  338.         }
  339.       }
  340.     }
  341.   }
  342.  
  343.  
  344.  
  345.   /* Display the list of possible animals remaining */
  346.  
  347.   void displayRemainingAnimals()
  348.   {
  349.     int index;
  350.  
  351.     Serial.println("Animali rimasti: ")
  352.     for ( index = 0; index < TOTAL_ANIMALS; index++ )
  353.     {
  354.       if ( animals_list[ index ] != NULL )
  355.         Serial.print(animals_list[index]);
  356.       Serial.print(" ");
  357.     }
  358.     Serial.println("");
  359.   }
  360.   int countRemainingAnimals()
  361.   {
  362.     int animals_remaining = 0;
  363.     int index;
  364.  
  365.     for ( index = 0; index < TOTAL_ANIMALS; index++ )
  366.     {
  367.       if ( animals_list[ index ] != NULL )
  368.         animals_remaining++;
  369.     }
  370.  
  371.     return animals_remaining;
  372.   }
  373.  
  374.   char* findLastAnimal()
  375.   {
  376.     int index;
  377.  
  378.     for ( index = 0; index < TOTAL_ANIMALS; index++ )
  379.     {
  380.       if ( animals_list[ index ] != NULL )
  381.         return animals_list[ index ];
  382.     }
  383.  
  384.     return NULL;
  385.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement