1

· · 休闲·娱乐

#include <bits/stdc++.h>
using namespace std;
#define flush cout.flush()
#define Made return
#define By 0
#define Kryagon ;
#define ll long long
const ll PRICE = 3000;
vector<ll> fread(const string & e){
    ifstream file("data.dat", ios::binary);
    if(!file){
        cout << e << endl;
        return {};
    }
    ll x, y;
    bool isu;
    if(!file.read(reinterpret_cast<char*>(&x), sizeof(ll)) || !file.read(reinterpret_cast<char*>(&y), sizeof(ll)) || !file.read(reinterpret_cast<char*>(&isu), sizeof(bool))){
        cout << "游戏文件损坏!" << endl;
        file.close();
        return {};
    }
    file.close();
    return {x, y, (ll)isu};
}
bool fwrite(ll x, ll y, bool isu, const string & e = "游戏发生了错误,无法继续游戏且异常结束。"){
    ofstream file("data.dat", ios::binary);
    if(!file){
        cout << e << endl;
        return false;
    }
    if(!file.write(reinterpret_cast<const char*>(&x), sizeof(ll)) || !file.write(reinterpret_cast<const char*>(&y), sizeof(ll)) || !file.write(reinterpret_cast<const char*>(&isu), sizeof(bool))){
        cout << e << endl;
        file.close();
        return false;
    }
    file.close();
    return true;
}
int isfemp(){
    ifstream file("data.dat", ios::binary | ios::ate);
    if(!file){
        cout << "初始化失败!" << endl;
        return 2;
    }
    ll size = file.tellg();
    file.close();
    return size == 0;
}
int grn(int min, int max){
    static mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
    uniform_int_distribution<>dis(min, max);
    return dis(gen);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << "规则详见README.txt" << endl;
    int status = isfemp();
    if(status == 2)return 1;
    if(status)if(!fwrite(1000, 0, false, "初始化失败!"))return 1;
    string todo;
    auto bf = fread("初始化失败!");
    if(bf == vector<ll>())return 1;
    ll x = bf[0], y = bf[1];
    bool isu = (bool)bf[2];
    while(true){
        if(x <= 0 && !y){
            if(!fwrite(1000, 0, false, "点数小于或等于0且没有道具,游戏无法继续,但无法重置存档且异常结束."))return 1;
            cout << "点数小于或等于0且没有道具,游戏无法继续,已自动重置存档且结束。" << endl;
            break;
        }
        cout << "请输入你要押多少点数:";
        flush;
        getline(cin, todo);
        if(todo == ".\\end"){
            if(!fwrite(x, y, isu, "游戏无法正常结束。"))return 1;
            cout << "游戏结束,再见!" << endl;
            break;
        }
        else if(todo == ".\\buy"){
            if(x < PRICE)cout << "余额不足,无法购买道具!" << endl;
            else{
                x -= PRICE;
                y++;
                if(!fwrite(x, y, isu, "游戏发生了错误,无法购买且异常结束。"))return 1;
                cout << "购买成功!" << endl;
            }
            continue;
        }
        else if(todo == ".\\sell"){
            if(!y)cout << "没有道具可出售!" << endl;
            else{
                x += PRICE;
                y--;
                if(!fwrite(x, y, isu, "游戏发生了错误,无法出售且异常结束。"))return 1;
                cout << "道具出售成功!" << endl;
            }
            continue;
        }
        else if(todo == ".\\use"){
            if(isu)cout << "不能一次使用两个道具!" << endl;
            else if(!y)cout << "没有道具可以使用!" << endl;
            else{
                y--;
                isu = true;
                if(!fwrite(x, y, isu, "游戏发生了错误,无法使用且异常结束。"))return 1;
                cout << "道具使用成功!" << endl;
            }
            continue;
        }
        else if(todo == ".\\reset"){
            while(true){
                cout << "确定要重置存档吗?(Y/N):";
                flush;
                string r;
                getline(cin, r);
                if(r == "Y"){
                    x = 1000;
                    y = 0;
                    isu = false;
                    if(!fwrite(x, y, false, "游戏发生了错误,无法重置且异常结束。"))return 1;
                    cout << "存档已重置!" << endl;
                    break;
                }
                else if(r == "N")break;
                else cout << "输入错误,请重新输入!" << endl;
            }
        }
        else if(todo == ".\\clear"){
            system("cls");
            continue;
        }
        else if(todo == ".\\query"){
            cout << "当前拥有:" << x << "点数," << y << "个道具," << (isu ? "正在使用道具。" : "未使用道具。") << endl;
            continue;
        }
        else{
            try{
                ll h = stoll(todo);
                if(h <= 0){
                    cout << "点数必须为正!" << endl;
                    continue;
                }
                else if(h > x){
                    cout << "没有足够的点数!" << endl;
                    continue;
                }
                int a = grn(1, 6), b = grn(1, 6), c = grn(1, 6);
                if(isu){
                    cout << "三个骰子其中一个结果为:" << a << endl;
                    isu = false;
                }
                string cho;
                while(true){
                    cout << "请输入你要押那个选项(大/小/单/双/豹子):";
                    flush;
                    getline(cin, cho);
                    if(cho != "大" && cho != "小" && cho != "单" && cho != "双" && cho != "豹子"){
                        cout << "输入错误,重新输入!" << endl;
                        continue;
                    }
                    break;
                }
                int s = a + b + c;
                bool win = false;
                ll mult = 1;
                if(cho == "大")win = (s > 10);
                else if(cho == "小")win = (s <= 10);
                else if(cho == "单")win = (s % 2 == 1);
                else if(cho == "双")win = (s % 2 == 0);
                else{
                    win = (a == b && b == c);
                    mult = 10;
                }
                if(win){
                    x += h * mult;
                    if(!fwrite(x, y, isu))return 1;
                    cout << "骰子结果为:" << a << ' ' << b << ' ' << c << endl;
                    cout << "恭喜你,猜对啦!获得了" << h * mult << "点数" << endl;
                }
                else{
                    x -= h;
                    if(!fwrite(x, y, isu))return 1;
                    cout << "骰子结果为:" << a << ' ' << b << ' ' << c << endl;
                    cout << "很遗憾,猜错了!损失了" << h << "点数" << endl;
                }
            }
            catch(...){
                cout << "输入错误,请重新输入!" << endl;
            }
        }
    }
    Made By Kryagon
}

需在 .cpp 文件同级路径创建一个名为 data.dat 的文件注意后缀为 .dat。

README.txt 内容:

游戏规则:
游戏开局会得到1000点数,无道具。玩家可以通过游戏获取更多点数,规则如下:
每局会掷三个骰子,玩家可以押一个点数,再猜它的属性。猜对了就可以赢得对应数量的点数,猜错了就会输掉对应数量的点数。
三个骰子的属性分以下几种:
1.大:和大于10。
2.小:和小于或等于10。
3.单:和为单数。
4.双:和为双数。
5.豹子:三个骰子结果相同,遇到该情况赢得的点数数量翻10倍,输掉的点数数量不变。
攒够3000点数可以购买一个道具,使用可以知道其中一个骰子的结果。道具也可以出售,出售后将减少一个道具,获得3000点数。
游戏补充:
在押点数的交互时可以输入以下内容实现各种功能:
1.输入.\end可以结束游戏。
2.输入.\buy和.\sell可以购买或出售道具。
3.输入.\use可以使用道具。
4.输入.\reset可以重置存档。
5.输入.\clear可以清屏。
6.输入.\query可以查询自己的点数和道具数量。
游戏发生错误的可能:
游戏都是依赖文件夹中的data.dat运行的,此文件丢失或损坏以及权限和内存等问题都有可能导致游戏运行不了。
注意:.\clear仅适用于Windows。