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

如何在c中为数组中的元素赋值?,c,arrays,C,Arrays,我尝试过二维数组,不同的原型函数,但我似乎无法为这个游戏实现评分系统。关于我能做什么有什么想法吗?我想从这段代码中获取输出,它是6个从1到6不等的数字,然后给它们分配一个值,我可以将它们相加,得到一个分数。如果我掷1,那就值100分。将点值指定给滚动值的最快和最有效的方法是什么 #include <stdio.h> #include <time.h> int main() { int i, r, diceRoll; char player1[20];

我尝试过二维数组,不同的原型函数,但我似乎无法为这个游戏实现评分系统。关于我能做什么有什么想法吗?我想从这段代码中获取输出,它是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
#包括
#包括
int main(){
int i,r,掷骰子;
charplayer1[20];
int temp,交换;
int-roll1[6];
srand(时间(空));
printf(“输入播放器1的名称:\n”);
scanf(“%s”和player1);
对于(i=0;i<6;i++){
r=(rand()%6)+1;
掷骰子=r;
roll1[i]=掷骰子;
}
而(1){
交换=0;
对于(i=0;i<6-1;i++){
if(roll1[i]>roll1[i+1]){
temp=roll1[i];
roll1[i]=roll1[i+1];
roll1[i+1]=温度;
交换=1;
}
}//为了
如果(交换==0){
打破
}
}//当
printf(“\n\n%s的骰子卷:\n\n”,玩家1);//打印出玩家1的骰子卷
返回0;
}//主要

为什么不直接聚合映射到相应点值的值?(除非它是直线100*滚动值,在这种情况下更容易。)

int评分=0;
对于(i=0;i<6;++i)
{
开关(roll1[i])
{
案例1:分数+=100;中断;
案例2:得分+=230;中断;
案例3:分数+=540;中断;
案例4:得分+=2320;中断;
案例5:得分+=13130;中断;
案例6:score+=454260;break;/*当然,更改为每个值所需的分数*/
}
}
printf(“\n\n%s的掷骰子:%d\n\n”,玩家1,分数);

“我最初的想法是让游戏像Farkle一样,1是100分,5是50分,其他的都是0,直到你得到3分(三个3分=300分,三个1分是1000分,等等)。任何输入都会非常感谢”有很多方法。您可以很容易地做到这一点,并使用Chux映射方法处理base和three of a种类

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);
int评分=0;
int face_得分=0;
int基点[6]={100,0,0,0,50,0};
int三类点[6]={300200300400500600};
int四类点[6]={
int repeat_计数器[6]={0,0,0,0,0};
int kind_mask=0;
int pair_count=0;
int三类计数=0;
int-four-of-of-a-种类计数=0;
对于(i=0;i<6;++i)
{

kind_mask |=1请改写您的问题,并指出您希望在代码中插入逻辑的位置。请解释数组插入与掷骰子有什么关系。我正在考虑将其制作成函数原型,并在以后打印分数时调用它。至于编辑,我在这里还是相当新的,我正在尝试找出如何更好地编辑我的问题。
intxx[]={0,10,40,90160250360};intscore=0;for(int i=0;i@chux,它似乎在某种程度上起作用。每当我不止一次这样做时,结果都不会改变。另外,你能解释一下这些数字是如何工作的吗?我还有其他的分数要实现。请和谢谢。我会给其他人留下一个详细的答案。
score+=xx[roll1[I]
simple uses
roll1[I]
作为数组的索引
xx[],这是添加到得分> /Calp>的愚蠢问题,但是是“Case' C,还是C++?我以前从来没有见过它,我应该用C。我原来想到的是让游戏像法克尔,其中1是100PTS,5是50PTS,其他的东西是0,直到你得到3的同类。(三个3s=300pts,三个1s=1000,等等)。任何输入都会非常受欢迎。我修改了示例。不确定点系统是否正确,或者代码是否没有语法/语义错误。但这应该有助于您了解情况。
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);