1、文曲星猜数字游戏规则
文曲星猜数字游戏是一款经典的益智游戏,游戏的目的是让玩家猜测一个由四位数字组成的密码。以下是该游戏的规则:
-
生成数字:游戏开始时,系统会随机生成一个四位数,这个数字是由0到9中的四个不同数字组成的。
-
猜测数字:玩家需要尝试猜测这个四位数是什么。玩家每次猜测时,需要输入四个数字。
-
获取反馈:每次玩家猜测后,系统会给出反馈,通常是一个包含两个字符的字符串,如
xAyB
。其中,A
表示玩家猜测中有多少位数字不仅是正确的,而且位置也是正确的;B
表示玩家猜测中有多少位数字是正确的,但不考虑位置。 -
猜测次数限制:玩家通常有一定的次数来猜测这个数字,比如10次。如果玩家在有限的次数内没有猜中,系统会显示出正确的数字。
-
胜利条件:玩家在限定的次数内猜中了这个四位数,就算作胜利。
正片开始
2、模拟文曲星猜数字游戏
模拟文曲星上的猜数游戏,先由计算机随机生成一个各位相异的4位数字,由用户来猜,根据用户猜测的结果给出提示:xAyB
。其中,A
前面的数字(即x
)表示有几位数字不仅数字猜对了,而且位置也正确,B
前面的数字(即y
)表示有几位数字猜对了,但是位置不正确。允许用户猜的最多次数由用户从键盘输入。如果猜对,则提示Congratulation!
;如果在规定的次数以内仍然猜不对,则给出提示Sorry,you haven't guess the right number!
。程序结束之前,在屏幕上显示这个正确的数字。
思路:
先定义三个数组,第一个数组用来存放正确答案(即计算机随机生成的四个互不相同的数字),第二个数组用存放用户猜测的4个不相同的数字,第三个数组用来存放x
和y
。然后定义一个函数Answer用来生成4个互不相同的数字,接着定义一个函数Guess给用户输入4个不相同的数字,最后定义一个函数Judge用来判断用户猜对的情况。
代码实现:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Answer(int answer[])
{
do
{
answer[0] = rand() % 10;
answer[1] = rand() % 10;
answer[2] = rand() % 10;
answer[3] = rand() % 10;
} while (answer[0]==answer[1]||answer[0]==answer[2]||answer[0]==answer[3]
||answer[1]==answer[2]||answer[3]||answer[2]==answer[3]);
}
void Guess(int guess[])
{
printf("请输入4个不相同的数字:");
do
{
int i = 0;
for (i = 0; i < 4; i++)
{
scanf("%d", &guess[i]);
}
} while (guess[0] == guess[1] || guess[0] == guess[2] || guess[0] == guess[3]
|| guess[1] == guess[2] || guess[3] || guess[2] == guess[3]);
}
void Jugde(int answer[], int guess[],int correct[])
{
int i = 0;
for ( i=0; i < 4; i++)
{
int j = 0;
for (j = 0; j < 4; j++)
{
if (answer[i] == guess[j] && i == j)
{
correct[0]++;
}
else if (answer[i] == guess[j]&&i!=j)
{
correct[1]++;
}
}
}
}
int main()
{
int answer[4] = { 0 };//存储正确答案的数组
int guess[4] = { 0 };//存储游戏玩家猜测的数组
int correct[2] = { 0 };//用来存放xAyB中的A和B
int max_count = 0;//允许猜测的最大的次数
srand((unsigned int)time(NULL));//初始化随机数的种子
Answer(answer);//产生相异的4位数字的函数
printf("----文曲星猜数游戏----\n");
printf("请输入你允许猜测的最大次数:");
scanf("%d", &max_count);
for (int i = 1; i <= max_count; i++)
{
correct[0] = 0;
correct[1] = 0;
Guess(guess);//游戏玩家猜测的函数
Jugde(answer,guess,correct);//判断游戏玩家猜测正确
if (correct[0] == 4)
{
printf("Congratulations!\n");
break;
}
else if (correct[0] != 4)
{
printf("%dA%dB\n", correct[0], correct[1]);
}
if (i == max_count)
{
printf("Sorry,you haven't guess the right number!\n");
printf("This number is:");
int j = 0;
for (j = 0; j < 4; j++)
{
printf("%d ", answer[j]);
}
}
}
return 0;
}
3、代码块分析
我们来逐个代码块分析:
第一块:
void Answer(int answer[])
{
do
{
answer[0] = rand() % 10;
answer[1] = rand() % 10;
answer[2] = rand() % 10;
answer[3] = rand() % 10;
} while (answer[0]==answer[1]||answer[0]==answer[2]||answer[0]==answer[3]
||answer[1]==answer[2]||answer[3]||answer[2]==answer[3]);
}
这个函数用来产生4个互不相同的数字,while括号里的条件就是用来确保产生的是4个不相同的数字。
第二块:
void Guess(int guess[])
{
printf("请输入4个不相同的数字:");
do
{
int i = 0;
for (i = 0; i < 4; i++)
{
scanf("%d", &guess[i]);
}
} while (guess[0] == guess[1] || guess[0] == guess[2] || guess[0] == guess[3]
|| guess[1] == guess[2] || guess[3] || guess[2] == guess[3]);
}
这个函数用来给用户输入4个互不相同的数字。
第三块:
void Jugde(int answer[], int guess[],int correct[])
{
int i = 0;
for ( i=0; i < 4; i++)
{
int j = 0;
for (j = 0; j < 4; j++)
{
if (answer[i] == guess[j] && i == j)
{
correct[0]++;
}
else if (answer[i] == guess[j]&&i!=j)
{
correct[1]++;
}
}
}
}
这个函数用来判断用户猜测的数字与计算机随机产生的数字位置即数字比较,并将值赋给x
和y
。它的三个参数是整型数组。
第四块:
int main()
{
int answer[4] = { 0 };//存储正确答案的数组
int guess[4] = { 0 };//存储游戏玩家猜测的数组
int correct[2] = { 0 };//用来存放xAyB中的A和B
int max_count = 0;//允许猜测的最大的次数
srand((unsigned int)time(NULL));//初始化随机数的种子
Answer(answer);//产生相异的4位数字的函数
printf("----文曲星猜数游戏----\n");
printf("请输入你允许猜测的最大次数:");
scanf("%d", &max_count);
for (int i = 1; i <= max_count; i++)
{
correct[0] = 0;
correct[1] = 0;
Guess(guess);//游戏玩家猜测的函数
Jugde(answer,guess,correct);//判断游戏玩家猜测正确
if (correct[0] == 4)
{
printf("Congratulations!\n");
break;
}
else if (correct[0] != 4)
{
printf("%dA%dB\n", correct[0], correct[1]);
}
if (i == max_count)
{
printf("Sorry,you haven't guess the right number!\n");
printf("This number is:");
int j = 0;
for (j = 0; j < 4; j++)
{
printf("%d ", answer[j]);
}
}
}
return 0;
}
反思:
对于复杂的程序实现,我们可以将整个程序分成独立一小块(即一个函数),然后逐步趋实现。目的是让我们思路更加清晰,写代码时不那么烦躁。就像我这个程序,我将整个程序分成四小块,第一块(Answer函数)实现随机生成4个不相同的数字;第二块(Guess函数)实现用户输入4个不相同数字;第三块(Judge函数)实现猜的情况,第四块实现整合,完成整个程序。
那么写到这里,本节内容就结束了,这篇博客花费了很长时间,但写完有满满的成就感,希望能帮助到大家,如果文章有不足的地方,欢迎在评论区留言指正,我们一起学习交流!
希望能得到大家的关注、点赞、评论、收藏! 你的支持是我最大的动力!!