This is the code to the math game mini project.

Dependencies:   4DGL-uLCD-SE mbed

Committer:
rjuste3
Date:
Fri Mar 13 13:52:29 2015 +0000
Revision:
0:aedab05ce6cc
Child:
1:03c9bef920a6
add LCD

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rjuste3 0:aedab05ce6cc 1 #include <math.h>
rjuste3 0:aedab05ce6cc 2 #include "mbed.h"
rjuste3 0:aedab05ce6cc 3 #include "uLCD_4DGL.h"
rjuste3 0:aedab05ce6cc 4 #include <time.h>
rjuste3 0:aedab05ce6cc 5
rjuste3 0:aedab05ce6cc 6 uLCD_4DGL uLCD(p9, p10,p11);
rjuste3 0:aedab05ce6cc 7 int operand1;
rjuste3 0:aedab05ce6cc 8 int operand2;
rjuste3 0:aedab05ce6cc 9 int op;
rjuste3 0:aedab05ce6cc 10 int answer;
rjuste3 0:aedab05ce6cc 11 int ans = 0;
rjuste3 0:aedab05ce6cc 12 int score = 0;
rjuste3 0:aedab05ce6cc 13 int seconds = 30;
rjuste3 0:aedab05ce6cc 14 float seed;
rjuste3 0:aedab05ce6cc 15 Timer t;
rjuste3 0:aedab05ce6cc 16 DigitalIn pb1(p13);
rjuste3 0:aedab05ce6cc 17 DigitalIn pb2(p14);
rjuste3 0:aedab05ce6cc 18 DigitalIn pb3(p15);
rjuste3 0:aedab05ce6cc 19 DigitalIn pb4(p16);
rjuste3 0:aedab05ce6cc 20 DigitalIn pb5(p17);
rjuste3 0:aedab05ce6cc 21 DigitalIn pb6(p18);
rjuste3 0:aedab05ce6cc 22
rjuste3 0:aedab05ce6cc 23
rjuste3 0:aedab05ce6cc 24 void generateProblem(){
rjuste3 0:aedab05ce6cc 25 operand1 = rand() % 40 + 1;
rjuste3 0:aedab05ce6cc 26 operand2 = rand() % 40 + 1;
rjuste3 0:aedab05ce6cc 27 op = rand() % 3;
rjuste3 0:aedab05ce6cc 28
rjuste3 0:aedab05ce6cc 29 if (op == 0){
rjuste3 0:aedab05ce6cc 30 uLCD.locate(0,0);
rjuste3 0:aedab05ce6cc 31 uLCD.printf("%d + %d\n",operand1, operand2);
rjuste3 0:aedab05ce6cc 32 answer = operand1 + operand2;
rjuste3 0:aedab05ce6cc 33 }
rjuste3 0:aedab05ce6cc 34 else if (op == 1){
rjuste3 0:aedab05ce6cc 35 uLCD.locate(0,0);
rjuste3 0:aedab05ce6cc 36 uLCD.printf("%d - %d\n",operand1, operand2);
rjuste3 0:aedab05ce6cc 37 answer = operand1 - operand2;
rjuste3 0:aedab05ce6cc 38 }
rjuste3 0:aedab05ce6cc 39 else if (op == 2){
rjuste3 0:aedab05ce6cc 40 uLCD.locate(0,0);
rjuste3 0:aedab05ce6cc 41 uLCD.printf("%d * %d\n",operand1, operand2);
rjuste3 0:aedab05ce6cc 42 answer = operand1 * operand2;
rjuste3 0:aedab05ce6cc 43 }
rjuste3 0:aedab05ce6cc 44
rjuste3 0:aedab05ce6cc 45
rjuste3 0:aedab05ce6cc 46 }
rjuste3 0:aedab05ce6cc 47 int main()
rjuste3 0:aedab05ce6cc 48 {
rjuste3 0:aedab05ce6cc 49 pb1.mode(PullUp);
rjuste3 0:aedab05ce6cc 50 pb2.mode(PullUp);
rjuste3 0:aedab05ce6cc 51 pb3.mode(PullUp);
rjuste3 0:aedab05ce6cc 52 pb4.mode(PullUp);
rjuste3 0:aedab05ce6cc 53 pb5.mode(PullUp);
rjuste3 0:aedab05ce6cc 54 pb6.mode(PullUp);
rjuste3 0:aedab05ce6cc 55
rjuste3 0:aedab05ce6cc 56
rjuste3 0:aedab05ce6cc 57 bool randomGen = true;
rjuste3 0:aedab05ce6cc 58 t.start();
rjuste3 0:aedab05ce6cc 59 while(randomGen){
rjuste3 0:aedab05ce6cc 60 uLCD.locate(1,0);
rjuste3 0:aedab05ce6cc 61 uLCD.printf("Push any button to start the game");
rjuste3 0:aedab05ce6cc 62 if(pb1==0 || pb2==0 || pb3==0 || pb4==0 || pb5==0 || pb6 == 0){
rjuste3 0:aedab05ce6cc 63 seed = t.read_us();
rjuste3 0:aedab05ce6cc 64 randomGen= false;
rjuste3 0:aedab05ce6cc 65 t.reset();
rjuste3 0:aedab05ce6cc 66 uLCD.cls();
rjuste3 0:aedab05ce6cc 67 }
rjuste3 0:aedab05ce6cc 68 }
rjuste3 0:aedab05ce6cc 69
rjuste3 0:aedab05ce6cc 70
rjuste3 0:aedab05ce6cc 71
rjuste3 0:aedab05ce6cc 72 srand((int)seed);
rjuste3 0:aedab05ce6cc 73
rjuste3 0:aedab05ce6cc 74
rjuste3 0:aedab05ce6cc 75
rjuste3 0:aedab05ce6cc 76 while(true){
rjuste3 0:aedab05ce6cc 77 generateProblem();
rjuste3 0:aedab05ce6cc 78 uLCD.locate(1,14);
rjuste3 0:aedab05ce6cc 79 uLCD.printf("Score: %d",score);
rjuste3 0:aedab05ce6cc 80 t.start();
rjuste3 0:aedab05ce6cc 81
rjuste3 0:aedab05ce6cc 82 while(t.read() <= seconds){
rjuste3 0:aedab05ce6cc 83 if(seconds - (int)t.read() < 10){
rjuste3 0:aedab05ce6cc 84 uLCD.locate(13,1);
rjuste3 0:aedab05ce6cc 85 uLCD.printf("%d",(seconds -(int)t.read()));
rjuste3 0:aedab05ce6cc 86 } else{
rjuste3 0:aedab05ce6cc 87 uLCD.locate(12,1);
rjuste3 0:aedab05ce6cc 88 uLCD.printf("%d",(seconds -(int)t.read()));
rjuste3 0:aedab05ce6cc 89 }
rjuste3 0:aedab05ce6cc 90 if(pb1 == 0)
rjuste3 0:aedab05ce6cc 91 {
rjuste3 0:aedab05ce6cc 92 ans = ans + 1;
rjuste3 0:aedab05ce6cc 93 uLCD.locate(1,2);
rjuste3 0:aedab05ce6cc 94 uLCD.printf("%d",ans);
rjuste3 0:aedab05ce6cc 95 wait(0.2);
rjuste3 0:aedab05ce6cc 96 }
rjuste3 0:aedab05ce6cc 97 else if(pb2 == 0)
rjuste3 0:aedab05ce6cc 98 {
rjuste3 0:aedab05ce6cc 99 ans = ans + 10;
rjuste3 0:aedab05ce6cc 100 uLCD.locate(1,2);
rjuste3 0:aedab05ce6cc 101 uLCD.printf("%d",ans);
rjuste3 0:aedab05ce6cc 102 wait(0.2);
rjuste3 0:aedab05ce6cc 103 }
rjuste3 0:aedab05ce6cc 104 else if(pb3 == 0)
rjuste3 0:aedab05ce6cc 105 {
rjuste3 0:aedab05ce6cc 106 ans = ans + 100;
rjuste3 0:aedab05ce6cc 107 uLCD.locate(1,2);
rjuste3 0:aedab05ce6cc 108 uLCD.printf("%d",ans);
rjuste3 0:aedab05ce6cc 109 wait(0.2);
rjuste3 0:aedab05ce6cc 110 }
rjuste3 0:aedab05ce6cc 111 else if(pb4 == 0)
rjuste3 0:aedab05ce6cc 112 {
rjuste3 0:aedab05ce6cc 113 ans = ans - 1;
rjuste3 0:aedab05ce6cc 114 uLCD.locate(1,2);
rjuste3 0:aedab05ce6cc 115 uLCD.printf("%d",ans);
rjuste3 0:aedab05ce6cc 116 wait(0.2);
rjuste3 0:aedab05ce6cc 117 }
rjuste3 0:aedab05ce6cc 118 else if(pb5 == 0)
rjuste3 0:aedab05ce6cc 119 {
rjuste3 0:aedab05ce6cc 120 ans = ans - 10;
rjuste3 0:aedab05ce6cc 121 uLCD.locate(1,2);
rjuste3 0:aedab05ce6cc 122 uLCD.printf("%d",ans);
rjuste3 0:aedab05ce6cc 123 wait(0.2);
rjuste3 0:aedab05ce6cc 124 }
rjuste3 0:aedab05ce6cc 125 else if(pb6 == 0)
rjuste3 0:aedab05ce6cc 126 {
rjuste3 0:aedab05ce6cc 127 ans = ans - 100;
rjuste3 0:aedab05ce6cc 128 uLCD.locate(1,2);
rjuste3 0:aedab05ce6cc 129 uLCD.printf("%d",ans);
rjuste3 0:aedab05ce6cc 130 wait(0.2);
rjuste3 0:aedab05ce6cc 131 }
rjuste3 0:aedab05ce6cc 132 }
rjuste3 0:aedab05ce6cc 133
rjuste3 0:aedab05ce6cc 134 if( ans == answer){
rjuste3 0:aedab05ce6cc 135 score += 1;
rjuste3 0:aedab05ce6cc 136 seconds -=3;
rjuste3 0:aedab05ce6cc 137 }else
rjuste3 0:aedab05ce6cc 138 {
rjuste3 0:aedab05ce6cc 139 uLCD.cls();
rjuste3 0:aedab05ce6cc 140 uLCD.printf("You lose!\n Your score is %d",score);
rjuste3 0:aedab05ce6cc 141 wait(5);
rjuste3 0:aedab05ce6cc 142 score = 0;
rjuste3 0:aedab05ce6cc 143 seconds = 30;
rjuste3 0:aedab05ce6cc 144 }
rjuste3 0:aedab05ce6cc 145 t.reset();
rjuste3 0:aedab05ce6cc 146
rjuste3 0:aedab05ce6cc 147 ans = 0;
rjuste3 0:aedab05ce6cc 148
rjuste3 0:aedab05ce6cc 149 uLCD.cls();
rjuste3 0:aedab05ce6cc 150
rjuste3 0:aedab05ce6cc 151
rjuste3 0:aedab05ce6cc 152 }
rjuste3 0:aedab05ce6cc 153
rjuste3 0:aedab05ce6cc 154 }