首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C-优化获胜者选择

C-优化获胜者选择
EN

Stack Overflow用户
提问于 2020-11-01 02:04:03
回答 1查看 30关注 0票数 0

我将游戏设置为7分半(类似于黑杰克),最后我必须检查谁的得分最高等于或低于7.5,并打印出得分为7.5的玩家,这就是我所做的,但我不知道是否有更好的方法来做到这一点

代码语言:javascript
复制
//Winner selection
    printf("\nThe winning player (s) is / are:\n");

    //checks highest score
    for (int i = 0; i < players; i++)
    {
        if (score[i] <= 7.5)
        {
            if (score[i] >= max_score)
            {
                max_score = score[i];
            }
        }
    }

    //checks players with that score
    for (int i = 0; i < players; i++)
    {
        if (score[i] <= 7.5)
        {
            if (score[i] == max_score)
            {
                winners[i] = 1;
            }
            else
            {
                winners[i] = -1;
            }
        }
        else
        {
            winners[i] = -1;
        }
    }

    //prints the winners
    for (int i = 0; i < players; i++)
    {
        if (winners[i] == 1)
        {
            printf("Player %d\n", i + 1);
        }
    }

如果你想看完整的代码,或者你想试着播放它,它在这里:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//deck
#define AS 1
#define DOS 2
#define TRES 3
#define CUATRO 4
#define CINCO 5
#define SEIS 6
#define SIETE 7
#define SOTA 0.5
#define CABALLO 0.5
#define REY 0.5
#define ncards 40

// rand num gen
int random_number(int a, int b)
{
    return a + rand() % (b - a + 1);
}

int main()
{
    int players, winners[4], j = 0;
    double score[4], max_score = 0;
    char play;
    //whole deck
    double cards[ncards] = {AS, DOS, TRES, CUATRO, CINCO, SEIS, SIETE, SOTA, CABALLO, REY,

                            AS, DOS, TRES, CUATRO, CINCO, SEIS, SIETE, SOTA, CABALLO, REY,

                            AS, DOS, TRES, CUATRO, CINCO, SEIS, SIETE, SOTA, CABALLO, REY,

                            AS, DOS, TRES, CUATRO, CINCO, SEIS, SIETE, SOTA, CABALLO, REY};

    //new seed
    srand((unsigned)time(NULL));

    //shuffle cards
    for (int i = 0; i < 100; i++)
    {
        cards[random_number(0, ncards)] = cards[random_number(0, ncards)];
    }

    //determine num of players
    do
    {
        printf("\nHow many players (maximum 4)? ");
        scanf("%d", &players);
    } while (players > 4);

    //game execution
    for (int i = 0; i < players; i++)
    {
        score[i] = 0;
        play = 'y';
        printf("\nPlayer %d ==========\n", i + 1);
        do
        {
            score[i] += cards[j];
            if (score[i] < 7.5)
            {
                printf("\nYour partial score is %.1lf.", score[i]);
                j++;
                printf("\nDo you want a card (y/n)? ");
                scanf(" %c", &play);
            }
            else if (score[i] == 7.5)
            {
                printf("You've exactly 7.5!");
            }

            else
            {
                printf("\nYou've gone !! You stay with %.1lf points!", score[i]);
            }

        } while (score[i] < 7.5 && play == 'y');
    }

    //Winner selection
    printf("\nThe winning player (s) is / are:\n");

    //checks highest score
    for (int i = 0; i < players; i++)
    {
        if (score[i] <= 7.5)
        {
            if (score[i] >= max_score)
            {
                max_score = score[i];
            }
        }
    }

    //checks players with that score
    for (int i = 0; i < players; i++)
    {
        if (score[i] <= 7.5)
        {
            if (score[i] == max_score)
            {
                winners[i] = 1;
            }
            else
            {
                winners[i] = -1;
            }
        }
        else
        {
            winners[i] = -1;
        }
    }

    //prints the winners
    for (int i = 0; i < players; i++)
    {
        if (winners[i] == 1)
        {
            printf("Player %d\n", i + 1);
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-01 02:20:08

您可以优化此循环:

代码语言:javascript
复制
for (int i = 0; i < players; i++)
{
    if (score[i] <= 7.5)
    {
        if (score[i] == max_score)
        {
            winners[i] = 1;
        }
        else
        {
            winners[i] = -1;
        }
    }
    else
    {
        winners[i] = -1;
    }
}

简单地说就是:

代码语言:javascript
复制
for (int i = 0; i < players; i++)
{
    winners[i] = ((score[i] <= 7.5) && (score[i] == max_score)) ? 1 : -1;
}

为了改进风格:

不用-1来表示非赢家,只需使用0即可。毕竟它是一个布尔值。

那么获胜者循环是简单的:

代码语言:javascript
复制
for (int i = 0; i < players; i++)
{
    winners[i] = ( (score[i] <= 7.5) && (score[i] == max_score) );
}

打印循环:

代码语言:javascript
复制
//prints the winners
for (int i = 0; i < players; i++)
{
    if (winners[i])
    {
        printf("Player %d\n", i + 1);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64624902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档