Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

show score

  • Loading branch information...
commit 33783d6986c7a0a0abd7cbf7d207d155eb6fb85b 1 parent b913d64
twobit authored October 24, 2011

Showing 1 changed file with 56 additions and 27 deletions. Show diff stats Hide diff stats

  1. 83  firmware/apps/replicate.c
83  firmware/apps/replicate.c
... ...
@@ -1,42 +1,41 @@
1 1
 #include <main.h>
2 2
 #include <stdlib.h>
  3
+#include "../libs/font.h"
3 4
 
4 5
 #define		MAX_CHAIN_LENGTH		128
5 6
 
6  
-typedef enum { INIT, SHOW, LISTEN, QUIT } State;
  7
+typedef enum { INIT, SHOW, BREAK, LISTEN, SCORE, QUIT } State;
7 8
 
8 9
 static State state = INIT;
9 10
 static uint8_t chain[MAX_CHAIN_LENGTH];
10  
-static uint8_t length = 3;
  11
+static uint8_t length = 1;
11 12
 static uint8_t step = 0;
12 13
 static uint8_t sub_step = 0;
13 14
 
  15
+static char score[5] = { ' ', ' ', '0', '0', '0' };
  16
+
14 17
 static uint8_t tick() {
  18
+	uint8_t x, y;
15 19
 
16 20
 	if(state == INIT) {
17 21
 		uint8_t i;
18 22
 		for(i = 0; i < length; i++) chain[i] = rand() & 1;
19  
-
20 23
 		state = SHOW;
21 24
 	}
22 25
 
23 26
 	if(state == SHOW) {
24  
-		++sub_step;
25  
-		if(sub_step < 10) {
26  
-
27  
-			uint8_t side = chain[step];
28  
-			uint8_t color = sub_step < 8 ? 8 - sub_step : 0;
29  
-
30  
-			uint8_t x, y;
31  
-			for(x = 0; x < LED_WIDTH; x++) {
32  
-				uint8_t c = (x >= LED_WIDTH / 2) ^ side;
33  
-				for(y = 0; y < LED_HEIGHT; y++)
34  
-				{
35  
-					setLedXY(x, y, c ? color : 0);
36  
-				}
37  
-			}
  27
+
  28
+		uint8_t side = chain[step];
  29
+		uint8_t color = sub_step < 7 ? 7 - sub_step : 0;
  30
+
  31
+		for(x = 0; x < LED_WIDTH; ++x) {
  32
+			uint8_t c = (x >= LED_WIDTH / 2) ^ side ? color : 0;
  33
+			for(y = 0; y < LED_HEIGHT; ++y)
  34
+				setLedXY(x, y, c);
38 35
 		}
39  
-		else {
  36
+
  37
+		++sub_step;
  38
+		if(sub_step == 10) {
40 39
 			sub_step = 0;
41 40
 			++step;
42 41
 			if(step == length) {
@@ -46,31 +45,62 @@ static uint8_t tick() {
46 45
 		}
47 46
 
48 47
 	}
  48
+	else if(state == BREAK) {
  49
+		++sub_step;
  50
+		if(sub_step == 5) {
  51
+			sub_step = 0;
  52
+			state = SHOW;
  53
+		}
  54
+	}
  55
+	else if(state == SCORE) {
  56
+
  57
+		for(x = 0; x < LED_WIDTH; ++x) {
  58
+
  59
+			uint16_t p = (sub_step / 4) + x;
  60
+			char c = score[p / 4 % 5];
  61
+			uint8_t bits = 0;
  62
+			if((p & 3) < 3) bits = font[c - 32][p & 3];
  63
+
  64
+			for(y = 0; y < LED_HEIGHT; ++y) {
  65
+				setLedXY(x, y, 7 * (bits & 1));
  66
+				bits >>= 1;
  67
+			}
  68
+		}
  69
+		++sub_step;
  70
+
  71
+	}
49 72
 
50 73
 	return state == QUIT;
51 74
 }
52 75
 
53 76
 static void key(key_type key, event_type event) {
54  
-	if(state != LISTEN || event != DOWN)
55  
-		return;
  77
+	if(event != DOWN) return;
  78
+	if(state == SCORE) state = QUIT;
  79
+	if(state != LISTEN) return;
56 80
 
57 81
 	if(key == chain[step]) {
58 82
 		++step;
  83
+		sub_step = 0;
59 84
 		if(step == length) {
60 85
 
61  
-			if(length == MAX_CHAIN_LENGTH)	// wow, you just beat the game!
62  
-				goto DO_QUIT;				// cos I like goto
  86
+			// wow, you just beat the game!
  87
+			if(length == MAX_CHAIN_LENGTH) goto DO_SCORE;
63 88
 
64 89
 			chain[step] = rand() & 1;
65 90
 			step = 0;
66 91
 			++length;
67  
-			state = SHOW;
  92
+			state = BREAK;
68 93
 		}
69 94
 	}
70 95
 	else {
71  
-		DO_QUIT:
72  
-		// TODO: print length before exiting
73  
-		state = QUIT;
  96
+		DO_SCORE:
  97
+		state = SCORE;
  98
+		sub_step = 12;
  99
+		uint8_t i;
  100
+		for(i = 4; i > 1; --i) {
  101
+			score[i] = '0' + length % 10;
  102
+			length /= 10;
  103
+		}
74 104
 	}
75 105
 }
76 106
 
@@ -79,4 +109,3 @@ void init(void) {
79 109
 	registerApp(tick, key, 7);
80 110
 }
81 111
 
82  
-

0 notes on commit 33783d6

Please sign in to comment.
Something went wrong with that request. Please try again.