asdf

Dependencies:   TextLCD mbed

Revision:
0:855c62a73ec1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 03 14:16:44 2017 +0000
@@ -0,0 +1,354 @@
+#include "mbed.h"
+#include "TextLCD.h"
+
+TextLCD lcd(PTC5, PTC6, PTC7, PTC0, PTC3, PTC4); // rs, e, d4-d7
+PwmOut bocina(PTB1);  //SONIDOS DEL JUEGO
+AnalogIn pot (PTB0);  //SELECCION DE MENU 
+DigitalOut ledazul(PTC12);  
+DigitalOut ledrojo(PTC13);
+DigitalOut ledverde(PTC16);
+DigitalOut ledamarillo(PTC17);
+DigitalIn start(PTD1);  //BOTON DE INICIO
+DigitalIn pushazul(PTD3);
+DigitalIn pushrojo(PTD2);
+DigitalIn pushverde(PTD0);
+DigitalIn pushamarillo(PTD5);
+Serial pc (USBTX, USBRX);
+Timer debounce;   
+
+int valorpot = 0, dificultad = 0, punt_max = 0, punt = 0;
+//VALORES DE DIFICULTAD DEL JUEGO, VALORES DE PUNTUACION.
+float speed = 0, final = 0,  fin_turno = 0;
+//VALORES Y BANDERAS DE JUEGO
+int says [200], player [200], cont_total = 0, move = 0;
+//VALORES Y REGISTROS DE JUGADOR Y COMPUTADORA
+
+int menu_principal(void);
+void logo(void);
+void game_start(void);
+void juego(void);
+void time_select(int dif);
+void simon(float speed);
+void end_game(int punt);
+int get_number(void);
+void comred(void);
+void comblue(void);
+void comgreen(void);
+void comyellow(void);
+
+int main() {             
+    while(1){
+        logo();    
+        juego();
+    }   
+}
+
+/**
+* Esta funcion tiene como proposito reiniciar los valores
+* de las variables del juego a sus valores predeterminados
+* cada vez que es llamada, despues, ejecutar el ciclo del
+* juego.
+*/
+void juego(void){
+    pc.printf("REINICIANDO VALORES DEL JUEGO\n");
+    bocina.write(0.50f);  // 50% duty cycle
+    bocina.period_us(0);    //CANCELAR SONIDO
+    valorpot = 0;
+    dificultad = 0;
+    speed = 0;
+    punt = 0;
+    final = 0;
+    cont_total = 0;    
+    dificultad = menu_principal();  //SELECCIONAR DIFICULTAD
+    if(dificultad == 3){
+        pc.printf("\nDIFICULTAD FACIL");
+    }else if(dificultad == 2){
+        pc.printf("\nDIFICULTAD NORMAL");
+    }else if(dificultad == 1){
+        pc.printf("\nDIFICULTAD DIFICIL");
+    }
+    time_select(dificultad);  //AJUSTAR VELOCIDAD DEL JUEGO
+    game_start();  //PRESENTACION DE VALORES PRE-JUEGO
+    simon(speed);  //INICIAR EL JUEGO
+    end_game(punt);  //PRESENTACION DE VALORES POST-JUEGO
+}
+
+/**
+* Esta funcion es el juego principal, al iniciar cada turno, se genera un nuevo
+* valor aleatorio para la secuencia del juego, este es agregado a un arreglo, despues,
+* la computadora despliega el arreglo ajustandolo a la velocidad de la dificultad 
+* elejida por el jugador. Cuando la seucencia es termnada, el jugador debe introducir 
+* en los botones del juego el mismo patron previamente desplegado, al fallar, termina el 
+* juego, al terminar una secuencia exitosamente, se aumenta su puntuacion.
+*/
+void simon(float speed){
+    int i = 0, prev = 0;
+    while(final != 1){
+        fin_turno = 0;
+        //JUEGA COM
+        lcd.cls();
+        lcd.printf("SIMON SAYS... ");
+        lcd.locate(0,1);    
+        move = rand() %4;  //GENERAR NUEVO MOMIENTO DE JUEGO ALEATORIO
+        move++;
+        pc.printf("\nRANDOM: %d\n",move);
+        if(move == prev){  //VERIFICAR QUE LOS MOVIMIENTOS DE MESCLEN ENTRE ELLOS
+            if(move != 4){
+                move = move++;
+            }else if (move == 4){
+                move = move--;
+            }
+        }
+        prev = move;
+        cont_total++;
+        says[cont_total] = move;
+        pc.printf("\nSIMON SAYS... ");
+        for(i = 1; i <= cont_total; i++){  //DESPLIEGE DE LA SECUENCIA DE JUEGO
+            pc.printf("%d",says[i]);
+            if(says[i] == 1){
+                comred();
+            }else if(says[i] == 2){
+                comblue();
+            }else if(says[i] == 3){
+                comyellow();
+            }else if(says[i] == 4){
+                comgreen();
+            }
+            wait(speed); 
+        }  
+        //JUEGA USR   
+        lcd.cls();
+        lcd.printf("\nPLAYER SAYS...");
+        pc.printf("\nPLAYER SAYS... ");
+        lcd.locate(0,1);     
+        while(fin_turno != 1){
+            for(i = 1; i <= cont_total; i++){  //ANALISIS DE MOVIMIENTOS DE USUARIO
+                    player[i] = get_number();
+                    pc.printf("%d",player[i]);
+                    if(says[i] != player[i]){  //CUANDO EL USUARIO FALLA...
+                        final = 1;
+                        fin_turno = 1;
+                        lcd.cls();
+                        lcd.printf("THAT'S NOT WHAT");
+                        lcd.locate(0,1);
+                        lcd.printf("SIMON SAID!!");
+                        lcd.locate(0,2);
+                        wait(3);
+                        return;                    
+                    }
+            }
+            punt++;  //AUMENTO DE PUNTUACION
+            fin_turno = 1;  //FIN DEL TURNO
+            wait(1);
+        }
+    }
+}
+
+/**
+* Esta funcion, espera a que el usuario presione uno de los botones del juego
+* ensendiendo el respectivo Led de este, haciendolo sonar, y regresando un
+* valor numerico respectivo para la matematica del juego.
+*/
+int get_number(void){
+    while(1){
+        if(debounce.read_ms()>200){    
+            if(pushrojo == 1){
+                bocina.period_us(2500);    //MUY BAJO
+                ledrojo = !ledrojo;
+                wait(0.2);
+                bocina.period_us(0);    //CANCELAR SONIDO
+                ledrojo = !ledrojo;
+                return 1;        
+            }else if(pushazul == 1){
+                bocina.period_us(2000);    //BAJO
+                ledazul = !ledazul;
+                wait(0.2);
+                bocina.period_us(0);    //CANCELAR SONIDO
+                ledazul = !ledazul;
+                return 2;
+            }else if(pushverde == 1){
+                bocina.period_us(1300);    //ALTO
+                ledverde = !ledverde;
+                wait(0.2);
+                bocina.period_us(0);    //CANCELAR SONIDO
+                ledverde = !ledverde;
+                return 4;
+            }else if(pushamarillo == 1){
+                bocina.period_us(1500);    //MEDIO
+                ledamarillo = !ledamarillo;
+                wait(0.2);
+                bocina.period_us(0);    //CANCELAR SONIDO
+                ledamarillo = !ledamarillo;
+                return 3;
+            }
+        }
+    }
+}
+
+/**
+* Ensiende y suena el boton azul por parte de la computadora.
+*/
+void comblue(void){
+    bocina.period_us(2000);    //BAJO
+    ledazul = !ledazul;
+    wait(0.2);
+    bocina.period_us(0);    //CANCELAR SONIDO
+    ledazul = !ledazul;
+}
+
+/**
+* Ensiende y suena el boton rojo por parte de la computadora.
+*/
+void comred(void){
+    bocina.period_us(2500);    //MUY BAJO
+    ledrojo = !ledrojo;
+    wait(0.2);
+    bocina.period_us(0);    //CANCELAR SONIDO
+    ledrojo = !ledrojo;  
+}
+
+/**
+* Ensiende y suena el boton verde por parte de la computadora.
+*/
+void comgreen(void){
+    bocina.period_us(1300);    //ALTO
+    ledverde = !ledverde;
+    wait(0.2);
+    bocina.period_us(0);    //CANCELAR SONIDO
+    ledverde = !ledverde;
+}
+
+/**
+* Ensiende y suena el boton amarillo por parte de la computadora.
+*/
+void comyellow(void){
+    bocina.period_us(1500);    //MEDIO
+    ledamarillo = !ledamarillo;
+    wait(0.2);
+    bocina.period_us(0);    //CANCELAR SONIDO
+    ledamarillo = !ledamarillo;
+}
+
+/**
+* Esta funcion termina el juego y compara la puntuacion obtenida
+* con la mayor almacenada, en caso de ser mayor a esta, se 
+* muestra un mensaje y se cambia la puntuacion mayor.
+*/
+void end_game(int punt){
+    if(punt > punt_max){  //COMPARACION DE PUNTAJES
+        punt_max = punt;
+        lcd.cls();
+        pc.printf("\nNEW HIGH SCORE\nFINAL SCORE : %d\n",punt_max);
+        lcd.printf("NEW HIGH SCORE!!");
+        lcd.locate(0,1);
+        lcd.printf("FINAL SCORE : %d",punt_max);
+        lcd.locate(0,2);
+        wait(3);    
+    }else if(punt <= punt_max){
+        lcd.cls();
+        pc.printf("\nFINAL SCORE : %d\n",punt);
+        lcd.printf("FINAL SCORE : %d",punt);
+        lcd.locate(0,2);
+        wait(3);         
+    }
+    juego();
+}
+
+/**
+* Esta funcion ajuta la velocidad del juego dependiendo
+* del valor de dificultad previamente seleccionado.
+*/
+void time_select(int dif){
+    switch(dif){
+        case 3:
+            speed = 1.0;  //FACIL
+            break;
+        case 2:
+            speed = 0.5;  //NORMAL
+            break;
+        case 1:
+            speed = 0.3;   //DIFICIL
+            break;
+    }
+}
+
+/**
+* Este proceso muestra la presentacion inicial del juego.
+*/
+void logo(void){
+    lcd.cls();
+    lcd.printf("LUIS VIZK");
+    lcd.locate(0,1);
+    lcd.printf("DANI ONTIVEROS");
+    lcd.locate(0,2);
+    wait(3);
+    lcd.cls();
+    lcd.printf("SPOOKY THIEF");
+    lcd.locate(0,1);            
+    lcd.printf("~~SIMON~~");
+    lcd.locate(0,3);            
+    wait(3);                
+}
+
+/**
+* Esta funcion calibra el valor del potenciometro del sistema
+* para elejir entre 3 valores predeterminados de dificultad
+* para el juego, dependiendo del voltaje que se resive.
+* El valor de dificultad seleccionado es guardado cuando el 
+* jugador presiona el boton de inicio.
+*/
+int menu_principal(void){
+    int opcion = 2;
+    debounce.start(); //INICIAR TIMER
+    while(start != 1){
+        lcd.cls();
+        lcd.printf("DIFFICULTY");
+        lcd.locate(0,1); 
+        valorpot = pot * 10;
+        if(valorpot <= 3){
+            lcd.printf("HARD");
+            lcd.locate(0,2); 
+            opcion = 1;
+        }else if(valorpot >= 4  && valorpot <= 9){
+            lcd.printf("NORMAL");
+            lcd.locate(0,2); 
+            opcion = 2;    
+        }else if(valorpot >= 10){
+            lcd.printf("EASY");
+            lcd.locate(0,2);                 
+            opcion = 3;
+        }
+        if(debounce.read_ms()>200){
+            debounce.reset();
+        }
+        wait(0.2);
+    }
+    return opcion;
+}
+
+/**
+* Esta funcion muestra el valor de puntuacion mas grande
+* en el juego y luego pide al usuario precionar el 
+* boton de inicio para comenzar el juego.
+*/
+void game_start(void){
+    lcd.cls();
+    debounce.start();  //INICIAR TIMER
+    lcd.printf("HIGH SCORE : %d",punt_max);
+    lcd.locate(0,1);
+    wait(1);
+    lcd.printf("PRESS START!!");
+    lcd.locate(0,1);
+    if(debounce.read_ms()>200){
+        while(start != 1){
+            wait(0.1);
+        }       
+        debounce.reset();
+        wait(1);
+        lcd.cls();
+        lcd.printf("GAME ON!!",punt_max);
+        pc.printf("\nINICIANDO JUEGO\n");
+        lcd.locate(0,1);
+        wait(1);
+    }    
+}
\ No newline at end of file