Final Trivia Game Code

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Fork of TriviaGame by Michael Balourdas

main.cpp

Committer:
mbalourdas9
Date:
2016-11-04
Revision:
1:1d5055ca2793
Parent:
0:e09703934ff4
Child:
3:9842b3f1d809

File content as of revision 1:1d5055ca2793:

#include "mbed.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "wave_player.h"
#include "mpr121.h"
#include <vector>

AnalogOut DACout(p18); // used to play sound on speaker
//wave player plays a *.wav file to D/A and a PWM
wave_player waver(&DACout);
InterruptIn interrupt(p26);
I2C i2c(p9, p10);
Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);

uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin;
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
int Score;
int HighScore = 0;
int question = 0;
bool answer = 1;
int index = 0;
bool check = 0;
int value = 0;
std::vector<int> v;


int main()
{
    srand(time(NULL));
    while (true){
        answer = 1;
        for (int i=0; i<10; i++){
            v.push_back(i+1);
        }
        check = 0;
        question = 0;
        uLCD.baudrate(3000000);
        uLCD.background_color(BLACK);
        uLCD.color(BLUE);
        uLCD.cls();
        if (Score > HighScore){
            HighScore = Score;
            uLCD.printf("NEW HIGH SCORE!\n %d", HighScore);
            wait(2);
        }
        Score = 0;
        uLCD.cls();
        uLCD.printf("\nTrivia Game \n Main Menu\n\n"); 
        uLCD.printf("High Score: %d \n", HighScore);
        uLCD.printf("\nFor New Game \n Press 0");
        while (value != 1){
            value=mpr121.read(0x00);
            value +=mpr121.read(0x01)<<8;  
            }
        uLCD.cls();
        if (v.size() !=0){
        while (answer == 1){
            if (v.size() == 0){
                break;
                }
            index = rand() %v.size();
            question = v.at(index);
            v.erase(v.begin()+index);
            check = 0;
            uLCD.cls();
            wait(1);
            if (question == 1){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("Who won the World Series in 2016?\n\n\n");
                uLCD.printf("1) Cleveland\nIndians\n");
                uLCD.printf("2) Chicago Cubs\n");
                uLCD.printf("3) San Diego\nPadres\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 2){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 4){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 8){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
            if (question == 2){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("What is the\n capital of South\n Dakota?\n\n");
                uLCD.printf("1) Pierre\n");
                uLCD.printf("2) Bismarck\n");
                uLCD.printf("3) Lakota\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 4){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 2){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score=Score+1;;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 8){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
        }
    }
            
  }
  if (question == 3){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("Who wrote The \nGrapes of Wrath?\n\n\n");
                uLCD.printf("1)Ernest Hemingway");
                uLCD.printf("2)George Orwell\n");
                uLCD.printf("3)John Steinbeck\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 2){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 8){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 4){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
            if (question == 4){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("What year was \nEinstein born?\n\n\n");
                uLCD.printf("1) 1900\n");
                uLCD.printf("2) 1879\n");
                uLCD.printf("3) 1873\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 2){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 4){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 8){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
            if (question == 5){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("Which song is by the Rolling Stones?\n\n\n");
                uLCD.printf("1) Paint It Black\n");
                uLCD.printf("2) Feel For \nThe Devil\n");
                uLCD.printf("3) Let It Be\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 4){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 2){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 8){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
             if (question == 6){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("Where was Hitler born?\n\n\n");
                uLCD.printf("1) Poland\n");
                uLCD.printf("2) Germany\n");
                uLCD.printf("3) Austria\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 4){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 8){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 2){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
             if (question == 7){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("The best selling\nmusic single of all time is?\n\n\n");
                uLCD.printf("1) Billie Jean\n");
                uLCD.printf("2) White Christmas\n");
                uLCD.printf("3) Candle in the \nWind\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 4){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 8){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 2){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
             if (question == 8){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                 uLCD.printf("What is the \ncurrency of \nJapan?\n\n\n");
                uLCD.printf("1) Pounds\n");
                uLCD.printf("2) Yen\n");
                uLCD.printf("3) Riki\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 2){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 4){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 8){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
             if (question == 9){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("Who was Harry \nHoudini?\n\n\n");
                uLCD.printf("1)An Escape Artist\n");
                uLCD.printf("2)Fighter Pilot in WWII\n");
                uLCD.printf("3) Harry Potter's Cousin\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 4){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 2){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 8){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
             if (question == 10){
                value = 0;
                uLCD.cls();
                uLCD.locate(1,2);
                uLCD.printf("What is glass \nmainly made up \nof?\n\n\n");
                uLCD.printf("1) Sand\n");
                uLCD.printf("2) Crystals\n");
                uLCD.printf("3) Highly \nCompressed \nWater\n");
                while (check == 0){
                    int value=mpr121.read(0x00);
                    value +=mpr121.read(0x01)<<8;
                    if (value == 4){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                        }
                    if (value == 2){
                        //play correct image
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x005E);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/right_taught.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        Score = Score+1;
                        uLCD.printf("Score: %d", Score);
                        wait(1);
                        answer = 1;
                        check = 1;
                        }
                    if (value == 8){
                        //play game over
                        uLCD.cls();
                        uLCD.media_init();
                        uLCD.set_sector_address(0x0000, 0x009F);
                        uLCD.display_image(0,0);
                        FILE *wave_file;   
                        wave_file=fopen("/sd/sounds/wrong_last.wav","r");
                        waver.play(wave_file);
                        fclose(wave_file);
                        wait(1);
                        uLCD.cls();
                        answer = 0;
                        check = 1;
                    }
                }
            }
  }
                        
                        
                        
                
                
     }   
     else{
                uLCD.printf("CONGRATS!");
                wait(1);
            }
         
}    
}