/*---------------------------------------------------------- * HTBLA-Leonding / Class: 2IHIF * --------------------------------------------------------- * Exercise Number: B1 * Title: Mine Sweeper User Interface Utilities * Author: Marc Tismonar * ---------------------------------------------------------- * Description: * Implementation of ms_ui_utils.h. * ---------------------------------------------------------- */ #include #include #include "ms_ui_utils.h" void msu_init_rand() { srand(time(0)); } CellIdx msu_get_random_index(Count upper_limit) { return (upper_limit == 0 ? 0 : rand() % (upper_limit)); } /** * Maps the given cell index to the corresponding column address. * * @param idx The index to map. * @return ColAddr The corresponding column address or a '0' value, * if the index cannot be mapped. */ ColAddr msu_idx_to_col_address(CellIdx idx) { if (idx > 0 && idx <= MAX_BOARD_SIZE) { return (ColAddr)idx; } return 0; } /** * Maps the given cell index to the corresponding row address. * * @param idx The index to map. * @return RowAddr The corresponding row address or a '0' value, * if the index cannot be mapped. */ RowAddr msu_idx_to_row_address(CellIdx idx) { if (idx > 0 && idx <= MAX_BOARD_SIZE) { return (RowAddr)idx; } return 0; } /** * Maps the given column address to the corresponding cell index. * * @param addr The column address to map. * @return CellIdx The corresponding cell index or 0, * if the address cannot be mapped. */ CellIdx msu_col_address_to_index(ColAddr addr) { if (addr > 0 && addr < MAX_BOARD_SIZE) { return (CellIdx)addr; } return 0; } /** * Maps the given row address to the corresponding cell index. * * @param addr The row address to map. * @return CellIdx The corresponding cell index or 0, * if the address cannot be mapped. */ CellIdx msu_row_address_to_index(RowAddr addr) { if (addr > 0 && addr < MAX_BOARD_SIZE) { return (CellIdx)addr; } return 0; } /** * Provides the character a user may type to perform the given action. * The key must be unique for each action. * * @param action The action for which the corresponding 'key' is requested. * @return char The corresponding 'key' character or '\0', * if the action cannot reasonably be mapped. */ char msu_get_action_char(Action action) { switch (action) { case MARK_MINE: return 'm'; case MARK_SUSPECT: return 's'; case CLEAR_MARKER: return 'c'; case UNCOVER: return 'u'; case QUIT_GAME: return 'q'; default: return '\0'; } } /** * Provides the action for the given 'key' character (the counterpart of ´msu_get_action_char´). * * @param key The key for the action. * @return Action The action corresponding to the given 'key' * or 'INVALID',if the key cannot be mapped. */ Action msu_get_action(char key) { switch (key) { case 'm': return MARK_MINE; case 's': return MARK_SUSPECT; case 'c': return CLEAR_MARKER; case 'u': return UNCOVER; case 'q': return QUIT_GAME; default: return UNKNOWN; } } /** * Maps the given cell marker to its presentation symbol * (the character shown in the game visualization). * * @param marker The marker. * @return char The corresponding character or '#', if * the marker cannot be mapped. */ char msu_get_marker_symbol(CellMarker marker) { switch (marker) { case NONE: return ' '; case MINE_DETECTED: return 'X'; case MINE_SUSPECTED: return '?'; default: return '#'; } } /** * Provides the symbol that represents a mine * (the character shown in the game visualization). * * @return char The symbol of a mine. */ char msu_get_mine_symbol() { return '*'; } /** * Maps the given game status to a user presentable label. * * @param status The status to map. * @return char* The text for the status or 0, if * the status cannot be mapped. */ char* msu_get_status_label(GameState status) { switch (status) { case IN_PROGRESS: return "In Progress"; case SOLVED: return "Solved"; case FAILED: return "Failed"; case INVALID: default: return "Invalid"; } }