#include #include #include #include #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; }