4
测试版,可能会有bug。纯手打,求个小小的赞。^_^
#include<bits/stdc++.h>
#include<Windows.h>
#include<conio.h>
#include<limits>
using namespace std;
string username;
int sum=0,xl=100,jin=0;
int p=0,sd=900;
int st[123]={900,800,700,580,430,310,170,90,5};
double bs=1;
int bt=0,zxq=0,mxd=0,hj=0;
double bss[10]={1,1.5,2,3,5,6,9,15,22};
int jg[18]={0,30,150,700,2500,10000,40000,100000,300000};
int pickaxe_level=1;
void wdd(string a){
a+=".txt";
string m="w";
FILE*fp=freopen(a.c_str(),m.c_str(),stdout);
}
char fb(){
return _getch();
}
int czi(char a){
return a-'0';
}
void wk(int t){
if(t<=10){
cout<<"你挖到了岩浆,烫**了!";
xl=0;
}else if(t<=30){
cout<<"恭喜你,挖到了钻石!";
sum+=500*bs;
}else if(t<=50){
cout<<"你遇到了怪物,因此而亡";
xl=0;
}else if(t<=100){
cout<<"你遇到了怪物,因此重伤";
xl-=60;
}else if(t<=180){
cout<<"你遇到了怪物,因此**";
xl-=20;
}else if(t<=250){
cout<<"你挖到了金矿石!";
sum+=200*bs;
}else if(t<=380){
cout<<"你挖到了铁矿石!";
sum+=100*bs;
}else if(t<=500){
cout<<"你挖到了:泥土";
sum+=bs;
}else if(t<=700){
cout<<"你挖到了:岩石";
sum+=bs;
}else if(t<=770){
cout<<"你挖到了青金石!";
sum+=80*bs;
}else if(t<=790){
cout<<"你挖空了,摔了一跤!";
xl-=5;
}else if(t<=800){
cout<<"你失足了,摔亡了!";
xl=0;
}else if(t<=840){
cout<<"你挖到了水,需要花一些时间弄干\n";
Sleep(2600);
if(t==840){
cout<<"你不小心呛**了!";
xl=0;
}else{
cout<<"衣服干了,继续前进";
}
}else if(t<=900){
cout<<"你挖到了:铜";
sum+=50*bs;
}else if(t<910){
cout<<"你有些累,回家了";
p=-1;
}else if(t<=999){
cout<<"你挖到了:煤炭";
sum+=30*bs;
}else{
cout<<"你曾经挖过这里";
}
if(xl==0){
p=-1;
}
}
bool isUserExist(string uname, string pwd, int &gold, int &p_level, int &bandage, int &inject, int &full_blood, int &drill) {
ifstream infile("user_info.txt", ios::in);
if (!infile.is_open()) {
// 无用户文件,直接返回不存在
infile.close();
return false;
}
string u, p;
int g, pl, b, i, fb, d;
while (infile >> u >> p >> g >> pl >> b >> i >> fb >> d) {
if (u == uname) {
// 用户名存在,验证密码
if (p == pwd) {
// 密码正确,赋值用户数据
gold = g;
p_level = pl;
bandage = b;
inject = i;
full_blood = fb;
drill = d;
infile.close();
return true;
} else {
// 密码错误
infile.close();
return false;
}
}
}
infile.close();
return false; // 用户名不存在
}
bool regUser(string uname, string pwd) {
// 先**用户名是否已存在
int tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
if (isUserExist(uname, pwd, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6)) {
return false;
}
ofstream outfile("user_info.txt", ios::app); // 追加模式,不覆盖原有用户
if (!outfile.is_open()) {
cout << "用户文件创建失败!" << endl;
Sleep(1000);
return false;
}
outfile << uname << " " << pwd << " 0 1 0 0 0 0" << endl;
outfile.close();
return true;
}
void saveUserInfo() {
vector<vector<string> > allUsers; // 存储所有用户数据
ifstream infile("user_info.txt", ios::in);
if (!infile.is_open()) {
cout << "用户文件不存在,无法保存!" << endl;
return;
}
string u, p, g, pl, b, i, fb, d;
while (infile >> u >> p >> g >> pl >> b >> i >> fb >> d) {
if (u == username) {
// 当前登录用户,更新数据
g = to_string(jin);
pl = to_string(pickaxe_level);
b = to_string(bt);
i = to_string(zxq);
fb = to_string(mxd);
d = to_string(hj);
}
allUsers.push_back( {u, p, g, pl, b, i, fb, d} );
}
infile.close();
// 重新写入所有用户数据
ofstream outfile("user_info.txt", ios::out); // 覆盖模式
if (!outfile.is_open()) {
cout << "保存用户数据失败!" << endl;
return;
}
for (auto &user : allUsers) {
for (int i=0; i<user.size(); i++) {
if (i > 0) outfile << " ";
outfile << user[i];
}
outfile << endl;
}
outfile.close();
cout << "用户数据保存成功!" << endl;
Sleep(1000);
}
void gameMain(){
short int kw[100][60][100];
string a;
cout<<" 欢迎来到矿工模拟器"
<<"\n\n\n"
<<" ________________ \n"
<<" | 矿工模拟器 | \n"
<<" ---------------- \n"
<<"\n\n\n\n\n"
<<" 按任意键进入\n\n\n ";
char linshi1;
linshi1=fb();
int t=234234;
system("cls");
string b;
while(1){
system("cls");
cout <<"当前用户:"<<username<<" 金币:" << jin << " 镐子等级:" << pickaxe_level << endl;
cout<<"====================================="<<endl;
cout<<" 1.商城\n 2.开始挖矿\n 3.教程\n 4.退出游戏\n 5.登录\n 6.注册";
cout<<endl<<" ";
b=fb();
if(b=="1"){ //商店页面
while(1){
system("cls");
cout<<"金币:"<<jin;
cout<<"\n\n";
cout<<"1.一级镐子:30金币 2.二级镐子:150金币 3.三级镐子:700金币 4.四级镐子:2500金币\n";
cout<<"\n";
cout<<"5.五级镐子:10000金币 6.六级镐子:40000金币 7.七级镐子:100000金币 8.八级镐子:300000金币\n" ;
cout<<"\n";
cout<<"9.绷带(a):600金币 10.注血器(b):1200金币 11.满血丹(c):2800金币 12.合金钻头(d):2000\n";
cout<<"\n";
cout<<"\n需要买什么?(按0返回)\n ";
char o;
o=fb();
if(o=='0'){
system("cls");
break;
}else if(czi(o)>0&&czi(o)<=8){
int level = czi(o);
if(jg[level]<=jin){
jin-=jg[level];
pickaxe_level = level; // 更新镐子等级
sd=st[level-1];
bs=bss[level-1];
cout << "购买成功!当前镐子等级:" << level << endl;
Sleep(500);
}else{
cout<<"金币不够!\n";
Sleep(1000);
system("cls");
continue;
}
}else if(o=='a'){
if(jin>=600){
bt++;
jin-=600;
cout << "购买绷带成功!当前数量:" << bt << endl;
Sleep(500);
}else{
cout<<"金币不够!\n";
Sleep(1000);
system("cls");
continue;
}
}else if(o=='b'){
if(jin>=1200){
zxq++;
jin-=1200;
cout << "购买注血器成功!当前数量:" << zxq << endl;
Sleep(500);
}else{
cout<<"金币不够!\n";
Sleep(1000);
system("cls");
continue;
}
}else if(o=='c'){
if(jin>=2800){
mxd++;
jin-=2800;
cout << "购买满血丹成功!当前数量:" << mxd << endl;
Sleep(500);
}else{
cout<<"金币不够!\n";
Sleep(1000);
system("cls");
continue;
}
}else if(o=='d'){
if(jin>=2000){
hj++;
jin-=2000;
cout << "购买合金钻头成功!当前数量:" << hj << endl;
Sleep(500);
}else{
cout<<"金币不够!\n";
Sleep(1000);
system("cls");
continue;
}
}else{
continue;
}
system("cls");
}
}else if(b=="2"){ //挖矿页面
xl=100;
srand(static_cast<unsigned int>(time(NULL)));
system("cls");
cout<<"\n 生成地形中···\n\n 加载中 ";
// 缩小地形生成范围,避免卡顿
for(int i=1;i<=50;i++){
if(i%10==0){
cout<<"█";
}
for(int k=1;k<=50;k++){
for(int j=1;j<=20;j++){
kw[i][j][k]=rand()%1000;
}
}
}
sum=0;
int x=rand()%50;
int y=rand()%25;
int z=rand()%45;
while(xl>0){
if(xl<100){
xl+=1;
}
if(xl>100) xl=100;
if(xl<=0){
xl=0;
}
system("cls");
if(p){
wk(kw[x%50][y%20][z%50]);
kw[x%50][y%20][z%50]=-1;
}
if(p==-1){
p=0;
cout<<"\n本次探险共获得"<<sum<<"金币";
jin+=sum;
Sleep(2000);
break;
}else if(xl==0){
p=0;
cout<<"\n本次探险共获得"<<sum<<"金币";
jin+=sum;
Sleep(2500);
break;
}
cout<<"\n位置: "
<<"\n 前后:"<<x
<<"\n 高度:"<<y
<<"\n 左右:"<<z<<"\n";
cout<<" 血量: "<<xl;
cout<<"\n 要往哪个方向挖?\n\n";
cout<<" 打开背包按0\n\n ";
char ch = fb();
a = ch;
if(ch=='a'){ // 左
z++;
p=1;
system("cls");
cout<<"挖掘中";
Sleep(sd);
system("cls");
}
else if(ch=='d'){ // 右
z--;
p=1;
system("cls");
cout<<"挖掘中";
Sleep(sd);
system("cls");
}
else if(ch=='w'){ // 前
x++;
p=1;
system("cls");
cout<<"挖掘中";
Sleep(sd);
system("cls");
}
else if(ch=='s'){ // 后
x--;
p=1;
system("cls");
cout<<"挖掘中";
Sleep(sd);
system("cls");
}
else if(ch==' '){ // 上
y++;
p=1;
system("cls");
cout<<"挖掘中";
Sleep(sd);
system("cls");
}
else if(ch=='x'){ // 下
y--;
p=1;
system("cls");
cout<<"挖掘中";
Sleep(sd);
system("cls");
}else if(ch=='f'){
cout<<"f";
cin>>a;
if(a=="anhui"){
cout<<"确定返回?\n";
cin>>a;
if(a=="shi"||a=="queding"){
jin+=sum;
break;
}
}
}else if(ch=='0'){ // 打开背包
system("cls");
int l=1;
cout<<"\n\n当前有"<<sum<<"金币\n\n";
cout<<"当前物品:";
if(bt){
cout<<l<<".绷带(a):"<<bt<<"个 ";
l++;
}
if(zxq){
cout<<l<<".注血器(b):"<<zxq<<"个 ";
l++;
}
if(mxd){
cout<<l<<".满血丹(c):"<<mxd<<"个 ";
l++;
}
if(hj){
cout<<l<<".合金钻头(d)"<<hj<<"个 ";
l++;
}
cout<<"\n\n按n可退出\n ";
char bag_op = fb();
if(bag_op=='n'){
p=0;
continue;
}else if(bag_op=='a'){
if(bt){
bt--;
xl+=15;
if(xl>100) xl=100;
Sleep(200);
}else{
cout<<"\n该道具不存在!";
Sleep(1000);
p=0;
continue;
}
}else if(bag_op=='b'){
if(zxq){
zxq--;
xl+=40;
if(xl>100) xl=100;
Sleep(200);
}else{
cout<<"\n该道具不存在!";
Sleep(1000);
p=0;
continue;
}
}else if(bag_op=='c'){
if(mxd){
mxd--;
xl=100;
Sleep(200);
}else{
cout<<"\n该道具不存在!";
Sleep(1000);
p=0;
continue;
}
}else if(bag_op=='d'){
cout<<"正在蓄力…\n";
Sleep(1000);
cout<<"轰!!!!!";
Sleep(1000);
cout<<"\n";
for(int i=1;i<=10;i++){
y--;
wk(kw[x%50][y%20][z%50]);
kw[x%50][y%20][z%50]=-1;
cout<<"\n";
}
cout<<"按任意键返回\n";
string ZS;
ZS=fb();
continue;
}
}else if(a=="fanhui"){
cout<<"确定返回?\n";
cin>>a;
if(a=="shi"||a=="queding"){
jin+=sum;
break;
}
}else{
continue;
}
t=rand()%200;
}
}else if(b=="3"){ // 教程
system("cls");
cout<<"\n\n\n 上:\" \" 下:x 左:a 右:d 前:w 后:s\n\n";
cout<<" 挖矿时退出:fanhui———shi或者qeuding (拼音)\n";
cout<<"\n\n\n ——按任意键退出——\n ";
b=fb();
continue;
}else if(b=="4"){ // 退出游戏
saveUserInfo(); // 保存用户数据
cout << "感谢游玩矿工模拟器!" << endl;
Sleep(1000);
exit(0);
}else if(b=="/"){ // 作弊码
cout<<"_____________________\n";
cout<<"| \n| /";
cin>>b;
if(b=="kgzb") {
jin=100000000;
cout << "作弊成功!金币已**!" << endl;
Sleep(500);
}
else if(b=="dxts"){
cout << "地形显示中..." << endl;
Sleep(1000);
}
}else if(b=="5"){
system("cls");
string uname, pwd;
cout << "请输入用户名:";
cin >> uname;
cout << "请输入密码:";
char ch;
while ((ch = _getch()) != '\r') { // \r 是回车
if (ch == '\b') { // 退格键
if (!pwd.empty()) {
pwd.pop_back();
cout << "\b \b"; // 清除控制台显示
}
} else {
pwd.push_back(ch);
cout << "*"; // 显示星号
}
}
cout << endl;
// **用户是否存在
int gold, p_level, bandage, inject, full_blood, drill;
if (isUserExist(uname, pwd, gold, p_level, bandage, inject, full_blood, drill)) {
// 登录成功,赋值全局变量
username = uname;
jin = gold;
pickaxe_level = p_level;
bt = bandage;
zxq = inject;
mxd = full_blood;
hj = drill;
// 更新镐子属**
sd = st[pickaxe_level-1];
bs = bss[pickaxe_level-1];
cout << "登录成功!欢迎你," << uname << "!" << endl;
Sleep(1000);
system("cls");
}else{
cout << "用户名不存在或密码错误!" << endl;
Sleep(1000);
}
}else if(b=="6"){
system("cls");
string uname, pwd, pwd2;
cout << "请输入用户名(不含空格):";
cin >> uname;
cout << "请输入密码(不含空格):";
cin >> pwd;
cout << "请再次输入密码:";
cin >> pwd2;
if (pwd != pwd2) {
cout << "两次密码输入不一致!" << endl;
Sleep(1000);
}
if (regUser(uname, pwd)) {
cout << "注册成功!请返回登录界面登录。" << endl;
Sleep(1000);
} else {
cout << "用户名已存在!" << endl;
Sleep(1000);
}
}
}
}
int main(){
SetConsoleTitle(TEXT("____/|\\|/ 矿工模拟器 /|\\|/____"));
gameMain();
return 0;
}
//放到dev-c++运行

