gpt4 book ai didi

c - 你如何为c中的数组中的元素赋值?

转载 作者:太空宇宙 更新时间:2023-11-04 08:33:13 24 4
gpt4 key购买 nike

我已经尝试过二维数组、不同的原型(prototype)函数,但我似乎无法为这个游戏实现评分系统。关于我能做什么的任何想法?我想从这段代码中获取 6 个从 1 到 6 的不同数字的输出,并为它们分配一个值,我可以将其加起来得出一个分数。前任。如果我掷出 1,它就值 100 分。将点值分配给滚动值的最快、最有效的方法是什么?

#include <stdio.h>
#include <time.h>

int main() {

int i, r, diceRoll;
char player1[20];
int temp, swapped;
int roll1[6];

srand(time(NULL));

printf("Enter name for Player 1:\n");
scanf(" %s", &player1);

for(i = 0; i < 6; i ++) {
r = ( rand() % 6 ) + 1;
diceRoll= r;
roll1[i] = diceRoll;
}

while(1) {
swapped = 0;
for( i = 0; i < 6-1; i++ ) {

if( roll1[i] > roll1[i + 1] ) {

temp = roll1[i];
roll1[i ] = roll1[i+1];
roll1[i+1] = temp;
swapped = 1;
}
}//for

if( swapped == 0) {
break;
}
}//while
printf("\n\n %s's Dice Roll:\n\n", player1); // prints out the dice rolls of player 1
return 0;
}//main

最佳答案

为什么不对映射到相应点值的值进行直接聚合? (除非是直接100 * roll值,那样的话就更简单了。)

int score = 0;
for( i = 0; i < 6; ++i )
{
switch( roll1[i] )
{
case 1: score += 100; break;
case 2: score += 230; break;
case 3: score += 540; break;
case 4: score += 2320; break;
case 5: score += 13130; break;
case 6: score += 454260; break; /* Of course, change to the score you want for each value. */
}
}
printf("\n\n %s's Dice Roll:%d\n\n", player1, score);

“我最初的想法是让游戏像 Farkle 一样,其中 1 是 100pts,5 是 50pts,其他一切都是 0,直到你得到 3 个相同的(三个 3s = 300pts,三个 1s 是 1000,等)。任何输入将不胜感激“有很多方法。您可以轻松地做到这一点,并使用 Chux 映射方法进行基本和三种操作。

int score = 0;
int face_score = 0;
int base_points[6] = { 100, 0, 0, 0, 50, 0 };
int three_of_a_kind_points[6] = { 300, 200, 300, 400, 500, 600 };
int four_of_a_kind_points[6] = {
int repeat_counter[6] = { 0, 0, 0, 0, 0, 0 };
int kind_mask = 0;
int pair_count = 0;
int three_of_a_kind_count = 0;
int four_of_a_kind_count = 0;
for( i = 0; i < 6; ++i )
{
kind_mask |= 1 << ( roll1[i] - 1 );
switch( ++repeat_counter[roll1[i] - 1] )
{
case 1: break;
case 2: ++pair_count; break;
case 3: score = three_of_a_kind_points[rolli[i] - 1]; ++three_of_a_kind_count; break;
case 4: score = 1000; ++four_of_a_kind_count; break;
case 5: score = 2000; break;
case 6: score = 3000; break;
}
}

if( pair_count == 3 ) /* Three pairs */
score = 1500;
else if( three_of_a_kind_count == 2 ) /* Two three of a kinds */
score = 2500;
else if( four_of_a_kind && ( pair_count == 2 ) ) /* A four of a kind and one pair (2 because four of a kind counted as a pair in the aggregation phase) */
score = 1500;
else if( kind_mask = 0x2F ) /* One of each type */
score = 1500; /* Straight */
else if( !score ) /* score only 1's and 5's */
score = repeat_counter[0] * 100 + repeat_counter[4] * 50;
printf("\n\n %s's Dice Roll:%d\n\n", player1, score);

我没有编译或运行这段代码,所以它可能不是 100% 正确。

关于c - 你如何为c中的数组中的元素赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27371130/

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