Esempio n. 1
0
/**
 * @brief  Step through the main stages in the code.
 * @return Void
 */
void sound_game_main(script_t* SCRIPT_ADDRESS, char* LANG_FILESET, char* MODE_FILESET)
{
  switch(next_state)
  {
    case STATE_MENU:
	  if (io_user_abort == true) {
		  PRINTF("[MD3] Quitting to main menu\n\r");
		  quit_mode();
		  io_init();
	  }
	  cell = get_cell();
	  if (cell == NO_DOTS) {
		  break;	
	  }
	  cell_pattern = GET_CELL_PATTERN(cell);
	  cell_control = GET_CELL_CONTROL(cell);
	  switch (cell_control) {
		  /*If user input is A, enter SUBMODE_PLAY; if input is B, enter SUBMODE_LEARN; 
			else go back to menu*/
		  case WITH_ENTER:
		    user_glyph = search_script(this_script, cell_pattern);
			if (cell_pattern == ENG_A) {
			    play_glyph(user_glyph);
				submode = SUBMODE_PLAY;
				next_state = STATE_GENQUES;
			} else if (cell_pattern == ENG_B) {
				play_glyph(user_glyph);
				submode = SUBMODE_LEARN;
				next_state = STATE_GENQUES;
			} else {
				play_mp3(lang_fileset, MP3_INVALID_PATTERN);
				next_state = STATE_MENU;
			}
		  break;
		  case WITH_LEFT:case WITH_RIGHT:case WITH_CANCEL:
		  break;
	  }
	break;
	  
	case STATE_GENQUES:
	  length_entered_word = 0;
	  current_word_index = 0;
	  sound_source = sound_source_list[choose_sound_source()];
	  user_glyph = NULL;
	  curr_glyph = NULL;
      user_word = free_word(user_word);
	  curr_word = free_word(curr_word);
	  next_state = STATE_PROMPT;
	  break;
	  
    case STATE_PROMPT:
	  switch(submode)
	  {
        case SUBMODE_PLAY:
		  play_mp3(MODE_FILESET, "PLSA");
		  play_sound(MODE_FILESET, sound_source, false);
		  break;
		
		case SUBMODE_LEARN:
	      play_mp3(MODE_FILESET, "PLSB");
		  play_sound(MODE_FILESET, sound_source, true);
		  break;
	    
		default:
		  break;  
	  }
	next_state = STATE_INPUT;
	break;
	
	case STATE_INPUT:
	if (io_user_abort == true) {
		PRINTF("[MD3] User aborted input\n\r");
		next_state = STATE_REPROMPT;
		io_init();
	}
	cell = get_cell();
	if (cell == NO_DOTS) {
		break;
	}
	cell_pattern = GET_CELL_PATTERN(cell);
	cell_control = GET_CELL_CONTROL(cell);
	switch (cell_control) {
		case WITH_ENTER:
		user_glyph = search_script(this_script, cell_pattern);
		next_state = STATE_CHECK;
		PRINTF("[MD3] Checking answer\n\r");
		break;
		case WITH_LEFT:
		next_state = STATE_PROMPT;
		break;
		case WITH_RIGHT:
		next_state = STATE_REPROMPT;
		break;
		case WITH_CANCEL:
		break;
	}
	break;

	case STATE_CHECK:
		curr_glyph = search_script(this_script, get_bits_from_letter(sound_source[length_entered_word]));
		if (glyph_equals(curr_glyph, user_glyph)) {
			play_glyph(curr_glyph);
			incorrect_tries = 0;
			length_entered_word++;
			user_word = add_glyph_to_word(user_word, user_glyph);
			if(length_entered_word != strlen(sound_source)) {
			  play_mp3(lang_fileset, "GOOD");
			  next_state = STATE_INPUT;
			} else { 
			  play_mp3(lang_fileset,  "GOOD");
			  play_mp3(lang_fileset, "NCWK");
			  switch (submode){
				  case SUBMODE_LEARN:
				  play_sound(MODE_FILESET, sound_source, true);
				  play_mp3(MODE_FILESET, "SAYS");
				  play_sound(MODE_FILESET, sound_source, false);
				  break;
				  
				  case SUBMODE_PLAY:
				  play_word(user_word);
				  play_sound(MODE_FILESET, sound_source, true);
			  }
			  next_state = STATE_GENQUES;
			}
		} else {
			play_glyph(user_glyph);
			incorrect_tries++;
			PRINTF("[MD3] User answered incorrectly\n\r");
			play_mp3(lang_fileset, "NO");
			play_mp3(lang_fileset, MP3_TRY_AGAIN);
			play_word(user_word);
			if (incorrect_tries == MAX_INCORRECT_TRIES_1) {
				play_mp3(MODE_FILESET, "PLWR");
				play_sound(MODE_FILESET, sound_source, true);
				curr_word = word_to_glyph_word(this_script, sound_source);
				play_word(curr_word);
			} else if (incorrect_tries >= 6) {
				play_glyph(curr_glyph);
				play_mp3(MODE_FILESET, "PRSS");
				play_dot_sequence(curr_glyph);
			}
			next_state = STATE_INPUT;
		}
		break;
	
	case STATE_REPROMPT:
	switch(create_dialog("SKIP",
	ENTER_CANCEL | LEFT_RIGHT)) {
		case NO_DOTS:
		break;

		case CANCEL:
		PRINTF("[MD3] Reissuing prompt");
		next_state = STATE_PROMPT;
		scrolled = false;
		break;

		case ENTER:
		PRINTF("[MD3] Skipping sound_source");
		if (scrolled)
		next_state = STATE_PROMPT;
		else
		next_state = STATE_GENQUES;
		scrolled = false;
		break;

		case RIGHT: case LEFT:
			PRINTF("[MD3] Next sound_source");
			length_entered_word = 0;
			current_word_index = 0;
			sound_source = sound_source_list[choose_sound_source()];
			user_word = NULL;
			switch(submode)
			{
				case SUBMODE_PLAY:
				play_sound(MODE_FILESET, sound_source, false);
				break;
				
				case SUBMODE_LEARN:
				play_sound(MODE_FILESET, sound_source, true);
				break;
				
				default:
				break;
			}
			scrolled = true;
		break;

		default:
		break;
	}
	break;

	default:
	break;
  }
}
void mode_letter_game_main() {
    switch (current_state) {

        case INITIAL:
            play_welcome();
            play_submode_choice();
            current_state = CHOOSE_LEVEL;
            break;

        case CHOOSE_LEVEL:

            switch(create_dialog(NULL, DOT_1 | DOT_2 | ENTER_CANCEL)) {
                case '1':
                    log_msg("Submode: Learn");
                    play_letter_instructions();
                    submode = SUBMODE_LEARN;
                    sort(this_script->letters, this_script->num_letters);
                    current_state = GENERATE_QUESTION;
                    break;

                case '2':
                    log_msg("Submode: Play");
                    play_letter_instructions();
                    shuffle(this_script->letters, this_script->num_letters);
                    should_shuffle = true;
                    current_state = GENERATE_QUESTION;
                    break;

                case CANCEL:
                    log_msg("Quitting to main menu");                    
                    quit_mode();
                    break;

                case ENTER:
                    log_msg("Re-issuing main menu prompt");
                    play_submode_choice();
                    current_state = CHOOSE_LEVEL;
                    break;

                default:
                    break;
            }
            break;


        case GENERATE_QUESTION:
            curr_glyph = get_next_letter(this_script, should_shuffle);
            log_msg("Next glyph: %s", curr_glyph->sound);
            play_next_letter_prompt();
            current_state = PROMPT;
            break;


        case PROMPT:
            play_glyph(curr_glyph);
            switch(submode) {
                case SUBMODE_LEARN:
                    play_dot_prompt();
                    play_dot_sequence(curr_glyph);
                    break;

                case SUBMODE_PLAY:
                default:
                    break;
            }
            current_state = GET_INPUT;
            break;

        case GET_INPUT:
            if (io_user_abort == true) {
                log_msg("User aborted input");
                current_state = PROMPT;
                io_init();
            }

            cell = get_cell();
            if (cell == NO_DOTS)
                break;
            cell_pattern = GET_CELL_PATTERN(cell);
            cell_control = GET_CELL_CONTROL(cell);
            log_msg("Pattern %d, control %d.", cell_pattern, cell_control);
            
            switch (cell_control) {
                case WITH_ENTER:
                    user_glyph = search_script(this_script, cell_pattern);
                    current_state = CHECK_ANSWER;
                    break;
                case WITH_LEFT:
                    current_state = PROMPT;
                    break;
                case WITH_RIGHT:
                    current_state = GENERATE_QUESTION;
                    break;
                case WITH_CANCEL:
                default:
                    break;

            }
            break;

        case CHECK_ANSWER:
            if (glyph_equals(curr_glyph, user_glyph)) {
                if (curr_glyph -> next == NULL) {
                    mistakes = 0;
                    log_msg("User answered correctly");
                    play_right();
                    current_state = GENERATE_QUESTION;
                }
                else {
                    curr_glyph = curr_glyph->next;
                    play_next_cell_prompt();                    
                    if (submode == SUBMODE_LEARN)
                        play_dot_sequence(curr_glyph);
                    else
                        play_glyph(curr_glyph);
                    current_state = GET_INPUT;
                }
            }
            else {
                mistakes++;
                log_msg("User answered incorrectly");
                play_wrong();
                curr_glyph = get_root(this_script, curr_glyph);  
                current_state = PROMPT;
                if (mistakes >= max_mistakes) {
                    play_glyph(curr_glyph);
                    play_dot_prompt();
                    play_dot_sequence(curr_glyph);
                    current_state = GET_INPUT;
                }
            }
        break;

        default:
            log_msg("Invalid state_t %d", current_state);
            quit_mode();
            break;
    }
}