sequence repeater game for BBC MicroBit
main.cpp@1:9df88e4a03fc, 2020-11-25 (annotated)
- Committer:
- ldjusberg
- Date:
- Wed Nov 25 23:52:19 2020 +0000
- Revision:
- 1:9df88e4a03fc
- Parent:
- 0:39832b2fd7a7
skeleton finished 2.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ldjusberg | 0:39832b2fd7a7 | 1 | #include "MicroBit.h" |
ldjusberg | 0:39832b2fd7a7 | 2 | |
ldjusberg | 0:39832b2fd7a7 | 3 | MicroBit uBit; |
ldjusberg | 0:39832b2fd7a7 | 4 | |
ldjusberg | 0:39832b2fd7a7 | 5 | // BTN 1 - 4 |
ldjusberg | 0:39832b2fd7a7 | 6 | // BTN 2 - 6 |
ldjusberg | 0:39832b2fd7a7 | 7 | // BTN 3 - 7 |
ldjusberg | 0:39832b2fd7a7 | 8 | // BTN 4 - 9 |
ldjusberg | 0:39832b2fd7a7 | 9 | |
ldjusberg | 0:39832b2fd7a7 | 10 | // LED 1 - 0 |
ldjusberg | 0:39832b2fd7a7 | 11 | // LED 2 - 1 |
ldjusberg | 0:39832b2fd7a7 | 12 | // LED 3 - 2 |
ldjusberg | 0:39832b2fd7a7 | 13 | // LED 4 - 3 |
ldjusberg | 0:39832b2fd7a7 | 14 | |
ldjusberg | 0:39832b2fd7a7 | 15 | int main() { |
ldjusberg | 0:39832b2fd7a7 | 16 | |
ldjusberg | 0:39832b2fd7a7 | 17 | uBit.init(); |
ldjusberg | 0:39832b2fd7a7 | 18 | bool gameOver = false; |
ldjusberg | 0:39832b2fd7a7 | 19 | int level = 0; |
ldjusberg | 0:39832b2fd7a7 | 20 | string sequence[10]; |
ldjusberg | 0:39832b2fd7a7 | 21 | |
ldjusberg | 0:39832b2fd7a7 | 22 | while(gameOver == false) { |
ldjusberg | 0:39832b2fd7a7 | 23 | |
ldjusberg | 0:39832b2fd7a7 | 24 | level++; |
ldjusberg | 0:39832b2fd7a7 | 25 | |
ldjusberg | 0:39832b2fd7a7 | 26 | // add action to sequence, if array is full than double the size |
ldjusberg | 0:39832b2fd7a7 | 27 | |
ldjusberg | 0:39832b2fd7a7 | 28 | if (level == sizeof(sequence)/sizeof(*sequence)) { |
ldjusberg | 0:39832b2fd7a7 | 29 | string newSequence[sizeof(sequence)/sizeof(*sequence) * 2]; |
ldjusberg | 0:39832b2fd7a7 | 30 | for (int i = 0; i < level; i++) { |
ldjusberg | 0:39832b2fd7a7 | 31 | newSequence[i] = sequence[i]; |
ldjusberg | 0:39832b2fd7a7 | 32 | sequence = newSequence; |
ldjusberg | 0:39832b2fd7a7 | 33 | } |
ldjusberg | 0:39832b2fd7a7 | 34 | } |
ldjusberg | 0:39832b2fd7a7 | 35 | |
ldjusberg | 0:39832b2fd7a7 | 36 | sequence[level-1] = selectRandomAction(); |
ldjusberg | 0:39832b2fd7a7 | 37 | |
ldjusberg | 0:39832b2fd7a7 | 38 | // display sequence with LEDS |
ldjusberg | 0:39832b2fd7a7 | 39 | |
ldjusberg | 0:39832b2fd7a7 | 40 | for (int i = 0; i < level; i++) { |
ldjusberg | 0:39832b2fd7a7 | 41 | displayAction(sequence[i]); |
ldjusberg | 0:39832b2fd7a7 | 42 | } |
ldjusberg | 0:39832b2fd7a7 | 43 | |
ldjusberg | 0:39832b2fd7a7 | 44 | // wait for user input until sequence is complete or time for input runs out, if input is incorrect then end game and exit loop, if not increment level and repeat loop |
ldjusberg | 0:39832b2fd7a7 | 45 | |
ldjusberg | 0:39832b2fd7a7 | 46 | for (int i = 0; i < level; i++) { |
ldjusberg | 0:39832b2fd7a7 | 47 | bool inputIsCorrect = listenForInput(sequence[i]); |
ldjusberg | 0:39832b2fd7a7 | 48 | if (inputIsCorrect == false { |
ldjusberg | 0:39832b2fd7a7 | 49 | gameOver = true; |
ldjusberg | 0:39832b2fd7a7 | 50 | onIncorrectInput(); |
ldjusberg | 0:39832b2fd7a7 | 51 | break; |
ldjusberg | 0:39832b2fd7a7 | 52 | } else { |
ldjusberg | 0:39832b2fd7a7 | 53 | onCorrectInput(); |
ldjusberg | 0:39832b2fd7a7 | 54 | } |
ldjusberg | 0:39832b2fd7a7 | 55 | } |
ldjusberg | 0:39832b2fd7a7 | 56 | |
ldjusberg | 0:39832b2fd7a7 | 57 | } |
ldjusberg | 0:39832b2fd7a7 | 58 | |
ldjusberg | 0:39832b2fd7a7 | 59 | release_fiber(); |
ldjusberg | 0:39832b2fd7a7 | 60 | } |
ldjusberg | 0:39832b2fd7a7 | 61 | |
ldjusberg | 0:39832b2fd7a7 | 62 | string selectRandomAction() { |
ldjusberg | 0:39832b2fd7a7 | 63 | |
ldjusberg | 0:39832b2fd7a7 | 64 | int random = rand() % 4; |
ldjusberg | 0:39832b2fd7a7 | 65 | string action = ""; |
ldjusberg | 0:39832b2fd7a7 | 66 | |
ldjusberg | 0:39832b2fd7a7 | 67 | if (random == 0) { |
ldjusberg | 0:39832b2fd7a7 | 68 | action = "BTN1"; |
ldjusberg | 0:39832b2fd7a7 | 69 | } else if (random == 1) { |
ldjusberg | 0:39832b2fd7a7 | 70 | action = "BTN2"; |
ldjusberg | 0:39832b2fd7a7 | 71 | } else if (random == 2) { |
ldjusberg | 0:39832b2fd7a7 | 72 | action = "BTN3"; |
ldjusberg | 0:39832b2fd7a7 | 73 | } else if (random == 3) { |
ldjusberg | 0:39832b2fd7a7 | 74 | action = "BTN4"; |
ldjusberg | 0:39832b2fd7a7 | 75 | } |
ldjusberg | 0:39832b2fd7a7 | 76 | |
ldjusberg | 0:39832b2fd7a7 | 77 | return action; |
ldjusberg | 0:39832b2fd7a7 | 78 | } |
ldjusberg | 0:39832b2fd7a7 | 79 | |
ldjusberg | 0:39832b2fd7a7 | 80 | void diplayAction(string action) { |
ldjusberg | 0:39832b2fd7a7 | 81 | |
ldjusberg | 0:39832b2fd7a7 | 82 | } |
ldjusberg | 0:39832b2fd7a7 | 83 | |
ldjusberg | 0:39832b2fd7a7 | 84 | bool listenForInput() { |
ldjusberg | 0:39832b2fd7a7 | 85 | |
ldjusberg | 0:39832b2fd7a7 | 86 | } |
ldjusberg | 0:39832b2fd7a7 | 87 | |
ldjusberg | 0:39832b2fd7a7 | 88 | // display something happy on microcontroller |
ldjusberg | 0:39832b2fd7a7 | 89 | void onCorrectInput() { |
ldjusberg | 0:39832b2fd7a7 | 90 | |
ldjusberg | 0:39832b2fd7a7 | 91 | } |
ldjusberg | 0:39832b2fd7a7 | 92 | |
ldjusberg | 0:39832b2fd7a7 | 93 | // display something sad on microcontroller |
ldjusberg | 0:39832b2fd7a7 | 94 | void onIncorrectInput() { |
ldjusberg | 0:39832b2fd7a7 | 95 | |
ldjusberg | 0:39832b2fd7a7 | 96 | } |