2301_80154085 2023-11-21 11:13 采纳率: 73.7%
浏览 6
已结题

c语言函数数组问题完整代码

  1. 两个小朋友玩“逃跑游戏”,小朋友A和B分别扔数字色子,然后根据色子的点数(04)查看游戏攻略来确定具体移动几步,先移动大于等于20步的小朋友获胜,此时扔了几次色子不确定,但如果扔了100次还未决定胜负,则停止游戏,并以跑的最远的小朋友为胜利者。2. 具体要求:a) 游戏攻略必须用数组char gameEquipment[EQUIPMENTLIST_SIZE] = { 'f','b','s','j','m' };表示,色子为0代表获得工具‘f’;色子为1代表获得工具‘b’;其中f代表前进1步,b代表后退1步,s代表本轮不移动,j代表本次跳跃前进5步,m为魔法棒,代表随机向前+或向后移动- (03)步。b) 分别用数组记录每个小朋友每次移动的步数(不是色子数);c) 程序需要至少包含一个main之外的函数,且该函数的形式参数有一维数组。本题建议分别设计一个打印输出函数和一个计算每次移动步数的函数。d) 打印输出函数可以参考示范图片格式进行输出。e) 输出最终胜利者是谁。
  • 写回答

6条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-11-21 11:13
    关注
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define EQUIPMENTLIST_SIZE 5
    
    char gameEquipment[EQUIPMENTLIST_SIZE] = {'f', 'b', 's', 'j', 'm'};
    
    int moveStep(char equipment) {
        int step;
        switch(equipment) {
            case 'f':
                step = 1;
                break;
            case 'b':
                step = -1;
                break;
            case 's':
                step = 0;
                break;
            case 'j':
                step = 5;
                break;
            case 'm':
                step = rand() % 11 - 5;
                break;
            default:
                step = 0;
                break;
        }
        return step;
    }
    
    void printMoves(int moves[], int size) {
        printf("Moves: ");
        for (int i = 0; i < size; i++) {
            printf("%d ", moves[i]);
        }
        printf("\n");
    }
    
    int main() {
        srand(time(0));
        int playerA[100] = {0};
        int playerB[100] = {0};
        int playerA_position = 0;
        int playerB_position = 0;
        int round = 0;
        
        while (round < 100) {
            int diceA = rand() % 6;
            int diceB = rand() % 6;
            
            char equipmentA = gameEquipment[diceA];
            char equipmentB = gameEquipment[diceB];
            
            int stepA = moveStep(equipmentA);
            int stepB = moveStep(equipmentB);
            
            playerA_position += stepA;
            playerB_position += stepB;
            
            playerA[round] = playerA_position;
            playerB[round] = playerB_position;
            
            if (playerA_position >= 20 || playerB_position >= 20) {
                break;
            }
            
            round++;
        }
        
        printMoves(playerA, round);
        printMoves(playerB, round);
        
        if (playerA_position >= 20 && playerB_position >= 20) {
            printf("Draw! Both players reached 20 or more steps.\n");
        } else if (playerA_position >= 20) {
            printf("Player A wins!\n");
        } else if (playerB_position >= 20) {
            printf("Player B wins!\n");
        } else {
            int max_position = playerA_position > playerB_position ? playerA_position : playerB_position;
            if (playerA_position == playerB_position) {
                printf("Draw! Both players reached the same maximum position: %d\n", max_position);
            } else if (max_position == playerA_position) {
                printf("Player A wins with the maximum position: %d\n", max_position);
            } else {
                printf("Player B wins with the maximum position: %d\n", max_position);
            }
        }
        
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月21日
  • 已采纳回答 11月21日
  • 创建了问题 11月21日