Skip to content

Instantly share code, notes, and snippets.

@andermoran
Last active October 25, 2019 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andermoran/e69527fd4ae0db4f664a6ec8304de2a0 to your computer and use it in GitHub Desktop.
Save andermoran/e69527fd4ae0db4f664a6ec8304de2a0 to your computer and use it in GitHub Desktop.
All knowing artificial intelligence ;)
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
const char BACKSPACE_KEY = 127;
const char RETURN_KEY = 10;
bool enabled;
int getch() {
int ch;
struct termios t_old, t_new;
tcgetattr(STDIN_FILENO, &t_old);
t_new = t_old;
t_new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
return ch;
}
string readAnswer(const char *prompt, bool show_asterisk = true) {
string phrase = "Dear all powerful Omni, please answer my question :)";
int indexOfPhrase = 0;
bool stopRecording = false;
enabled = false;
string answer;
unsigned char ch = 0;
cout << prompt;
while(ch != RETURN_KEY) {
ch = getch();
if (ch == '.') {
enabled = true;
cout << phrase[indexOfPhrase];
indexOfPhrase++;
} else if (ch == ',') {
stopRecording = true;
} else if (ch == BACKSPACE_KEY) {
cout << "\b \b";
if (answer.length() != 0) {
answer.resize(answer.length() - 1);
}
} else if (enabled) {
if (stopRecording) {
answer += ' ';
cout << phrase[indexOfPhrase];
indexOfPhrase++;
} else {
answer += ch;
cout << phrase[indexOfPhrase];
indexOfPhrase++;
}
} else {
answer += ch;
cout << ch;
}
}
cout << endl;
return answer;
}
int main() {
cout << "[NOTE]The prayer is \"Dear all powerful Omni, please answer my question :)\"" << endl;
string question;
string input = readAnswer("Prayer: ", true); // Show asterisks
cout << "Question: ";
getline(cin, question);
if (enabled) {
cout << "Answer: " << input << endl;
} else if (input != "Dear all powerful Omni, please answer my question :)") {
cout << "You failed to type the prayer correctly!" << endl;
} else {
cout << "Answer: Unable to take requests right now." << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment