Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Matrix PinDetect mbed
Fork of 4180_Project_Finalcode by
main.cpp
- Committer:
- hdao9
- Date:
- 2018-04-22
- Revision:
- 2:1525f7ae330e
- Parent:
- 1:85d63d7193e3
- Child:
- 4:37b7ed4aa26b
File content as of revision 2:1525f7ae330e:
#include "mbed.h"
#include "NeoStrip.h"
#include "PinDetect.h"
#include "Matrix.h"
#define N 64
NeoStrip strip(p18, N); //8x8 neopixel matrix with each neopixel addressable by its index which range from 0 to 63
Matrix board(8,7); //the Connect Four game board is a 6x7 matrix, the neopixel matrix is 8x8, created an 8x7 matrix to keep track of moves
PinDetect right(p5);
PinDetect left(p7);
PinDetect center(p8);
int pos = 0; //current position of led in Neopixel index
int row = 0;
int col = 0;
int winmode = 0; //0 means the game is not won, 1-4 are different ways to win
int red = 0xFF0000;
int blue = 0x0000FF;
int color = red;
int winningcolor = 0;
void alloff() //turn off all neopixel at the beginning
{
for (int i = 0; i < N; i++)
strip.setPixel(i, 0, 0, 0);
strip.write();
}
int matrix2index(int r, int c) //convert row,col of matrix to index of neopixel that range from 0 to 63
{
return (r-1)*8 + c - 1;
}
//when the game is won, flash the neopixels that lead to winning forever until the game is restarted
void display_winner(int mode, int r, int c, int wincolor)
{
switch (mode)
{
case 1:
while (1)
{
strip.setPixel(matrix2index(r,c), wincolor);
strip.setPixel(matrix2index(r,c+1), wincolor);
strip.setPixel(matrix2index(r,c+2), wincolor);
strip.setPixel(matrix2index(r,c+3), wincolor);
strip.write();
wait(0.2);
strip.setPixel(matrix2index(r,c), 0, 0, 0);
strip.setPixel(matrix2index(r,c+1), 0, 0, 0);
strip.setPixel(matrix2index(r,c+2), 0, 0, 0);
strip.setPixel(matrix2index(r,c+3), 0, 0, 0);
strip.write();
wait(0.2);
}
case 2:
while (1)
{
strip.setPixel(matrix2index(r,c), wincolor);
strip.setPixel(matrix2index(r+1,c), wincolor);
strip.setPixel(matrix2index(r+2,c), wincolor);
strip.setPixel(matrix2index(r+3,c), wincolor);
strip.write();
wait(0.2);
strip.setPixel(matrix2index(r,c), 0, 0, 0);
strip.setPixel(matrix2index(r+1,c), 0, 0, 0);
strip.setPixel(matrix2index(r+2,c), 0, 0, 0);
strip.setPixel(matrix2index(r+3,c), 0, 0, 0);
strip.write();
wait(0.2);
}
case 3:
while (1)
{
strip.setPixel(matrix2index(r,c), wincolor);
strip.setPixel(matrix2index(r+1,c+1), wincolor);
strip.setPixel(matrix2index(r+2,c+2), wincolor);
strip.setPixel(matrix2index(r+3,c+3), wincolor);
strip.write();
wait(0.2);
strip.setPixel(matrix2index(r,c), 0, 0, 0);
strip.setPixel(matrix2index(r+1,c+1), 0, 0, 0);
strip.setPixel(matrix2index(r+2,c+2), 0, 0, 0);
strip.setPixel(matrix2index(r+3,c+3), 0, 0, 0);
strip.write();
wait(0.2);
}
case 4:
while (1)
{
strip.setPixel(matrix2index(r,c), wincolor);
strip.setPixel(matrix2index(r+1,c-1), wincolor);
strip.setPixel(matrix2index(r+2,c-2), wincolor);
strip.setPixel(matrix2index(r+3,c-3), wincolor);
strip.write();
wait(0.2);
strip.setPixel(matrix2index(r,c), 0, 0, 0);
strip.setPixel(matrix2index(r+1,c-1), 0, 0, 0);
strip.setPixel(matrix2index(r+2,c-2), 0, 0, 0);
strip.setPixel(matrix2index(r+3,c-3), 0, 0, 0);
strip.write();
wait(0.2);
}
default:
break;
}
}
void check_winner()
{
//check 4 in a row
for (int r = 1; r < 9; r++)
{
for (int c = 1; c < 6; c++)
{
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))
{//have winner
row = r; col = c; winmode = 1; winningcolor = color;
return; //avoid the uncessary of continue to check
}
}
}
//check 4 in a col
for (int c = 1; c < 9; c++)
{
for (int r = 1; r < 6; r++)
{
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))
{//have winner
row = r; col = c; winmode = 2; winningcolor = color;
return;
}
}
}
//check 4 in a forward diagonal
for (int r = 3; r < 6; r++)
{
for (int c = 1; c < 5; c++)
{
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))
{//have winner
row = r; col = c; winmode = 3; winningcolor = color;
return;
}
}
}
//check 4 in a reverse diagonal
for (int r = 3; r < 6; r++)
{
for (int c = 4; c < 8; c++)
{
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))
{//have winner
row = r; col = c; winmode = 4; winningcolor = color;
return;
}
}
}
}
void right_hit_callback (void) { //move to the flashing neopixel to the right
//led1 = !led1;
strip.setPixel(pos, 0, 0, 0); //turn off the current neopixel
if (pos < 6) pos = pos + 1; //move the neopixel to the right if not out of range
}
void left_hit_callback (void) { //move the flashing neopixel to the left
//led3 = !led3;
strip.setPixel(pos, 0, 0, 0);
if (pos > 0) pos = pos - 1;
}
void center_hit_callback (void) { //drop the coin
//turn off the flashing neopixel on top row
strip.setPixel(pos, 0, 0, 0);
//then coin need to appear at the current column, on the lowest row that not occupied
col = pos + 1;
for(row = 8; row > 1; row-- )
{
if (board.getNumber(row,col) == 0) break; //break out of loop when found the lowest unoccupied row
}
//convert to neopixel index to turn of that neopixel, but only if not all rows in 6x7 board are occupied
if (row > 2) strip.setPixel(matrix2index(row,col), color);
else return;
//after added LED to that position
if (color == red)
{
board.add( row, col, 1.0);//update matrix to have a matrix to check winner
check_winner(); //check if the winning move is made
color = blue; //change player/color
}
else
{
board.add( row, col, 2.0);
check_winner();
color = red;
}
}
int main() {
//initialize the matrix to 0 meaning all rows and cols are unoccupied
board << 0 << 0 << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << 0 << 0 << 0 << 0 << 0
<< 0 << 0 << 0 << 0 << 0 << 0 << 0;
// Use internal pullups for pushbutton
right.mode(PullUp);
left.mode(PullUp);
center.mode(PullUp);
// Delay for initial pullup to take effect
wait(.05);
// Setup Interrupt callback functions for a pb hit
right.attach_deasserted(&right_hit_callback); //used to move coin
left.attach_deasserted(&left_hit_callback); //used to move coin
center.attach_deasserted(¢er_hit_callback); //used to drop coin
// Start sampling pb inputs using interrupts
right.setSampleFrequency();
left.setSampleFrequency();
center.setSampleFrequency();
wait(.01);
float bright = 0.2; // 20% is plenty for indoor use
strip.setBrightness(bright); // set default brightness
alloff(); //initial all neopixels to off
while(1)
{
//winmode is initialized to 0 (not won and the function return)
//winmode is changed in check_winner
//need to call display_winner here rather than calling from interrupt fuction because display_winner loop forever if the game is won
display_winner(winmode,row,col,winningcolor);
//flashing the current position indicating which col will the disc be dropped at
strip.setPixel(pos, color);
strip.write();
wait(0.2);
strip.setPixel(pos, 0, 0, 0);
strip.write();
wait(0.2);
}
}
