gpt4 book ai didi

c - 添加分数最有效的方法?

转载 作者:行者123 更新时间:2023-11-30 15:26:54 25 4
gpt4 key购买 nike

我仍在尝试制作骰子游戏,我只需要知道为我的游戏添加分数的最佳方式是什么,以便我可以打印出明确的获胜者。我是否需要一堆 if-else 语句,或者创建一个头文件会更容易吗?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>

void clean_stdin(void) {
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}


int main() {

int i, r, diceRoll;
char player1[20];
int score1[5];
int sum;
srand(time(NULL));

printf("\t\t\t Welcome to Farkle Lite!\n");
printf(" Highest score wins!\n\n");
printf(" The rules:\n");
printf(" 1 = 100 pts, 5 = 50 pts. Everything else is 0.\n\n Get 3 of a kind, and multiply the result by 100 (Ex. Three 4s = 400 pts).\n");
printf(" If you get more than 3 of a kind, any repeats will double the score \n (Ex. Four 4s = 800 pts, Five 4s = 1600.)\n\n");
printf(" Rolling 1-6 gets you an automatic 1500 pts!\n\n");
printf("Enter name for Player 1:\n");
fgets (player1, 20, stdin);
clean_stdin();

printf("\n\n %s Dice Roll:\n\n", player1);

for(i = 0; i < 6; i ++){
r = (rand()%6)+1;
diceRoll= r;
score1[i]= diceRoll;
sum = score[i];
printf(" test %d \n", score1[i]);
}
printf(" %d", score1[i]);
return 0;
}

最佳答案

这是一个使用函数的好机会。每个玩家将掷骰子六次,每个玩家的得分总计,并且每个玩家都可以输入自己的名字。我们可以创建一个函数来集中这种行为。为此,我们需要将玩家姓名和玩家分数存储在数组中,以便第二次运行该函数时不会覆盖第一次运行的信息。下面是一个例子:

void play (int playerNo, char* name, int* score)
{
int loop;
int role;

printf("Enter name for Player %d:\n", playerNo);
fgets (name, 20, stdin);
clean_stdin();

printf("\n\n %s Dice Roll:\n\n", name);

*score = 0;
for(loop = 0; loop<6; loop++)
{
role = (rand()%6)+1;
*score += role;
printf (" %d\n", role);
}
}

此功能将允许用户输入名称,然后将骰子移动 6 次,总计得分。退出后,playerNames 和playerScores 数组中的相应条目将被更新。

得到这个之后,我们只需要循环播放器,为每个播放器调用这个函数:

int main() 
{
const int playerCount = 2;
char playerNames [playerCount][20];
int playerScores[playerCount];
int loop;
int highestScore = 0;
int highestPlayer;

// Seed rng
srand(time(NULL));

// Instructions
printf("\t\t\t Welcome to Farkle Lite!\n");
printf(" Highest score wins!\n\n");
printf(" The rules:\n");
printf(" 1 = 100 pts, 5 = 50 pts. Everything else is 0.\n\n Get 3 of a kind, and multiply the result by 100 (Ex. Three 4s = 400 pts).\n");
printf(" If you get more than 3 of a kind, any repeats will double the score \n (Ex. Four 4s = 800 pts, Five 4s = 1600.)\n\n");
printf(" Rolling 1-6 gets you an automatic 1500 pts!\n\n");

// Let each player play the game
for (loop=0; loop<playerCount; loop++)
play (loop+1, &playerNames[loop][0], &playerScores[loop]);

// Tally scores
for (loop=0; loop<playerCount; loop++)
{
if (playerScores[loop] > highestScore)
{
highestScore = playerScores[loop];
highestPlayer = loop;
}
}

// Display winner
printf ("Player %d: %s wins with a scrore of %d",
highestPlayer+1,
playerNames[highestPlayer],
playerScores[highestPlayer]);

return 0;
}

之后我们可以遍历所有分数,记录最高的分数,然后显示获胜者。

关于c - 添加分数最有效的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27260725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com