#include <LiquidCrystal_I2C.h>
#include <time.h>


LiquidCrystal_I2C lcd(0x27,20,4); //initializing LCD (address, width, height)

bool condition = true;

void setup() {


  int buttonPin = 2;
  pinMode(buttonPin, INPUT);

  int dino_x=1;




  lcd.init();
  lcd.backlight();
  uint8_t tree[8] = {
      0b00000,
      0b01000,
      0b01000,
      0b01000,
      0b01100,
      0b01100,
      0b11100,
      0b11100,

    };
  lcd.createChar(3,tree);

  uint8_t chare[8] = {
    0b00000,
    0b01010,
    0b11111,
    0b11111,
    0b01110,
    0b00100,
    0b00000,
    0b00000,

    };
  lcd.createChar(4,chare);

  };

void move_tree(int i, int s){ 
 
  lcd.setCursor(i,1);
  lcd.write(3);
  lcd.setCursor(s, 1);
  lcd.write(3);
  delay(150);
  lcd.setCursor(i,1);
  lcd.print(" ");  
  lcd.setCursor(s,1);
  lcd.print(" "); 

}

void move_dino(int BS, int x){

  if(BS ==1){
    lcd.setCursor(x, 1);
    lcd.print(" ");
    lcd.setCursor(x, 0);;
    lcd.write(4);
  }

  else if(BS ==0){
    lcd.setCursor(x, 0);
    lcd.print(" ");
    lcd.setCursor(x, 1);
    lcd.write(4);
  }

}

bool end_game(bool BS, int i, int s){

  if ((BS == 0)&&((i == 1)||(s == 1))){

    return false;

  }

  else{
    return true;
  }


}

int count_digits(int score){
  int digits;


  do{

    score = score /10;
    digits++;

  }

  while(score!=0);

  return digits;

}

void count_score(int score){

  int digs = count_digits(score);
  lcd.setCursor((16 - digs), 0);
  lcd.print(score);

}


void game(){

  int i = 16 ;
  int s = 24;
  bool BS;
  int x = 1;
  int points = 0;
  int digs = 0;



  do{

    int buttonPin = 2;
    int buttonState = digitalRead(buttonPin);
    BS = buttonState;

    digs = count_digits(points);
    lcd.setCursor(14,0);
    lcd.print(digs);

    move_dino(buttonState, x);
    move_tree(i,s);
    

    i--;
    s--;

    if (i==0){
      i = random(16,20);
      points++;
    }

    else if(s==0){
      s = random(20,24);
      points++;

    }

  

  }

  while(end_game(BS, i,s));


}




void loop() {

  if(condition){
    game();
  }

  condition = false;
  lcd.setCursor(1,0);
  lcd.print(" ");
  lcd.setCursor(6,0);
  lcd.print("GAME");
  lcd.setCursor(4,1);
  lcd.print("ENDED");
  

}


// THINGS TO DO YET: 

    // - Write the score function.
    // - Write the function for the end game condition (aka when the tree and the dino collidde)
    // - write the score function 
    // - Write the move_dino function 
    // - Write the game function that calls the move_dino ands the move_tree function
    // - Write the function for the max time the dino can stay in the air before it 
    //   starts falling down.
    // - Make the move dino function buitton-press-time sensitive
    // - correct the functions for two trees coming simullateneosuly so that they dont come
    //   together.