Connect Four Mbed Game

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Committer:
emathew92
Date:
Wed Oct 22 17:58:25 2014 +0000
Revision:
0:b3a0e0ee149d
Trial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emathew92 0:b3a0e0ee149d 1 #include "mbed.h"
emathew92 0:b3a0e0ee149d 2 #include "PinDetect.h"
emathew92 0:b3a0e0ee149d 3 #include "uLCD_4DGL.h"
emathew92 0:b3a0e0ee149d 4 #define YELLOW 0xFFFF00
emathew92 0:b3a0e0ee149d 5 uLCD_4DGL uLCD(p9, p10, p11);
emathew92 0:b3a0e0ee149d 6 DigitalOut myled(LED1);
emathew92 0:b3a0e0ee149d 7 PinDetect pb1(p18);
emathew92 0:b3a0e0ee149d 8 PinDetect pb2(p12);
emathew92 0:b3a0e0ee149d 9 PinDetect pb3(p13);
emathew92 0:b3a0e0ee149d 10 PinDetect pb4(p14);
emathew92 0:b3a0e0ee149d 11 PinDetect pb5(p15);
emathew92 0:b3a0e0ee149d 12 PinDetect pb6(p16);
emathew92 0:b3a0e0ee149d 13 PinDetect pb7(p17);
emathew92 0:b3a0e0ee149d 14 // Global column variable
emathew92 0:b3a0e0ee149d 15 int volatile col_value=0;
emathew92 0:b3a0e0ee149d 16 //Declaration of function that prints the coin on the board
emathew92 0:b3a0e0ee149d 17 void print_coin(int column);
emathew92 0:b3a0e0ee149d 18 //Declaration of function that prints the board on the uLCD
emathew92 0:b3a0e0ee149d 19 void display_board(void);
emathew92 0:b3a0e0ee149d 20 //Declaration of function that checks if we have a winner
emathew92 0:b3a0e0ee149d 21 void check_winner(int, int);
emathew92 0:b3a0e0ee149d 22 //Declaration of function that outputs the identity of the winner
emathew92 0:b3a0e0ee149d 23 void display_winner(char);
emathew92 0:b3a0e0ee149d 24 //Declaration of function that initializes the board - 2D array to a value of x
emathew92 0:b3a0e0ee149d 25 void init_board(void);
emathew92 0:b3a0e0ee149d 26 int player=0;
emathew92 0:b3a0e0ee149d 27 int array_x=0;
emathew92 0:b3a0e0ee149d 28 int array_y=0;
emathew92 0:b3a0e0ee149d 29 int row_calc[7]; //holds the no of coins already dropped into a column
emathew92 0:b3a0e0ee149d 30 int circle_x=0;
emathew92 0:b3a0e0ee149d 31 int circle_y=0;
emathew92 0:b3a0e0ee149d 32 char board[6][7];//2D array that stores the status of the board on every move
emathew92 0:b3a0e0ee149d 33 int count=0;
emathew92 0:b3a0e0ee149d 34 char check='x';
emathew92 0:b3a0e0ee149d 35 // Callback routine is interrupt activated by a debounced pb1 hit
emathew92 0:b3a0e0ee149d 36 void pb1_hit_callback (void)
emathew92 0:b3a0e0ee149d 37 {
emathew92 0:b3a0e0ee149d 38 myled=!myled;
emathew92 0:b3a0e0ee149d 39 col_value=1;
emathew92 0:b3a0e0ee149d 40 }
emathew92 0:b3a0e0ee149d 41 // Callback routine is interrupt activated by a debounced pb2 hit
emathew92 0:b3a0e0ee149d 42 void pb2_hit_callback (void)
emathew92 0:b3a0e0ee149d 43 {
emathew92 0:b3a0e0ee149d 44 myled=!myled;
emathew92 0:b3a0e0ee149d 45 col_value=2;
emathew92 0:b3a0e0ee149d 46 }
emathew92 0:b3a0e0ee149d 47 // Callback routine is interrupt activated by a debounced pb3 hit
emathew92 0:b3a0e0ee149d 48 void pb3_hit_callback (void)
emathew92 0:b3a0e0ee149d 49 {
emathew92 0:b3a0e0ee149d 50 myled=!myled;
emathew92 0:b3a0e0ee149d 51 col_value=3;
emathew92 0:b3a0e0ee149d 52 }
emathew92 0:b3a0e0ee149d 53 // Callback routine is interrupt activated by a debounced pb4 hit
emathew92 0:b3a0e0ee149d 54 void pb4_hit_callback (void)
emathew92 0:b3a0e0ee149d 55 {
emathew92 0:b3a0e0ee149d 56 myled=!myled;
emathew92 0:b3a0e0ee149d 57 col_value=4;
emathew92 0:b3a0e0ee149d 58 }
emathew92 0:b3a0e0ee149d 59 // Callback routine is interrupt activated by a debounced pb5 hit
emathew92 0:b3a0e0ee149d 60 void pb5_hit_callback (void)
emathew92 0:b3a0e0ee149d 61 {
emathew92 0:b3a0e0ee149d 62 myled=!myled;
emathew92 0:b3a0e0ee149d 63 col_value=5;
emathew92 0:b3a0e0ee149d 64 }
emathew92 0:b3a0e0ee149d 65 // Callback routine is interrupt activated by a debounced pb6 hit
emathew92 0:b3a0e0ee149d 66 void pb6_hit_callback (void)
emathew92 0:b3a0e0ee149d 67 {
emathew92 0:b3a0e0ee149d 68 myled=!myled;
emathew92 0:b3a0e0ee149d 69 col_value=6;
emathew92 0:b3a0e0ee149d 70 }
emathew92 0:b3a0e0ee149d 71 // Callback routine is interrupt activated by a debounced pb7 hit
emathew92 0:b3a0e0ee149d 72 void pb7_hit_callback (void)
emathew92 0:b3a0e0ee149d 73 {
emathew92 0:b3a0e0ee149d 74 myled=!myled;
emathew92 0:b3a0e0ee149d 75 col_value=7;
emathew92 0:b3a0e0ee149d 76 }
emathew92 0:b3a0e0ee149d 77 int main()
emathew92 0:b3a0e0ee149d 78 {
emathew92 0:b3a0e0ee149d 79 uLCD.baudrate(3000000);
emathew92 0:b3a0e0ee149d 80 pb1.mode(PullUp);
emathew92 0:b3a0e0ee149d 81 pb2.mode(PullUp);
emathew92 0:b3a0e0ee149d 82 pb3.mode(PullUp);
emathew92 0:b3a0e0ee149d 83 pb4.mode(PullUp);
emathew92 0:b3a0e0ee149d 84 pb5.mode(PullUp);
emathew92 0:b3a0e0ee149d 85 pb6.mode(PullUp);
emathew92 0:b3a0e0ee149d 86 pb7.mode(PullUp);
emathew92 0:b3a0e0ee149d 87 wait(0.04);
emathew92 0:b3a0e0ee149d 88 // Setup Interrupt callback functions for a given pb hit
emathew92 0:b3a0e0ee149d 89 pb1.attach_deasserted(&pb1_hit_callback);
emathew92 0:b3a0e0ee149d 90 pb2.attach_deasserted(&pb2_hit_callback);
emathew92 0:b3a0e0ee149d 91 pb3.attach_deasserted(&pb3_hit_callback);
emathew92 0:b3a0e0ee149d 92 pb4.attach_deasserted(&pb4_hit_callback);
emathew92 0:b3a0e0ee149d 93 pb5.attach_deasserted(&pb5_hit_callback);
emathew92 0:b3a0e0ee149d 94 pb6.attach_deasserted(&pb6_hit_callback);
emathew92 0:b3a0e0ee149d 95 pb7.attach_deasserted(&pb7_hit_callback);
emathew92 0:b3a0e0ee149d 96 // Start sampling pb inputs using interrupts
emathew92 0:b3a0e0ee149d 97 pb1.setSampleFrequency();
emathew92 0:b3a0e0ee149d 98 pb2.setSampleFrequency();
emathew92 0:b3a0e0ee149d 99 pb3.setSampleFrequency();
emathew92 0:b3a0e0ee149d 100 pb4.setSampleFrequency();
emathew92 0:b3a0e0ee149d 101 pb5.setSampleFrequency();
emathew92 0:b3a0e0ee149d 102 pb6.setSampleFrequency();
emathew92 0:b3a0e0ee149d 103 pb7.setSampleFrequency();
emathew92 0:b3a0e0ee149d 104 init_board();
emathew92 0:b3a0e0ee149d 105 display_board();
emathew92 0:b3a0e0ee149d 106
emathew92 0:b3a0e0ee149d 107 while(1)
emathew92 0:b3a0e0ee149d 108 {
emathew92 0:b3a0e0ee149d 109 print_coin(col_value);
emathew92 0:b3a0e0ee149d 110 }
emathew92 0:b3a0e0ee149d 111 }
emathew92 0:b3a0e0ee149d 112 void init_board(void)
emathew92 0:b3a0e0ee149d 113 {
emathew92 0:b3a0e0ee149d 114 for(int i=0;i<6;i++)
emathew92 0:b3a0e0ee149d 115 {
emathew92 0:b3a0e0ee149d 116 for(int j=0;j<7;j++)
emathew92 0:b3a0e0ee149d 117 {
emathew92 0:b3a0e0ee149d 118 board[i][j]='x';
emathew92 0:b3a0e0ee149d 119 }
emathew92 0:b3a0e0ee149d 120 }
emathew92 0:b3a0e0ee149d 121 }
emathew92 0:b3a0e0ee149d 122 void display_board(void)
emathew92 0:b3a0e0ee149d 123 {
emathew92 0:b3a0e0ee149d 124 uLCD.filled_rectangle(0, 0 , 128, 108, BLUE) ;
emathew92 0:b3a0e0ee149d 125 for(int j=0; j<6; j++)//This loop will be executed for each row once
emathew92 0:b3a0e0ee149d 126 {
emathew92 0:b3a0e0ee149d 127 circle_y=9+j*18;
emathew92 0:b3a0e0ee149d 128 for(int i=0; i<7; i++) //This loop will be executed 7 times for each row
emathew92 0:b3a0e0ee149d 129 {
emathew92 0:b3a0e0ee149d 130 circle_x=9+i*18;
emathew92 0:b3a0e0ee149d 131 uLCD.filled_circle(circle_x, circle_y , 8, BLACK) ;
emathew92 0:b3a0e0ee149d 132 }
emathew92 0:b3a0e0ee149d 133 }
emathew92 0:b3a0e0ee149d 134 }
emathew92 0:b3a0e0ee149d 135
emathew92 0:b3a0e0ee149d 136 void print_coin(int column)
emathew92 0:b3a0e0ee149d 137 {
emathew92 0:b3a0e0ee149d 138 if(0<column && column<8)
emathew92 0:b3a0e0ee149d 139 {
emathew92 0:b3a0e0ee149d 140 circle_x=9+(column-1)*18;
emathew92 0:b3a0e0ee149d 141 circle_y=(99-((row_calc[column-1])*18));
emathew92 0:b3a0e0ee149d 142 array_x=(5-(row_calc[column-1]));
emathew92 0:b3a0e0ee149d 143 array_y=(column-1);
emathew92 0:b3a0e0ee149d 144 if(player%2==0)
emathew92 0:b3a0e0ee149d 145 {
emathew92 0:b3a0e0ee149d 146 uLCD.filled_circle(circle_x, circle_y , 8, RED) ;
emathew92 0:b3a0e0ee149d 147 board[array_x][array_y]='r';//check
emathew92 0:b3a0e0ee149d 148 }
emathew92 0:b3a0e0ee149d 149 else
emathew92 0:b3a0e0ee149d 150 {
emathew92 0:b3a0e0ee149d 151 uLCD.filled_circle(circle_x, circle_y , 8, YELLOW) ;
emathew92 0:b3a0e0ee149d 152 board[array_x][array_y]='y';//check
emathew92 0:b3a0e0ee149d 153 }
emathew92 0:b3a0e0ee149d 154 row_calc[column-1]+=1;
emathew92 0:b3a0e0ee149d 155 player++;
emathew92 0:b3a0e0ee149d 156 check_winner(array_x,array_y);
emathew92 0:b3a0e0ee149d 157 wait(5);
emathew92 0:b3a0e0ee149d 158 }
emathew92 0:b3a0e0ee149d 159 }
emathew92 0:b3a0e0ee149d 160
emathew92 0:b3a0e0ee149d 161 void check_winner(int a, int b)
emathew92 0:b3a0e0ee149d 162 {
emathew92 0:b3a0e0ee149d 163 for(int i=0;i<4;i++)
emathew92 0:b3a0e0ee149d 164 {
emathew92 0:b3a0e0ee149d 165 if(board[a][i]==board[a][i+1] && board[a][i+1]==board[a][i+2] && board[a][i+2]==board[a][i+3])
emathew92 0:b3a0e0ee149d 166 {
emathew92 0:b3a0e0ee149d 167 uLCD.locate(0,15);
emathew92 0:b3a0e0ee149d 168 if(board[a][i]=='r' || board[a][i]=='y'){
emathew92 0:b3a0e0ee149d 169 uLCD.printf("row hit %c%c%c%c",board[a][i],board[a][i+1],board[a][i+2],board[a][i+3] );
emathew92 0:b3a0e0ee149d 170 display_winner(board[a][i]);
emathew92 0:b3a0e0ee149d 171 while(1)
emathew92 0:b3a0e0ee149d 172 {
emathew92 0:b3a0e0ee149d 173 }
emathew92 0:b3a0e0ee149d 174 }
emathew92 0:b3a0e0ee149d 175 }
emathew92 0:b3a0e0ee149d 176 }
emathew92 0:b3a0e0ee149d 177
emathew92 0:b3a0e0ee149d 178
emathew92 0:b3a0e0ee149d 179 /* To check if we have 4 similar coins in a particular column*/
emathew92 0:b3a0e0ee149d 180 for(int i=0;i<3;i++)
emathew92 0:b3a0e0ee149d 181 {
emathew92 0:b3a0e0ee149d 182 if(board[i][b]==board[i+1][b] && board[i+1][b]==board[i+2][b] && board[i+2][b]==board[i+3][b])
emathew92 0:b3a0e0ee149d 183 {
emathew92 0:b3a0e0ee149d 184 uLCD.locate(0,15);
emathew92 0:b3a0e0ee149d 185 if(board[i][b]=='r' || board[i][b]=='y'){
emathew92 0:b3a0e0ee149d 186 uLCD.printf("col hit %c%c%c%c",board[i][b],board[i+1][b],board[i+2][b]==board[i+3][b] );
emathew92 0:b3a0e0ee149d 187 display_winner(board[i][b]);
emathew92 0:b3a0e0ee149d 188 while(1)
emathew92 0:b3a0e0ee149d 189 {
emathew92 0:b3a0e0ee149d 190 }
emathew92 0:b3a0e0ee149d 191 }
emathew92 0:b3a0e0ee149d 192 }
emathew92 0:b3a0e0ee149d 193 }
emathew92 0:b3a0e0ee149d 194
emathew92 0:b3a0e0ee149d 195 //To check if we have 4 similar coins in the forward diagonal
emathew92 0:b3a0e0ee149d 196 int left_bottom_x=a;
emathew92 0:b3a0e0ee149d 197 int left_bottom_y=b;
emathew92 0:b3a0e0ee149d 198 while(left_bottom_x<5 && left_bottom_y>0)
emathew92 0:b3a0e0ee149d 199 {
emathew92 0:b3a0e0ee149d 200 left_bottom_x++;
emathew92 0:b3a0e0ee149d 201 left_bottom_y--;
emathew92 0:b3a0e0ee149d 202 }
emathew92 0:b3a0e0ee149d 203 while((((left_bottom_x)-3)>(-1)) && (((left_bottom_y)+3)<7))
emathew92 0:b3a0e0ee149d 204 {
emathew92 0:b3a0e0ee149d 205 if((board[left_bottom_x][left_bottom_y]==board[left_bottom_x-1][left_bottom_y+1]) && (board[left_bottom_x][left_bottom_y]==board[left_bottom_x-2][left_bottom_y+2]) && (board[left_bottom_x][left_bottom_y]==board[left_bottom_x-3][left_bottom_y+3]) )
emathew92 0:b3a0e0ee149d 206 {
emathew92 0:b3a0e0ee149d 207 if(board[left_bottom_x][left_bottom_y]=='r'||board[left_bottom_x][left_bottom_y]=='y'){
emathew92 0:b3a0e0ee149d 208 display_winner(board[left_bottom_x][left_bottom_y]);
emathew92 0:b3a0e0ee149d 209 while(1)
emathew92 0:b3a0e0ee149d 210 {
emathew92 0:b3a0e0ee149d 211 }
emathew92 0:b3a0e0ee149d 212 }
emathew92 0:b3a0e0ee149d 213 }
emathew92 0:b3a0e0ee149d 214 left_bottom_x--;
emathew92 0:b3a0e0ee149d 215 left_bottom_y++;
emathew92 0:b3a0e0ee149d 216 }
emathew92 0:b3a0e0ee149d 217
emathew92 0:b3a0e0ee149d 218
emathew92 0:b3a0e0ee149d 219 //To check if we have 4 similar coins in the backward diagonal
emathew92 0:b3a0e0ee149d 220 int right_bottom_x=a;
emathew92 0:b3a0e0ee149d 221 int right_bottom_y=b;
emathew92 0:b3a0e0ee149d 222 while((right_bottom_x<5) && (right_bottom_y<6))
emathew92 0:b3a0e0ee149d 223 {
emathew92 0:b3a0e0ee149d 224 right_bottom_x++;
emathew92 0:b3a0e0ee149d 225 right_bottom_y++;
emathew92 0:b3a0e0ee149d 226 }
emathew92 0:b3a0e0ee149d 227
emathew92 0:b3a0e0ee149d 228 while((((right_bottom_x)-3)>(-1)) && (((right_bottom_y)-3)>(-1)))
emathew92 0:b3a0e0ee149d 229 {
emathew92 0:b3a0e0ee149d 230 if((board[right_bottom_x][right_bottom_y]==board[right_bottom_x-1][right_bottom_y-1]) && (board[right_bottom_x][right_bottom_y]==board[right_bottom_x-2][right_bottom_y-2]) && (board[right_bottom_x][right_bottom_y]==board[right_bottom_x-3][right_bottom_y-3]) )
emathew92 0:b3a0e0ee149d 231 {
emathew92 0:b3a0e0ee149d 232 uLCD.locate(0,15);
emathew92 0:b3a0e0ee149d 233 if(board[right_bottom_x][right_bottom_y]=='r'||board[right_bottom_x][right_bottom_y]=='y'){
emathew92 0:b3a0e0ee149d 234 uLCD.printf("bdi hit %c%c%c%c",board[right_bottom_x][right_bottom_y],board[right_bottom_x-1][right_bottom_y-1],board[right_bottom_x-2][right_bottom_y-2],board[right_bottom_x-3][right_bottom_y-3] );
emathew92 0:b3a0e0ee149d 235 display_winner(board[right_bottom_x][right_bottom_y]);
emathew92 0:b3a0e0ee149d 236 while(1)
emathew92 0:b3a0e0ee149d 237 {
emathew92 0:b3a0e0ee149d 238 }
emathew92 0:b3a0e0ee149d 239 }
emathew92 0:b3a0e0ee149d 240 }
emathew92 0:b3a0e0ee149d 241 right_bottom_x--;
emathew92 0:b3a0e0ee149d 242 right_bottom_y--;
emathew92 0:b3a0e0ee149d 243 }
emathew92 0:b3a0e0ee149d 244 }
emathew92 0:b3a0e0ee149d 245
emathew92 0:b3a0e0ee149d 246 void display_winner(char a)
emathew92 0:b3a0e0ee149d 247 {
emathew92 0:b3a0e0ee149d 248 if(a=='r' || a=='y')
emathew92 0:b3a0e0ee149d 249 {
emathew92 0:b3a0e0ee149d 250 wait(2);
emathew92 0:b3a0e0ee149d 251 uLCD.cls();
emathew92 0:b3a0e0ee149d 252 uLCD.locate(4,6);
emathew92 0:b3a0e0ee149d 253 if(a=='r')
emathew92 0:b3a0e0ee149d 254 uLCD.printf("RED WINS");
emathew92 0:b3a0e0ee149d 255 else
emathew92 0:b3a0e0ee149d 256 uLCD.printf("YELLOW WINS");
emathew92 0:b3a0e0ee149d 257 }
emathew92 0:b3a0e0ee149d 258 }
emathew92 0:b3a0e0ee149d 259