Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Game::interceptInput(int a_lineNum, int a_lineDrawPoint, const int a_maxDigits, char a_validChars[], int a_validCharLength, eInput a_inputType, int a_minRange, int a_maxRange) {
- bool validEntry = false;
- int digitCount = 0;
- int validCharSum = 0;
- //Capture all incoming key-presses until something valid has been entered
- while (!validEntry) {
- if (_kbhit()) {
- char ch = _getch();
- //If pressed key is a valid key print it out
- for (int i = 0; i < a_validCharLength; i++) {
- if (ch != a_validChars[i]) continue;
- else if (ch == a_validChars[i] && digitCount < a_maxDigits) {
- DrawChar((a_lineDrawPoint + 1) + digitCount, a_lineNum + 1, ch, CYAN);
- //Add up appropriately for a number
- if (a_inputType == NUM || a_inputType == NUM_RANGE) {
- validCharSum *= 10; //Prepare it for the appropriate value
- validCharSum += (ch - '0'); //Add the value inside of the char instead of the ASCII code
- //Add up to get single character
- } else {
- validCharSum += ch;
- }
- digitCount++;
- }
- }
- //Valid entry is finished, user has hit enter. Now check if its within the appropriate range.
- if (ch == 0x0D && digitCount > 0) {
- //If we want to check if number input is in a valid range
- if (a_inputType == NUM_RANGE) {
- if (validCharSum >= a_minRange && validCharSum < a_maxRange) {
- DrawTextLine(1, a_lineNum, GREEN, "SUCCESS ", 100);
- return validCharSum;
- }
- else {
- DrawTextLine(1, a_lineNum, RED, "NUMBER OUT OF RANGE", 100);
- digitCount = 0;
- validCharSum = 0;
- for (int i = 0; i < a_maxDigits; i++)
- DrawTextLine((a_lineDrawPoint + i), a_lineNum + 1, WHITE, "\t", 1); // \t gives empty space
- continue;
- }
- }
- return validCharSum;
- }
- //Player has made a mistake, clear space proportionate to max digits
- if (ch == 0x08) {
- for (int i = 0; i < a_maxDigits; i++)
- DrawTextLine((a_lineDrawPoint + i), a_lineNum + 1, WHITE, "\t", 1); // \t gives empty space
- validCharSum = 0;
- digitCount = 0;
- }
- }
- }
- return 0; //All code paths now return a value
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement