117.info
人生若只如初见

使用C语言实现珠玑妙算Mastermind小游戏

以下是使用C语言实现珠玑妙算小游戏的代码:

#include 
#include 
#include 
#define CODE_LENGTH 4
#define MAX_ATTEMPTS 10
void generateSecretCode(int* secretCode) {
srand(time(NULL));
for(int i = 0; i < CODE_LENGTH; i++) {
secretCode[i] = rand() % 6 + 1; // Generate a random number between 1 and 6
}
}
int* getUserGuess() {
static int userGuess[CODE_LENGTH];
printf("Enter your guess (4 digits between 1 and 6): ");
for(int i = 0; i < CODE_LENGTH; i++) {
scanf("%d", &userGuess[i]);
}
return userGuess;
}
void checkGuess(int* secretCode, int* userGuess, int* numCorrectPosition, int* numCorrectDigit) {
*numCorrectPosition = 0;
*numCorrectDigit = 0;
int secretCodeCopy[CODE_LENGTH];
int userGuessCopy[CODE_LENGTH];
for(int i = 0; i < CODE_LENGTH; i++) {
secretCodeCopy[i] = secretCode[i];
userGuessCopy[i] = userGuess[i];
}
// Check for correct digits in correct position
for(int i = 0; i < CODE_LENGTH; i++) {
if(userGuess[i] == secretCode[i]) {
(*numCorrectPosition)++;
secretCodeCopy[i] = -1;
userGuessCopy[i] = -1;
}
}
// Check for correct digits in wrong position
for(int i = 0; i < CODE_LENGTH; i++) {
for(int j = 0; j < CODE_LENGTH; j++) {
if(userGuessCopy[i] == secretCodeCopy[j] && userGuessCopy[i] != -1) {
(*numCorrectDigit)++;
secretCodeCopy[j] = -1;
userGuessCopy[i] = -1;
break;
}
}
}
}
int main() {
int secretCode[CODE_LENGTH];
int numAttempts = 0;
generateSecretCode(secretCode);
printf("*** Welcome to Mastermind! ***\n");
printf("Guess the 4-digit code. Each digit is between 1 and 6.\n");
while(numAttempts < MAX_ATTEMPTS) {
int numCorrectPosition, numCorrectDigit;
int* userGuess = getUserGuess();
checkGuess(secretCode, userGuess, &numCorrectPosition, &numCorrectDigit);
if(numCorrectPosition == CODE_LENGTH) {
printf("Congratulations! You guessed the code correctly.\n");
return 0;
} else {
printf("Incorrect. Number of correct digits in correct position: %d\n", numCorrectPosition);
printf("Number of correct digits in wrong position: %d\n", numCorrectDigit);
printf("Number of attempts remaining: %d\n\n", MAX_ATTEMPTS - numAttempts - 1);
}
numAttempts++;
}
printf("Game over! You ran out of attempts. The secret code was: ");
for(int i = 0; i < CODE_LENGTH; i++) {
printf("%d ", secretCode[i]);
}
printf("\n");
return 0;
}

这个代码实现了一个简单的珠玑妙算游戏,其中有一个4位数的秘密代码,玩家需要在限定次数内猜出正确的代码。代码中的generateSecretCode函数用于生成秘密代码,getUserGuess函数用于获取玩家的猜测,checkGuess函数用于检查猜测的正确性。游戏主循环在玩家猜测错误时进行迭代,直到玩家猜测正确或者次数用尽。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fec3eAzsLBg5UBVM.html

推荐文章

  • c语言goto用法是什么

    在C语言中,goto语句用于从一个位置跳转到另一个位置。它可以用于跳出循环、条件语句或者函数。goto语句的语法如下:
    goto label;
    ...
    label: //...

  • goto在c语言中的运用方法是什么

    goto 语句在 C 语言中允许程序从一个位置跳转到另一个位置。然而,在实际编程中,过度使用 goto 语句通常被认为是一种不好的编程实践,因为它可能导致代码结构混...

  • c语言堆和栈的区别是什么

    C语言中的堆和栈是两种不同的内存分配方式。 栈(Stack):栈是由编译器自动分配和释放的内存区域,用于存储局部变量和函数调用的上下文信息。栈的特点是先进后出...

  • C语言结构体数组怎么定义和使用

    在C语言中,可以通过定义结构体数组来存储多个具有相同数据类型的结构体对象。下面是定义和使用结构体数组的示例代码:
    #include // 定义一个结构体类型

  • 用C语言实现圣诞树(简易版+进阶版)

    简易版圣诞树实现:
    #include int main() {
    int rows, i, j, k;
    printf("请输入圣诞树的行数:");
    scanf("%d", &rows);
    for (i = 1; i

  • C语言之结构体定义typedef struct用法详解

    在C语言中,使用struct关键字可以定义一个结构体类型,用于存储不同类型的数据。为了方便使用,可以使用typedef关键字给结构体类型取一个别名。
    typedef st...

  • 使用C语言实现学生成绩管理系统

    下面是一个使用C语言实现学生成绩管理系统的简单示例:
    #include #include #define MAX_STUDENTS 100
    typedef struct {
    char name[50];
    int...

  • c++中的system("pause")的作用和含义解析

    在C++中,system(“pause”)的作用是暂停程序的执行,等待用户按下任意键继续执行下去。
    具体含义解析如下: system函数是C标准库中的函数,用于执行操作系...