//Include required libraries
// Traffic light follows N, E, S, W, 
// If a lane is lighted in this state or previous state, it cannot be next state
// If a lane has less than 5 cars, let it go first
// Check opposite car?

#include <stdio.h>
int n, s, e, w;
int on, os, oe, ow;
char currentstate = 'n';
char previousstate = 'n';
int canshort = 0;
int counter = 0;
int  shorted = 4;

void setup()
{
  Serial.begin(115200);
  n = random(0,20);
  s = random(0,20);
  e = random(0,20);
  w = random(0,20);
  printf("%i,%i,%i,%i\n",n,e,s,w);
}

void loop()
{
  on = os = ow = oe = 0;
  currentstate = decide_state(n,e,w,s,currentstate, previousstate, on, os, oe, ow);
  printf("\ncanshort = %i, counter = %i, shorted = %i \n", canshort, counter, shorted);
  printf("current state is %c, previous state is %c\n", currentstate, previousstate);
  switch (currentstate)
  {
    case 'n':
      n = random(0,6);
      e += random(0,6);
      w += random(0,6);
      s += random(0,6);
      break;
    
    case 's':
      s = random(0,6);
      n += random(0,6);
      e += random(0,6);
      w += random(0,6);
      break;
    
    case 'e':
      e = random(0,6);
      n += random(0,6);
      s += random(0,6);
      w += random(0,6);
      break;
    
    case 'w':
      w = random(0,6);
      n += random(0,6);
      s += random(0,6);
      e += random(0,6);
      break;
    
  }
  printf("n = %i, e = %i, s = %i, w = %i\n",n,e,s,w);

  delay(1000);
}


char decide_state(int n, int e, int w, int s, char cs, char ps, int on, int os, int oe, int ow)
{
  int temp;

  if (canshort >= 3)
  {
    if (n<5 && n > 0 && cs != 'n' && ps != 'n' && on == 0)
    {
      canshort = 0;
      shorted = 0;
      previousstate = cs;
      return 'n';
    }
    if (s<5 && s > 0 && cs != 's' && ps != 's' && os == 0)
    {
      canshort = 0;
      shorted = 2;
      previousstate = cs;
      return 's';
    }
    if (e<5 && e > 0 && cs != 'e' && ps != 'e' && oe == 0)
    {
      canshort = 0;
      shorted = 1;
      previousstate = cs;
      return 'e';
    }
    if (w<5 && w > 0 && cs != 'w'  && ps != 'w' && ow == 0)
    {
      canshort = 0;
      shorted = 3;
      previousstate = cs;
      return 'w';
    }
  }
  
  canshort ++;
  counter ++;
  temp = counter % 4;
  if (temp == shorted)
  {
    shorted = 4;
    temp++;
    counter++;
    if (temp == 4)
    {
      temp -= 4;
    }
  } 

  while(1)
  {
    switch (temp)
    {
      case 0:
        if (on)
        {
          temp ++;  
        }
        else
        {
          previousstate = cs;
          return 'n';
        }
        
      case 1:
        if (oe)
        {
          temp ++;  
        }
        else
        {
          previousstate = cs;
          return 'e';
        }

      case 2:
        if (os)
        {
          temp ++;  
        }
        else
        {
          previousstate = cs;
          return 's';
        }

      case 3:
        if (ow)
        {
          temp ++;  
        }
        else
        {
          previousstate = cs;
          return 'w';
        }
    }

  }
}