This is the code to the math game mini project.
Dependencies: 4DGL-uLCD-SE mbed
main.cpp@1:03c9bef920a6, 2015-03-13 (annotated)
- Committer:
- rjuste3
- Date:
- Fri Mar 13 14:33:54 2015 +0000
- Revision:
- 1:03c9bef920a6
- Parent:
- 0:aedab05ce6cc
Mini project
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:03c9bef920a6 | 25 | //generates random numbers between 1 and 40 as operands |
rjuste3 | 0:aedab05ce6cc | 26 | operand1 = rand() % 40 + 1; |
rjuste3 | 0:aedab05ce6cc | 27 | operand2 = rand() % 40 + 1; |
rjuste3 | 0:aedab05ce6cc | 28 | op = rand() % 3; |
rjuste3 | 0:aedab05ce6cc | 29 | |
rjuste3 | 1:03c9bef920a6 | 30 | //genearte random operator and appropriate answer |
rjuste3 | 0:aedab05ce6cc | 31 | if (op == 0){ |
rjuste3 | 0:aedab05ce6cc | 32 | uLCD.locate(0,0); |
rjuste3 | 0:aedab05ce6cc | 33 | uLCD.printf("%d + %d\n",operand1, operand2); |
rjuste3 | 0:aedab05ce6cc | 34 | answer = operand1 + operand2; |
rjuste3 | 0:aedab05ce6cc | 35 | } |
rjuste3 | 0:aedab05ce6cc | 36 | else if (op == 1){ |
rjuste3 | 0:aedab05ce6cc | 37 | uLCD.locate(0,0); |
rjuste3 | 0:aedab05ce6cc | 38 | uLCD.printf("%d - %d\n",operand1, operand2); |
rjuste3 | 0:aedab05ce6cc | 39 | answer = operand1 - operand2; |
rjuste3 | 0:aedab05ce6cc | 40 | } |
rjuste3 | 0:aedab05ce6cc | 41 | else if (op == 2){ |
rjuste3 | 0:aedab05ce6cc | 42 | uLCD.locate(0,0); |
rjuste3 | 0:aedab05ce6cc | 43 | uLCD.printf("%d * %d\n",operand1, operand2); |
rjuste3 | 0:aedab05ce6cc | 44 | answer = operand1 * operand2; |
rjuste3 | 0:aedab05ce6cc | 45 | } |
rjuste3 | 0:aedab05ce6cc | 46 | |
rjuste3 | 0:aedab05ce6cc | 47 | |
rjuste3 | 0:aedab05ce6cc | 48 | } |
rjuste3 | 0:aedab05ce6cc | 49 | int main() |
rjuste3 | 0:aedab05ce6cc | 50 | { |
rjuste3 | 0:aedab05ce6cc | 51 | pb1.mode(PullUp); |
rjuste3 | 0:aedab05ce6cc | 52 | pb2.mode(PullUp); |
rjuste3 | 0:aedab05ce6cc | 53 | pb3.mode(PullUp); |
rjuste3 | 0:aedab05ce6cc | 54 | pb4.mode(PullUp); |
rjuste3 | 0:aedab05ce6cc | 55 | pb5.mode(PullUp); |
rjuste3 | 0:aedab05ce6cc | 56 | pb6.mode(PullUp); |
rjuste3 | 0:aedab05ce6cc | 57 | |
rjuste3 | 0:aedab05ce6cc | 58 | |
rjuste3 | 0:aedab05ce6cc | 59 | bool randomGen = true; |
rjuste3 | 0:aedab05ce6cc | 60 | t.start(); |
rjuste3 | 1:03c9bef920a6 | 61 | while(randomGen){ //push any button to start game |
rjuste3 | 0:aedab05ce6cc | 62 | uLCD.locate(1,0); |
rjuste3 | 0:aedab05ce6cc | 63 | uLCD.printf("Push any button to start the game"); |
rjuste3 | 0:aedab05ce6cc | 64 | if(pb1==0 || pb2==0 || pb3==0 || pb4==0 || pb5==0 || pb6 == 0){ |
rjuste3 | 0:aedab05ce6cc | 65 | seed = t.read_us(); |
rjuste3 | 0:aedab05ce6cc | 66 | randomGen= false; |
rjuste3 | 0:aedab05ce6cc | 67 | t.reset(); |
rjuste3 | 0:aedab05ce6cc | 68 | uLCD.cls(); |
rjuste3 | 0:aedab05ce6cc | 69 | } |
rjuste3 | 0:aedab05ce6cc | 70 | } |
rjuste3 | 0:aedab05ce6cc | 71 | |
rjuste3 | 0:aedab05ce6cc | 72 | |
rjuste3 | 0:aedab05ce6cc | 73 | |
rjuste3 | 0:aedab05ce6cc | 74 | srand((int)seed); |
rjuste3 | 0:aedab05ce6cc | 75 | |
rjuste3 | 0:aedab05ce6cc | 76 | |
rjuste3 | 0:aedab05ce6cc | 77 | |
rjuste3 | 0:aedab05ce6cc | 78 | while(true){ |
rjuste3 | 1:03c9bef920a6 | 79 | generateProblem(); //generate problem |
rjuste3 | 0:aedab05ce6cc | 80 | uLCD.locate(1,14); |
rjuste3 | 1:03c9bef920a6 | 81 | uLCD.printf("Score: %d",score);//display score |
rjuste3 | 0:aedab05ce6cc | 82 | t.start(); |
rjuste3 | 0:aedab05ce6cc | 83 | |
rjuste3 | 0:aedab05ce6cc | 84 | while(t.read() <= seconds){ |
rjuste3 | 1:03c9bef920a6 | 85 | if(seconds - (int)t.read() < 10){ //display time limit |
rjuste3 | 0:aedab05ce6cc | 86 | uLCD.locate(13,1); |
rjuste3 | 0:aedab05ce6cc | 87 | uLCD.printf("%d",(seconds -(int)t.read())); |
rjuste3 | 0:aedab05ce6cc | 88 | } else{ |
rjuste3 | 0:aedab05ce6cc | 89 | uLCD.locate(12,1); |
rjuste3 | 0:aedab05ce6cc | 90 | uLCD.printf("%d",(seconds -(int)t.read())); |
rjuste3 | 0:aedab05ce6cc | 91 | } |
rjuste3 | 1:03c9bef920a6 | 92 | //push pushbutton to add up your answer to approach correct answer |
rjuste3 | 0:aedab05ce6cc | 93 | if(pb1 == 0) |
rjuste3 | 0:aedab05ce6cc | 94 | { |
rjuste3 | 0:aedab05ce6cc | 95 | ans = ans + 1; |
rjuste3 | 0:aedab05ce6cc | 96 | uLCD.locate(1,2); |
rjuste3 | 0:aedab05ce6cc | 97 | uLCD.printf("%d",ans); |
rjuste3 | 0:aedab05ce6cc | 98 | wait(0.2); |
rjuste3 | 0:aedab05ce6cc | 99 | } |
rjuste3 | 0:aedab05ce6cc | 100 | else if(pb2 == 0) |
rjuste3 | 0:aedab05ce6cc | 101 | { |
rjuste3 | 0:aedab05ce6cc | 102 | ans = ans + 10; |
rjuste3 | 0:aedab05ce6cc | 103 | uLCD.locate(1,2); |
rjuste3 | 0:aedab05ce6cc | 104 | uLCD.printf("%d",ans); |
rjuste3 | 0:aedab05ce6cc | 105 | wait(0.2); |
rjuste3 | 0:aedab05ce6cc | 106 | } |
rjuste3 | 0:aedab05ce6cc | 107 | else if(pb3 == 0) |
rjuste3 | 0:aedab05ce6cc | 108 | { |
rjuste3 | 0:aedab05ce6cc | 109 | ans = ans + 100; |
rjuste3 | 0:aedab05ce6cc | 110 | uLCD.locate(1,2); |
rjuste3 | 0:aedab05ce6cc | 111 | uLCD.printf("%d",ans); |
rjuste3 | 0:aedab05ce6cc | 112 | wait(0.2); |
rjuste3 | 0:aedab05ce6cc | 113 | } |
rjuste3 | 0:aedab05ce6cc | 114 | else if(pb4 == 0) |
rjuste3 | 0:aedab05ce6cc | 115 | { |
rjuste3 | 0:aedab05ce6cc | 116 | ans = ans - 1; |
rjuste3 | 0:aedab05ce6cc | 117 | uLCD.locate(1,2); |
rjuste3 | 0:aedab05ce6cc | 118 | uLCD.printf("%d",ans); |
rjuste3 | 0:aedab05ce6cc | 119 | wait(0.2); |
rjuste3 | 0:aedab05ce6cc | 120 | } |
rjuste3 | 0:aedab05ce6cc | 121 | else if(pb5 == 0) |
rjuste3 | 0:aedab05ce6cc | 122 | { |
rjuste3 | 0:aedab05ce6cc | 123 | ans = ans - 10; |
rjuste3 | 0:aedab05ce6cc | 124 | uLCD.locate(1,2); |
rjuste3 | 0:aedab05ce6cc | 125 | uLCD.printf("%d",ans); |
rjuste3 | 0:aedab05ce6cc | 126 | wait(0.2); |
rjuste3 | 0:aedab05ce6cc | 127 | } |
rjuste3 | 0:aedab05ce6cc | 128 | else if(pb6 == 0) |
rjuste3 | 0:aedab05ce6cc | 129 | { |
rjuste3 | 0:aedab05ce6cc | 130 | ans = ans - 100; |
rjuste3 | 0:aedab05ce6cc | 131 | uLCD.locate(1,2); |
rjuste3 | 0:aedab05ce6cc | 132 | uLCD.printf("%d",ans); |
rjuste3 | 0:aedab05ce6cc | 133 | wait(0.2); |
rjuste3 | 0:aedab05ce6cc | 134 | } |
rjuste3 | 0:aedab05ce6cc | 135 | } |
rjuste3 | 0:aedab05ce6cc | 136 | |
rjuste3 | 1:03c9bef920a6 | 137 | if( ans == answer){//if correct answer then add score and decrease time limit |
rjuste3 | 0:aedab05ce6cc | 138 | score += 1; |
rjuste3 | 0:aedab05ce6cc | 139 | seconds -=3; |
rjuste3 | 0:aedab05ce6cc | 140 | }else |
rjuste3 | 0:aedab05ce6cc | 141 | { |
rjuste3 | 0:aedab05ce6cc | 142 | uLCD.cls(); |
rjuste3 | 0:aedab05ce6cc | 143 | uLCD.printf("You lose!\n Your score is %d",score); |
rjuste3 | 0:aedab05ce6cc | 144 | wait(5); |
rjuste3 | 0:aedab05ce6cc | 145 | score = 0; |
rjuste3 | 0:aedab05ce6cc | 146 | seconds = 30; |
rjuste3 | 0:aedab05ce6cc | 147 | } |
rjuste3 | 0:aedab05ce6cc | 148 | t.reset(); |
rjuste3 | 0:aedab05ce6cc | 149 | |
rjuste3 | 0:aedab05ce6cc | 150 | ans = 0; |
rjuste3 | 0:aedab05ce6cc | 151 | |
rjuste3 | 0:aedab05ce6cc | 152 | uLCD.cls(); |
rjuste3 | 0:aedab05ce6cc | 153 | |
rjuste3 | 0:aedab05ce6cc | 154 | |
rjuste3 | 0:aedab05ce6cc | 155 | } |
rjuste3 | 0:aedab05ce6cc | 156 | |
rjuste3 | 0:aedab05ce6cc | 157 | } |