#include <io.h>
#include <system.h>
#include <stdio.h>
#include <math.h>

#define IOWR_VGA_DATA(base, offset, data) \
        IOWR_32DIRECT(base, (offset) * 32, data) 
int main()
{
   int i;
   int score_ones = 0;
   int score_tens = 0;
   int score_hundy = 0;
   int temp = 0;
   int sOtemp = 0;
   int sTtemp = 0;
   int sHtemp = 0;
   int sdelay = 0;
   
   
   int flagon = 0;
   int flagdataout = 0;
   int flag_row = 0;
   int flag_col = 0;
   
   int fuel_amount = 158;
   int fuel_sendout = 0;
   int flag_address = 0;
   int pstatus = 0;
   int estatus[3]={0,0,0}; 
   int pstatus_data = 0;
   int lives=3;
   int flag_collected=0;
   int temp_s=0;
   int flag1=1;
   int flag2=1;
   int flag3=1;
   int flag4=1;
   int flag5=1;
   int flag6=1;
   int flag7=1;
   int flag8=1;
   int flag9=1;
   int flag10=1;
   printf("Hello Michael\n");
  
  
    
  int x = 375, y =32;
  
  int ex[3]={0,385, 765};
  int ey[3]={964,964,964};

  int vx = 0, vy= 0;
  int evx[3] = {0,0,0};
  int evy[3] = {0,0,0};
  
  int delay=0;
  int delay_smoke=0;
  
  int w, ew1, ew2, ew3;
  unsigned char code;
  unsigned char previous;
  int change=0;
  int wallhit =0;
  
  int convert_to_tile_r;
  int convert_to_tile_c;
  int convert_to_tile_r_e[3];
  int convert_to_tile_c_e[3];
  
  
  int player_x;
  int player_y;
  int enemy_x[3];
  int enemy_y[3];
  
  int rem_up =1;
  int rem_down =1;
  int rem_right =1;
  int rem_left =1;
  
  int rem_up_e[3] = {1,1,1};
  int rem_down_e[3] ={1,1,1};
  int rem_right_e[3] ={1,1,1};
  int rem_left_e[3] ={1,1,1};
  
  ////////////////////
  
   int smoke_1_on=0;
   int smoke_2_on=0;
   int smoke_3_on=0;
   int smoke_1_timer=0;
   int smoke_2_timer=0;
   int smoke_3_timer=0;
   int smoke_1_xposition=0;
   int smoke_2_xposition=0;
   int smoke_3_xposition=0;
   int smoke_1_yposition=0;
   int smoke_2_yposition=0;
   int smoke_3_yposition=0;
  // int flagdataout;
  // brick : 1111 (0xf)
  // road  : 0000 (0x0)
  //                              0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44
  //                              |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |  
  int control_array[47][47] = {0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 0
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 1
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 2
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 3
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 4
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 5
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 6
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 7
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 8
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 9
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 10
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 11
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 12
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 13
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 14
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 15
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 16
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 17
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 18
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 19
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 20
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 21
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 22
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 23
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 24 
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 25
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 26
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 27
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,// 28
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 29
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 30
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 31
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 32
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 33
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 34
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 35
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0x0, 0xf, 0xf, 0x0, 0xf, 0xf, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 36
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 37
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 38
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 39
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 40
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 41
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 42
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, // 43
                               0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf}; // 44
      code = IORD_8DIRECT(PS2_KEYBOARD_BASE,4);   
      previous = code;
 
 int flag1_pos_x=12;
 int flag1_pos_y=8;
 
 int flag2_pos_x=8;
 int flag2_pos_y=8;
 
 int flag3_pos_x=10;
 int flag3_pos_y=10;
 
 int flag4_pos_x=10;
 int flag4_pos_y=8;
 
 int flag5_pos_x=35;
 int flag5_pos_y=14;
 
 int flag6_pos_x=16;
 int flag6_pos_y=35;
 
 int flag7_pos_x=37;
 int flag7_pos_y=24;
 
 int flag8_pos_x=26;
 int flag8_pos_y=29;
 
 int flag9_pos_x=37;
 int flag9_pos_y=30;
 
 int flag10_pos_x=31;
 int flag10_pos_y=14;
 
    flagdataout = (1 << 25) + (1 << 24) + (flag1_pos_y << 12) + flag1_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (2 << 25) + (1 << 24) + (flag2_pos_y << 12) + flag2_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (3 << 25) + (1 << 24) + (flag3_pos_y << 12) + flag3_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (4 << 25) + (1 << 24) + (flag4_pos_y << 12) + flag4_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (5 << 25) + (1 << 24) + (flag5_pos_y << 12) + flag5_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (6 << 25) + (1 << 24) + (flag6_pos_y << 12) + flag6_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (7 << 25) + (1 << 24) + (flag7_pos_y << 12) + flag7_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (8 << 25) + (1 << 24) + (flag8_pos_y << 12) + flag8_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (9 << 25) + (1 << 24) + (flag9_pos_y << 12) + flag9_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (10 << 25) + (1 << 24) + (flag10_pos_y << 12) + flag10_pos_x;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
   /*
   flagdataout = (1 << 25) + (1 << 24) + (8 << 12) + 12;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (2 << 25) + (1 << 24) + (8 << 12) + 8;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (3 << 25) + (1 << 24) + (10 << 12) + 10;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (4 << 25) + (1 << 24) + (8 << 12) + 10;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (5 << 25) + (1 << 24) + (14 << 12) + 35;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (6 << 25) + (1 << 24) + (35 << 12) + 16;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (7 << 25) + (1 << 24) + (24 << 12) + 37;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (8 << 25) + (1 << 24) + (29 << 12) + 26;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (9 << 25) + (1 << 24) + (30 << 12) + 37;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
  flagdataout = (10 << 25) + (1 << 24) + (14 << 12) + 31;
 IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
*/

   for (;;)
   
    {
//  int i=0;
//  for (i=0;i<5;i++)
//  {
//  flagdataout = (i << 25) + (1 << 24) + (7 << 12) + i+10;
//       //flag # (0-9)
//       //flag ON or OFF
//       //Flag Row #
//       //Flag Column #
// IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
//  }
 
 
       // LEFT  ARROW: 224 107 224 240 107
      // RIGHT ARROW: 224 116 224 240 116
      // UP    ARROW: 224 117 224 240 117
      // DOWN  ARROW: 224 114 224 240 114
           
         code = IORD_8DIRECT(PS2_KEYBOARD_BASE,4);
         
         if(code != previous){
            wallhit=0;
            previous = code;
         }
         
            int smoke_timer=0;
            smoke_timer++;
            int smokedataout = 0;
            int j = 0;
//            int smokex=7;
//            smokedataout = (1 << 25) + (1 << 24) + (convert_to_tile_r << 12) + convert_to_tile_c;
//             IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);
//            if (smoke_timer==500)
//             {
//             smoke_timer=0;
//             smokedataout = (1 << 25) + (0 << 24) + (convert_to_tile_r << 12) + convert_to_tile_c;
//             IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);
//             }
             
//            for (j = 1; j < 3; j++)
//            {
//       
//            smokedataout = (j+1 << 25) + (1 << 24) + (8 << 12) + smokex;
//              
//            IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);
//            printf("%d\n", smokedataout);
//            smokex = smokex + 2;
//            
//   }
       
            
         while(!IORD_8DIRECT(PS2_KEYBOARD_BASE, 0))
         {
//            
//            if (sdelay == 100000){
//                
//                //TEST FOR SCORE
//                if (score_ones < 9)
//                    score_ones++;
//                else
//                    score_ones = 0;
//                   
//                if (score_tens < 9)
//                    score_tens++;
//                else
//                    score_tens = 0;
//                  
//                if (score_hundy < 9)
//                    score_hundy++;
//                else
//                    score_hundy = 0;
//            }
//                
                
                //TEST FOR FUEL GAUGE
                
            if (delay_smoke == 10000){
                if (fuel_amount > 0)
                    fuel_amount--;
                else              
                    fuel_amount = 157;
                    
                delay_smoke=0;    
                IOWR_32DIRECT(LEDS_BASE, 8, fuel_amount);
                
         //
               printf("%d\n", fuel_amount);
               }
                

//                //TEST FOR FLAGS AND SMOKE
//                /*if (flagon == 0)
//                    flagon = 1;
//                else
//                    flagon = 0;*/
//                      
//                
//  
//               // flag_row = 7;
//                //flag_col = 7;

///////////////////// score///////
////////////////// saunder ////
         //       sOtemp = (score_hundy << 8) + (score_tens << 4) + score_ones;
                
                if (temp_s!=flag_collected)
                {
                    temp_s=flag_collected;
                    score_tens=score_tens+flag_collected;
                    if (score_tens>=10)
                    {
                        score_tens=score_tens-10;
                        score_hundy++;
                    }
                
                sOtemp = (score_hundy << 8) + (score_tens << 4) + 0;
                IOWR_32DIRECT(LEDS_BASE, 12, sOtemp); 
                }
                if (lives > 0)
                    lives--;
                else
                    lives=3;
                
 //               IOWR_32DIRECT(LEDS_BASE, 36, lives);
//            }
//            else
//                sdelay++;
              
            if (delay == 600){
                
                
                convert_to_tile_c = (player_x)/32;
                convert_to_tile_r = (player_y)/32;
                player_x = x + 240; // /32 = 7.5 -> round to eight
                player_y = y + 240;
                rem_up = (player_y - 12)%32;
                rem_down = 32 - (player_y + 12)%32;
                rem_right = 32 - (player_x + 12)%32;
                rem_left = (player_x - 12)%32;
                
                if(rem_right ==32)
                    rem_right=0;
                
                if(rem_down==32)
                    rem_down =0;
                 
                 
                    
                for(i=0; i<3;i++){
                    
                    enemy_x[i] = ex[i]+240;
                    enemy_y[i] = ey[i]+240;
                    
                    convert_to_tile_c_e[i] = (enemy_x[i])/32;
                    convert_to_tile_r_e[i] = (enemy_y[i])/32;
                    
                    rem_up_e[i] = (enemy_y[i] - 12)%32;
                    rem_down_e[i] = 32 - (enemy_y[i] + 12)%32;
                    rem_right_e[i] = 32 - (enemy_x[i] + 12)%32;
                    rem_left_e[i] = (enemy_x[i] - 12)%32;
                    
                    if(rem_right_e[i] ==32)
                        rem_right_e[i]=0;
                
                    if(rem_down_e[i]==32)
                        rem_down_e[i] =0;
                
                    if((ex[i]-x)*(ex[i]-x) > (ey[i]-y)*(ey[i]-y)){
                        if(ex[i]-x > 0)
                            evx[i]=-1;
                        else
                            evx[i]=1;    
                    }else{
                        if(ey[i]-y > 0)
                            evy[i]=-1;
                        else
                            evy[i]=1; 
                    }
                }    
                    
                    
                //printf("%d %d   , %d %d  , up:%d  down:%d  right:%d  left:%d,  wallhit: %d\n",convert_to_tile_c, convert_to_tile_r,player_x,player_y, rem_up, rem_down,rem_right, rem_left, wallhit);
                
         //       printf("%d %d   , %d %d  , up:%d  down:%d  right:%d  left:%d\n",convert_to_tile_c_e[0], convert_to_tile_r_e[0], enemy_x[0], enemy_y[0], rem_up_e[0], rem_down_e[0], rem_right_e[0], rem_left_e[0]);
             
                
                /////////////////////////////////////////////////////////////////////////////
                //////////////////////////////For Player///////////////////////////////////
                /////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////// left
                if (vx == - 1) 
                {
                    if(rem_up <=8 && rem_down <=8){
                        if ( x > 0 && (rem_left > 0 || control_array[convert_to_tile_r][convert_to_tile_c-1] != 0xf)){
                            x += vx;
                            pstatus = 1;
                        }
                        else{
                            vx = 0;
                            vy = 1;
                            pstatus = 2;
                            wallhit =1;
                        }
                    }
                    
                    else{
                        if(rem_down > rem_up){
                            if ( x > 0 && (rem_left > 0 || (control_array[convert_to_tile_r][convert_to_tile_c-1] != 0xf  && control_array[convert_to_tile_r+1][convert_to_tile_c-1] != 0xf))){
                                x += vx;
                                pstatus = 1;
                            }
                            else{
                                vx = 0;
                                vy = 1;
                                pstatus = 2;
                                wallhit =1;
                            }
                        }
                        else{
                            if ( x > 0 && (rem_left > 0 || (control_array[convert_to_tile_r][convert_to_tile_c-1] != 0xf  && control_array[convert_to_tile_r-1][convert_to_tile_c-1] != 0xf))){
                                x += vx;
                                pstatus = 1;
                            }
                            else{
                                vx = 0;
                                vy = 1;
                                pstatus = 2;
                                wallhit =1;
                           }
                        }
                
                    }
                    
                    
                }
                
                ///////////////////////////////// right
                if (vx == 1){  
                    if(rem_up <=8 && rem_down <=8){
                        if (rem_right > 0 || control_array[convert_to_tile_r][convert_to_tile_c+1] != 0xf){
                            x += vx;
                            pstatus = 0;
                        }
                        else{
                            vx = 0;
                            vy = -1;
                            pstatus = 3;
                            wallhit =1;
                       }
                    }
                    else{
                        if(rem_down > rem_up){
                            if ( rem_right > 0 || (control_array[convert_to_tile_r][convert_to_tile_c+1] != 0xf  && control_array[convert_to_tile_r+1][convert_to_tile_c+1] != 0xf)){
                                x += vx;
                                pstatus = 0;
                            }
                            else{
                                vx = 0;
                                vy =-1;
                                pstatus = 3;
                                wallhit =1;
                            }    
                        }
                        else{
                            if ( rem_right > 0 || (control_array[convert_to_tile_r][convert_to_tile_c+1] != 0xf  && control_array[convert_to_tile_r-1][convert_to_tile_c+1] != 0xf)){
                                x += vx;
                                pstatus = 0;
                            }
                            else{
                                vx = 0;
                                vy =-1;
                                pstatus = 3;
                                wallhit =1;
                            }
                        }
                
                    }
                    
                }
                
                /////////////////////////////////////////////UP
                if (vy == -1) 
                {
                    if(rem_right <=8 && rem_left <=8 ){
                        if ( y > 0 &&(rem_up > 0 || control_array[convert_to_tile_r-1][convert_to_tile_c] != 0xf)){
                            y += vy;
                            pstatus =3;
                        }
                        else{////////////
                            vy = 0;
                            vx = -1;
                            pstatus = 1;
                            wallhit =1;
                        }    
                    }
                    else{
                        if(rem_right > rem_left){
                            if ( y > 0 && (rem_up > 0 || (control_array[convert_to_tile_r-1][convert_to_tile_c] != 0xf  && control_array[convert_to_tile_r-1][convert_to_tile_c+1] != 0xf))){
                                y += vy;
                                pstatus = 3;
                            }
                            else{
                                vy = 0;
                                vx = -1;
                                pstatus = 1;
                                wallhit =1;
                            }
                        }
                        else{
                            if ( y > 0 && (rem_up > 0 || (control_array[convert_to_tile_r-1][convert_to_tile_c] != 0xf  && control_array[convert_to_tile_r-1][convert_to_tile_c-1] != 0xf))){
                                y += vy;
                                pstatus = 3 ;
                            }
                            else{
                                vy = 0;
                                vx = -1;
                                pstatus = 1;
                                wallhit =1;
                            }
                        }
                            
                    }
                }
                
                /////////////////////////////////////////////DOWN
                if (vy == 1){ 
                    if(rem_right <=8 && rem_left<=8){
                        if ( rem_down > 0 || control_array[convert_to_tile_r+1][convert_to_tile_c] != 0xf){
                            y += vy;
                            pstatus = 2;
                        }
                        else{
                                vy = 0;
                                vx = 1;
                                wallhit =1;
                                pstatus = 0;
                            }
                   }
                   else{
                        if(rem_right > rem_left){
                            if ( rem_down > 0 || (control_array[convert_to_tile_r+1][convert_to_tile_c] != 0xf  && control_array[convert_to_tile_r+1][convert_to_tile_c+1] != 0xf)){
                                y += vy;
                                pstatus = 2;
                            }
                            else{
                                vy = 0;
                                vx = 1;
                                pstatus = 0;
                                wallhit =1;
                            }
                        }
                        else{
                            if ( rem_down > 0 || (control_array[convert_to_tile_r+1][convert_to_tile_c] != 0xf  && control_array[convert_to_tile_r+1][convert_to_tile_c-1] != 0xf)){
                                y += vy;
                                pstatus = 2;
                            }
                            else{
                                vy = 0;
                                vx = 1;
                                pstatus = 0;
                                wallhit =1;
                            }
                        }
                            
                    }               
                }
                
                /////////////////////////////////////////////////////////////////////////////
                //////////////////////////////For Enemey///////////////////////////////////
                /////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////// left
                
                for(i=0; i<3; i++){
                if (evx[i] == - 1) 
                {
                    if(rem_up_e[i] <=8 && rem_down_e[i] <=8){
                        if ( ex[i] > 0 && (rem_left_e[i] > 0 || control_array[convert_to_tile_r_e[i]][convert_to_tile_c_e[i]-1] != 0xf)){
                            ex[i] += evx[i];
                            estatus[i] = 1;
                        }
                        else{
                            evx[i] = 0;
                            evy[i] = 1;
                            estatus[i] = 2;
                        }
                    }
                    
                    else{
                        if(rem_down_e[i] > rem_up_e[i]){
                            if ( ex[i] > 0 && (rem_left_e[i] > 0 || (control_array[convert_to_tile_r_e[i]][convert_to_tile_c_e[i]-1] != 0xf  && control_array[convert_to_tile_r_e[i]+1][convert_to_tile_c_e[i]-1] != 0xf))){
                                ex[i] += evx[i];
                                estatus[i] = 1;
                            }
                            else{
                                evx[i] = 0;
                                evy[i] = 1;
                                estatus[i] = 2;
                            }
                        }
                        else{
                            if ( ex[i] > 0 && (rem_left_e[i] > 0 || (control_array[convert_to_tile_r_e[i]][convert_to_tile_c_e[i]-1] != 0xf  && control_array[convert_to_tile_r_e[i]-1][convert_to_tile_c_e[i]-1] != 0xf))){
                                ex[i] += evx[i];
                                estatus[i] = 1;
                            }
                            else{
                                evx[i] = 0;
                                evy[i] = 1;
                                estatus[i] = 2;
                           }
                        }
                
                    }
                    
                    
                }
                
                ///////////////////////////////// right
                if (evx[i] == 1){  
                    if(rem_up_e[i] <=8 && rem_down_e[i] <=8){
                        if (rem_right_e[i] > 0 || control_array[convert_to_tile_r_e[i]][convert_to_tile_c_e[i]+1] != 0xf){
                            ex[i] += evx[i];
                            estatus[i] = 0;
                        }
                        else{
                            evx[i] = 0;
                            evy[i] = -1;
                            estatus[i] = 3;
                            //wallhit =1;
                       }
                    }
                    else{
                        if(rem_down_e[i] > rem_up_e[i]){
                            if ( rem_right_e[i] > 0 || (control_array[convert_to_tile_r_e[i]][convert_to_tile_c_e[i]+1] != 0xf  && control_array[convert_to_tile_r_e[i]+1][convert_to_tile_c_e[i]+1] != 0xf)){
                                ex[i] += evx[i];
                                estatus[i] = 0;
                            }
                            else{
                                evx[i] = 0;
                                evy[i] =-1;
                                estatus[i] = 3;
                                //wallhit =1;
                            }    
                        }
                        else{
                            if ( rem_right_e[i] > 0 || (control_array[convert_to_tile_r_e[i]][convert_to_tile_c_e[i]+1] != 0xf  && control_array[convert_to_tile_r_e[i]-1][convert_to_tile_c_e[i]+1] != 0xf)){
                                ex[i] += evx[i];
                                estatus[i] = 0;
                            }
                            else{
                                evx[i] = 0;
                                evy[i] =-1;
                                estatus[i] = 3;
                                //wallhit =1;
                            }
                        }
                
                    }
                    
                }
                
                /////////////////////////////////////////////UP
                if (evy[i] == -1) 
                {
                    if(rem_right_e[i] <=8 && rem_left_e[i] <=8 ){
                        if ( ey[i] > 0 &&(rem_up_e[i] > 0 || control_array[convert_to_tile_r_e[i]-1][convert_to_tile_c_e[i]] != 0xf)){
                            ey[i] += evy[i];
                            estatus[i] = 1;
                        }
                        else{////////////
                            evy[i] = 0;
                            evx[i] = -1;
                            estatus[i] = 1;
                            //wallhit =1;
                        }    
                    }
                    else{
                        if(rem_right_e[i] > rem_left_e[i]){
                            if ( ey[i] > 0 && (rem_up_e[i] > 0 || (control_array[convert_to_tile_r_e[i]-1][convert_to_tile_c_e[i]] != 0xf  && control_array[convert_to_tile_r_e[i]-1][convert_to_tile_c_e[i]+1] != 0xf))){
                                ey[i] += evy[i];
                                estatus[i] = 1;
                            }
                            else{
                                evy[i] = 0;
                                evx[i] = -1;
                                estatus[i] = 1;
                                
                            }
                        }
                        else{
                            if ( ey[i] > 0 && (rem_up_e[i] > 0 || (control_array[convert_to_tile_r_e[i]-1][convert_to_tile_c_e[i]] != 0xf  && control_array[convert_to_tile_r_e[i]-1][convert_to_tile_c_e[i]-1] != 0xf))){
                                ey[i] += evy[i];
                                estatus[i] = 1;
                            }
                            else{
                                evy[i] = 0;
                                evx[i] = -1;
                                estatus[i] = 1;
                                //wallhit =1;
                            }
                        }
                            
                    }
                }
                
                /////////////////////////////////////////////DOWN
                if (evy[i] == 1){ 
                    if(rem_right_e[i] <=8 && rem_left_e[i]<=8){
                        if ( rem_down_e[i] > 0 || control_array[convert_to_tile_r_e[i]+1][convert_to_tile_c_e[i]] != 0xf){
                            ey[i] += evy[i];
                            estatus[i] = 2;
                        }
                        else{
                                evy[i] = 0;
                                evx[i] = 1;
                                //wallhit =1;
                                estatus[i] = 0;
                            }
                   }
                   else{
                        if(rem_right_e[i] > rem_left_e[i]){
                            if ( rem_down_e[i] > 0 || (control_array[convert_to_tile_r_e[i]+1][convert_to_tile_c_e[i]] != 0xf  && control_array[convert_to_tile_r_e[i]+1][convert_to_tile_c_e[i]+1] != 0xf)){
                                ey[i] += evy[i];
                                estatus[i] = 2;
                            }
                            else{
                                evy[i] = 0;
                                evx[i] = 1;
                                estatus[i] = 0;
                                //wallhit =1;
                            }
                        }
                        else{
                            if ( rem_down_e[i] > 0 || (control_array[convert_to_tile_r_e[i]+1][convert_to_tile_c_e[i]] != 0xf  && control_array[convert_to_tile_r_e[i]+1][convert_to_tile_c_e[i]-1] != 0xf)){
                                ey[i] += evy[i];
                                estatus[i] = 2;
                            }
                            else{
                                evy[i] = 0;
                                evx[i] = 1;
                                estatus[i] = 0;
                                //wallhit =1;
                            }
                        }
                            
                    }               
                }
                
                } // for loop (enemy)
                
                //w = y*pow(2,16)+x;
                //first 5 bits for address
                //next 12 bits for x
                //next 12 bits for y
                //bits 24-26 for player stats

                  
                    
                w = (y << 16) + x;
                ew1 = (1 << 27) + (ey[0] << 12) + ex[0];
                ew2 = (1 << 27) + (ey[1] << 12) + ex[1];
                ew3 = (1 << 27) + (ey[2] << 12) + ex[2];
                
                
                //////////// playerr
                
  /////////////////
  
  ////////////   saunder's data...
  
 
                convert_to_tile_c = (player_x)/32;
                convert_to_tile_r = (player_y)/32;
                if (convert_to_tile_r==flag1_pos_y && convert_to_tile_c==flag1_pos_x && flag1==1)
                {   
                    flag1=0;
                    flag_collected++;
                    flagdataout = (1 << 25) + (0 << 24) + (flag1_pos_y << 12) + flag1_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                }
                else if (convert_to_tile_r==8 && convert_to_tile_c==flag2_pos_x && flag2==1)
                { 
                    flag2=0;
                    flag_collected++;
                    flagdataout = (2 << 25) + (0 << 24) + ( flag2_pos_y<< 12) + flag2_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                }
                else if (convert_to_tile_r==flag3_pos_y && convert_to_tile_c==flag3_pos_x && flag3==1)
                { 
                    flag3=0;
                    flag_collected++;
                    flagdataout = (3 << 25) + (0 << 12) + (flag3_pos_y << 12) + flag3_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 }
                 else if (convert_to_tile_r==flag4_pos_y && convert_to_tile_c==flag4_pos_x && flag4==1)
                 { 
                    flag4=0;
                    flag_collected++;
                    flagdataout = (4 << 25) + (0 << 24) + (flag4_pos_y<< 12) + flag4_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 } 
                 else if (convert_to_tile_r==flag5_pos_y && convert_to_tile_c==flag5_pos_x && flag5==1)
                 {         
                    flag5=0; 
                    flag_collected++;     
                    flagdataout = (5 << 25) + (0 << 24) + (flag5_pos_y << 12) + flag5_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 }
                   else if (convert_to_tile_r==flag6_pos_y && convert_to_tile_c==flag6_pos_x && flag6==1)
                 {
                    flag6=0;
                    flag_collected++;
                    flagdataout = (6 << 25) + (0 << 24) + (flag6_pos_y << 12) + flag6_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 }
                   else if (convert_to_tile_r==flag7_pos_y && convert_to_tile_c==flag7_pos_x && flag7==1)
                 {
                    flag7=0;
                    flag_collected++;
                    flagdataout = (7 << 25) + (0 << 24) + (flag7_pos_y << 12) + flag7_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 }
                 else if (convert_to_tile_r==flag8_pos_y  && convert_to_tile_c==flag8_pos_x && flag8==1)
                 {
                    flag8=0;
                    flag_collected++;
                    flagdataout = (8 << 25) + (0 << 24) + (flag8_pos_y << 12) + flag8_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 }
                 else if (convert_to_tile_r==flag9_pos_y && convert_to_tile_c==flag9_pos_x && flag9==1)
                 {
                    flag9=0;
                    flag_collected++;
                    flagdataout = (9 << 25) + (0 << 24) + (flag9_pos_y << 12) + flag9_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 }
                 else if (convert_to_tile_r==flag10_pos_y  && convert_to_tile_c==flag10_pos_x && flag10==1)
                 {
                    flag10=0;
                    flag_collected++;
                    flagdataout = (10 << 25) + (0 << 24) + (flag10_pos_y << 12) + flag10_pos_x;
                    IOWR_32DIRECT(LEDS_BASE, 16, flagdataout);
                 }
 /////// saunder's code
 
                //printf("%x\n", w); 
                IOWR_32DIRECT(LEDS_BASE, 0, w); // player
                IOWR_32DIRECT(LEDS_BASE, 4, pstatus); // player sprite (turning or dead)
                
                //IOWR_32DIRECT(LEDS_BASE, 8, ew1);
                IOWR_32DIRECT(LEDS_BASE, 20, ew1);
                //IOWR_32DIRECT(LEDS_BASE, 12, estatue[0]);
                
                //IOWR_32DIRECT(LEDS_BASE, 16, ew2);
                IOWR_32DIRECT(LEDS_BASE, 24, ew2);
                //IOWR_32DIRECT(LEDS_BASE, 20, estatue[1]);
                
                //IOWR_32DIRECT(LEDS_BASE, 24, ew3);
                IOWR_32DIRECT(LEDS_BASE, 28, ew3);
                //IOWR_32DIRECT(LEDS_BASE, 28, estatue[2]);
                
                //IOWR_32DIRECT(LEDS_BASE, 0, pstatus_data);
                delay = 0;
               
               //printf("code : %c, changed? : %d, \n", code, change);
     
               
               
            }
            else
            {
                if(smoke_1_timer>=1000000 && smoke_1_on==1)
               {
               smokedataout = (1 << 25) + (0 << 24) + ( smoke_1_xposition << 12) + smoke_1_yposition;
                 IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);
                 smoke_1_on=0;
                 smoke_1_timer=0;
               }
               if(smoke_2_timer>=1000000 && smoke_2_on==1)
               {
               smokedataout = (2 << 25) + (0 << 24) + ( smoke_2_xposition << 12) + smoke_2_yposition;
                 IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);
                 smoke_2_on=0;                 
                 smoke_2_timer=0;
               }
               if(smoke_3_timer>=1000000 && smoke_3_on==1)
               {
               smokedataout = (3 << 25) + (0 << 24) + ( smoke_3_xposition << 12) + smoke_3_yposition;
                 IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);                            
                 smoke_3_timer=0;
                 smoke_3_on=0;
               }
                
                smoke_1_timer++;
                smoke_2_timer++;
                smoke_3_timer++;
                delay++;  
                delay_smoke++;
        
            }
        } // while
        
        
        
        if(wallhit ==0){
            switch (code)
            {
            case 107: // LEFT
                    //if( rem_left !=0 ){
                    vx = -1;
                    vy = 0;
                    //}
                    break;
                
            case 116: // RIGHT
                    vx = 1;
                    vy = 0;
                    break;
                
            case 117: // UP
      
   
                    vy = -1;
                    vx =  0;
                    break;
            
                
            case 114:
            
                    vy = 1;
                    vx = 0;
                    break;
            
            case 0x29: //space bar = emergency break for now REMOVE LATER
                
                ///////////////////
                /////////////          saunder's code
                ///////////////////
                //////////////////////   i have no idea why sometimes when i enter a left key..
                /////////////////         it will enter this look
                
                smokedataout = (1+temp << 25) + (1 << 24) + (convert_to_tile_r << 12) + convert_to_tile_c;
                if(temp==0 && smoke_1_on==0 && fuel_amount>30)
                {
                    smoke_1_on=1;
                           
                 smoke_1_timer=0;
                    smoke_1_xposition=convert_to_tile_c;
                    smoke_1_yposition=convert_to_tile_r;     
                temp=1;
                IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);  
                fuel_amount=fuel_amount-15;             
                }
                else if(temp==1 && smoke_2_on==0 && fuel_amount>30) 
                {
                    smoke_2_on=1;
                               
                 smoke_2_timer=0;
                    smoke_2_xposition=convert_to_tile_c;
                    smoke_2_yposition=convert_to_tile_r;   
                temp=2;
                IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);            
                
                fuel_amount=fuel_amount-15;                 
                }    
                else if(temp==2 && smoke_3_on==0&& fuel_amount>30)
                {
                               
                      fuel_amount=fuel_amount-15;             
                       smoke_3_timer=0;
                    smoke_3_on=1;
                    smoke_3_xposition=convert_to_tile_c;
                    smoke_3_yposition=convert_to_tile_r;   
                temp=0;
                IOWR_32DIRECT(LEDS_BASE, 32, smokedataout);
                }
                  
           
                vx=0;
                vy=0;
                break;
            
            default :   
                    break;
        }
            
        
        } // if not wallhit      
        
        
    
  }

  
    
  return 0;
}