玩家昵称生成器の实现 C++

0
8

低水平内容,有待指正,还请大家看完.

我们在注册游戏时,有些用户名游戏可以自动生成,下面带大家来实现一下这一功能.

思路:

用三个可以容纳字符串的数组(这里以string为例)存放一些字符串,然后输出他们。

举个栗子:二次元世界的神奇小姐姐

二次元世界  →  Where数组

的 → 用cout输出

神奇     →  describe数组

小姐姐    →  Who数组

这里如果使用string字符串数组,就是这句代码:

cout<<Where[rand()]<<"的"<<Describe[rand()]<<Who[rand()];

Where数组: 哪里

Describe数组: 怎么样(形容)

Who数组: 谁

看到这里,很多猿和媛就要发话了,rand()重复怎么办?

很简单,用这句代码放main里:

srand(time(0));

到此,思路差不多了,给大家放个例程,给个更完善的思路:

#include <iostream>
#include <cstring>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
using namespace std;
string Where[]={"外星","宇宙","二次元","中国","美国","泰国","越南","澳洲","北极","南极","印度","韩国","日本","上海","东京","北京","南京","天津","湖南","湖北","河北","陕西","山西","河南","内蒙古","中国台湾","大阪","名古屋","尼泊尔","四川","武汉","英国","巴基斯坦","伊朗","土耳其","海南","葡萄牙","西班牙","法国","瑞士","瑞典","荷兰","钉钉","四维空间","三次元","四次元","五维空间","主世界","异世界"};
string describe[]={"神奇","无敌","超级","厉害","幽默","搞笑","腻害","好玩","","","","","","","","","隐忍","高兴","流弊","软萌","可爱","小小","好玩","炒鸡可爱"}; 
string Who[]={"史蒂夫","史蒂夫大叔","牛仔","史莱姆","牛牛","羊羊","猫咪","狗狗","玩家","玩家","玩家","客人","骑士","TNT","红鲤鱼和绿鲤鱼","红鲤鱼与绿鲤鱼与驴","战马","小可爱","妹子"};
int main()
{
    while(1)
    {
        cout<<"你的昵称:"<<Where[rand()%(49-1)]<<"的"<<describe[rand()%(24-1)]<<Who[rand()%(19-1)]<<endl;
        cout<<"不满意?按任意键以换一个昵称";
        getch();
        system("cls");
    }
    return 0;
}

下面是我通过这个东东实现的玩家昵称生成器plus:

/*各个函数的使用:
系统自带的函数: 
rand() 随机数
sysytem(命令) 调用cmd命令 
system("cls") 清屏
system("title 标题") 更改在窗口最上面的名字,一言难尽,自己开个程序试一hà吧
system("color *1*2") *1 代表背景 *2代表字体颜色(前景),*1*2中间不用加空格 
Sleep(时间)    顾名思义,让程序”休息“一小会,自己可以试一下,时间处填1000为1秒,以此类推
getch() 让程序无需回车录入一个字符,有点像system("pause"),只不过把按哪个键记录下来了
goto 地点    用法:goto where;就会跳转到写有where:(没有分号)的地方 

自己搞的函数(当然都定义了):
Enter(数量) 数量处填\n的数量,比如我填3程序就会换行三行
Space(数量) 数量处填3就会输出三个空格
Tab(数量)   数量处 填3就会输出三个Tab(\t)
run(时间,提示语(string),回车数,空格数) 比如我填run(3,正在加载中,3,3)就会先输出
3个换行再输出3个空格再显示3秒“正在加载中”的动画,自己可以复制粘贴试试看
conformity(换行数,空格数) 排版的一个工具,集合了Enter和Space,比如我填3和3就会
换3行再输出3个空格    
*/ 
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <windows.h>
#include <cstdlib>
#include <conio.h>
#include <ctime>
using namespace std;
HWND hwnd=GetForegroundWindow();//让hwnd代表最前端的窗口 
void Enter(int x)//回车函数 
{
    for(int i=1;i<=x;i++)    cout<<endl;
}
void Space(int x)//空格函数 
{
    for(int i=1;i<=x;i++)    cout<<" ";
}
void Tab(int x)//制表符函数 
{
    for(int i=1;i<=x;i++)    cout<<"\t";
}
void conformity(int a,int b)//排版函数 
{
    Enter(a);
    Space(b);
}
void run(int x,string a,int enter,int space)//开始动画 
{
    for(int i=1;i<=x;i++)
    {
        system("cls");
        conformity(enter,space);
        cout<<a; 
        if(i%3==1) cout<<".";
        if(i%3==2) cout<<"..";
        if(i%3==0)    cout<<"...";
        Sleep(500);
        
    }
}
string Where[]={"外星","宇宙","二次元","中国","美国","泰国","越南","澳洲","北极","南极","印度","韩国","日本","上海","东京","北京","南京","天津","湖南","湖北","河北","陕西","山西","河南","内蒙古","中国台湾","大阪","名古屋","尼泊尔","四川","武汉","英国","巴基斯坦","伊朗","土耳其","海南","葡萄牙","西班牙","法国","瑞士","瑞典","荷兰","钉钉","四维空间","三次元","四次元","五维空间","主世界","异世界"};
string describe[]={"神奇","无敌","超级","厉害","幽默","搞笑","腻害","好玩","","","","","","","","","隐忍","高兴","流弊","软萌","可爱","小小","好玩","炒鸡可爱"}; 
string Who[]={"史蒂夫","史蒂夫大叔","牛仔","史莱姆","牛牛","羊羊","猫咪","狗狗","玩家","玩家","玩家","客人","骑士","TNT","红鲤鱼和绿鲤鱼","红鲤鱼与绿鲤鱼与驴","战马","小可爱","妹子"};
string area;//存放地区 
bool flag;
char cheek,t;
int main()
{
    system("cls"); //清屏的好习惯 
    system("color 07");//颜色初始化 
    ShowWindow(hwnd,SW_MAXIMIZE);//窗口初始化 
    srand(time(0));//选取随机数种子 
    
    system("title 玩家昵称生成器1.0");//设置标题 
    
    conformity(20,85);//排版 
    
    cout<<"玩家昵称生成器"<<endl;
    Space(65);//空格 
    
    cout<<"你 愿意透露你的地区吗?这将关乎到你的昵称(昵称始终是中文的)"<<endl;
    Space(78);//空格 
    cout<<"什么,我不愿透露我所在的地区?"<<endl<<endl;
    Space(78);//空格 
    cout<<"输入Y以确认,N以不愿透露地区";
    cheek=getch();//存放Y和N 
    if(cheek=='n')    {flag=true;cout<<cheek;goto there;}//如果不愿透露地区,就传送到there: 
    
    system("cls");//清屏 
    conformity(20,83);//排版 
    cout<<"请输入你的地区(城市):"<<endl; 
    Space(83);//空格 
    cin>>area;//输入地区 
    
    there://传送门 
    system("cls");//清屏 
    conformity(20,85);//排版 
    cout<<"玩家昵称生成器"<<endl;
    Space(85);//空格 
    cout<<"按任意键以生成:";
    getch();//实现按任意键 
    
    system("cls");//清屏 
    run(3,"in run",20,85);//"加载中"动画 
    
    here://传送门 
    int ttt=rand()%31;//生成类似于抽奖机的字体滚动效果,什么效果见运行的exe 
    for(int i=1;i<=ttt;i++)
    {
        system("cls");//清屏 
        conformity(20,85);//排版 
        if(flag==true)
        cout<<"你的昵称:"<<Where[rand()%(49-1)]<<"的"<<describe[rand()%(24-1)]<<Who[rand()%(19-1)];
        else
        cout<<"你的昵称:"<<area<<"的"<<describe[rand()%(24-1)]<<Who[rand()%(19-1)];
    }
    
    Enter(5);//回车 
    Space(85);//空格 
    cout<<"什么,我不满意?按Tab键以换一个名字"<<endl;
    Space(85);//空格 
    cout<<"什么,我满意了?按空格键以退出";
    //排除法 
    t=getch();
    if(t==' ')    return 0;//退出 
    goto here;//传送门 
    return 0;
}

本人小学生,难怪有这么低水平的内容,还请各位前辈指正.

<

发布回复

请输入评论!
请输入你的名字