Skip to content

Instantly share code, notes, and snippets.

@jaydp17
Last active December 21, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaydp17/6250988 to your computer and use it in GitHub Desktop.
Save jaydp17/6250988 to your computer and use it in GitHub Desktop.
Arduino almost working code
#include <BiColorLED.h>
// LEDPad.cpp
class LEDPad
{
public:
BiColorLED leds[9];
LEDPad() {
int j = 0;
for (int i = 0; i < 9; ++i) {
if(j == 14) { j++; i--; continue; }
leds[i] = BiColorLED(j++,j++);
}
}
void turnOnLED(int i, int color) {
leds[i].setColor(color);
}
};
// Board.cpp
//
// [ 0, 1, 2,
// 3, 4, 5,
// 6, 7, 8 ]
//
const int CONTINUE = 10; // status continue
const int P1_Wins = 1; // player 1 wins
const int P2_Wins = 2; // player 2 wins
const int DRAW = 3;
const int EMPTY = 0;
const int X = 1; // red - player 1
const int O = 2; // green - player 2
class Board
{
public:
LEDPad lp;
Board() {
currentBlink = 0;
currentOn = true;
for (int i = 0; i < 9; ++i) {
cells[i] = EMPTY;
}
user = 1;
status = CONTINUE;
}
int moveUp() {
int temp = currentBlink - 3;
while (temp >= 0) {
Serial.println("Inside Down");
if( cells[temp] == EMPTY ) {
stopBlinking();
currentBlink = temp;
return true;
}
temp -= 3;
}
return false;
}
int moveDown() {
int temp = currentBlink + 3;
while (temp < 9) {
Serial.println("Inside Down");
if( cells[temp] == EMPTY ) {
stopBlinking();
currentBlink = temp;
return true;
}
temp += 3;
}
return false;
}
int moveLeft() {
int temp = currentBlink - 1;
while (temp >= 0 && currentBlink - temp <= 3) {
Serial.println("Inside LEFT");
if( cells[temp] == EMPTY ) {
stopBlinking();
currentBlink = temp;
return true;
}
temp -= 1;
}
return false;
}
int moveRight() {
int temp = currentBlink + 1;
while (temp < 9) {
Serial.println("Inside RIGHT");
if( cells[temp] == EMPTY ) {
stopBlinking();
currentBlink = temp;
return true;
}
temp += 1;
}
return false;
}
void stopBlinking() {
lp.turnOnLED(currentBlink, 0);
}
//
// returns
// 1 = Player 1 wins
// 2 = Player 2 wins
// 0 = Draw
// 10 = Continue
//
int checkWinner() {
if (cells[0] != 0 && cells[0] == cells[4] && cells[4] == cells[8]) {
if (cells[0] == X) status = P1_Wins;
else status = P2_Wins;
return status;
} else if (cells[2] != 0 && cells[2] == cells[4] && cells[4] == cells[6]) {
if (cells[2] == X) status = P1_Wins;
else status = P2_Wins;
return status;
}
for (int i = 0; i < 3; ++i)
{
if (cells[i] != 0 && cells[i] == cells[i+3] && cells[i+3] == cells[i+6]) {
if (cells[i] == X) status = P1_Wins;
else status = P2_Wins;
return status;
} else if (cells[i*3] != 0 && cells[i*3] == cells[(i*3)+1] && cells[(i*3)+1] == cells[(i*3)+2]) {
if (cells[i*3] == X) status = P1_Wins;
else status = P2_Wins;
return status;
}
}
int d;
for (d = 0; d < 9; ++d) {
if(cells[d] == 0) break;
}
if(d == 9){
status = DRAW;
return status;
}
status = CONTINUE;
return status;
}
void blink() {
Serial.print("CurrentOn : ");
Serial.println(currentOn);
if(currentOn) {
lp.turnOnLED(currentBlink, 0);
} else {
lp.turnOnLED(currentBlink, user);
}
currentOn = !currentOn;
for (int i = 0; i < 9; ++i) {
Serial.print(cells[i]);
Serial.print(" ");
}Serial.println("");
}
void pressOK() {
lp.turnOnLED(currentBlink, user);
cells[currentBlink] = user;
for (int i = 0; i < 9; ++i) {
if(cells[i]==0)
currentBlink = i;
}
if (user == 1) user = 2;
else user = 1;
checkWinner();
}
void setColorToAllLED(int color) {
for (int i = 0; i < 9; ++i) {
lp.turnOnLED(i,color);
}
}
int cells[9];
int currentBlink;
bool currentOn;
// 1 is player 1 -- RED
// 2 is player 2 -- GREEN
int user;
int status;
};
class GamePad
{
public:
Board board;
long blinkTime;
GamePad(){
board = Board();
j = 1;
Button[0][0] = 1; Button[0][1] = 805; Button[0][2] = 838; // button 1
Button[1][0] = 2; Button[1][1] = 720; Button[1][2] = 750; // button 1
Button[2][0] = 3; Button[2][1] = 560; Button[2][2] = 611; // button 1
Button[3][0] = 4; Button[3][1] = 260; Button[3][2] = 340; // button 1
Button[4][0] = 5; Button[4][1] = 150; Button[4][2] = 179; // button 1
Button[5][0] = 6; Button[5][1] = 75; Button[5][2] = 90; // button 1
analogpin = A5;
label = 0;
counter = 0;
time = 0;
debounce_count = 100;
current_state = 0;
}
private:
int j; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and there high and low values
// 1 = Left
// 2 = TOP
// 3 = RIGHT
// 4 = DOWN
// 5 = OK
// 6 = SWITCH MODE
int Button[6][3];
int analogpin; // analog pin to read the buttons
int label; // for reporting the button label
int counter; // how many times we have seen new value
long time; // the last time the output pin was sampled
int debounce_count; // number of millis/samples to consider before declaring a debounced input
int current_state; // the debounced input value
int ButtonVal;
public:
void start() {
while(board.checkWinner() == CONTINUE) {
// If we have gone on to the next millisecond
if(blinkTime == millis()) {
Serial.println("Blinking");
board.blink();
blinkTime = millis() + 600;
}
if (millis() != time) {
// check analog pin for the button value and save it to ButtonVal
ButtonVal = analogRead(analogpin);
// Serial.println(ButtonVal);
if(ButtonVal == current_state && counter >0) {
counter--;
}
if(ButtonVal != current_state) {
counter++;
}
// If ButtonVal has shown the same value for long enough let's switch it
if (counter >= debounce_count) {
counter = 0;
current_state = ButtonVal;
//Checks which button has been pressed
if (ButtonVal > 0) {
ButtonCheck();
}
}
time = millis();
}
}
// stop blinking
board.stopBlinking();
//Declare winner
if(board.checkWinner() == DRAW) {
while(true) {
board.setColorToAllLED( 1 );
delay(200);
board.setColorToAllLED( 2 );
delay(200);
}
}
while(true) {
board.setColorToAllLED( board.checkWinner() );
delay(300);
board.setColorToAllLED( 0 );
delay(300);
}
}
void ButtonCheck() {
// loop for scanning the button array.
for(int i = 0; i <= 6; i++) {
// checks the ButtonVal against the high and low vales in the array
if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1]) {
// stores the button number to a variable
label = Button[i][0];
Action();
}
}
}
void Action() {
if(label == 1) {
Serial.println("LEFT");
board.moveLeft();
}
if(label == 2) {
Serial.println("TOP");
board.moveUp();
}
if(label == 3) {
Serial.println("Right");
board.moveRight();
}
if(label == 4) {
Serial.println("DOWN");
board.moveDown();
}
if(label == 5) {
Serial.println("OK");
board.pressOK();
}
if(label == 6) {
Serial.println("MODE");
}
//Serial.println("Button =:");
//Serial.println(label);
//delay(200);
}
};
GamePad gp;
//Board bda;
void setup() {
// Serial.begin(9600);
gp.blinkTime = millis() + 600;
}
void loop() {
gp.start();
//bda.setColorToAllLED(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment