blob: 2fe8c4e0a3a2592c4df6f12fec3c5c104084eaac (
plain)
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
108
109
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define MORSE_CODE_LENGTH 4
#define MAX_CODE_LENGTH 5
#define NUMBER_OF_POTENTIOMETERS 4
typedef enum {
UNINITIALIZED,
IDLE,
PLAYING,
SOLVED
} HardwarePuzzleState;
typedef struct {
HardwarePuzzleState current_state;
char code[MAX_CODE_LENGTH + 1];
bool tone_playing;
int morse_code_step;
int morse_code_number_step;
unsigned long ms_delay;
int potentiometer_values[NUMBER_OF_POTENTIOMETERS];
int correct_potentiometer_values[NUMBER_OF_POTENTIOMETERS];
int logic_gate_values[8];
} PuzzleState;
void generate_random_four_digit_code(PuzzleState *state) {
printf("Generating random morse code...\nCode: ");
for (int i = 0; i < MORSE_CODE_LENGTH; i++) {
state->correct_potentiometer_values[i] = rand() % 10;
state->code[i] = state->correct_potentiometer_values[i] + '0';
}
state->code[MORSE_CODE_LENGTH] = '\0';
printf("%s\n", state->code);
}
void enter_state(PuzzleState *state, HardwarePuzzleState new_state) {
state->current_state = new_state;
}
void render(PuzzleState *state) {
if (state->ms_delay == 0) {
state->ms_delay = millis() + play_morse_code(state);
} else if (millis() >= state->ms_delay) {
state->ms_delay = 0;
}
if (state->current_state == MORSECODE) {
int values = state->potentiometer_values[0] * 1000 +
state->potentiometer_values[1] * 100 +
state->potentiometer_values[2] * 10 +
state->potentiometer_values[3] * 1;
// GpioManager::instance().set_display_value(values); // Voeg GPIO-besturing toe voor Arduino
}
}
void process_state(PuzzleState *state) {
get_user_input(state);
render(state);
puzzle_check(state);
}
void change_gamestage(PuzzleState *state, HardwarePuzzleState new_state) {
state->current_state = new_state;
}
int play_morse_code(PuzzleState *state) {
if (state->current_state == MORSECODE) {
// Implementeer de morsecode logica
}
return 0;
}
void get_user_input(PuzzleState *state) {
if (state->current_state == LOGICGATES) {
// Implementeer de logica voor het verkrijgen van gebruikersinvoer voor LOGICGATES
} else if (state->current_state == MORSECODE) {
// Implementeer de logica voor het verkrijgen van gebruikersinvoer voor MORSECODE
}
}
void puzzle_check(PuzzleState *state) {
if (state->current_state == LOGICGATES) {
// Implementeer de logica voor het controleren van de puzzel voor LOGICGATES
} else if (state->current_state == MORSECODE) {
// Implementeer de logica voor het controleren van de puzzel voor MORSECODE
}
}
void initialize_game(PuzzleState *state) {
srand(time(NULL)); // Initialiseer de random seed met de huidige tijd
state->current_state = UNINITIALIZED;
enter_state(state, IDLE); // Ga naar de IDLE-state na initialisatie
generate_random_four_digit_code(state);
change_gamestage(state, PLAYING); // Ga naar de PLAYING-state om de puzzel te spelen
}
int main() {
PuzzleState puzzle_state;
initialize_game(&puzzle_state);
// Simuleer de loop van de state machine
while (puzzle_state.current_state != SOLVED) {
process_state(&puzzle_state); // Blijf in de huidige staat
}
return 0;
}
|