可以帮我写个切水果的小游戏吗,用api+c实现
时间: 2024-03-06 16:46:49 浏览: 30
好的,我可以提供一些思路和代码片段,但需要您自己动手实现,因为这是一道编程题目,需要您自己完成。
首先,我们需要一个随机生成水果的函数,可以使用rand()函数生成随机数,再通过switch语句将随机数转换为对应的水果名称。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generateFruit(char *fruit) {
srand(time(NULL));
int random = rand() % 5;
switch (random) {
case 0:
strcpy(fruit, "apple");
break;
case 1:
strcpy(fruit, "orange");
break;
case 2:
strcpy(fruit, "banana");
break;
case 3:
strcpy(fruit, "watermelon");
break;
case 4:
strcpy(fruit, "grape");
break;
}
}
```
接下来,我们需要一个函数来接收用户输入的切水果指令,并根据指令判断是否切中了水果。代码如下:
```c
int cutFruit(char *fruit, char *command) {
int cut = 0;
if (strcmp(fruit, "apple") == 0 && strcmp(command, "cut apple") == 0) {
printf("You cut the apple!\n");
cut = 1;
} else if (strcmp(fruit, "orange") == 0 && strcmp(command, "cut orange") == 0) {
printf("You cut the orange!\n");
cut = 1;
} else if (strcmp(fruit, "banana") == 0 && strcmp(command, "cut banana") == 0) {
printf("You cut the banana!\n");
cut = 1;
} else if (strcmp(fruit, "watermelon") == 0 && strcmp(command, "cut watermelon") == 0) {
printf("You cut the watermelon!\n");
cut = 1;
} else if (strcmp(fruit, "grape") == 0 && strcmp(command, "cut grape") == 0) {
printf("You cut the grape!\n");
cut = 1;
} else {
printf("You missed the fruit!\n");
}
return cut;
}
```
最后,我们可以在主函数中循环调用以上两个函数,直到用户成功切中水果为止。完整的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generateFruit(char *fruit) {
srand(time(NULL));
int random = rand() % 5;
switch (random) {
case 0:
strcpy(fruit, "apple");
break;
case 1:
strcpy(fruit, "orange");
break;
case 2:
strcpy(fruit, "banana");
break;
case 3:
strcpy(fruit, "watermelon");
break;
case 4:
strcpy(fruit, "grape");
break;
}
}
int cutFruit(char *fruit, char *command) {
int cut = 0;
if (strcmp(fruit, "apple") == 0 && strcmp(command, "cut apple") == 0) {
printf("You cut the apple!\n");
cut = 1;
} else if (strcmp(fruit, "orange") == 0 && strcmp(command, "cut orange") == 0) {
printf("You cut the orange!\n");
cut = 1;
} else if (strcmp(fruit, "banana") == 0 && strcmp(command, "cut banana") == 0) {
printf("You cut the banana!\n");
cut = 1;
} else if (strcmp(fruit, "watermelon") == 0 && strcmp(command, "cut watermelon") == 0) {
printf("You cut the watermelon!\n");
cut = 1;
} else if (strcmp(fruit, "grape") == 0 && strcmp(command, "cut grape") == 0) {
printf("You cut the grape!\n");
cut = 1;
} else {
printf("You missed the fruit!\n");
}
return cut;
}
int main() {
char fruit[20];
generateFruit(fruit);
printf("Please cut the %s: ", fruit);
char command[20];
int success = 0;
while (!success) {
fgets(command, 20, stdin);
success = cutFruit(fruit, command);
if (!success) {
printf("Please try again: ");
} else {
printf("Congratulations!\n");
}
}
return 0;
}
```
希望这些代码能对您有所帮助,祝您编程愉快!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)