一个问答小游戏

本文介绍了一个使用C语言编写的问答小游戏,共有15道题目,每个题目有四个选项和一个正确答案。游戏设有求助热线功能,包括50/50、call a friend和ask the audience。每道题目有不同的奖励策略,答错会根据题目位置有不同的惩罚。这是一个适合初学者的C语言实践项目,涵盖了函数和循环等基础知识。
摘要由CSDN通过智能技术生成

题目设置

总共15道题,每个题四个选项,只有一个正确答案。

求助热线

三个求助选项,50/50:去掉两个错误答案,call a friend:随机得到一个答案,ask the audience:得到观众关于四个选项的投票概率。

奖励与惩罚

每个题目都有相应的奖励金额,答对一题奖金池都会增加,在1-5题答错,则失去所有奖励,6-10题答错,则可以带走5000,11-15题答错则可以带走50000.

具体代码

// An highlighted block

#include "pch.h"
#include <iostream>
#include<time.h>
#include <windows.h>
#include <stdlib.h>


void gotoxy(int x, int y)  
{
   
	COORD coord;
	coord.X = y;
	coord.Y = x;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
/*void color(short x)
{
	if (x >= 0 && x <= 15)
	{
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	}
	else
	{
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	}
}*/


struct game
{
   
	int questionNumber;
	int gameReward;
	char questiontitle[150];
	char choiceA[50];
	char choiceB[50];
	char choiceC[50];
	char choiceD[50];
	char correctanswer;



};

void reward(int money)
{
   
	gotoxy(0, 50);
	printf("\t\t\t\t\tReward:$%d\n", money);

}
void lifelinemessage(int number50,int numberfriend,int numberauidence)
{
   
	gotoxy(1, 50);
	printf("\t\t\t\t\t**************************\t\t");
	gotoxy(2, 50);
	printf("\t\t\t\t\t[A]ChoiceA");
	gotoxy(3, 50);
	printf("\t\t\t\t\t[B]ChoiceB");
	gotoxy(4, 50);
	printf("\t\t\t\t\t[C]ChoiceC");
	gotoxy(5, 50);
	printf("\t\t\t\t\t[D]ChoiceD");
	gotoxy(6, 50);
	printf("\t\t\t\t\t[1]50/50 lifeline: %d",number50);
	gotoxy(7, 50);
	printf("\t\t\t\t\t[2]Call a Friend lifeline: %d",numberfriend);
	gotoxy(8, 50);
	printf("\t\t\t\t\t[3]Ask the Audience: %d",numberauidence);
	gotoxy(9, 50);
	printf("\t\t\t\t\t[4]Go home");
	gotoxy(10, 50);
	printf("\t\t\t\t\t**************************\t\t");


}
char letter(char word)
{
   
	if (word >= 97 && word <= 122)
	{
   
		return word - 32;
	}
	else 
	{
   
		return word;
	}
}

char wronganswer(char input)
{
   
	
		if (input != '1'&& input != '2'&& input != '3'&& input != '4')
		{
   
			printf("Wrong Input!\n");
			printf("Enter the lifeline you want to choose:  ");
			scanf_s(" %c", &input);
			input = letter(input);
			return wronganswer(input);
		
	    }
	else
	{
   

		return input;
	}

}

char wronganswer2(char input)
{
   
	if (input < 65 || input>68)
	{
   
		printf("Wrong Input!\n");
		printf("Enter your answer:  ");
		scanf_s(" %c", &input);
		input = letter(input);
		return wronganswer2(input);
	}
	else
	{
   

		return input;
	}

}


bool wronganswer3(char input)
{
   
	
	if (input != 89 && input != 78)
	{
   
		printf("Wrong Input!\n");
		printf("Are you ready for the game?(y/n) ");
		scanf_s(" %c", &input);
		input = letter(input);
		return wronganswer3(input);

	}
	else if(input=='N')
	{
   
		return false;
	}

}

char wronganswer4(char input)
{
   

	if (input != 89 && input != 78)
	{
   
		printf("Wrong Input!\n");
		printf("Do you want to use lifeline?(y/n) ");
		scanf_s(" %c", &input);
		input = letter(input);
		return wronganswer4(input);

	}
	else if (input == 'N')
	{
   
		return false;
	}

}

void lifeline50(char correct)
{
   
	char respond[4] = {
    'A','B','C','D' };
	char wronganswer1, wronganswer2;
	int n = 0;
	int x, y = 0;
	int length = strlen(respond);
	for (int i = 0; i < 4; i++)
	{
   
		if (respond[i] == correct)
		{
   
			n = i;
		}
	}
	for (int j = n; j < 4; j++)
	{
   
		respond[j] = respond[j + 1];
	}
	
	    x = rand() % 2;
		wronganswer1 = respond[x];
		wronganswer2 = respond[x+1];
	
		if (wronganswer1 != wronganswer2)
		{
   
			wronganswer2= wronganswer2;

		}
		else
		{
   
			wronganswer2= respond[rand() % 3];
		}
		//color(4);
		printf("The two wrong choices are %c and %c\n", wronganswer1, wronganswer2);
}//50/50

void callfriend()
{
   
	char respond[4] = {
    'A','B','C','D' };
	printf("The correct answer the friend gives is %c\n", respond[rand() % 4]);

}//Call a friend

void askaudience()
{
   
	float a, b, c, d = 0;
	float summary = 0;
	a = rand() % 100;
	b = rand() % 100;
	c = rand() % 100;
	d = rand() % 100;
	summary = a + b + c + d;
	printf("The percentage of choice A is %.2f %%\n", 100 * a / summary);
	printf("The percentage of choice B is %.2f %%\n", 100*b / summary );
	printf("The percentage of choice C is %.2f %%\n", 100*c / summary );
	printf("The percentage of choice D is %.2f %%\n", 100*d / summary);

}



int main()
{
   
	char input = 'a';
	char answer;
	int money = 0;
	bool answer1 = true;
	printf("\t\t\tWho Wants to Be a Millionaire\t\t\t\n");
	printf("\t\t********************************************\t\t\t\n");
	printf("Are you ready for the game?(y/n)");
	scanf_s("%c", &input);
	input=letter(input);
	answer1=wronganswer3(input);
	if (answer1 == false)
	{
   
		return 0;
	}
	system("cls");
	
	/*char lifeline50[30] = " ";
	char lifelinecallfriend[30] = " ";
	char lifelineaskauidence[30] =" ";*/
	srand(time(NULL));
	/*
	//lifeline50(correct);
	//callfriend();
	askaudience();
	system("PAUSE");
	system("cls");
	//system("PAUSE");*/


//The questions and answers
	game question1;
	question1.questionNumber = 1;
	question1.gameReward = 500;
	strcpy_s(question1.questiontitle, "Which company is The Legend of Zelda: Breath of the Wild from?");
	strcpy_s(question1.choiceA, "Nintendo");
	strcpy_s(question1.choiceB, "Capcom");
	strcpy_s(question1.choiceC, "Riot");
	strcpy_s(question1.choiceD, "Valve");
	question1.correctanswer='A';

	game question2;
	question2.questionNumber = 2;
	question2.gameReward = 1000;
	strcpy_s(question2.questiontitle, "What kind of game is DOTA2?");
	strcpy_s(question2.choiceA, "MMORPG");
	strcpy_s(question2.choiceB, "MOBA");
	strcpy_s(question2.choiceC, "ARPG");
	strcpy_s(question2.choiceD, "JRPG");
	question2.correctanswer='B';

	game question3;
	question3.questionNumber = 3;
	question3.gameReward = 2000;
	strcpy_s(question3.questiontitle, "How many roles does League of Legends have?");
	strcpy_s(question3.choiceA, "144");
	strcpy_s(question3.choiceB, "142");
	strcpy_s(question3.choiceC, "140");
	strcpy_s(question3.choiceD, "143");
	question3.correctanswer='A';

	game question4;
	question4.questionNumber = 4;
	question4.gameReward = 3000;
	strcpy_s(question4.questiontitle, "How many years has World of Warcraft been running?");
	strcpy_s(question4.choiceA, "13");
	strcpy_s(question4.choiceB, "18");
	strcpy_s(question4.choiceC, "15");
	strcpy_s(question4.choiceD, "14");
	question4.correctanswer='C';

	game question5;
	question5.questionNumber = 5;
	question5.gameReward = 5000;
	strcpy_s(question5.questiontitle, "Which game has the first prize in the world?");
	strcpy_s(question5.choiceA, "DOTA2");
	strcpy_s(question5.choiceB, "LEAGUE OF LEGENDS");
	strcpy_s(question5.choiceC, "CS.GO");
	strcpy_s(question5.choiceD, "PUBG");
	question5.correctanswer='A';

	game question6;
	question6.questionNumber = 6;
	question6.gameReward = 7500;
	strcpy_s(question6.questiontitle, "What kind of game is final Fantasy 14?");
	strcpy_s(question6.choiceA, "MOBA");
	strcpy_s(question6.choiceB, "MMORPG");
	strcpy_s(question6.choiceC, "JRPG");
	strcpy_s(question6.choiceD, "RTS");
	question6.correctanswer='B';

	game question7;
	question7.questionNumber = 7;
	question7.gameReward = 10000;
	strcpy_s(question7.questiontitle, "When did the PORTAL come out of the game?");
	strcpy_s(question7.choiceA, "2005");
	strcpy_s(question7.choiceB, "2077");
	strcpy_s(question7.choiceC, "2007");
	strcpy_s(question7.choiceD, "2009");
	question7.correctanswer='C';

	game question8;
	question8.questionNumber = 8;
	question8.gameReward = 15000;
	strcpy_s(question8.questiontitle, "Which game is the first to have an official e-sports competition?");
	strcpy_s(question8.choiceA, "STARCRAFT");
	strcpy_s(question8.choiceB, "LEAGUE OF LEGENDS");
	strcpy_s(question8.choiceC, "DOTA2");
	strcpy_s(question8.choiceD, "CS 1.6");
	question8.correctanswer='A';

	game question9;
	question9.questionNumber = 9;
	question9.gameReward = 25000;
	strcpy_s(question9.questiontitle<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的环保问答小游戏的Python代码: ```python # 导入random模块,用于随机选择问题 import random # 定义问题及答案 questions = { "1. 什么是碳足迹?": "碳足迹是指一个人、一个组织或一个活动所产生的二氧化碳排放量。", "2. 开车还是骑自行车对环境更友好?": "骑自行车对环境更友好。", "3. 怎样节约水资源?": "可以减少洗澡时间,使用节水淋浴头,修理漏水管道等。", "4. 什么是垃圾分类?": "垃圾分类是按照不同的分类标准将垃圾分为可回收物、有害垃圾、厨余垃圾和其他垃圾。", "5. 什么是绿色出行?": "绿色出行是指使用环保交通工具、共享交通工具、步行或骑行等环保出行方式。" } # 定义游戏函数 def game(): print("欢迎来到环保问答小游戏!") while True: # 随机选择一个问题 question = random.choice(list(questions.keys())) print(question) # 等待用户输入答案 answer = input("请输入答案:") # 判断答案是否正确 if answer == questions[question]: print("回答正确!") else: print("回答错误!") # 启动游戏 game() ``` 运行代码后,程序会随机选择一个问题并输出,等待用户输入答案。如果用户输入的答案正确,程序会输出“回答正确!”;否则,程序会输出“回答错误!”。然后,程序会再次随机选择一个问题并等待用户输入答案,如此循环,直到用户选择退出游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值