C - 优化获胜者选择

C - Optimize winner selection

我要把游戏做成七点半(类似于黑杰克),最后我必须检查谁的最高分等于或低于 7.5,并打印出那个得分的玩家,这是我做了什么,但不知道是否有更优化的方法

//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);
        }
    }

如果您想查看完整代码或想尝试在此处播放它,请访问:

#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);
        }
    }
}

你可以优化这个循环:

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;
    }
}

简单地是:

for (int i = 0; i < players; i++)
{
    winners[i] = ((score[i] <= 7.5) && (score[i] == max_score)) ? 1 : -1;
}

以及改进风格:

而不是 -1 来指示 non-winner,只需使用零。毕竟是布尔值。

那么赢家循环就是:

for (int i = 0; i < players; i++)
{
    winners[i] = ( (score[i] <= 7.5) && (score[i] == max_score) );
}

打印循环:

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