/** * This function read a position by clicking in * the HTML interface's board * * @v info_s when the HTML interface is chosen * @return void * @date 2014-05-13 * @author PL Team **/ void PL_HTMLread_move(position_t *pos) { /* Maximum size for the buffer to read a move: 3 characters + 1 \n + 1 \0 => 5 */ #define READ_MOVE_MAX_BUFF_LEN (5) //The move_in_board (being the move read from the user) is 5 to verify the length of the input //char move_in_board[READ_MOVE_MAX_BUFF_LEN]; char move_in_board[READ_MOVE_MAX_BUFF_LEN]; // Maximum number of characters we want to read with fgets (includes '\n' and '\0') int maxLength = READ_MOVE_MAX_BUFF_LEN; //This allows one more character in order to verify the length of the input FILE *mov; startServer(); printf("Write your move [3 characters max]: "); //fgets(move_in_board, maxLength, stdin); //gets in the string move_in_board a maximum of maxLength characters coming from the STDIN mov = fopen("move.mv","r+"); fgets(move_in_board, maxLength, mov); fclose(mov); /* Terminate the string at the first \n */ terminate_string_at_first_slash_n(move_in_board); pos->X = input_is_digit(move_in_board); pos->Y = input_is_char(move_in_board); pos->Y = toupper(pos->Y); pos->Y_int = board_col_to_matrix_idx(pos->Y); }
void read_move1(position_t *pos) { /* Maximum size for the buffer to read a move: 3 characters + 1 \n + 1 \0 => 5 */ #define READ_MOVE_MAX_BUFF_LEN (5) int dimension = board_get_size(); //The move_in_board (being the move read from the user) is 5 to verify the length of the input // char move_in_board[READ_MOVE_MAX_BUFF_LEN]; char move_in_board[READ_MOVE_MAX_BUFF_LEN]; // Maximum number of characters we want to read with fgets (includes '\n' and '\0') int maxLength = READ_MOVE_MAX_BUFF_LEN; //This allows one more character in order to verify the length of the input // Keep the number of char in the move_in_board string size_t move_in_board_strlen; do { do { printf("Write your move [3 characters max]: "); fgets(move_in_board, maxLength, stdin); //gets in the string move_in_board a maximum of maxLength characters coming from the STDIN /* Terminate the string at the first \n */ terminate_string_at_first_slash_n(move_in_board); /* Compute # of chars in move_in_board */ move_in_board_strlen = strlen(move_in_board); if( move_in_board_strlen > 3) { clean_buffer_keyboard(); printf("The move has a maximum of three characters!\n"); } } while(move_in_board_strlen > 3); pos->X = input_is_digit(move_in_board); pos->Y = input_is_char(move_in_board); pos->Y = toupper(pos->Y); pos->Y_int = board_col_to_matrix_idx(pos->Y); //extern int board_col_to_matrix_idx(char col); if(pos->X < 1 || pos->X > dimension) { printf("Invalid number!\nThe number has to be between 1 - %d\n\n", dimension); } if(pos->Y_int == -1 || pos->Y_int >= dimension) { printf("Invalid letter!\nIt has to be between A - %c\n\n", 64+dimension); } } while(pos->Y_int == -1 || pos->Y_int >= dimension || pos->X < 1 || pos->X > dimension); printf("Part of the board! \n"); }