文言文猜数字

黄锦昱  •  1个月前


文言文猜数字

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Start();
void GetResults();

int i, j, life, maxrand;
char c;

void Start() {
	i = 0;
	j = 0;
	life = 0;
	maxrand = 6;
	
	cout << "择难度一概:\n"; // the user has to select a difficutly level
	cout << "1 : 易 (0-15)\n";
	cout << "2 : 中 (0-30)\n";
	cout << "3 : 难 (0-50)\n";
	cout << "或键入更键出。\n";
	c = 30;

	cin >> c;                   // read the user's choice
	cout << "\n";

	switch (c) {
		case '1':
			maxrand = 15;  // the random number will be between 0 and maxrand
			break;
		case '2':
			maxrand = 30;
			break;
		case '3':
			maxrand = 50;
			break;
		default:
			exit(0);
		break;
	}

	life = 5;         // number of lifes of the player
	srand((unsigned)time(NULL)); // init Rand() function
	j = rand() % maxrand;  // j get a random value between 0 and maxrand
	
	GetResults();
}

void GetResults() {
	if (life <= 0) { // if player has no more life then he loses
		cout << "汝输矣!\n\n";
		Start();
	}

	cout << "键入数曰: \n";
	cin >> i;
	
	if((i>maxrand) || (i<0)) { // if the user number isn't correct, restart
		cout << "数之不在。区间 \n" << maxrand;
		GetResults();
	}

	if(i == j) {
		cout << "汝嬴!\n\n"; // the user found the secret number
		Start();
	} else if(i>j) {
		cout << "太大矣。\n";
		life = life - 1;
		cout << "余生曰: " << life << "\n\n";
		GetResults();
	} else if(i<j) {
		cout << "小矣!\n";
		life = life - 1;
		cout << "余生曰: " << life << "\n\n";
		GetResults();
	}
}

int main() {
	cout << "**大奖戏笑**\n";
	cout << "此戏中,知一数也。\n";
	cout << "会告汝数甚大。\n";
	cout << "比寻之秘数甚小。\n\n";
	Start();
	return 0;
}

评论:


A班--林奕朗  •  1个月前

6


周子隽  •  1个月前