C语言小游戏代码补全

请问这样一个游戏剩余的代码该怎么完成,现在已经完成了主函数部分:石头游戏是由两堆石头和两个玩家一起玩的。在轮到她的时候,一个玩家从较大的石头堆中取出一些石头。她取出的石头数量必须是小堆中石头数量的正倍。例如,让有序对(6,14)描述一个配置,在较小的堆中有6个石头,在较大的堆中有14个石头,然后第一个玩家可以从较大的堆中移除6或12块石头。从一堆石头中取出所有石头的玩家将赢得比赛。你的任务是用C代码编写3个函数,这样用户就可以对抗电脑了。当两名玩家中的一名获胜时,代码将显示谁获胜并退出。

#include 
int main(void)
{
int pile1, pile2;
int move1, move2;
printf("Input number of stones in pile 1: ");
scanf("%i",&pile1);
printf("Input number of stones in pile 2: ");
scanf("%i",&pile2);
// display piles
printf("Current piles (%i,%i)\n",pile1,pile2);
while (1) {
// ask user input
// determine if input is feasible
// repeat asking for input until move is feasible
do {
printf("Player 1 (user): How many stones do 
you want to move from
the largest pile?\n");
scanf("%i",&move1);
} while (!feasible(pile1,pile2,move1));
// perform move
if (pile1else {
pile2 = pile2+move1;
pile1 = pile1-move1;
}
// display new piles
printf("Current piles (%i,%i)\n",pile1,pile2);
if (win(pile1,pile2)) {
printf("You win!\n");// user is the winner!
break;
}
// compute computer move
move2 = computer(pile1,pile2);
printf("Computer moves %i stones from the largest 
pile\n",move2);
// perform move
if (pile1else {
pile2 = pile2+move2;
pile1 = pile1-move2;
}
// display new piles
printf("Current piles (%i,%i)\n",pile1,pile2);
if (win(pile1,pile2)) {
printf("The computer wins!\n");// computer is the 
winner!
break;
}
}
return 0;
}


已跑过用例(3,4)用户选择3,电脑直接选择7胜利 (6,14)用户选择12,


int feasible(int pile1, int pile2, int move) {
    int min_pile =  pile1 < pile2 ? pile1 : pile2; // 较小的石头堆
    int max_pile = pile1 > pile2 ? pile1 : pile2; // 较大的石头堆
    // 如果取出的数量不是小堆中石头数量的正倍或者大于较大石头堆的石头数量,则无效
    if (move <= 0 || move%min_pile != 0 || move > max_pile) { 
        return 0;
    }
    return 1;
}
//如果有一方的石子为空了,就直接赢了
int win(int pile1, int pile2) {
    if (pile1 == 0 || pile2 == 0) {
        return 1;
    }
    return 0;
}
 
int computer(int pile1, int pile2) {
    int min_pile = pile1 < pile2 ? pile1 : pile2;
    int max_pile = pile1 > pile2 ? pile1 : pile2;
    int min_move = min_pile;//最小移动的是当前最小堆
    int max_move = max_pile / min_move; //最大的移动是当前最大堆
    int i_move = min_move;
    //电脑如果发现当前移动导致最大的堆全部拿走就赢了,直接返回当前移动的步数
    for (i_move; min_move < max_move;) {
        if (max_pile - i_move == 0) { 
            return i_move;
        }
        i_move += min_pile;//每次增加最小堆
    }
    printf("Computer moves % i stones from the largest pile\n", min_move);
    return min_move;
}

望采纳

每次取多少,是由外部输入吗?

//feasible函数实现
int feasible(int pile1, int pile2, int move) {
int min_pile = pile1 < pile2 ? pile1 : pile2; // 较小的石头堆
int max_pile = pile1 > pile2 ? pile1 : pile2; // 较大的石头堆
// 如果取出的数量不是小堆中石头数量的正倍或者大于较大石头堆的石头数量,则无效
if (move <= 0 || move > 2 * min_pile || move > max_pile) {
printf(“Invalid move. Try again.\n”);
return 0;
}
return 1;
}

//win函数实现
int win(int pile1, int pile2) {
if (pile1 == 0 && pile2 == 0) {
return 1;
}
return 0;
}

//computer函数实现
int computer(int pile1, int pile2) {
int min_pile = pile1 < pile2 ? pile1 : pile2;
int max_pile = pile1 > pile2 ? pile1 : pile2;
int move = min_pile;
if (move * 2 <= max_pile) {
move *= 2;
} else if (max_pile - move > 0) {
move = max_pile - move;
}
printf(“Computer moves %i stones from the largest pile\n”,move);
return move;
}


下面是三个函数的代码:// 判断输入是否合法
int feasible(int pile1, int pile2, int move) {
    if (move <= 0) {
        printf("Invalid move. You must remove at least one stone.\n");
        return 0;
    }
    if (move > pile1 && move > pile2) {
        printf("Invalid move. You cannot remove more stones than the largest pile.\n");
        return 0;
    }
    if (move > pile2 && pile1 % pile2 != 0) {
        printf("Invalid move. You can only remove a multiple of the smaller pile.\n");
        return 0;
    }
    return 1;
}// 判断是否获胜
int win(int pile1, int pile2) {
    if (pile1 == 0 || pile2 == 0) {
        return 1;
    }
    return 0;
}// 计算电脑移动的石头数
int computer(int pile1, int pile2) {
    int move;
    if (pile1 < pile2 && pile2 % pile1 == 0) {
        move = pile2 / pile1;
    } else {
        move = pile2 % pile1;
    }
    if (move == 0) {
        move = 1;
    }
    return move;
}

好的,以下是游戏剩余的代码实现:

```c
#include <stdio.h>

//定义全局变量表示两堆石头的数量
int small_heap = 6;
int big_heap = 14;

//定义函数判断是否输入合法
int is_valid_input(int input) 
{
    if (input <= 0 || input > big_heap / 2 || input % small_heap != 0) {
        return 0;
    }
    return 1;
}

//定义函数模拟玩家操作
void player_take(int take_num) 
{
    big_heap -= take_num;
    small_heap -= take_num / small_heap;
}

//定义函数模拟电脑操作
void computer_take() 
{
    int take_num = (big_heap - small_heap) % (small_heap * 2);
    if (take_num == 0) {
        take_num = small_heap;
    }
    big_heap -= take_num;
    small_heap -= take_num / small_heap;
}

//定义主函数模拟游戏流程
int main() 
{
    printf("Welcome to the stone game!\n\n");
    
    //循环直到有一个玩家胜利
    while (1) {
        int player_input;
        
        //要求玩家输入取出的石头数
        do {
            printf("Small heap has %d stones, and big heap has %d stones. Please enter a number between 1 and %d which is a multiple of %d: ", small_heap, big_heap, big_heap / 2, small_heap);
            scanf("%d", &player_input);
        } while (!is_valid_input(player_input));
        
        player_take(player_input);//玩家取出石头
        printf("Player takes %d stones.\n", player_input);
        
        if (small_heap == 0) {//判断玩家是否获胜
            printf("\nYou win!\n");
            break;
        }
        
        computer_take();//电脑取出石头
        printf("Computer takes %d stones.\n", big_heap - small_heap);
        
        if (small_heap == 0) {//判断电脑是否获胜
            printf("\nComputer wins!\n");
            break;
        }
    }
    
    return 0;
}

使用方法:
将以上代码保存为一个.c文件,使用gcc编译器进行编译,并运行生成的可执行文件即可开始游戏。在游戏过程中根据提示输入玩家所取的石头数,如果输入不合法则需要重新输入。当一方获胜时,会显示当前胜利者并退出游戏。

```

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7449662
  • 除此之外, 这篇博客: 专升本C语言中的 有甲、乙、丙三人对一块矿石进行判断,每人判断两次,甲认为这块矿石不是铁也不是铜;乙认为这块矿石不是铁,是锡;并认为这块矿石不是锡,是铁;老工人两次判断都对,普通队员两次判断一对一错,实习生两次判断都错。则矿石是什么矿?甲乙丙身份分别是什么 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include<stdio.h>
    //铁0铜1锡2 
    int main(){
    	int a,a1,a2,b,b1,b2,c,c1,c2;
    	int x;
    	for(x=0;x<3;x++){
    		a1=(x!=0);
    		a2=(x!=1);
    		a=a1&&a2;
    		b1=(x!=0);
    		b2=(x==2);
    		b=b1&&b2;
    		c1=(x!=2);
    		c2=(x==0);
    		c=c1&&c2;
    		if((a+b+c==1)&&a1+b1+c1+a2+b2+c2==3){
    			switch(x){
    				case 0:printf("矿石是铁矿\n");break;
    				case 1:printf("矿石是铜矿\n");break;
    				case 2:printf("矿石是锡矿\n");break;
    			}
    			if(a1==a2&&a1==1)printf("甲是老工人\n");
    			else if((a1==0&&a2==1)||(a2==0&&a1==1))printf("甲是普通工人\n");
    			else if(a1==a2&&a1==0)printf("甲是实习生\n");
    			if(b1==b2&&b1==1)printf("乙是老工人\n");
    			else if((b1==0&&b2==1)||(b2==0&&b1==1))printf("乙是普通工人\n");
    			else if(b1==b2&&b1==0)printf("乙是实习生\n");
    			if(c1==c2&&c1==1)printf("丙是老工人\n");
    			else if((c1==0&&c2==1)||(c2==0&&c1==1))printf("丙是普通工人\n");
    			else if(c1==c2&&c1==0)printf("丙是实习生\n");
    		} 
    	}
    	return 0;
    }
    
  • 您还可以看一下 汤小洋老师的前端实战案例之石头剪刀布游戏(零基础入门)课程中的 石头剪刀布的效果展示小节, 巩固相关知识点

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
题目中要求编写三个函数:feasible、win和computer。

  1. feasible函数用于判断玩家输入是否合法,返回值为1表示合法,返回值为0表示不合法。该函数的实现如下:
int feasible(int pile1, int pile2, int move1) {
    if (move1 <= 0) { // 输入必须为正整数
        return 0;
    }
    if (move1 > pile1 && move1 > pile2) { // 输入不能超过两堆石头数
        return 0;
    }
    if (move1 > pile1 && move1 % pile1 != 0) { // 如果从大堆取,必须是小堆数量的正倍
        return 0;
    }
    if (move1 > pile2 && move1 % pile2 != 0) { // 同上,如果从大堆取,必须是小堆数量的正倍
        return 0;
    }
    return 1; // 输入合法
}
  1. win函数用于判断玩家是否胜利,返回值为1表示胜利,返回值为0表示未胜利。该函数的实现如下:
int win(int pile1, int pile2) {
    if (pile1 == 0 && pile2 == 0) { // 如果两堆石头都被取完,表示胜利
        return 1; // 胜利
    }
    return 0; // 未胜利
}
  1. computer函数用于计算电脑下一步应该移动几个石头。该函数的实现如下:
int computer(int pile1, int pile2) {
    int move;
    if (pile1 == pile2) { // 如果两堆石头数量相等,电脑随机从哪堆取
        move = rand() % pile1 + 1;
    } else { // 否则,电脑将从数量更多的堆中取出等于两堆数量差的数量
        int diff = abs(pile1 - pile2);
        if (pile1 > pile2) {
            move = pile1 - diff;
        } else {
            move = pile2 - diff;
        }
    }
    return move;
}

将这三个函数加入到已有代码中,并进行适当的修改,得到完整的代码如下:

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

// 函数声明
int feasible(int pile1, int pile2, int move1);
int win(int pile1, int pile2);
int computer(int pile1, int pile2);

int main(void)
{
    int pile1, pile2;
    int move1, move2;
    printf("Input number of stones in pile 1: ");
    scanf("%i",&pile1);
    printf("Input number of stones in pile 2: ");
    scanf("%i",&pile2);
    // display piles
    printf("Current piles (%i,%i)\n",pile1,pile2);
    while (1) {
        // ask user input
        // determine if input is feasible
        // repeat asking for input until move is feasible
        do {
            printf("Player 1 (user): How many stones do you want to move from the largest pile?\n");
            scanf("%i",&move1);
        } while (!feasible(pile1,pile2,move1));
        // perform move
        if (pile1<pile2) {
            pile2 = pile2-move1;
            pile1 = pile1+move1;
        } else {
            pile2 = pile2+move1;
            pile1 = pile1-move1;
        }
        // display new piles
        printf("Current piles (%i,%i)\n",pile1,pile2);
        if (win(pile1,pile2)) {
            printf("You win!\n");// user is the winner!
            break;
        }
        // compute computer move
        move2 = computer(pile1,pile2);
        printf("Computer moves %i stones from the largest pile\n",move2);
        // perform move
        if (pile1<pile2) {
            pile2 = pile2-move2;
            pile1 = pile1+move2;
        } else {
            pile2 = pile2+move2;
            pile1 = pile1-move2;
        }
        // display new piles
        printf("Current piles (%i,%i)\n",pile1,pile2);
        if (win(pile1,pile2)) {
            printf("The computer wins!\n");// computer is the winner!
            break;
        }
    }
    return 0;
}

int feasible(int pile1, int pile2, int move1) {
    if (move1 <= 0) { // 输入必须为正整数
        return 0;
    }
    if (move1 > pile1 && move1 > pile2) { // 输入不能超过两堆石头数
        return 0;
    }
    if (move1 > pile1 && move1 % pile1 != 0) { // 如果从大堆取,必须是小堆数量的正倍
        return 0;
    }
    if (move1 > pile2 && move1 % pile2 != 0) { // 同上,如果从大堆取,必须是小堆数量的正倍
        return 0;
    }
    return 1; // 输入合法
}

int win(int pile1, int pile2) {
    if (pile1 == 0 && pile2 == 0) { // 如果两堆石头都被取完,表示胜利
        return 1; // 胜利
    }
    return 0; // 未胜利
}

int computer(int pile1, int pile2) {
    int move;
    if (pile1 == pile2) { // 如果两堆石头数量相等,电脑随机从哪堆取
        move = rand() % pile1 + 1;
    } else { // 否则,电脑将从数量更多的堆中取出等于两堆数量差的数量
        int diff = abs(pile1 - pile2);
        if (pile1 > pile2) {
            move = pile1 - diff;
        } else {
            move = pile2 - diff;
        }
    }
    return move;
}

如果我的回答解决了您的问题,请采纳!