-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtamagotchi.c
83 lines (56 loc) · 1.47 KB
/
tamagotchi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "tamagotchi.h"
#include <pthread.h>
#include <unistd.h>
#define SIMULATION_SPEED 1
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);
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;
}