Add tamagotchi content. - brcon2024-hackathons - Bitreichcon 2024 Hackathons HTML git clone git://bitreich.org/brcon2024-hackathons git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/brcon2024-hackathons DIR Log DIR Files DIR Refs DIR Tags DIR Submodules --- DIR commit f0deab294f953d30f76cb86f38521a7ebdcdc6b0 DIR parent 0b190cea54fbc97803635f2bf771a2edd6b262e5 HTML Author: Christoph Lohmann <20h@r-36.net> Date: Thu, 8 Aug 2024 23:08:50 +0200 Add tamagotchi content. Diffstat: D mqtt/tamagotchi | 1 - A mqtt/tamagotchi/LICENSE | 21 +++++++++++++++++++++ A mqtt/tamagotchi/README.md | 31 +++++++++++++++++++++++++++++++ A mqtt/tamagotchi/Tamagotchi.png | 0 A mqtt/tamagotchi/tamagotchi.c | 84 +++++++++++++++++++++++++++++++ A mqtt/tamagotchi/tamagotchi.h | 138 ++++++++++++++++++++++++++++++ 6 files changed, 274 insertions(+), 1 deletion(-) --- DIR diff --git a/mqtt/tamagotchi b/mqtt/tamagotchi @@ -1 +0,0 @@ -Subproject commit 61808fed51f116f4c1b5ab622a9e3e76738b303b DIR diff --git a/mqtt/tamagotchi/LICENSE b/mqtt/tamagotchi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Rui Catarino + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. DIR diff --git a/mqtt/tamagotchi/README.md b/mqtt/tamagotchi/README.md @@ -0,0 +1,31 @@ +# Tamagotchi +[](https://spdx.org/licenses/MIT.html) + +<p align="center"> + <img src="https://github.com/ruitcatarino/tamagotchi/blob/master/Tamagotchi.png?raw=true" alt="Tamagotchi start example."/> +</p> + +## Overview +Tamagotchi is a simple virtual pet program developed in C. It enables users to interact with a digital pet, offering actions such as feeding, playing, and monitoring its well-being. The simulation is threaded and utilizes the `SIMULATION_SPEED` constant, representing each year in seconds. + +## Installation + +### Clone the repository +```bash +git clone https://github.com/ruitcatarino/tamagotchi +cd tamagotchi +``` + +### Build from source code and run it +``` bash +gcc tamagotchi.c -o tamagotchi -pthread +./tamagotchi +``` + +## Contributing + +Feel free to submit a [pull request](https://github.com/ruitcatarino/tamagotchi/pull/new/main) or an [issue](https://github.com/ruitcatarino/tamagotchi/issues/new)! + +## License + +This project is open-source and available under the [MIT License](LICENSE). You are free to use, modify, and distribute it in accordance with the license terms. DIR diff --git a/mqtt/tamagotchi/Tamagotchi.png b/mqtt/tamagotchi/Tamagotchi.png Binary files differ. DIR diff --git a/mqtt/tamagotchi/tamagotchi.c b/mqtt/tamagotchi/tamagotchi.c @@ -0,0 +1,84 @@ +#include "tamagotchi.h" +#include <pthread.h> +#include <unistd.h> + +#define SIMULATION_SPEED 5 + +void *time_thread(void *arg){ + struct Tamagotchi *tamagotchi = (struct Tamagotchi *)arg; + + while (tamagotchi->alive){ + sleep(SIMULATION_SPEED); + + tamagotchi->hunger += 3; + tamagotchi->happiness -= 3; + tamagotchi->age++; + + check_status(tamagotchi); + } + + pthread_exit(NULL); +} + +void create(struct Tamagotchi* tamagotchi){ + + //tamagotchi_name(tamagotchi); + + strcpy(tamagotchi->name, "pazz0"); + tamagotchi->age=0; + tamagotchi->age_of_death=rand() % 61 + 50; + tamagotchi->hunger=0; + tamagotchi->happiness=100; + tamagotchi->alive=true; +} + +void life(struct Tamagotchi* tamagotchi){ + + int choice; + + srand(time(NULL)); + + while(tamagotchi->alive){ + + scanf("%d", &choice); + + if(tamagotchi->alive == false){ + break; + } + + if (choice == 1){ + feed(tamagotchi); + } else if (choice == 2){ + play(tamagotchi); + } else if (choice == 3){ + break; + } else{ + printf("I miss you :/ \n"); + } + + check_status(tamagotchi); + + } + +} + +int main() { + + struct Tamagotchi tamagotchi; + + create(&tamagotchi); + + tamagotchi_status_and_menu(&tamagotchi); + + pthread_t tid; + if (pthread_create(&tid, NULL, time_thread, (void *)&tamagotchi) != 0){ + perror("Error creating thread"); + return 1; + } + + life(&tamagotchi); + + pthread_join(tid, NULL); + + return 0; +} DIR diff --git a/mqtt/tamagotchi/tamagotchi.h b/mqtt/tamagotchi/tamagotchi.h @@ -0,0 +1,138 @@ +#ifndef TAMAGOTCHI_TAMAGOTCHI_H +#define TAMAGOTCHI_TAMAGOTCHI_H + +#include <stdio.h> +#include <string.h> +#include <stdbool.h> +#include <stdlib.h> +#include <time.h> + +struct Tamagotchi{ + + char name[50]; + int age; + int age_of_death; + int hunger; + int happiness; + bool alive; + +}; + +void tamagotchi_sad(){ + printf(" .^._.^.\n" + " | ; ; |\n" + " ( --- )\n" + " .' '.\n" + " |/ \\|\n" + " \\ /-\\ /\n" + " V V\n"); +} + +void tamagotchi_happy(){ + printf(" .^._.^.\n" + " | . . |\n" + " ( \\_/ )\n" + " .' '.\n" + " |/ \\|\n" + " \\ /-\\ /\n" + " V V\n"); +} + +void clearScreen(){ + #ifdef _WIN32 + system("cls"); + #else + system("clear"); + #endif +} + +void print_menu(){ + printf("What would you like to do?\n"); + printf("1. Feed\n"); + printf("2. Play\n"); + printf("3. Quit\n"); + printf("(Any onther number) Do nothing\n"); +} + +void tamagotchi_status_and_menu(struct Tamagotchi* tamagotchi){ + + if(tamagotchi->hunger > 60 || tamagotchi->happiness <= 25){ + tamagotchi_sad(); + }else { + tamagotchi_happy(); + } + printf("My name is %s, and I'm %d years old!\n",tamagotchi->name,tamagotchi->age); + + if (tamagotchi->hunger > 60){ + printf("Hunger: (%d%%)\n\tI am starving :( \n",tamagotchi->hunger); + } else if (tamagotchi->hunger > 25){ + printf("Hunger: (%d%%)\n\tI am hungry \n",tamagotchi->hunger); + } else{ + printf("Hunger: (%d%%)\n\tI am not hungry :)\n",tamagotchi->hunger); + } + + if (tamagotchi->happiness > 50){ + printf("Happiness: (%d%%)\n\tI am happy :) \n",tamagotchi->happiness); + } else if (tamagotchi->happiness > 25){ + printf("Happiness: (%d%%)\n\tI am a okay \n",tamagotchi->happiness); + } else{ + printf("Happiness: (%d%%)\n\tI am sad :( \n",tamagotchi->happiness); + } + + print_menu(); +} + +void tamagotchi_name(struct Tamagotchi* tamagotchi){ + char name_var[11]; + + printf("Hello, my name is ...\n"); + + scanf(" %[^\n]s",name_var); + + while (strlen(name_var)>10 || strlen(name_var)<0){ + printf("Please choose another name I wont remember that\nMy name must be between 0 and 10 characters.\n"); + + scanf(" %[^\n]s",name_var); + } + + strcpy(tamagotchi->name,name_var); +} + +void feed(struct Tamagotchi *tamagotchi){ + if (tamagotchi->hunger < 5){ + tamagotchi->hunger=0; + } else{ + tamagotchi->hunger-=5; + } + +} + +void play(struct Tamagotchi *tamagotchi){ + if (tamagotchi->happiness > 96){ + tamagotchi->happiness=100; + } else{ + tamagotchi->happiness+=5; + } +} + +void check_status(struct Tamagotchi *tamagotchi){ + clearScreen(); + + if (tamagotchi->happiness <= 0){ + tamagotchi_sad(); + printf("I died from being unhappy!\n"); + tamagotchi->alive = false; + } else if (tamagotchi->hunger >= 100){ + tamagotchi_sad(); + printf("I died from being too hungry!\n"); + tamagotchi->alive = false; + } else if (tamagotchi->age >= tamagotchi->age_of_death){ + tamagotchi_sad(); + printf("I died from old age!\n"); + tamagotchi->alive = false; + } else { + tamagotchi_status_and_menu(tamagotchi); + } +} + +#endif //TAMAGOTCHI_TAMAGOTCHI_H