Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #define checkWin() Serial.println("MAAK DEZE FUNCTIE")
  2. #define MAX_TIME 100
  3. #define OFF_LIMIT 50
  4.  
  5. struct button{
  6.  int pin;
  7.  bool status = false;
  8.  int counter = 0;
  9. };
  10. button buttons[4];
  11. char veld[4][4] = {
  12.   0
  13. };
  14.  
  15. bool beurt = 0;
  16.  
  17. void onButtonChange(int idx,bool onoff){
  18.   if(onoff&&!veld[idx][0]){
  19.     veld[idx][0]=beurt?2:1;
  20.     beurt=!beurt;
  21.    
  22.    }
  23.  
  24. //Serial.println("BUTTON: "+ String(idx) + " turned "+ (onoff?"on":"off"));
  25. }
  26. void updateButtons(){
  27.   for(int i = 0;i<4;i++){
  28.     bool status = !digitalRead(buttons[i].pin);
  29.     //Serial.print(String(status));
  30.     buttons[i].counter += status?1:-1;
  31.     if(buttons[i].counter<0){buttons[i].counter=0;}
  32.     if(buttons[i].counter>MAX_TIME){buttons[i].counter=MAX_TIME;}
  33.     if(buttons[i].counter<OFF_LIMIT){
  34.       if(buttons[i].status){
  35.         buttons[i].status=false;
  36.         onButtonChange(i,false);
  37.       }
  38.     }
  39.     if(buttons[i].counter==MAX_TIME){
  40.       if(!buttons[i].status){
  41.        buttons[i].status=true;
  42.        onButtonChange(i,true);
  43.      }
  44.    }
  45.  }
  46.   //Serial.println();
  47. }
  48.  
  49. void setup() {
  50.   veld[0][0]=1;
  51.   veld[3][0]=2;
  52.   veld[3][3]=1;
  53.   Serial.begin(9600);
  54.   int pins[] ={9,8,7,6};
  55.   for(int i = 0; i<4;i++){
  56.    buttons[i].pin=pins[i];
  57.    pinMode(pins[i], INPUT_PULLUP);
  58.   }
  59.   bord();
  60. }
  61.  
  62.  
  63. long counter = 0;
  64.  
  65. void loop() {
  66.   counter++;
  67.   updateButtons();
  68.   if(counter%500==0){
  69.     Serial.print("\n\n\n\n\n");
  70.     bord();
  71.  
  72.  
  73.   if(!gravity()){
  74.     //checkWin();
  75.   }
  76.   }
  77. }
  78.  
  79.  
  80.  
  81. void bord() {
  82.     for (int i = 0; i < 4; i++) {
  83.       for (int j = 0; j < 4; j++) {
  84.         switch (veld[j][i]) {
  85.           case 0:
  86.             Serial.print('-');
  87.             break;
  88.           case 1:
  89.             Serial.print('X');
  90.             break;
  91.           case 2:
  92.             Serial.print('O');
  93.             break;
  94.         }
  95.  
  96.       }
  97.       Serial.println();
  98.     }
  99.   }
  100.  
  101.  
  102. void checkwin(){
  103.   //if (veld[0][3] = 1
  104. }
  105.  
  106. bool gravity(){
  107.   bool moved = false;
  108.   for(int x = 0;x<4;x++){
  109.     for(int y = 2;y>=0 /* De onderste rij kan nooit naarbenden, dus hoeven we dat ook niet te checken*/;y--){
  110.       if(veld[x][y]&&!veld[x][y+1]){
  111.         char current = veld[x][y];
  112.         veld[x][y]=0;
  113.         veld[x][y+1]=current;
  114.         moved=true;
  115.       }
  116.     }
  117.   }
  118.   return moved;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement