Ghua975 2023-04-06 14:24 采纳率: 83.3%
浏览 114
已结题

C语言小游戏代码补全

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

#include <stdio.h>
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;
}


  • 写回答

7条回答 默认 最新

  • 前网易架构师-高司机 游戏服务器领域优质创作者 2023-04-06 17:45
    关注

    已跑过用例(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;
    }
    
    

    望采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 赞助了问题酬金15元 4月6日
  • 创建了问题 4月6日

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)