New year's gift in 2020 - make a game from scratch 3
About 2478 words, 1 figure, 90 lines of code
about
"Make a game from scratch" series is an example of a small program for novice programmers. The code is relatively short, so I hope you can follow the idea step by step to manually enter the code. Do not copy Stickers! ——That will only make the article meaningless to you. Unless you've fully understood the principles of this code
Source: Baidu pictures
text
These days, I suddenly wanted to make a code to explore the intelligent voice assistant (such as Siri), and found that I couldn't do it at all. So I made a simple chat robot MIKE.
To achieve this game, we need:
- String type to store the user's input and query the keywords in the input, which is in the string library.
- In order to implement some functions, we need more libraries
Therefore, our pretreatment is as follows:
#include<iostream> #include<string> #include<sstream> #include<ctime> #include<cstdlib> #define M 5 using namespace std;
You may not understand what M is for, don't BB, write your code!
Code
Now let me tell you what M is for. It is used to set the size of each array
string greet[M]={"Hello, boy.", "Hello, earthman", "What can I do for you?", "Ask me a question", "Ha-ha"}; string joke[M]={"Xiao Ming bought a book to teach people how to fall asleep quickly. He saw the dawn every day." "Commander in chief of Inner Mongolia Navy", "Judge: why do you print counterfeit money?\n The defendant said innocently: because I can't print real money.", "Thief a: how much did you rob today?\n Thief B: No, I'll read the newspaper tomorrow.", "Patient: doctor, you left the scissors in my stomach.\n Doctor: never mind. I have another one. "}; string story[M]={"Once upon a time, there was a mountain and a temple in it" "Once upon a time, there was a man named Mike He's standing right in front of you." "I like listening to stories." "#$@&&@!!*$%^^&%^#^&^&#@U*&^%$%#$%^&*......" "Foresee the future, and listen to the next chapter."}; string sorry[M]={"Sorry, my IQ is too low to understand", "Let me see", "I'm not going to answer this question", "No comment", "I'll let you know next time"};
This is the complete answer. Next, there are some functions:
bool allDigits(string s){ for(int i=0;i<s.size();i++) if(s[i]<'0'||s[i]>'9') return 0; return 1; } void decompose(string s){ stringstream ss; int n,c=0; ss<<s; ss>>n; if(n<=1) {cout<<n<<"Not prime numbers."<<endl;return;} for(int i=2;i*i<=n;i++) while(n%i==0){ if(c==0) {cout<<i; c++;} else cout<<"*"<<i; n/=i; } if(c==0) cout<<n<<"It's a prime number."<<endl; else if(n>1) cout<<"*"<<n<<endl; else cout<<endl; }
Finally, the main function contains the Q & A between users and MIKE, as well as the keyword capture:
int main(){ cout<<"Hello, I'm a chat robot. You can ask me all kinds of questions"<<endl; while(1){ string s; getline(cin,s); if(s=="") continue; if(s=="88") break; else if(s.find("name")!=-1||s.find("who")!=-1) cout<<"My name is MIKE,You are my master"<<endl; else if(s.find("year")!=-1||s.find("How big?")!=-1) cout<<"I'm 100 years old. I'll never die"<<endl; else if(s.find("which")!=-1) cout<<"Everywhere"<<endl; else if(s.find("Meeting")!=-1) cout<<"I don't know anything but chat"<<endl; else if(s.find("living")!=-1) cout<<"My birthday is February 30. Please give me a birthday present"<<endl; else if(s.find("wisdom")!=-1) cout<<"I have an IQ of 59 and I haven't passed yet"<<endl; else if(s.find("The Ball")!=-1||s.find("Xing")!=-1||s.find("love")!=-1) cout<<"I love watching football, but I can't play football because I have no legs"<<endl; else if(s.find("laugh")!=-1) cout<<"Let me tell you a joke:"<<endl<<joke[rand()%M]<<endl; else if(s.find("matter")!=-1) cout<<"Let me tell you a story:"<<endl<<story[rand()%M]<<endl; else if(s.find("Hello")!=-1) cout<<greet[rand()%M]<<endl; else if(allDigits(s)){ cout<<s<<"It's a good number"<<endl; decompose(s); } else if(s.find("Bye")!=-1) cout<<"Bye! Looking forward to the next meeting"<<endl; } else cout<<sorry[rand()%M]<<endl; } cout<<"Bye! Looking forward to the next meeting"<<endl; return 0; }
That's all the code. Just write here and go to bed.