Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #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"
- #define ANIMAL_DELIMITER " "
- #define TOTAL_ANIMALS 30
- #define CONTINUE_KEY "gioca"
- #define CONTINUE_PIN 9
- #define YES_KEY "si"
- #define YES_PIN 9
- #define NO_KEY "no"
- #define NO_PIN 8
- #define EXIT_KEY "esci"
- #define BUFFER_SIZE 32
- #define DEBUG_STATUS 0
- char* animals_list[ TOTAL_ANIMALS ] = { NULL };
- int mammals[ TOTAL_ANIMALS ] = { 0 };
- int flying_animals[ TOTAL_ANIMALS ] = { 0 };
- int water_animals[ TOTAL_ANIMALS ] = { 0 };
- int beak[ TOTAL_ANIMALS ] = { 0 };
- int nocturnal_animals[ TOTAL_ANIMALS ] = { 0 };
- int shell_animals[ TOTAL_ANIMALS ] = { 0 };
- int slithering_animals[ TOTAL_ANIMALS ] = { 0 };
- int whiskers[ TOTAL_ANIMALS ] = { 0 };
- int paws[ TOTAL_ANIMALS ] = { 0 };
- int stripes[ TOTAL_ANIMALS ] = { 0 };
- int fur[ TOTAL_ANIMALS ] = { 0 };
- int fourlegged_animals[ TOTAL_ANIMALS ] = { 0 };
- int stinging_animals[ TOTAL_ANIMALS ] = { 0 };
- int tail[ TOTAL_ANIMALS ] = { 0 };
- int scales[ TOTAL_ANIMALS ] = { 0 };
- int carnivores[ TOTAL_ANIMALS ] = { 0 };
- int spots[ TOTAL_ANIMALS ] = { 0 };
- int gills[ TOTAL_ANIMALS ] = { 0 };
- int horns[ TOTAL_ANIMALS ] = { 0 };
- int feathers[ TOTAL_ANIMALS ] = { 0 };
- int continue_game = 1;
- int guesses = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(YES_PIN, INPUT);
- pinMode(NO_PIN, INPUT);
- digitalWrite(YES_PIN, HIGH);
- digitalWrite(NO_PIN, HIGH);
- //time_t start_time, end_time;
- setupGameData();
- startGame();
- Serial.println("Gioco Avviato a" && millis());
- }
- void loop() {
- guesses++;
- switch ( guesses )
- {
- case 1:
- askUserQuestion( guesses, "E' un mammifero?", mammals );
- break;
- case 2:
- askUserQuestion( guesses, "Vola?", flying_animals );
- break;
- case 3:
- askUserQuestion( guesses, "Vive soprattutto o sempre in acqua?", water_animals );
- break;
- case 4:
- askUserQuestion( guesses, "Ha un becco?", beak );
- break;
- case 5:
- askUserQuestion( guesses, "E' notturno?", nocturnal_animals );
- break;
- case 6:
- askUserQuestion( guesses, "Ha una conchiglia?", shell_animals );
- break;
- case 7:
- askUserQuestion( guesses, "Striscia?", slithering_animals );
- break;
- case 8:
- askUserQuestion( guesses, "Ha la barba?", whiskers );
- break;
- case 9:
- askUserQuestion( guesses, "Ha le zampe?", paws );
- break;
- case 10:
- askUserQuestion( guesses, "Ha le strisce?", stripes );
- break;
- case 11:
- askUserQuestion( guesses, "Ha il pelo?", fur );
- break;
- case 12:
- askUserQuestion( guesses, "Ha quattro gambe?", fourlegged_animals );
- break;
- case 13:
- askUserQuestion( guesses, "Può pungere?", stinging_animals );
- break;
- case 14:
- askUserQuestion( guesses, "Ha una coda?", tail );
- break;
- case 15:
- askUserQuestion( guesses, "Ha le squame?", scales );
- break;
- case 16:
- askUserQuestion( guesses, "E' carnivoro?", carnivores );
- break;
- case 17:
- askUserQuestion( guesses, "Ha delle macchie?", spots );
- break;
- case 18:
- askUserQuestion( guesses, "Ha le branchie?", gills );
- break;
- case 19:
- askUserQuestion( guesses, "Ha le corna o le zanne?", horns );
- break;
- case 20:
- askUserQuestion( guesses, "Ha le piume?", feathers );
- break;
- default :
- Serial.println("Congratulazioni! Hai vinto, non riesco ad indovinare il tuo animale");
- continue_game = 0;
- break;
- }
- if ( DEBUG_STATUS )
- displayRemainingAnimals();
- if ( countRemainingAnimals() == 1 )
- {
- Serial.print("Il tuo animale è il ");
- Serial.println(findLastAnimal());
- continue_game = 0;
- }
- while ( continue_game );
- }
- void setupGameData()
- {
- char* animal;
- int index;
- animal = strtok_r( strdup( ANIMALS_LIST ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- animals_list[ index ] = animal;
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( MAMMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- mammals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( FLYING_ANIMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- flying_animals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( WATER_ANIMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- water_animals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( BEAK ) , ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- beak[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( NOCTURNAL_ANIMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- nocturnal_animals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( SHELL_ANIMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- shell_animals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( SLITHERING_ANIMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- slithering_animals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( WHISKERS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- whiskers[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( PAWS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- paws[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( STRIPES ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- stripes[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( FUR ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- fur[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( FOURLEGGED_ANIMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- fourlegged_animals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( STINGING_ANIMALS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- stinging_animals[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( TAIL ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- tail[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( SCALES ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- scales[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( CARNIVORES ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- carnivores[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( SPOTS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- spots[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( GILLS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- gills[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( HORNS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- horns[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- animal = strtok_r( strdup( FEATHERS ), ANIMAL_DELIMITER );
- for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
- {
- feathers[ index ] = atoi( animal );
- animal = strtok_r( NULL, ANIMAL_DELIMITER );
- }
- }
- void startGame()
- {
- char buffer[ BUFFER_SIZE ];
- int index;
- Serial.println("Benvenuto nel gioco degli animali");
- Serial.println("Scegli un animale dalla lista:")
- for ( index = 0; index < TOTAL_ANIMALS; index++ ) {
- Serial.print(animals_list[index]);
- Serial.print(" ");
- }
- }
- void askUserQuestion( int guessNumber, char* question, int* animalData )
- {
- int buttonY = digitalRead(YES_PIN);
- int buttonN = digitalRead(NO_PIN);
- char buffer[ BUFFER_SIZE ];
- int index;
- Serial.println(question);
- while ( buttonY == LOW || buttonN == LOW )
- {
- for ( index = 0; index < TOTAL_ANIMALS; index++ )
- {
- if ( animals_list[ index ] != NULL )
- {
- if ( buttonY == LOW)
- {
- if ( animalData[ index ] != 1 )
- animals_list[ index ] = NULL;
- }
- else
- {
- if ( animalData[ index ] == 1 )
- animals_list[ index ] = NULL;
- }
- }
- }
- }
- /* Display the list of possible animals remaining */
- void displayRemainingAnimals()
- {
- int index;
- Serial.println("Animali rimasti: ")
- for ( index = 0; index < TOTAL_ANIMALS; index++ )
- {
- if ( animals_list[ index ] != NULL )
- Serial.print(animals_list[index]);
- Serial.print(" ");
- }
- Serial.println("");
- }
- int countRemainingAnimals()
- {
- int animals_remaining = 0;
- int index;
- for ( index = 0; index < TOTAL_ANIMALS; index++ )
- {
- if ( animals_list[ index ] != NULL )
- animals_remaining++;
- }
- return animals_remaining;
- }
- char* findLastAnimal()
- {
- int index;
- for ( index = 0; index < TOTAL_ANIMALS; index++ )
- {
- if ( animals_list[ index ] != NULL )
- return animals_list[ index ];
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement