簡體   English   中英

選擇性忽略隨機生成的數字序列的某些部分(以C表示)

[英]Selectively ignoring parts of a randomly generated sequence of numbers (in C)

我有一個可能難以理解的問題-但我會盡力解釋。

我正在用C語言對Simon Game進行編程。此實現專門讀取/寫入硬件DAQ模塊,該模塊具有4個LED顯示屏和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);
        }
    }
}

在出現問題的地方,我不確定“ digitalRead(sequence [!i]”)的行為;我需要此行來讀取所有不應按下的開關。

不過,我認為編譯器無法理解我要在此處執行的操作-例如,如果序列中的第一個數字為3(代表第四個LED),則需要指定其他每個數字(0, 1、2)及其相應的開關不應該被按下。

一種解決方案是將當前數字存儲在序列中,每個LED具有一組四個TRUE / FALSE標志,並監視三個非當前數字及其對應關系。 開關看看是否被按下了?



我對編寫此程序感到非常沮喪。 我是編程新手。 任何幫助表示贊賞。

我不確定我是否正確理解了該游戲的規則,但有一件事情會立即跳出來

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.

在C中, ! 運算符是一元NOT。 當應用於整數i ,等效於if (i == 0) return 1; else return 0; 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM