Instead of add to array, only add last

I followed a Simon Says tutorial

It has different modes.
Default = the simon says game
If you press the green button on startup it’s a battle version of the game.
If you press the yellow button on startup a small song is played.

I added the following.
If you press the red button it’s the simon says game and changed default mode into a baby mode.

The game should be almost the same as simon says, but only without a sequence.
So only 1 random led (and sound) and if you press this button, only a new random button will be shown. Until the currentMove = ROUNDS_TO_WIN

I used the same code as the memory game, cause the game is very similar, but am not able to make it so that it does not “add” a button to the sequence.

boolean play_baby(void)
{

  randomSeed(millis()); // Seed the random generator with random amount of millis()

  gameRound = 0; // Reset the game to the beginning

  while (gameRound < ROUNDS_TO_WIN)
  {
    baby_moves(); // Add a button to the current moves, then play them back

    playMoves(); // Play back the current game board

    // Then require the player to repeat the sequence.
    for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++)
    {
      byte choice = wait_for_button(); // See what button the user presses

      if (choice == 0) return false; // If wait timed out, player loses

      if (choice != gameBoard[currentMove]) return false; // If the choice is incorect, player loses

    }

    delay(1000); // Player was correct, delay before playing moves
  }

  return true; // Player made it through all the rounds to win!
  Serial.print("You've won");
}

The baby moves code is

void baby_moves(void)
{
  byte newButton = random(0, 4); //min (included), max (exluded)

  // We have to convert this number, 0 to 3, to CHOICEs
  if(newButton == 0) newButton = CHOICE_RED;
  else if(newButton == 1) newButton = CHOICE_GREEN;
  else if(newButton == 2) newButton = CHOICE_BLUE;
  else if(newButton == 3) newButton = CHOICE_YELLOW;

  gameBoard[gameRound++] = newButton; // Add this new button to the game array
  Serial.println(gameRound);
}

Hope somebody can help me with this problem so I have a nice and easy game for my baby :slight_smile:

I think this should do it…

while (gameRound < 1)

Instead of…

while (gameRound < ROUNDS_TO_WIN)

Edit or maybe…

gameBoard[0] = newButton; // Add this new button to the game array

Instead of…

gameBoard[gameRound++] = newButton; // Add this new button to the game array

And then…

boolean play_baby(void)
{

  randomSeed(millis()); // Seed the random generator with random amount of millis()

  gameRound = 0; // Reset the game to the beginning
  int babyRound = 0;

  while (babyRound < ROUNDS_TO_WIN)
  {
    baby_moves(); // Add a button to the current moves, then play them back
    babyRound++;

    playMoves(); // Play back the current game board

    // Then require the player to repeat the sequence.
    byte choice = wait_for_button(); // See what button the user presses

    if (choice == 0) return false; // If wait timed out, player loses

    if (choice != gameBoard[0]) return false; // If the choice is incorect, player loses

    delay(1000); // Player was correct, delay before playing moves
  }

  return true; // Player made it through all the rounds to win!
  Serial.print("You've won");
}

I will try it tomorrow and get back to you :slight_smile:

Many tnx.

@pcbbc (and other off course)

I see some changes in the game that looks very promising, but we’re not quite there yet.
Hope you still have a tip for me.

In Serial mode I get the message that the baby game is started:

BABY MODE ENABLED
0

But there’s no random led :slightly_frowning_face:

The game does seem to work. Because tt will give the timeout after 3 seconds and the game start again.

I also think a random led is choosen, but it’s not show. If I hit for example the red button every time the game starts again, sometimes I get a good hit. I also see in serial monitor that there is a good hit because I get

BABY MODE ENABLED
0
0 <----- good hit

When there is not a good hit, I get

BABY MODE ENABLED
0
BABY MODE ENABLED
0
BABY MODE ENABLED
0
BABY MODE ENABLED
0

When the random led will be showing, I believe it’s complete. And it should be extra nice if in serial mode the actual gameRound will be print, instead of only zero’s :slight_smile: :slight_smile: :slight_smile:

Many tnx hope you understand my problem

update.

The game seem to work.

I also serial print the random button

gameBoard[0] = newButton; // Add this new button to the game array
  Serial.println(gameRound);
  Serial.println(newButton);

I did a complete came with pressing the right button based on the number in serial monitor.

If the playback will be working, it's complete, only I don't know how :slight_smile: :slight_smile: :slight_smile:

A small update,

I believe the playBabymoves function is not correct.

void playBabyMoves(void)
{
  for (byte currentMove = 0 ; currentMove < gameRound ; currentMove++)
  {
    toner(gameBoard[currentMove], 150);

    // Wait some amount of time between button playback
    // Shorten this to make game harder
    delay(150); // 150 works well. 75 gets fast.
  }
}

Hope somebody know what code to put in.