 
   // TODO(bhomberg): close all solenoids (??)
 }
-
-
-


int have_won = false;

// this runs once when the program starts
void setup() 
{

  }
}

// True if positions pos1 and pos2 are connected on gear_num.
boolean positionsConnected(int gear_num, int pos1, int pos2)
{
  // We just do things the simplistic way: do a bunch of casework and search
  // through the array one at a time.
  if (gear_num == 0)
  {
    for (int i=0; i<6; i++)
    {
      if (gear_0_connections[i][0] == pos1
          && gear_0_connections[i][1] == pos2
          || gear_0_connections[i][0] == pos2
          && gear_0_connections[i][1] == pos1)
      {
        return true;
      }
    }
  }
  else if (gear_num == 1)
  {
    for (int i=0; i<12; i++)
    {
      if (gear_1_connections[i][0] == pos1
          && gear_1_connections[i][1] == pos2
          || gear_1_connections[i][0] == pos2
          && gear_1_connections[i][1] == pos1)
      {
        return true;
      }
    }
  }
  else  // (gear_num == 2)
  {
    for (int i=0; i<9; i++)
    {
      if (gear_2_connections[i][0] == pos1
          && gear_2_connections[i][1] == pos2
          || gear_2_connections[i][0] == pos2
          && gear_2_connections[i][1] == pos1)
      {
        return true;
      }
    }
  }
  return false;
}

// Check if we have a connection.  rotation is the amount we have rotated,
// relative to 0 being the start position.  (That is, the position of the
// selector gear.)  Call only if all gears are lodged.
boolean haveConnection(int rotation, int gear_types[],
                       int gear_orientations[])
{
  // TODO(benkraft): Make sure these match the actual directions of rotation in
  // the final mechanism.
  int rotation_0 = (rotation + gear_orientations[0]) % 12;
  int rotation_1 = (12 - rotation + gear_orientations[1]) % 12;
  int rotation_2 = (rotation + gear_orientations[2]) % 12;
  // These are indexed by the ordering on the left side.
  boolean first_connections[3] = {false, false, false};
  boolean second_connections[3] = {false, false, false};
  for (int i = 11; i<14; i++)
  {
    for (int j = 2; j<5; j++)
    {
      if (positionsConnected(
            gear_types[0], (rotation_0 + i) % 12, (rotation_0 + j) % 12))
      {
        first_connections[j - 2] = true;
      }
    }
  }
  for (int i = 8; i<11; i++)
  {
    for (int j = 2; j<5; j++)
    {
      if (first_connections[(12 - i) % 3] && positionsConnected(
            gear_types[0], (rotation_0 + i) % 12, (rotation_0 + j) % 12))
      {
        second_connections[j - 2] = true;
      }
    }
  }
  for (int i = 8; i<11; i++)
  {
    for (int j = 0; j<2; j++)
    {
      if (second_connections[i % 3] && positionsConnected(
            gear_types[0], (rotation_0 + i) % 12, (rotation_0 + j) % 12))
      {
        return true;
      }
    }
  }
  return false;
}

