#Game Design: Simon Says, trouble with detecting cheating

4 messages · Page 1 of 1 (latest)

strong jetty
#

Hey guys, I'm programming a Simon Says game on an FPGA.
Problem: I cannot detect if a player is cheating. I've defined 'cheating' as pressing more than 1 button during the player's turn, but whenever I do that, somehow it's being flagged as 'incorrect' and not cheating.

Current output: Pressing more than one button leads to a msg saying the player was 'incorrect'.

Expected output: msg saying the player was 'cheating' and the lights flash 5 times.

There's something wrong with my logic but I don't know what. The board is working fine and it's debounced. Any advice appreciated. Thank you

void cheating_state(int btnValue) {
    xil_printf("Entered CHEATING state. \n\r");

    // Flash all LEDs  5 times at 2 Hz (flashes on for 0.5 seconds, off for 0.5 seconds)
    flash_leds(5, 2);  // 3 flashes at 1Hz

    // Transition to the GAME_OVER state after flashing the LEDs
    current_state = GAME_OVER;
}```
cyan furnaceBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

strong jetty
#
void player_state() {
    xil_printf("Entered PLAYER state.\n\r");
    player_index = 0;

    while (player_index < current_sequence_length) {
        int btnValue = getbutton();  // Get the button value

        // Wait until a button is pressed
        while (btnValue == 0) {
            btnValue = getbutton();  // Keep checking for button press
        }

        // Count how many buttons were pressed
        int numButtonsPressed = count_set_bits(btnValue);  // Count the number of bits set in btnValue
        xil_printf("Number of buttons pressed: %d\n\r", numButtonsPressed);

        // Check if more than one button was pressed (cheating)
        if (numButtonsPressed > 1) {
            xil_printf("Cheating detected!\n\r");
current_state = CHEATING;  // Move to cheating state
return;  // Exit player state}

        // If the correct button is pressed
        if (btnValue == (1 << game_sequence[player_index])) {
            xil_printf("Player pressed correct button: %d\n\r", game_sequence[player_index]);

            // Display the button pressed on the LEDs
            XGpio_DiscreteWrite(&btnLed, 2, 1 << game_sequence[player_index]);  // Light up the correct LED

            usleep(400000);  // Delay to show the LED for 0.4 seconds
            XGpio_DiscreteWrite(&btnLed, 2, 0x0);  // Turn off the LED

            player_index++;  // Move to the next step in the sequence
        } else {
            // If the wrong button is pressed
            xil_printf("Wrong button! Expected: %d, but got: %d\n\r", game_sequence[player_index], btnValue);

flash_leds(3,1);
            current_state = GAME_OVER;
            return;
        }
    }

    xil_printf("Player successfully repeated the sequence. Returning to SIMON state.\n\r");
    current_state = SIMON;  // Return to Simon's turn
}```
#
int count_set_bits(int value) {
    int count = 0;
    while (value) {
        count += value & 1;  // Add 1 to count if the least significant bit is set
        value >>= 1;  // Shift right to check the next bit
    }
    return count;
}```