Programming Help - Controlling number of random flags being sent


#1

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.


#2

: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.


#3

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!