|
| 1 | +/* |
| 2 | + * Domineering |
| 3 | + * |
| 4 | + * Allows you to play the game domineering |
| 5 | + * (See https://en.wikipedia.org/wiki/Domineering) |
| 6 | + * with the use of a potentiometer. Player take |
| 7 | + * turns removing two blinking leds by turning the |
| 8 | + * potentiometer. After a user stops turning the |
| 9 | + * potentiometer the blinking slows down and removes |
| 10 | + * the two blinking leds, giving the turn to the |
| 11 | + * other player. |
| 12 | + * |
| 13 | + * The size of the playing field can be changed by |
| 14 | + * modifying BOTTOM and TOP defines (taking into |
| 15 | + * account: (0 <= BOTTOM < TOP <= 8) |
| 16 | + */ |
| 17 | +static const int DATA_PIN = 20; |
| 18 | +static const int CLK_PIN = 5; |
| 19 | +static const int CS_PIN = 21; |
| 20 | + |
| 21 | +#include "LedControl.h" |
| 22 | +#include "Timer.h" |
| 23 | + |
| 24 | +#define POTPIN A5 // Potentiometer |
| 25 | + |
| 26 | +LedControl lc = LedControl(DATA_PIN, CLK_PIN, CS_PIN, 1); |
| 27 | + |
| 28 | +#define brightness 1 //Values from 1 to 15 to set the brightness |
| 29 | +uint8_t matrix[8]; |
| 30 | +#define MAT(X,Y) (matrix[X] & (1 << Y)) |
| 31 | +#define SETMAT(X,Y) matrix[X] |= (1 << Y) |
| 32 | +#define RESETMAT(X,Y) matrix[X] &= ~(1 << Y) |
| 33 | + |
| 34 | +uint8_t apple_x; |
| 35 | +uint8_t apple_y; |
| 36 | + |
| 37 | +uint8_t snake_x[70]; |
| 38 | +uint8_t snake_y[70]; |
| 39 | +uint8_t snake_len; |
| 40 | +uint8_t snake_dir; |
| 41 | +uint8_t make_longer = 0; |
| 42 | + |
| 43 | +void show_field(bool apple_on) |
| 44 | +{ |
| 45 | + for (uint8_t i = 0; i < 8; i++) |
| 46 | + matrix[i] = 0; |
| 47 | + for (uint8_t i = 0; i < snake_len; i++) |
| 48 | + SETMAT(snake_x[i], snake_y[i]); |
| 49 | + if (apple_on) |
| 50 | + SETMAT(apple_x, apple_y); |
| 51 | + for (uint8_t x = 0; x < 8; x++) |
| 52 | + lc.setColumn(0, x, matrix[x]); |
| 53 | +} |
| 54 | + |
| 55 | +void place_random_apple() |
| 56 | +{ |
| 57 | + bool ok = false; |
| 58 | + while (!ok) |
| 59 | + { |
| 60 | + apple_x = random(8); |
| 61 | + apple_y = random(8); |
| 62 | + ok = true; |
| 63 | + for (uint8_t i = 0; i < snake_len; i++) |
| 64 | + if (snake_x[i] == apple_x && snake_y[i] == apple_y) |
| 65 | + { |
| 66 | + ok = false; |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void setup() { |
| 73 | + pinMode(POTPIN, INPUT); |
| 74 | + |
| 75 | + lc.shutdown(0, false); |
| 76 | + lc.setIntensity(0, brightness); |
| 77 | + lc.clearDisplay(0); |
| 78 | + Serial.begin(9600); |
| 79 | + Serial.println("Start"); |
| 80 | + snake_len = 3; |
| 81 | + snake_x[0] = 4; |
| 82 | + snake_y[0] = 6; |
| 83 | + snake_x[1] = 5; |
| 84 | + snake_y[1] = 6; |
| 85 | + snake_x[2] = 6; |
| 86 | + snake_y[2] = 6; |
| 87 | + snake_dir = 0; |
| 88 | + place_random_apple(); |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +void loop() { |
| 93 | + int startp = analogRead(POTPIN); |
| 94 | + for (uint8_t i = 0; i < 5; i++) |
| 95 | + { |
| 96 | + show_field(true); |
| 97 | + delay(100); |
| 98 | + show_field(false); |
| 99 | + delay(100); |
| 100 | + } |
| 101 | + if (make_longer > 0) |
| 102 | + { |
| 103 | + snake_len++; |
| 104 | + make_longer--; |
| 105 | + } |
| 106 | + |
| 107 | + for (uint8_t i = snake_len-1; i > 0; i--) |
| 108 | + { |
| 109 | + snake_x[i] = snake_x[i-1]; |
| 110 | + snake_y[i] = snake_y[i-1]; |
| 111 | + } |
| 112 | + int endp = analogRead(POTPIN); |
| 113 | + if (endp > startp + 50) |
| 114 | + snake_dir = (snake_dir + 1)%4; |
| 115 | + else if (endp < startp - 50) |
| 116 | + snake_dir = (snake_dir + 3)%4; |
| 117 | + if (snake_dir == 0) |
| 118 | + snake_x[0]--; |
| 119 | + else if (snake_dir == 2) |
| 120 | + snake_x[0]++; |
| 121 | + else if (snake_dir == 1) |
| 122 | + snake_y[0]--; |
| 123 | + else if (snake_dir == 3) |
| 124 | + snake_y[0]++; |
| 125 | + |
| 126 | + if ( snake_x[0] < 0 || snake_x[0] > 7 |
| 127 | + || snake_y[0] < 0 || snake_y[0] > 7) |
| 128 | + { |
| 129 | + delay(2000); |
| 130 | + setup(); |
| 131 | + } |
| 132 | + for (uint8_t i = 1; i < snake_len; i++) |
| 133 | + if (snake_x[0] == snake_x[i] && snake_y[0] == snake_y[i]) |
| 134 | + { |
| 135 | + delay(2000); |
| 136 | + setup(); |
| 137 | + } |
| 138 | + if (snake_x[0] == apple_x && snake_y[0] == apple_y) |
| 139 | + { |
| 140 | + make_longer = 2; |
| 141 | + place_random_apple(); |
| 142 | + } |
| 143 | +} |
| 144 | +/* |
| 145 | + uint8_t len = 0; |
| 146 | + uint8_t v_x = 0; |
| 147 | + uint8_t v_y = 0; |
| 148 | + if (vert_player) |
| 149 | + { |
| 150 | + v_x = 1; |
| 151 | + for (uint8_t y = BOTTOM; y < TOP; y++) |
| 152 | + for (uint8_t x = BOTTOM; x < TOP-1; x++) |
| 153 | + if (MAT(x,y) != 0 && MAT(x+1, y) != 0) |
| 154 | + { |
| 155 | + x_p[len] = x; |
| 156 | + y_p[len] = y; |
| 157 | + len++; |
| 158 | + } |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + v_y = 1; |
| 163 | + for (uint8_t x = BOTTOM; x < TOP; x++) |
| 164 | + for (uint8_t y = BOTTOM; y < TOP-1; y++) |
| 165 | + if (MAT(x,y) != 0 && MAT(x, y+1) != 0) |
| 166 | + { |
| 167 | + x_p[len] = x; |
| 168 | + y_p[len] = y; |
| 169 | + len++; |
| 170 | + } |
| 171 | + } |
| 172 | + if (len == 0) |
| 173 | + { |
| 174 | + init_field(); |
| 175 | + return; |
| 176 | + } |
| 177 | +
|
| 178 | + int prev_pos = -1; |
| 179 | + bool moved = len == 1; |
| 180 | + int d = 50; |
| 181 | + for (;;) |
| 182 | + { |
| 183 | + int val = ; |
| 184 | + int pos = (val - 12)/(1000/len); |
| 185 | + if (pos < 0) |
| 186 | + pos = 0; |
| 187 | + if (pos >= len) |
| 188 | + pos = len - 1; |
| 189 | + if (prev_pos != -1 && pos != prev_pos) |
| 190 | + { |
| 191 | + moved = true; |
| 192 | + d = 50; |
| 193 | + } |
| 194 | + else if (moved) |
| 195 | + d += 50; |
| 196 | + prev_pos = pos; |
| 197 | + |
| 198 | + RESETMAT(x_p[pos], y_p[pos]); |
| 199 | + RESETMAT(x_p[pos]+v_x, y_p[pos]+v_y); |
| 200 | + showmat(); |
| 201 | + |
| 202 | + delay(d); |
| 203 | +
|
| 204 | + if (d > 500) |
| 205 | + break; |
| 206 | + |
| 207 | + SETMAT(x_p[pos], y_p[pos]); |
| 208 | + SETMAT(x_p[pos]+v_x, y_p[pos]+v_y); |
| 209 | + showmat(); |
| 210 | + |
| 211 | + delay(d); |
| 212 | + } |
| 213 | + vert_player = !vert_player; |
| 214 | +} |
| 215 | +*/ |
0 commit comments