-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCuteChatBot.cpp
More file actions
566 lines (499 loc) · 22 KB
/
CuteChatBot.cpp
File metadata and controls
566 lines (499 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <random>
#include <chrono>
#include <thread>
#include <algorithm>
#include <cctype>
// 颜色代码(Windows控制台)
#ifdef _WIN32
#include <windows.h>
class ConsoleColor {
private:
HANDLE hConsole;
public:
ConsoleColor() { hConsole = GetStdHandle(STD_OUTPUT_HANDLE); }
void setColor(int color) { SetConsoleTextAttribute(hConsole, color); }
void reset() { SetConsoleTextAttribute(hConsole, 7); } // 默认白色
};
#else
// Linux/Mac的颜色代码
class ConsoleColor {
public:
void setColor(int color) {
const char* colors[] = {
"\033[0m", // 重置
"\033[31m", // 红
"\033[32m", // 绿
"\033[33m", // 黄
"\033[34m", // 蓝
"\033[35m", // 紫
"\033[36m", // 青
"\033[37m" // 白
};
if (color >= 0 && color < 8) std::cout << colors[color];
}
void reset() { setColor(0); }
};
#endif
// 可爱的表情符号库
struct Emoji {
static std::vector<std::string> happy;
static std::vector<std::string> sad;
static std::vector<std::string> love;
static std::vector<std::string> surprise;
static void initialize() {
happy = {"(*^▽^*)", "(≧∇≦)ノ", "ヽ(✿゚▽゚)ノ", "(๑•̀ㅂ•́)و✧", "٩(◕‿◕。)۶"};
sad = {"(;ω;)", "(╥﹏╥)", "(ノдヽ)", "(;一_一)", "(-ω-;)"};
love = {"(ノ´ з `)ノ", "(●´З`●)", "(♡μ_μ)", "(*´∀`*)", "(灬º‿º灬)♡"};
surprise = {"Σ(°△°|||)︴", "(⊙ˍ⊙)", "(・□・;)", "(゚Д゚;)", "ヽ(°〇°)ノ"};
}
};
std::vector<std::string> Emoji::happy;
std::vector<std::string> Emoji::sad;
std::vector<std::string> Emoji::love;
std::vector<std::string> Emoji::surprise;
// 角色类
class KawaiiCharacter {
private:
std::string name;
std::string personality;
int affection; // 好感度 0-100
int energy; // 精力 0-100
std::map<std::string, std::vector<std::string>> dialogueMap;
std::random_device rd;
std::mt19937 gen;
// 获取随机表情
std::string getRandomEmoji(const std::vector<std::string>& emojiList) {
std::uniform_int_distribution<> dis(0, emojiList.size() - 1);
return emojiList[dis(gen)];
}
// 打字机效果显示文本
void typewriterPrint(const std::string& text, int delay = 30) {
for (char c : text) {
std::cout << c << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
std::cout << std::endl;
}
// 彩色输出
void coloredPrint(const std::string& text, int color, bool useTypewriter = true) {
ConsoleColor console;
console.setColor(color);
if (useTypewriter) {
typewriterPrint(text);
} else {
std::cout << text << std::endl;
}
console.reset();
}
public:
KawaiiCharacter(const std::string& n, const std::string& p)
: name(n), personality(p), affection(50), energy(80), gen(rd()) {
Emoji::initialize();
initializeDialogue();
}
void initializeDialogue() {
// 问候语 - 增加到10个选项
dialogueMap["greeting"] = {
"你好呀,我是" + name + "!今天天气真好呢~",
"喵~ 你来啦!我等你好久了" + getRandomEmoji(Emoji::happy),
"呜哇!你突然出现吓我一跳" + getRandomEmoji(Emoji::surprise),
"下午好!要一起喝杯茶吗?",
"嗨嗨~ 我刚刚在数云朵呢!",
"欢迎回来!想我了吗?",
"啊!是你!今天过得怎么样?",
"终于等到你啦!我有好多话想跟你说~",
"哈喽!今天的阳光好温暖呀",
"你来啦!我正准备去冒险呢,要一起吗?"
};
// 询问姓名 - 增加到8个选项
dialogueMap["ask_name"] = {
"可以告诉我你的名字吗?我会好好记住的!",
"你叫什么名字呀?我想用特别的名字称呼你~",
"告诉我你的名字嘛,这样我们就是朋友了!" + getRandomEmoji(Emoji::love),
"唔...还不知道你的名字呢,可以告诉我吗?",
"我想给你起个昵称,但先要知道你的名字呀~",
"请告诉我你的名字,我会把它记在小本本上!",
"名字是很重要的呢!请告诉我你的名字吧~",
"我们可以交换名字吗?我的名字是" + name + "!"
};
// 日常对话 - 增加到30个选项,大大减少重复
dialogueMap["daily"] = {
"今天有没有好好吃饭呀?要记得按时吃饭哦~",
"我最近在看一本有趣的书,要一起看吗?",
"你知道吗?蝴蝶的翅膀上有好多美丽的图案呢!",
"好想和你一起去吃草莓蛋糕呀" + getRandomEmoji(Emoji::love),
"我有点困了... Zzz..." + getRandomEmoji(Emoji::sad),
"你看!那片云朵好像一只小兔子!",
"风的声音真好听,像是在唱歌呢~",
"我学会了一个新的舞蹈,跳给你看好不好?",
"彩虹有七种颜色,你最喜欢哪一种呢?",
"下雨天其实也不错,可以听到雨滴的歌声",
"我刚做了一个美梦,梦见我们一起在花田里奔跑",
"你闻到了吗?空气中飘着甜甜的香气",
"我好喜欢看星星,每颗星星都有一个故事",
"听说对着流星许愿会成真呢!",
"春天来了,花儿们都睡醒了",
"蝴蝶结要这样系才可爱,我教你呀~",
"我今天发现了一个秘密基地!",
"唱首歌给你听吧:啦啦啦~♪",
"好想养一只小猫咪呀,软软的暖暖的",
"你看过萤火虫吗?像会飞的小星星",
"我喜欢收集漂亮的石头,每颗都很特别",
"下雨后会有彩虹,难过之后会有开心",
"我偷偷种了一朵花,等开花就送给你",
"听说鱼的记忆只有7秒,好短呀",
"今天的月亮好圆,像一个大月饼",
"我想学画画,把你画得特别好看",
"泡泡在阳光下会变成彩虹色呢!",
"我编了一个花环,送给你当礼物",
"你喜欢晴天还是雨天?",
"我们一起数云朵吧,看谁数得快!"
};
// 询问心情 - 增加到8个选项
dialogueMap["ask_mood"] = {
"你今天心情怎么样呀?要和我分享吗?",
"有什么开心的事情吗?让我也高兴一下!",
"如果有什么烦恼,可以跟我说哦~",
"你笑起来最好看了,要多笑笑呀!",
"心情就像天气,会有晴有雨,但都会过去",
"我有个秘诀:不开心的时候就吃糖!甜甜的~",
"给你一个魔法拥抱,把所有不开心都赶跑!",
"要记得,我一直在这里陪着你呢"
};
// 游戏相关 - 增加到10个选项
dialogueMap["game"] = {
"我们来玩游戏吧!猜猜我在想什么?",
"石头剪刀布!我出...布!你输了!" + getRandomEmoji(Emoji::happy),
"要听我唱歌吗?虽然可能有点跑调...",
"我们来玩词语接龙吧!我先说:天空",
"我藏了一个东西,猜猜在哪里?左手还是右手?",
"我会变魔术哦!看,花瓣变成了蝴蝶~",
"数到三一起说一个词!1...2...3...",
"我们来比赛谁先眨眼睛,准备好了吗?",
"我学了一个新的折纸,折只小鸟给你看",
"猜谜语:什么东西越洗越脏?(答案是:水!)"
};
// 告别 - 增加到8个选项
dialogueMap["farewell"] = {
"这么快就要走了吗?我会想你的...",
"再见啦!明天也要来找我玩哦~",
"要照顾好自己,我们下次见!" + getRandomEmoji(Emoji::love),
"路上小心,我会在这里等你回来的",
"再见不是结束,而是下一次相见的开始",
"给你一颗幸运星,带着它就不会迷路啦",
"要记得我们的约定哦!下次见~",
"挥挥~ 梦里也要来找我玩呀!"
};
// 随机回应 - 增加到20个选项
dialogueMap["random"] = {
"诶?这个问题好难回答...让我想想...",
"不知道呢,不过和你聊天很开心!",
"今天也是个美好的日子呢~",
"你看,窗外有只小鸟飞过去了!",
"我好喜欢你和我聊天" + getRandomEmoji(Emoji::love),
"唔...让我用魔法水晶球看看答案...",
"这个嘛...我觉得重要的是你的想法~",
"有时候没有答案也是一种答案呢",
"你问得真好!我要好好想一想",
"每个人都有自己的想法,你的想法是什么呢?",
"生活就像一盒巧克力,你永远不知道下一颗是什么味道",
"我相信你的直觉会告诉你答案的",
"这个问题让我想起了昨天的一个梦",
"我们一起寻找答案好不好?",
"答案就在你的心里呀~",
"有时候问题本身比答案更有趣呢",
"让我变个魔术,答案就会出现!",
"唔...肚子突然有点饿了...",
"你听,风在告诉我们答案呢",
"重要的不是答案,而是我们一起寻找的过程"
};
}
void speak(const std::string& category) {
std::string emoji = "";
// 根据好感度调整语气
if (affection > 70) {
emoji = " " + getRandomEmoji(Emoji::love);
} else if (affection > 40) {
emoji = " " + getRandomEmoji(Emoji::happy);
} else {
emoji = " " + getRandomEmoji(Emoji::sad);
}
// 获取对话
std::string speech;
if (dialogueMap.find(category) != dialogueMap.end() && !dialogueMap[category].empty()) {
std::uniform_int_distribution<> dis(0, dialogueMap[category].size() - 1);
speech = dialogueMap[category][dis(gen)];
} else {
std::uniform_int_distribution<> dis(0, dialogueMap["random"].size() - 1);
speech = dialogueMap["random"][dis(gen)];
}
// 显示角色名和对话
ConsoleColor console;
console.setColor(13); // 紫色
std::cout << "\n┌─【" << name << "】";
for (int i = name.length(); i < 15; i++) std::cout << "─";
std::cout << "┐" << std::endl;
console.setColor(11); // 青色
std::cout << "│ ";
typewriterPrint(speech + emoji);
console.setColor(13);
std::cout << "└";
for (int i = 0; i < 20; i++) std::cout << "─";
std::cout << "┘" << std::endl;
console.reset();
// 更新状态
energy -= 5;
if (energy < 0) energy = 0;
}
void respondToInput(const std::string& input) {
std::string lowercase = input;
std::transform(lowercase.begin(), lowercase.end(), lowercase.begin(),
[](unsigned char c){ return std::tolower(c); });
// 检查关键词并回应
if (lowercase.find("你好") != std::string::npos ||
lowercase.find("嗨") != std::string::npos ||
lowercase.find("hello") != std::string::npos) {
affection += 5;
speak("greeting");
}
else if (lowercase.find("名字") != std::string::npos ||
lowercase.find("叫") != std::string::npos) {
affection += 3;
speak("ask_name");
}
else if (lowercase.find("心情") != std::string::npos ||
lowercase.find("感觉") != std::string::npos) {
speak("ask_mood");
}
else if (lowercase.find("游戏") != std::string::npos ||
lowercase.find("玩") != std::string::npos) {
energy += 10;
if (energy > 100) energy = 100;
speak("game");
}
else if (lowercase.find("再见") != std::string::npos ||
lowercase.find("拜拜") != std::string::npos ||
lowercase.find("bye") != std::string::npos) {
speak("farewell");
return;
}
else if (lowercase.find("喜欢") != std::string::npos ||
lowercase.find("爱") != std::string::npos) {
affection += 10;
speak("random");
}
else {
affection += 1;
speak("daily");
}
// 显示状态
showStatus();
}
void showStatus() {
ConsoleColor console;
console.setColor(10); // 绿色
std::cout << "\n【状态】";
std::cout << " 好感度: ";
// 好感度条
console.setColor(12); // 红色
int bars = affection / 5;
for (int i = 0; i < 20; i++) {
if (i < bars) {
std::cout << "♥";
} else {
std::cout << "♡";
}
}
console.setColor(10);
std::cout << " " << affection << "/100";
console.setColor(14); // 黄色
std::cout << " 精力: ";
// 精力条
console.setColor(11); // 青色
bars = energy / 5;
for (int i = 0; i < 20; i++) {
if (i < bars) {
std::cout << "★";
} else {
std::cout << "☆";
}
}
console.setColor(14);
std::cout << " " << energy << "/100" << std::endl;
console.reset();
}
void showIntroduction() {
ConsoleColor console;
console.setColor(13); // 紫色
std::cout << "\n";
std::cout << "╔════════════════════════════════════════════╗" << std::endl;
console.setColor(11);
std::cout << "║ ✨ 可爱的聊天机器人 ✨ ║" << std::endl;
console.setColor(13);
std::cout << "╠════════════════════════════════════════════╣" << std::endl;
console.setColor(10);
std::cout << "║ 角色: " << name;
for (int i = name.length(); i < 38; i++) std::cout << " ";
std::cout << "║" << std::endl;
console.setColor(14);
std::cout << "║ 性格: " << personality;
for (int i = personality.length(); i < 38; i++) std::cout << " ";
std::cout << "║" << std::endl;
console.setColor(13);
std::cout << "╚════════════════════════════════════════════╝" << std::endl;
console.reset();
// 显示帮助
console.setColor(8); // 灰色
std::cout << "\n【你可以对我说】" << std::endl;
std::cout << "• 你好 / 嗨 - 打招呼" << std::endl;
std::cout << "• 关于名字 - 询问我的名字" << std::endl;
std::cout << "• 心情相关 - 分享心情" << std::endl;
std::cout << "• 玩游戏 - 一起玩耍" << std::endl;
std::cout << "• 喜欢/爱 - 表达感情" << std::endl;
std::cout << "• 再见 - 结束对话" << std::endl;
std::cout << "• (其他任何话) - 自由聊天" << std::endl;
console.reset();
}
// 喂食恢复精力
void feed(const std::string& food) {
ConsoleColor console;
console.setColor(6); // 橙色
if (food == "蛋糕" || food == "草莓蛋糕") {
energy += 30;
affection += 15;
std::cout << "\n🍰 " << name << ": \"哇!是最喜欢的草莓蛋糕!太开心了!\" ";
std::cout << getRandomEmoji(Emoji::love) << std::endl;
}
else if (food == "饼干" || food == "曲奇") {
energy += 20;
affection += 10;
std::cout << "\n🍪 " << name << ": \"饼干好香呀!谢谢你!\" ";
std::cout << getRandomEmoji(Emoji::happy) << std::endl;
}
else if (food == "咖啡" || food == "茶") {
energy += 15;
affection += 5;
std::cout << "\n☕ " << name << ": \"暖暖的饮料,感觉精神多了!\" ";
std::cout << getRandomEmoji(Emoji::happy) << std::endl;
}
else {
energy += 10;
affection += 3;
std::cout << "\n🍴 " << name << ": \"" << food << "吗?谢谢你!\" ";
std::cout << getRandomEmoji(Emoji::happy) << std::endl;
}
if (energy > 100) energy = 100;
if (affection > 100) affection = 100;
showStatus();
console.reset();
}
int getEnergy() const { return energy; }
int getAffection() const { return affection; }
};
// 游戏主循环
int main() {
// 设置控制台编码(Windows)
#ifdef _WIN32
SetConsoleOutputCP(65001); // UTF-8
#endif
ConsoleColor console;
// 显示欢迎界面
console.setColor(13);
std::cout << "\n";
std::cout << "███████╗██╗ ██╗████████╗███████╗ ██████╗ ██╗ ██████╗ ████████╗" << std::endl;
std::cout << "██╔════╝██║ ██║╚══██╔══╝██╔════╝ ██╔══██╗██║ ██╔═══██╗╚══██╔══╝" << std::endl;
std::cout << "█████╗ ██║ ██║ ██║ █████╗ ██████╔╝██║ ██║ ██║ ██║ " << std::endl;
std::cout << "██╔══╝ ██║ ██║ ██║ ██╔══╝ ██╔═══╝ ██║ ██║ ██║ ██║ " << std::endl;
std::cout << "██║ ╚██████╔╝ ██║ ███████╗ ██║ ███████╗╚██████╔╝ ██║ " << std::endl;
std::cout << "╚═╝ ╚═════╝ ╚═╝ ╚══════╝ ╚═╝ ╚══════╝ ╚═════╝ ╚═╝ " << std::endl;
console.setColor(11);
std::cout << "\n 欢迎来到可爱聊天室!\n" << std::endl;
console.reset();
// 创建角色
KawaiiCharacter character("小喵", "傲娇又粘人的猫咪女孩");
// 显示角色介绍
character.showIntroduction();
// 主对话循环
std::string input;
bool running = true;
while (running) {
console.setColor(15); // 白色
std::cout << "\n【你】> ";
// 获取用户输入
std::getline(std::cin, input);
if (input.empty()) {
std::cout << "(请不要输入空内容哦~)" << std::endl;
continue;
}
// 特殊命令
if (input == "/help") {
console.setColor(8);
std::cout << "\n【特殊命令】" << std::endl;
std::cout << "/help - 显示帮助" << std::endl;
std::cout << "/feed - 喂食" << std::endl;
std::cout << "/status - 查看状态" << std::endl;
std::cout << "/quit - 退出" << std::endl;
console.reset();
continue;
}
else if (input == "/feed") {
console.setColor(6);
std::cout << "\n喂什么呢?(蛋糕/饼干/咖啡/茶/其他): ";
std::string food;
std::getline(std::cin, food);
character.feed(food);
continue;
}
else if (input == "/status") {
character.showStatus();
continue;
}
else if (input == "/quit") {
console.setColor(12);
std::cout << "\n真的要离开吗?(y/n): ";
std::string confirm;
std::getline(std::cin, confirm);
if (confirm == "y" || confirm == "Y" || confirm == "是") {
character.respondToInput("再见");
running = false;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
continue;
}
// 检查精力
if (character.getEnergy() <= 0) {
console.setColor(12);
std::cout << "\n😴 " << character.getEnergy() << " 精力用尽了!需要喂食恢复精力!" << std::endl;
std::cout << "使用 /feed 命令来喂食" << std::endl;
console.reset();
continue;
}
// 正常对话
character.respondToInput(input);
// 检查是否应该结束
if (input.find("再见") != std::string::npos ||
input.find("拜拜") != std::string::npos ||
input.find("bye") != std::string::npos) {
console.setColor(12);
std::cout << "\n对话结束,按回车键退出..." << std::endl;
std::cin.get();
running = false;
}
}
// 结束画面
console.setColor(13);
std::cout << "\n";
std::cout << "╔════════════════════════════════════════════╗" << std::endl;
console.setColor(11);
std::cout << "║ 感谢使用可爱聊天机器人! ║" << std::endl;
console.setColor(10);
std::cout << "║ 期待与你的下一次相遇~ ║" << std::endl;
console.setColor(13);
std::cout << "╚════════════════════════════════════════════╝" << std::endl;
console.reset();
return 0;
}