![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Connect 4 game using the NeoPixel Matrix 8x8 RGB LED and a 5-way Tactile Navigation Switch.
Dependencies: Matrix PinDetect mbed
Fork of 4180_Project_Finalcode by
main.cpp@1:85d63d7193e3, 2018-04-21 (annotated)
- Committer:
- hdao9
- Date:
- Sat Apr 21 23:47:41 2018 +0000
- Revision:
- 1:85d63d7193e3
- Parent:
- 0:f0807589902f
- Child:
- 2:1525f7ae330e
finished code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hdao9 | 0:f0807589902f | 1 | #include "mbed.h" |
hdao9 | 0:f0807589902f | 2 | #include "NeoStrip.h" |
hdao9 | 0:f0807589902f | 3 | #include "PinDetect.h" |
hdao9 | 1:85d63d7193e3 | 4 | #include "Matrix.h" |
hdao9 | 0:f0807589902f | 5 | |
hdao9 | 0:f0807589902f | 6 | #define N 64 |
hdao9 | 0:f0807589902f | 7 | |
hdao9 | 0:f0807589902f | 8 | NeoStrip strip(p18, N); |
hdao9 | 0:f0807589902f | 9 | |
hdao9 | 1:85d63d7193e3 | 10 | Matrix board(8,7); |
hdao9 | 1:85d63d7193e3 | 11 | |
hdao9 | 0:f0807589902f | 12 | PinDetect right(p5); |
hdao9 | 1:85d63d7193e3 | 13 | //PinDetect down(p6); |
hdao9 | 0:f0807589902f | 14 | PinDetect left(p7); |
hdao9 | 0:f0807589902f | 15 | PinDetect center(p8); |
hdao9 | 1:85d63d7193e3 | 16 | //PinDetect up(p9); |
hdao9 | 0:f0807589902f | 17 | |
hdao9 | 1:85d63d7193e3 | 18 | //DigitalOut led1(LED1); |
hdao9 | 1:85d63d7193e3 | 19 | //DigitalOut led2(LED2); |
hdao9 | 1:85d63d7193e3 | 20 | //DigitalOut led3(LED3); |
hdao9 | 1:85d63d7193e3 | 21 | //DigitalOut led4(LED4); |
hdao9 | 0:f0807589902f | 22 | |
hdao9 | 0:f0807589902f | 23 | int pos = 0; |
hdao9 | 1:85d63d7193e3 | 24 | int row = 0; |
hdao9 | 1:85d63d7193e3 | 25 | int col = 0; |
hdao9 | 1:85d63d7193e3 | 26 | int winmode = 0; |
hdao9 | 0:f0807589902f | 27 | |
hdao9 | 0:f0807589902f | 28 | int red = 0xFF0000; |
hdao9 | 0:f0807589902f | 29 | int blue = 0x0000FF; |
hdao9 | 0:f0807589902f | 30 | int color = red; |
hdao9 | 1:85d63d7193e3 | 31 | int winningcolor = 0; |
hdao9 | 0:f0807589902f | 32 | |
hdao9 | 0:f0807589902f | 33 | void alloff() |
hdao9 | 0:f0807589902f | 34 | { |
hdao9 | 0:f0807589902f | 35 | for (int i = 0; i < N; i++) |
hdao9 | 0:f0807589902f | 36 | strip.setPixel(i, 0, 0, 0); |
hdao9 | 0:f0807589902f | 37 | strip.write(); |
hdao9 | 0:f0807589902f | 38 | } |
hdao9 | 0:f0807589902f | 39 | |
hdao9 | 1:85d63d7193e3 | 40 | int matrix2index(int r, int c) |
hdao9 | 1:85d63d7193e3 | 41 | { |
hdao9 | 1:85d63d7193e3 | 42 | return (r-1)*8 + c - 1; |
hdao9 | 1:85d63d7193e3 | 43 | } |
hdao9 | 1:85d63d7193e3 | 44 | |
hdao9 | 1:85d63d7193e3 | 45 | void display_winner(int mode, int r, int c, int wincolor) |
hdao9 | 1:85d63d7193e3 | 46 | { |
hdao9 | 1:85d63d7193e3 | 47 | switch (mode) |
hdao9 | 1:85d63d7193e3 | 48 | { |
hdao9 | 1:85d63d7193e3 | 49 | case 1: |
hdao9 | 1:85d63d7193e3 | 50 | while (1) |
hdao9 | 1:85d63d7193e3 | 51 | { |
hdao9 | 1:85d63d7193e3 | 52 | strip.setPixel(matrix2index(r,c), wincolor); |
hdao9 | 1:85d63d7193e3 | 53 | strip.setPixel(matrix2index(r,c+1), wincolor); |
hdao9 | 1:85d63d7193e3 | 54 | strip.setPixel(matrix2index(r,c+2), wincolor); |
hdao9 | 1:85d63d7193e3 | 55 | strip.setPixel(matrix2index(r,c+3), wincolor); |
hdao9 | 1:85d63d7193e3 | 56 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 57 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 58 | strip.setPixel(matrix2index(r,c), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 59 | strip.setPixel(matrix2index(r,c+1), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 60 | strip.setPixel(matrix2index(r,c+2), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 61 | strip.setPixel(matrix2index(r,c+3), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 62 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 63 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 64 | } |
hdao9 | 1:85d63d7193e3 | 65 | break; |
hdao9 | 1:85d63d7193e3 | 66 | case 2: |
hdao9 | 1:85d63d7193e3 | 67 | while (1) |
hdao9 | 1:85d63d7193e3 | 68 | { |
hdao9 | 1:85d63d7193e3 | 69 | strip.setPixel(matrix2index(r,c), wincolor); |
hdao9 | 1:85d63d7193e3 | 70 | strip.setPixel(matrix2index(r+1,c), wincolor); |
hdao9 | 1:85d63d7193e3 | 71 | strip.setPixel(matrix2index(r+2,c), wincolor); |
hdao9 | 1:85d63d7193e3 | 72 | strip.setPixel(matrix2index(r+3,c), wincolor); |
hdao9 | 1:85d63d7193e3 | 73 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 74 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 75 | strip.setPixel(matrix2index(r,c), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 76 | strip.setPixel(matrix2index(r+1,c), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 77 | strip.setPixel(matrix2index(r+2,c), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 78 | strip.setPixel(matrix2index(r+3,c), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 79 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 80 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 81 | } |
hdao9 | 1:85d63d7193e3 | 82 | break; |
hdao9 | 1:85d63d7193e3 | 83 | case 3: |
hdao9 | 1:85d63d7193e3 | 84 | while (1) |
hdao9 | 1:85d63d7193e3 | 85 | { |
hdao9 | 1:85d63d7193e3 | 86 | strip.setPixel(matrix2index(r,c), wincolor); |
hdao9 | 1:85d63d7193e3 | 87 | strip.setPixel(matrix2index(r+1,c+1), wincolor); |
hdao9 | 1:85d63d7193e3 | 88 | strip.setPixel(matrix2index(r+2,c+2), wincolor); |
hdao9 | 1:85d63d7193e3 | 89 | strip.setPixel(matrix2index(r+3,c+3), wincolor); |
hdao9 | 1:85d63d7193e3 | 90 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 91 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 92 | strip.setPixel(matrix2index(r,c), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 93 | strip.setPixel(matrix2index(r+1,c+1), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 94 | strip.setPixel(matrix2index(r+2,c+2), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 95 | strip.setPixel(matrix2index(r+3,c+3), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 96 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 97 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 98 | } |
hdao9 | 1:85d63d7193e3 | 99 | break; |
hdao9 | 1:85d63d7193e3 | 100 | case 4: |
hdao9 | 1:85d63d7193e3 | 101 | while (1) |
hdao9 | 1:85d63d7193e3 | 102 | { |
hdao9 | 1:85d63d7193e3 | 103 | strip.setPixel(matrix2index(r,c), wincolor); |
hdao9 | 1:85d63d7193e3 | 104 | strip.setPixel(matrix2index(r+1,c-1), wincolor); |
hdao9 | 1:85d63d7193e3 | 105 | strip.setPixel(matrix2index(r+2,c-2), wincolor); |
hdao9 | 1:85d63d7193e3 | 106 | strip.setPixel(matrix2index(r+3,c-3), wincolor); |
hdao9 | 1:85d63d7193e3 | 107 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 108 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 109 | strip.setPixel(matrix2index(r,c), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 110 | strip.setPixel(matrix2index(r+1,c-1), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 111 | strip.setPixel(matrix2index(r+2,c-2), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 112 | strip.setPixel(matrix2index(r+3,c-3), 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 113 | strip.write(); |
hdao9 | 1:85d63d7193e3 | 114 | wait(0.2); |
hdao9 | 1:85d63d7193e3 | 115 | } |
hdao9 | 1:85d63d7193e3 | 116 | break; |
hdao9 | 1:85d63d7193e3 | 117 | default: |
hdao9 | 1:85d63d7193e3 | 118 | break; |
hdao9 | 1:85d63d7193e3 | 119 | } |
hdao9 | 1:85d63d7193e3 | 120 | } |
hdao9 | 1:85d63d7193e3 | 121 | |
hdao9 | 1:85d63d7193e3 | 122 | void check_winner() |
hdao9 | 1:85d63d7193e3 | 123 | { |
hdao9 | 1:85d63d7193e3 | 124 | //check 4 in a row |
hdao9 | 1:85d63d7193e3 | 125 | for (int r = 1; r < 9; r++) |
hdao9 | 1:85d63d7193e3 | 126 | { |
hdao9 | 1:85d63d7193e3 | 127 | for (int c = 1; c < 6; c++) |
hdao9 | 1:85d63d7193e3 | 128 | { |
hdao9 | 1:85d63d7193e3 | 129 | if (board.getNumber(r,c) != 0 && board.getNumber(r,c) == board.getNumber(r,c+1) && board.getNumber(r,c+1) == board.getNumber(r,c+2) && board.getNumber(r,c+2) == board.getNumber(r,c+3)) |
hdao9 | 1:85d63d7193e3 | 130 | {//have winner |
hdao9 | 1:85d63d7193e3 | 131 | row = r; col = c; winmode = 1; winningcolor = color; |
hdao9 | 1:85d63d7193e3 | 132 | } |
hdao9 | 1:85d63d7193e3 | 133 | } |
hdao9 | 1:85d63d7193e3 | 134 | } |
hdao9 | 1:85d63d7193e3 | 135 | //check 4 in a col |
hdao9 | 1:85d63d7193e3 | 136 | for (int c = 1; c < 9; c++) |
hdao9 | 1:85d63d7193e3 | 137 | { |
hdao9 | 1:85d63d7193e3 | 138 | for (int r = 1; r < 6; r++) |
hdao9 | 1:85d63d7193e3 | 139 | { |
hdao9 | 1:85d63d7193e3 | 140 | if (board.getNumber(r,c) != 0 && board.getNumber(r,c) == board.getNumber(r+1,c) && board.getNumber(r+1,c) == board.getNumber(r+2,c) && board.getNumber(r+2,c) == board.getNumber(r+3,c)) |
hdao9 | 1:85d63d7193e3 | 141 | {//have winner |
hdao9 | 1:85d63d7193e3 | 142 | row = r; col = c; winmode = 2; winningcolor = color; |
hdao9 | 1:85d63d7193e3 | 143 | } |
hdao9 | 1:85d63d7193e3 | 144 | } |
hdao9 | 1:85d63d7193e3 | 145 | } |
hdao9 | 1:85d63d7193e3 | 146 | //check 4 in a forward diagonal |
hdao9 | 1:85d63d7193e3 | 147 | for (int r = 3; r < 6; r++) |
hdao9 | 1:85d63d7193e3 | 148 | { |
hdao9 | 1:85d63d7193e3 | 149 | for (int c = 1; c < 5; c++) |
hdao9 | 1:85d63d7193e3 | 150 | { |
hdao9 | 1:85d63d7193e3 | 151 | if (board.getNumber(r,c) != 0 && board.getNumber(r,c) == board.getNumber(r+1,c+1) && board.getNumber(r+1,c+1) == board.getNumber(r+2,c+2) && board.getNumber(r+2,c+2) == board.getNumber(r+3,c+3)) |
hdao9 | 1:85d63d7193e3 | 152 | {//have winner} |
hdao9 | 1:85d63d7193e3 | 153 | row = r; col = c; winmode = 3; winningcolor = color; |
hdao9 | 1:85d63d7193e3 | 154 | } |
hdao9 | 1:85d63d7193e3 | 155 | } |
hdao9 | 1:85d63d7193e3 | 156 | } |
hdao9 | 1:85d63d7193e3 | 157 | //check 4 in a reverse diagonal |
hdao9 | 1:85d63d7193e3 | 158 | for (int r = 3; r < 6; r++) |
hdao9 | 1:85d63d7193e3 | 159 | { |
hdao9 | 1:85d63d7193e3 | 160 | for (int c = 4; c < 8; c++) |
hdao9 | 1:85d63d7193e3 | 161 | { |
hdao9 | 1:85d63d7193e3 | 162 | if (board.getNumber(r,c) != 0 && board.getNumber(r,c) == board.getNumber(r+1,c-1) && board.getNumber(r+1,c-1) == board.getNumber(r+2,c-2) && board.getNumber(r+2,c-2) == board.getNumber(r+3,c-3)) |
hdao9 | 1:85d63d7193e3 | 163 | {//have winner} |
hdao9 | 1:85d63d7193e3 | 164 | row = r; col = c; winmode = 4; winningcolor = color; |
hdao9 | 1:85d63d7193e3 | 165 | } |
hdao9 | 1:85d63d7193e3 | 166 | } |
hdao9 | 1:85d63d7193e3 | 167 | } |
hdao9 | 1:85d63d7193e3 | 168 | } |
hdao9 | 1:85d63d7193e3 | 169 | |
hdao9 | 0:f0807589902f | 170 | void right_hit_callback (void) { //move to the flashing LED to the right |
hdao9 | 0:f0807589902f | 171 | //led1 = !led1; |
hdao9 | 0:f0807589902f | 172 | strip.setPixel(pos, 0, 0, 0); |
hdao9 | 1:85d63d7193e3 | 173 | if (pos < 6) pos = pos + 1; |
hdao9 | 0:f0807589902f | 174 | } |
hdao9 | 0:f0807589902f | 175 | void left_hit_callback (void) { //move the flashing LED to the left |
hdao9 | 0:f0807589902f | 176 | //led3 = !led3; |
hdao9 | 0:f0807589902f | 177 | strip.setPixel(pos, 0, 0, 0); |
hdao9 | 0:f0807589902f | 178 | if (pos > 0) pos = pos - 1; |
hdao9 | 0:f0807589902f | 179 | } |
hdao9 | 0:f0807589902f | 180 | |
hdao9 | 0:f0807589902f | 181 | //A random position in a 8x8 matrix have the neopixel index position: (row-1)*8 + col - 1 //with row and col start from 1 |
hdao9 | 0:f0807589902f | 182 | void center_hit_callback (void) { //drop the coin |
hdao9 | 0:f0807589902f | 183 | //led1 = 0; led2 = 0; led3 = 0; led4 = 0; |
hdao9 | 0:f0807589902f | 184 | // in order to drop a coin, first the flashing on top row need to disappear |
hdao9 | 0:f0807589902f | 185 | strip.setPixel(pos, 0, 0, 0); |
hdao9 | 0:f0807589902f | 186 | //then coin need to appear at the current column (which can be get from "pos"), on the lowest row not occupied |
hdao9 | 0:f0807589902f | 187 | //how to keep track of occupied row? |
hdao9 | 0:f0807589902f | 188 | col = pos + 1; |
hdao9 | 1:85d63d7193e3 | 189 | for(row = 8; row > 1; row-- ) |
hdao9 | 0:f0807589902f | 190 | { |
hdao9 | 1:85d63d7193e3 | 191 | if (board.getNumber(row,col) == 0) break; //have to break out of for loop becuz only want the first empty row |
hdao9 | 0:f0807589902f | 192 | } |
hdao9 | 0:f0807589902f | 193 | //got col from pos, once got row, then can convert to index position and light up led at that position |
hdao9 | 1:85d63d7193e3 | 194 | if (row > 2) strip.setPixel(matrix2index(row,col), color); |
hdao9 | 1:85d63d7193e3 | 195 | else return; |
hdao9 | 0:f0807589902f | 196 | //after added LED to that position |
hdao9 | 1:85d63d7193e3 | 197 | if (color == red) |
hdao9 | 1:85d63d7193e3 | 198 | { |
hdao9 | 1:85d63d7193e3 | 199 | board.add( row, col, 1.0);//update matrix to have a matrix to check winner |
hdao9 | 1:85d63d7193e3 | 200 | check_winner(); |
hdao9 | 1:85d63d7193e3 | 201 | color = blue; //change player/color |
hdao9 | 1:85d63d7193e3 | 202 | } |
hdao9 | 1:85d63d7193e3 | 203 | else |
hdao9 | 1:85d63d7193e3 | 204 | { |
hdao9 | 1:85d63d7193e3 | 205 | board.add( row, col, 2.0); |
hdao9 | 1:85d63d7193e3 | 206 | check_winner(); |
hdao9 | 1:85d63d7193e3 | 207 | color = red; |
hdao9 | 1:85d63d7193e3 | 208 | } |
hdao9 | 1:85d63d7193e3 | 209 | //there will be no dropping affect to avoid having to temporary stop the while loop in main |
hdao9 | 0:f0807589902f | 210 | } |
hdao9 | 0:f0807589902f | 211 | |
hdao9 | 0:f0807589902f | 212 | void one() //turn on one at a time to test strip |
hdao9 | 0:f0807589902f | 213 | { |
hdao9 | 0:f0807589902f | 214 | for (int i = 0; i < N; i++) |
hdao9 | 0:f0807589902f | 215 | { |
hdao9 | 0:f0807589902f | 216 | strip.setPixel(i, (uint8_t)(255), 0, 0); |
hdao9 | 0:f0807589902f | 217 | strip.write(); |
hdao9 | 0:f0807589902f | 218 | wait(1); |
hdao9 | 0:f0807589902f | 219 | } |
hdao9 | 1:85d63d7193e3 | 220 | } |
hdao9 | 0:f0807589902f | 221 | |
hdao9 | 0:f0807589902f | 222 | int main() |
hdao9 | 0:f0807589902f | 223 | { |
hdao9 | 1:85d63d7193e3 | 224 | |
hdao9 | 1:85d63d7193e3 | 225 | board << 0 << 0 << 0 << 0 << 0 << 0 << 0 |
hdao9 | 1:85d63d7193e3 | 226 | << 0 << 0 << 0 << 0 << 0 << 0 << 0 |
hdao9 | 1:85d63d7193e3 | 227 | << 0 << 0 << 0 << 0 << 0 << 0 << 0 |
hdao9 | 1:85d63d7193e3 | 228 | << 0 << 0 << 0 << 0 << 0 << 0 << 0 |
hdao9 | 1:85d63d7193e3 | 229 | << 0 << 0 << 0 << 0 << 0 << 0 << 0 |
hdao9 | 1:85d63d7193e3 | 230 | << 0 << 0 << 0 << 0 << 0 << 0 << 0 |
hdao9 | 1:85d63d7193e3 | 231 | << 0 << 0 << 0 << 0 << 0 << 0 << 0 |
hdao9 | 1:85d63d7193e3 | 232 | << 0 << 0 << 0 << 0 << 0 << 0 << 0; |
hdao9 | 1:85d63d7193e3 | 233 | |
hdao9 | 0:f0807589902f | 234 | // Use internal pullups for pushbutton |
hdao9 | 0:f0807589902f | 235 | right.mode(PullUp); |
hdao9 | 1:85d63d7193e3 | 236 | //down.mode(PullUp); |
hdao9 | 0:f0807589902f | 237 | left.mode(PullUp); |
hdao9 | 0:f0807589902f | 238 | center.mode(PullUp); |
hdao9 | 1:85d63d7193e3 | 239 | //up.mode(PullUp); |
hdao9 | 0:f0807589902f | 240 | // Delay for initial pullup to take effect |
hdao9 | 0:f0807589902f | 241 | wait(.05); |
hdao9 | 0:f0807589902f | 242 | // Setup Interrupt callback functions for a pb hit |
hdao9 | 0:f0807589902f | 243 | right.attach_deasserted(&right_hit_callback); //used to move coin |
hdao9 | 1:85d63d7193e3 | 244 | //down.attach_deasserted(&down_hit_callback); // not used |
hdao9 | 0:f0807589902f | 245 | left.attach_deasserted(&left_hit_callback); //used to move coin |
hdao9 | 0:f0807589902f | 246 | center.attach_deasserted(¢er_hit_callback); //used to drop coin |
hdao9 | 1:85d63d7193e3 | 247 | //up.attach_deasserted(&up_hit_callback); // not used |
hdao9 | 0:f0807589902f | 248 | // Start sampling pb inputs using interrupts |
hdao9 | 0:f0807589902f | 249 | right.setSampleFrequency(); |
hdao9 | 1:85d63d7193e3 | 250 | //down.setSampleFrequency(); |
hdao9 | 0:f0807589902f | 251 | left.setSampleFrequency(); |
hdao9 | 0:f0807589902f | 252 | center.setSampleFrequency(); |
hdao9 | 1:85d63d7193e3 | 253 | //up.setSampleFrequency(); |
hdao9 | 0:f0807589902f | 254 | wait(.01); |
hdao9 | 0:f0807589902f | 255 | |
hdao9 | 0:f0807589902f | 256 | float bright = 0.2; // 20% is plenty for indoor use |
hdao9 | 0:f0807589902f | 257 | strip.setBrightness(bright); // set default brightness |
hdao9 | 0:f0807589902f | 258 | alloff(); |
hdao9 | 0:f0807589902f | 259 | |
hdao9 | 0:f0807589902f | 260 | while(1) //flashing LED |
hdao9 | 0:f0807589902f | 261 | { |
hdao9 | 1:85d63d7193e3 | 262 | display_winner(winmode,row,col,winningcolor); |
hdao9 | 0:f0807589902f | 263 | strip.setPixel(pos, color); |
hdao9 | 0:f0807589902f | 264 | strip.write(); |
hdao9 | 0:f0807589902f | 265 | wait(0.2); |
hdao9 | 0:f0807589902f | 266 | strip.setPixel(pos, 0, 0, 0); |
hdao9 | 0:f0807589902f | 267 | strip.write(); |
hdao9 | 0:f0807589902f | 268 | wait(0.2); |
hdao9 | 0:f0807589902f | 269 | } |
hdao9 | 0:f0807589902f | 270 | |
hdao9 | 0:f0807589902f | 271 | } |