-
Notifications
You must be signed in to change notification settings - Fork 5
/
virtual-pet.c
107 lines (100 loc) · 2.06 KB
/
virtual-pet.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
struct Pet
{
char name[20];
int hunger;
int boredom;
};
void initPet(struct Pet *pet, char *name)
{
strcpy(pet->name, name);
pet->hunger = 0;
pet->boredom = 0;
}
void play(struct Pet *pet)
{
pet->boredom -= 5;
if (pet->boredom < 0)
{
pet->boredom = 0;
}
pet->hunger += 1;
}
void feed(struct Pet *pet)
{
pet->hunger -= 3;
if (pet->hunger < 0)
{
pet->hunger = 0;
}
}
void checkStatus(struct Pet *pet)
{
if (pet->hunger >= 10)
{
printf("%s is hungry!\n", pet->name);
}
if (pet->boredom >= 10)
{
printf("%s is bored!\n", pet->name);
}
if (pet->hunger == 0 && pet->boredom == 0)
{
printf("%s is happy and healthy!\n", pet->name);
}
}
int isAlive(struct Pet *pet)
{
return pet->hunger < 10 && pet->boredom < 10;
}
int main()
{
srand(time(NULL));
struct Pet myPet;
printf("Welcome to Virtual Pet Game!\n");
printf("What would you like to name your pet? ");
char name[20];
scanf("%s", name);
initPet(&myPet, name);
while (isAlive(&myPet))
{
checkStatus(&myPet);
printf("What would you like to do? (1 = Play, 2 = Feed, 3 = Quit) ");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
play(&myPet);
break;
case 2:
feed(&myPet);
break;
case 3:
exit(0);
default:
printf("Invalid choice. Try again.\n");
break;
}
int randomEvent = rand() % 3;
if (randomEvent == 0)
{
feed(&myPet);
printf("Your pet found some food to eat!\n");
}
else if (randomEvent == 1)
{
play(&myPet);
printf("Your pet found a toy to play with!\n");
}
else
{
printf("Your pet is resting.\n");
}
}
printf("Game over. %s has died.\n", myPet.name);
return 0;
}