Programming Help - Controlling number of random flags being sent

I’ve been trying to put together my first game, a 2-player “Match 2” game, but I’m having a bit of trouble with the randomness element I’m trying to implement.

The overall idea is that when the game starts, the tiles (4 tiles for a two player game) will randomize, setting 2 tiles as Red for Player 1, and two tiles as blue for Player 2. The goal is to be the first player to press both of your tiles.

Essentially, I have one tile that acts as the “controller” that would keep track of the number of tiles that are specified for each player. This tile would have to be touching all of the other tiles in play in order to keep track of each tile’s state. When this tile is pressed after the code is loaded, it calls this function, which randomizes itself as well as the tiles it is connected to:

void placeFlags()
{
  int state = rand(1);
  if (state == 0 && Player1Count < 2)
    {
      setColor(RED);
      ++Player1Count;
    }
  else if (state == 1 && Player2Count < 2)
    {
      setColor(BLUE);
      ++Player2Count;
    }
  FOREACH_FACE(f)
  {
    //Skip Inactive Face
    if (getLastValueReceivedOnFace(f) != STANDBY)
        continue;
    // Randomize and send colors and states. 
    else if (getLastValueReceivedOnFace(f) == STANDBY)
    {
     state = rand(1);
      if (state == 0 && Player1Count < 2)
      {
        setValueSentOnFace(PLAYER1, f); 
        ++Player1Count;
      }
      else if (state == 1 && Player2Count < 2)
      {
        setValueSentOnFace(PLAYER2, f); 
        ++Player2Count;
      }    
    }
  }
}

This function is called earlier in the main loop, while the tile is waiting for a button press:

    //Ensure that only 2 of each piece is sent.
    if (Player1Count != 2 || Player2Count != 2)
    {
      Player1Count = 0;
      Player2Count = 0;
      placeFlags(); 
    }

What happens now is that at most, 2 or 3 of the tiles will display their state, but the others will simply remain blank. I have a feeling it’s because the Player1Count or Player2Count variables have exceeded the max count of 2, as I will on occasion get all of the tiles to light up correctly with a button press.

Can anyone see what I might be doing wrong here? If someone has a better idea of how to implement the same logic please let me know! If the rest of the code is needed I’ll be happy to provide it, I’m not sure if this function makes sense without it’s context to someone else.

Thanks for any help you guys can offer!

EDIT 1: In case anyone want’s to see the rest of the code, you can find it here.

EDIT 2: Updated the code to avoid calling placeFlags() recursively.

:open_mouth: Just seeing this now. This game has some fun and tricky elements to it that are really interesting. i.e. making this game work for 6 players, 12 blinks, I think it’s totally doable, and could be a mess of fingers at the end :slight_smile:

Also, great post, just read through your program and will reply with one approach.

The mess of fingers was exactly the kind of interaction I was hoping for :slight_smile: I do appreciate any help you can give me, I’m only really able to work on this during my lunch breaks at work, so progress has been a bit slow. Thanks!

1 Like

@jbobrow Sorry to do a bit of gravedigging with this thread, but I’m finally getting some other projects off my plate and am excited to get back into my beta dev kit. Did you have any suggestions for the code I was trying to implement here?

Sorry, I know its been a while, but if you did have any suggestions I’d be happy to hear them.

1 Like

Great that you’re coming back to this. I’d love to take a swing at this but also gonna call out @danking222 and @bigjosh in case they have some quick tips, I imagine @martin might also enjoy taking a look :smiley:

One small thing with the latest version of the Blinks SDK, you’ll want to use random() instead of rand()

Right, thanks for pointing that out, I need to do some updating.

Also I apologize in advance to anyone who looks at my code :neutral_face: If anything needs further explanation let me know.

Was looking through it last night, just wanted to make sure I understood the desired game mechanic.

A cluster of n Blinks gives a ready/set/go and then reveals a field of color, only 2 Blinks are of the same color and it is your job as a player to hit the 2 Blinks with your color first. This is a score for your team and then this repeats until a desired winning score…

Just want to make sure i really understand the game before I start suggesting methods to tighten up the code.

Yes, you’ve got the basic idea. The code I’ve implemented so far though was simply to get the 4 tiles randomized evenly, so two tiles should be red and two blue. In the code on github, theres a function that resets the field on a double press, but other than that none of the scoring or win conditions have been added.

EDIT: Also my apologies, I have yet to update the firmware on my own dev kit and in the code, so some deprecated functions are probably still in place (like rand()),