跳到正文

个人记录

小游戏 · 11 蒟蒻的24点游戏

xutongwei约 13 分钟
原文
文章信息与版本

文章信息

发布
2019年10月6日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 13 分钟

版本

  1. v1当前
C++
#include <iostream> 
#include <string>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
int n;
double a[30];
string fu[10] = {"0", " + ", " - ", " * ", " / "};

void print(string s,int b){
	for(int i = 0; i < s.size(); i++){
		cout << s[i];
		Sleep(b);
	}
	return;
}

void output(int r){
	for(int i = 1; i <= r; i++){
		cout << a[i] << "   ";
	}
	cout << endl;
}

bool panduan(double x){
	for(int i = 1; i <= n; i++){
		if(a[i] == x){
			return true;
		}
	}
	return false;
}

double suanhe(double x, int xy, double y){
	if(xy == 1){
		return x + y;
	}else if(xy == 2){
		return x - y;
	}else if(xy == 3){
		return x * y;
	}else if(xy == 4){
		return x / y;
	}else{
		cout << "您的输入有误哦" << endl;
		return -1;
	}
}

void mem(int r){
	system("cls");
	p1:Sleep(500);
	cout << "请选择模式" << endl;
	Sleep(500);
	cout << "1.扑克牌模式(1~13)" << endl;
	Sleep(500);
	cout << "2.超大数模式(1~100)" << endl;
	Sleep(500);
	cout << "3.疯狂模式(随机小数)" << endl;
	Sleep(500);
	cout << "请输入:";
	int nm3;
	cin >> nm3;
	for(int i = 1; i <= r; i++){
		srand(time(NULL));
		if(nm3 == 1){
			a[i] = (rand() % 13) + 1;
		}else if(nm3 == 2){
			a[i] = (rand() % 100) + 1;
		}else if(nm3 == 3){
			double nm4 = rand() % 10000;
			a[i] = nm4 / 100.0;
			system("cls");
			print("生成数据中,请稍后",50);
			print("▁▂▃▅▆▇",100);
			system("cls");
		}else{
			Sleep(500);
			cout << "您的输入有误哦" << endl;
			Sleep(500) ;
			system("cls");
			goto p1;
		}
		system("cls");
		print("生成数据中,请稍后",50);
		print("▁▂▃▅▆▇",100);
		system("cls");
		/*
		Sleep(100);
		cout << "生" << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成" << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成数" << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成数据" << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成数据中" << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成数据中," << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成数据中,请" << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成数据中,请稍" << endl;
		Sleep(25);
		system("cls");
		Sleep(25);
		cout << "生成数据中,请稍后" << endl;
		Sleep(25);
		system("cls");
		Sleep(50);
		cout << "生成数据中,请稍后▁" << endl;
		Sleep(50);
		system("cls");Sleep(50);
		cout << "生成数据中,请稍后▁▂" << endl;
		Sleep(50);
		system("cls");Sleep(50);
		cout << "生成数据中,请稍后▁▂▃" << endl;
		Sleep(50);
		system("cls");Sleep(50);
		cout << "生成数据中,请稍后▁▂▃▅" << endl;
		Sleep(50);
		system("cls");Sleep(50);
		cout << "生成数据中,请稍后▁▂▃▅▆" << endl;
		Sleep(50);
		system("cls");Sleep(50);
		cout << "生成数据中,请稍后▁▂▃▅▆▇" << endl;
		Sleep(50);
		system("cls");
		*/
	}
}

void play(int r){
	mem(r);
	while(true){
		system("cls");
		output(r);
		if(r == 1){
			if(a[1] == 24){
				Sleep(500);
				cout << "恭喜您,挑战成功!" << endl;
				Sleep(3000);
			}else{
				Sleep(500);
				cout << "挑战失败……" << endl;
				Sleep(3000);
			}
			break;
		}
		double b1 = 0;
		double b2 = 0;
		double bb1 = 0.1;
		double bb2 = 0.1;
		bool h1 = false;
		bool h2 = false;
		bool h3 = false;
		bool bh1 = false;
		bool bh2 = false;
		int c1 = 0;
		while(true){
			char nm5;
			nm5 = getch();
			if(nm5 == '+'){
				if(h1 && !h2){
					cout << "+";
					c1 = 1;
				}else{
					continue;
				}
			}else if(nm5 == '-'){
				if(h1 && !h2){
					cout << "-";
					c1 = 2;
				}else{
					continue;
				}
			}else if(nm5 == '*'){
				if(h1 && !h2){
					cout << "*";
					c1 = 3;
				}else{
					continue;
				}
			}else if(nm5 == '/'){
				if(h1 && !h2){
					cout << "/";
					c1 = 4;
				}else{
					continue;
				}
			}else if(nm5 == 'C'){
				system("cls");
				output(r);
				b1 = 0.0;
				b2 = 0.0;
				bb1 = bb2 = 0.1;
				bh1 = bh2 = h1 = h2 = false;
				c1 = 0;
			}else if(nm5 == 'c'){
				if(h1){
					if(h2){
						system("cls");
						output(r);
						cout << b1;
						cout << fu[c1];
						b2 = 0;
						bb2 = 0.1;
						bh2 = false;
					}else{
						system("cls");
						output(r);
						cout << b1;
						c1 = 0;
					}
				}else{
					system("cls");
					output(r);
					b1 = 0;
					bb1 = 0.1;
					bh1 = false;
				}
			}else if(nm5 == ' '){
				cout << " ";
				if(h1){
					if(h2){
						if(panduan(b2)){
							a[r + 1] = suanhe(b1, c1, b2);
							int d = 0;
							for(int i = 1; i <= r + 1; i++){
								if(a[i] == b1){
									d++;
								}else if(a[i] == b2){
									d++;
								}else{
									a[i - d] = a[i];
								}
							}
							a[r + 1] = a[r] = 0;
							r--;
							break;
						}else{
							cout << "您的输入有误哦" << endl;
							Sleep(500);
							system("cls");
							output(r);
							cout << b1;
							cout << fu[c1];
							b2 = 0;
							bb2 = 0.1;
							bh2 = false;
						}
					}else{
						if(c1){
							h2 = true;
						}
					}
				}else{
					if(panduan(b1)){
						h1 = true;
					}else{
						cout << "您的输入有误哦" << endl;
						Sleep(500);
						system("cls");
						output(r);
						b1 = 0;
						bb1 = 0.1;
						bh1 = false;
					}
				}
			}else if(nm5 == '.'){
				if(h1){
					if(h2){
						if(bh2){
							continue;
						}else{
							bh2 = true;
							cout << ".";
						}
					}else{
						continue;
					}
				}else{
					if(bh1){
						continue;
					}else{
						bh1 = true;
						cout << ".";
					}
				}
			}else if(nm5 <= '9' && nm5 >= '0'){
				int nm6 = nm5 - '0';
				if(h1){
					if(h2){
						cout << nm5;
						if(bh2){
							b2 = b2 + bb2 * nm6;
							bb2 /= 10;
						}else{
							b2 = b2 * 10 + nm6;
						}
					}else{
						continue;
					}
				}else{
					cout << nm5;
					if(bh1){
						b1 = b1 + bb1 * nm6;
						bb1 /= 10;
					}else{
						b1 = b1 * 10 + nm6;
					}
				}
			}else{
				cout << "您的输入有误哦" << endl;
				Sleep(500);
				if(h1){
					if(h2){
						system("cls");
						output(r);
						cout << b1;
						cout << fu[c1];
					}else{
						system("cls");
						output(r);
						cout << b1;
					}
				}else{
					system("cls");
					output(r);
				}
			}
		}
	}
}

void input(){
	while(true){
		system("cls");
		Sleep(500);
		cout << "请输入您要的数字个数(3~9)" << endl;
		string nm1;
		cin >> nm1;
		if(nm1.size() == 1 && nm1[0] >= '3' && nm1[0] <= '9'){
			n = nm1[0] - '0';
			play(n);
			system("cls");
			Sleep(500);
			cout << "您还想再来一局吗?(Y or N)" << endl;
			char nm2;
			nm2 = getch();
			if(nm2 == 'Y' || nm2 == 'y' || nm2 == '1'){
				system("cls");
			}else if(nm2 == 'N' || nm2 == 'n' || nm2 == '0'){
				Sleep(500);
				cout << "游戏结束!" << endl;
				break;
			}
		}else{
			Sleep(500);
			cout << "您的输入有误哦" << endl;
			Sleep(500);
		}
		
	}
	return;
}

int main( ){
	system("mode con cols=50 lines=20");
	Sleep(1000);
	cout << "欢迎来到24点游戏!" << endl;
	Sleep(1000);
	cout << "游戏开始……" << endl;
	Sleep(1000);
	system("pause");
	input();
	system("pause") ;
	return 0;
}
原文
存档评论2 条抓取于