繁体   English   中英

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

[英]How do you assign values to elements in an array in c?

我尝试了二维数组,改变了原型功能,但似乎无法为该游戏实现评分系统。 关于我能做什么的任何想法? 我想从此代码中获取输出,这些输出是1-6中的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 *滚动值,在这种情况下甚至更容易。)

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是100分,5是50分,其他一切都是0,直到得到3类(三个3分= 300分,三个1分是1000,依此类推)。任何输入将不胜感激”有很多方法。 您可以轻松地做到这一点,并将Chux map方法用于基础和三种。

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%正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM