In C write a version of the 'Animals' guessing game, you can use a temporary file to…
- User
In C write a version of the 'Animals' guessing game, you can use a temporary file to hold the data tree generated if desired?
- Assistant
Below is a simple version of the "Animals Guessing Game" written in C. This program will ask users yes/no questions to guess the animal they are thinking of. After each game, it learns by asking the user for information on the animal it could not guess and builds a binary tree that grows with each new game.
The game stores the questions and animals in a binary tree format, where each non-leaf node is a question, and each leaf node is an animal. If the answer to the question is "yes," it goes to the left child, and if "no," it goes to the right child.
For this example, I'll use a simple in-memory structure to model the binary tree instead of a temporary file. To make the game persistent beyond a single run, you could serialize the data structure to a temporary file, and deserialize it when the program starts up.
Here is a simple C program for the Animals Guessing Game:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node { char *data; struct Node *yes; struct Node *no; } Node; // Function to initialize a new node with a given question or animal Node *createNode(char *data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = strdup(data); newNode->yes = NULL; newNode->no = NULL; return newNode; } // Function to insert a new question and animal into the game tree void insertNode(Node **node, char *question, char *animal) { Node *oldNode = *node; *node = createNode(question); // Assume the new animal is the answer to the "yes" branch of the new question (*node)->yes = createNode(animal); (*node)->no = oldNode; } // Play one round of the game void playGame(Node *node) { char answer[3]; while (node->yes && node->no) { // if both yes/no branches exist, we have a question node printf("%s (yes/no)\n", node->data); scanf("%2s", answer); if (strcmp(answer, "yes") == 0) { node = node->yes; } else { node = node->no; } } printf("Is it a %s? (yes/no)\n", node->data); scanf("%2s", answer); if (strcmp(answer, "yes") == 0) { printf("I guessed it!\n"); } else { printf("I give up. What animal were you thinking of?\n"); char animal[100]; scanf("%99s", animal); printf("Give me a question that would distinguish a %s from a %s.\n", animal, node->data); char question[256]; getchar(); // consume newline from previous input fgets(question, 255, stdin); question[strcspn(question, "\n")] = 0; // remove newline character printf("And what would be the answer for a %s? (yes/no)\n", animal); scanf("%2s", answer); if (strcmp(answer, "yes") == 0) { insertNode(&node, question, animal); } else { insertNode(&node, question, strdup(node->data)); // duplicate string for the new animal free(node->yes->data); // free the duplicated string that's now wrong node->yes->data = strdup(animal); // assign the correct animal } } } // Cleanup function to free allocated memory void freeTree(Node *node) { if (node) { freeTree(node->yes); freeTree(node->no); free(node->data); free(node); } } int main() { Node *root = createNode("Does it live underwater?"); // Initialize with some basic questions and animals root->yes = createNode("shark"); root->no = createNode("monkey"); char playAgain; do { playGame(root); printf("Would you like to play again? (y/n)\n"); scanf(" %c", &playAgain); } while (playAgain == 'y' || playAgain == 'Y'); freeTree(root); return 0; }Save this code to a file called
animals.cand compile it withgcc animals.c -o animals. Then, run the game with./animals.This program starts with a basic tree and builds upon it each time you play. If you'd like permanent storage, you'll need to add file reading and writing functions to serialize and deserialize the game tree from a file.
Remember to sanitize user input in actual use cases to prevent buffer overflows and other security-related issues. The above code is simplified and assumes the input is well-formed.