Skip to content

Commit 8110439

Browse files
Commit
1 parent 958c12f commit 8110439

11 files changed

+889
-4
lines changed

Tamagoshi-vache-refaire.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* @Author: ThearchyHelios
3+
* @Date: 2022-05-04 13:06:10
4+
* @LastEditTime: 2022-05-05 15:38:34
5+
* @LastEditors: ThearchyHelios
6+
* @Description:
7+
* @FilePath: /Projet_cowsay_L1S2/Tamagoshi-vache-refaire.c
8+
*/
9+
10+
#include <stdio.h>
11+
#include <string.h>
12+
#include <stdlib.h>
13+
#include <unistd.h>
14+
15+
typedef int state;
16+
typedef int condition;
17+
18+
#define STATENUM 4
19+
#define STATE1 0
20+
#define STATE2 1
21+
#define STATE3 2
22+
#define STATETRAP 3
23+
24+
#define CONDITIONS 2
25+
#define CONDITION1 0
26+
#define CONDITION2 1
27+
28+
typedef void (*actiontype)(state mystate, condition mycondition);
29+
typedef struct
30+
{
31+
state next;
32+
actiontype action;
33+
} trasition, *ptrasition;
34+
35+
void action1(state mystate, condition myconditon);
36+
void action2(state mystate, condition myconditon);
37+
void action3(state mystate, condition myconditon);
38+
void actiontrap(state mystate, condition myconditon);
39+
trasition t1 = {
40+
STATE2, action1};
41+
trasition t2 = {
42+
STATE3, action2};
43+
trasition t3 = {
44+
STATE2, action3};
45+
trasition tt = {
46+
STATETRAP, actiontrap};
47+
48+
void action1(state mystate, condition myconditon)
49+
{
50+
printf("action1 one triggered\n");
51+
}
52+
void action2(state mystate, condition myconditon)
53+
{
54+
printf("action2 one triggered\n");
55+
}
56+
void action3(state mystate, condition myconditon)
57+
{
58+
printf("action3 one triggered\n");
59+
}
60+
void actiontrap(state mystate, condition myconditon)
61+
{
62+
printf("actiontrap one triggered\n");
63+
}
64+
65+
ptrasition transition_table[STATENUM][CONDITIONS] = {
66+
/* c1, c2*/
67+
/* s1 */ &t1,
68+
&tt,
69+
/* s2 */ &tt,
70+
&t2,
71+
/* s3 */ &t3,
72+
&tt,
73+
/* st */ &tt,
74+
&tt,
75+
};
76+
typedef struct
77+
{
78+
state current;
79+
} StateMachine, *pStateMachine;
80+
81+
state step(pStateMachine machine, condition mycondition)
82+
{
83+
ptrasition t = transition_table[machine->current][mycondition];
84+
(*(t->action))(machine->current, mycondition);
85+
machine->current = t->next;
86+
printf("the current state is %d\n", t->next);
87+
return machine->current;
88+
}
89+
int main(int argc, char *argv[])
90+
{
91+
StateMachine mymachine;
92+
mymachine.current = STATE1;
93+
int mycon;
94+
char ch;
95+
while (1)
96+
{
97+
scanf("%d", &mycon);
98+
step(&mymachine, mycon);
99+
}
100+
return 0;
101+
}

Tamagoshi-vache.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ int main(int argc, char *argv[])
567567
}
568568
}
569569
// random event
570-
// Thunder
570+
// Thunder: life - 2
571571
if (hour + minite == rand() % 100)
572572
{
573573
strcpy(message, "It's a thunder! You lost life!");
@@ -577,7 +577,7 @@ int main(int argc, char *argv[])
577577
tongue = "^";
578578
affiche_vache(&length, message, eyes, tongue, &tail, time_tick, hour, minite, food);
579579
}
580-
// Hunger
580+
// Hunger: food - 4
581581
else if (hour + minite == rand() % 100)
582582
{
583583
strcpy(message, "It's a hunger! You lost food!");
@@ -587,7 +587,7 @@ int main(int argc, char *argv[])
587587
tongue = "^";
588588
affiche_vache(&length, message, eyes, tongue, &tail, time_tick, hour, minite, food);
589589
}
590-
// Fire
590+
// Fire: life - 2 && food - 2
591591
else if (hour + minite == rand() % 100)
592592
{
593593
strcpy(message, "It's a fire! You lost life and food!");
@@ -598,7 +598,7 @@ int main(int argc, char *argv[])
598598
tongue = "^";
599599
affiche_vache(&length, message, eyes, tongue, &tail, time_tick, hour, minite, food);
600600
}
601-
// Mercy
601+
// Mercy: food + 5
602602
else if (hour + minite == rand() % 100)
603603
{
604604
strcpy(message, "Mercy is coming! Food + 5");

a.out

16.6 KB
Binary file not shown.
5.87 KB
Loading
28.5 KB
Loading
30.2 KB
Loading
32.7 KB
Loading
13.1 KB
Loading
29.2 KB
Loading

0 commit comments

Comments
 (0)