Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define checkWin() Serial.println("MAAK DEZE FUNCTIE")
- #define MAX_TIME 100
- #define OFF_LIMIT 50
- struct button{
- int pin;
- bool status = false;
- int counter = 0;
- };
- button buttons[4];
- char veld[4][4] = {
- 0
- };
- bool beurt = 0;
- void onButtonChange(int idx,bool onoff){
- if(onoff&&!veld[idx][0]){
- veld[idx][0]=beurt?2:1;
- beurt=!beurt;
- }
- //Serial.println("BUTTON: "+ String(idx) + " turned "+ (onoff?"on":"off"));
- }
- void updateButtons(){
- for(int i = 0;i<4;i++){
- bool status = !digitalRead(buttons[i].pin);
- //Serial.print(String(status));
- buttons[i].counter += status?1:-1;
- if(buttons[i].counter<0){buttons[i].counter=0;}
- if(buttons[i].counter>MAX_TIME){buttons[i].counter=MAX_TIME;}
- if(buttons[i].counter<OFF_LIMIT){
- if(buttons[i].status){
- buttons[i].status=false;
- onButtonChange(i,false);
- }
- }
- if(buttons[i].counter==MAX_TIME){
- if(!buttons[i].status){
- buttons[i].status=true;
- onButtonChange(i,true);
- }
- }
- }
- //Serial.println();
- }
- void setup() {
- veld[0][0]=1;
- veld[3][0]=2;
- veld[3][3]=1;
- Serial.begin(9600);
- int pins[] ={9,8,7,6};
- for(int i = 0; i<4;i++){
- buttons[i].pin=pins[i];
- pinMode(pins[i], INPUT_PULLUP);
- }
- bord();
- }
- long counter = 0;
- void loop() {
- counter++;
- updateButtons();
- if(counter%500==0){
- Serial.print("\n\n\n\n\n");
- bord();
- if(!gravity()){
- //checkWin();
- }
- }
- }
- void bord() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- switch (veld[j][i]) {
- case 0:
- Serial.print('-');
- break;
- case 1:
- Serial.print('X');
- break;
- case 2:
- Serial.print('O');
- break;
- }
- }
- Serial.println();
- }
- }
- void checkwin(){
- //if (veld[0][3] = 1
- }
- bool gravity(){
- bool moved = false;
- for(int x = 0;x<4;x++){
- for(int y = 2;y>=0 /* De onderste rij kan nooit naarbenden, dus hoeven we dat ook niet te checken*/;y--){
- if(veld[x][y]&&!veld[x][y+1]){
- char current = veld[x][y];
- veld[x][y]=0;
- veld[x][y+1]=current;
- moved=true;
- }
- }
- }
- return moved;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement