选择性地忽略随机生成的数字序列的部分(在C中)

时间:2015-04-10 07:52:52

标签: c

我有一个可能难以理解的问题 - 但我会尽力解释。

我正在编写C语言中的Simon Game。此实现专门读取/写入具有4个LED显示屏的硬件DAQ模块和4个相应的切换开关。

根据游戏规则,我播种并生成0到3之间的随机数字序列(序列长度任意5)。在游戏中,如果玩家按下错误的开关(即显示蓝色但按下绿色),则游戏结束并重新开始。

我设置游戏的方式如下:
(我还没有包含功能代码" blinkLED"这里 - 它会打开/关闭实际的LED。)

    void runSimon(void){
    int sequence[MAX_SEQ_LEN];
    int i;
    int count = 0;

    // Seeds the random number generator.
    srand((unsigned)time(NULL));

    // Generate the random LED sequence & store it as an array.
    for (i = 0; i < MAX_SEQ_LEN; i++){
        sequence[i] = (rand() % NUM_LEDS);
    }

    // The game begins!
    while (continueSuperLoop() == TRUE){

        // Loop the game while the sequence length is less than the pre-defined maximum (currently it's 5).
        while (count < MAX_SEQ_LEN){

            for (i = 0; i <= count; i++){

                // Blink the first 'count' LEDs in the sequence, one at a time.
                blinkLED(sequence[i], 1, ONE_SEC);

                //
                //
                //THE ISSUE SHOULD BE HERE (!)
                //
                // Monitors whether or not the player has made a mistake...if so, blink the red LED thrice, then restart the game. 
                if (digitalRead(sequence[ !i ] == SWITCH_ON)){
                    blinkLED(LED_1_R, 3, HALF_SEC);
                    Sleep(3 * ONE_SEC);
                    continue;
                }

                // Monitors whether or not the correct switch is being pressed -- waits for it to be released
                while (digitalRead(sequence[i]) == SWITCH_ON){}
            }
            count++;
        }

        // If 'count' is equal to 'MAX_SEQ_LEN', the green LED blinks 3x to indicate the player has won .
        if (count == MAX_SEQ_LEN){
            blinkLED(LED_0_G, 3, HALF_SEC);
            Sleep(3 * ONE_SEC);
        }
    }
}

在我指出问题的地方,我不确定&#34; digitalRead(序列[!i]&#34;表现如何;我需要这一行来读取每个不应该的开关被按下。

我不认为编译器理解我在这里尝试做什么 - 例如,如果序列中的第一个数字是3(代表第4个LED),我需要指定其他所有不应按数字(0,1,2)及其对应的开关。

解决方案是将序列中的当前数字存储,每个LED具有一组四个TRUE / FALSE标志,并监控三个非当前数字及其对应关系。切换以查看它们是否被按下?



我对编写这个程序感到非常沮丧。我对编程很陌生。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

我不确定我是否正确理解了这个游戏的规则,但有一件事就是立即跳出来了

digitalRead(sequence[ !i ]

我想你想要

!digitalRead(sequence[ i ]

此外,您需要修复游戏流程。现在是:

1. Light LED.
2. Check if user pressed the right button.

您需要等待一段时间才能检查开关或等待按下任何开关并查看它是否正确。所以像这样:

 1. Light LED.
 2. Wait for timeout or ANY switch to be pressed.
 3. If timeout: error
 4. else: check if switch that was pressed is correct.

答案 1 :(得分:0)

在C中,!运算符是一元NOT。应用于整数i时,它等同于if (i == 0) return 1; else return 0;。然后,您使用!i作为sequence数组的索引,因此它将是sequence[0]sequence[1],显然这不是您想要的。此外,您的==位于digitalRead来电:)

我建议明确检查每个其他按钮是否被按下。像这样:

int isOtherPressed = 0;
for (ledId = 0; ledId < NUM_LEDS; ledId++) {
    if (ledId != sequence[i] && digitalRead(ledId) == SWITCH_ON) {
        isOtherPressed = 1;
    }
}

if (isOtherPressed) {
    // restart the game
}

然而,我对你拥有的整个游戏玩法持怀疑态度,但也许只是因为我不知道digitalRead是如何运作的。例如,您使用continue的方式似乎并没有停止游戏。也许你的意思是break