C语言简单猜数字游戏

代码:

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

void Start();
void GetResults();

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


int main() {
	system("title Jackpot Game");
	
	while (1) {
		printf("** Jackpot game **\n");
		printf("The goal of this game is to guess a number.\n");
		printf("Jackpot will tell you if the number is too big or too\n");
		printf("small compared to the secret number to find.\n\n");
	
		Start();
	}
	return 0;
}

void Start() {
	i = 0;
	j = 0;
	life = 0;
	maxrand = 6;

	printf("Select difficulty mode:\n"); // the user has to select a difficutly level
	printf("1 : Easy (0-15)\n");
	printf("2 : Medium (0-30)\n");
	printf("3 : Difficult (0-50)\n");

choose:
	scanf("%c", &c);                   // read the user's choice
	printf("\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:
			goto choose;
			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
		printf("You lose !\n\n");
		Start();
	}

	printf("Type a number: \n");
	scanf("%d", &i);

	if((i > maxrand) || (i < 0)) { // if the user number isn't correct, restart
		printf("Error: number not between 0 and %d\n", maxrand);
		GetResults();
	}

	if(i == j) {
		printf("YOU WIN!\n\n"); // the user found the secret number
		Start();
	} else if(i > j) {
		printf("Too BIG\n");
		life = life - 1;
		printf("Lives remaining: %d\n\n", life);
		GetResults();
	} else if(i < j) {
		printf("Too SMALL\n");
		life = life - 1;
		printf("Lives remaining: %d\n\n", life);
		GetResults();
	}
}

运行结果:

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值