我将游戏设置为7分半(类似于黑杰克),最后我必须检查谁的得分最高等于或低于7.5,并打印出得分为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);
}
}
}发布于 2020-11-01 02:20:08
您可以优化此循环:
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来表示非赢家,只需使用0即可。毕竟它是一个布尔值。
那么获胜者循环是简单的:
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);
}
}https://stackoverflow.com/questions/64624902
复制相似问题