#include <MD_MAX72xx.h>
#include <IRremote.h>
#include "display.h"
#include "cpuMoves.h"
#define PIN_RECEIVER 2 // Signal Pin of IR receiver
IRrecv receiver(PIN_RECEIVER);
//const int maxX = MAX_DEVICES * 8 - 1;
//const int maxY = 7;
//display variables
float flashTimer = 10000;
int flashOn;
int flashOff;
int pressed;
// Game Board Variables
int stage;
bool winner;
void setup() {
pinMode(Max7219_pinCLK, OUTPUT);
pinMode(Max7219_pinCS, OUTPUT);
pinMode(Max7219_pinDIN, OUTPUT);
mx.begin();
//mx.control(MD_MAX72XX::INTENSITY, MAX_INTENSITY / 2);
mx.clear();
mx.setPoint(0, 0, true);
//Serial.begin(115200);
receiver.enableIRIn(); // Start the receiver
flashOn = 0; //count
flashOff = flashTimer;
turn = 'u';
pressed = 0;
difficulty = 0; //1=difficulty 2=whos turn 3=gameplay, 4=reset
stage = 1; //1=difficulty 2=whos turn 3=gameplay, 4=reset
winner = false;
endGame = '0';
randomSeed(analogRead(0));
}
void loop() {
//Iniatialise each stages Leds
if(stage == 1){
setDifficulty();
}else if(stage == 2){
setWhoGoesFirst();
}else if(stage == 3){
setBoard();
// CPUs Turn
if(turn == 'c' && !winner){
cpuMove();
}
}
// Check for input and store
if (receiver.decode()) {
pressed = translateIR();
receiver.resume(); // Receive the next value
}
// If a button is pressed
if(pressed != 0){
// If difficulty is selected
if(stage == 1 && pressed > 0 && pressed < 4){
//pressed 1,2 or 3,
difficulty = pressed;
stage = 2;
removeDifficulty();
// If cpu or Human is selected
}else if(stage == 2 && (pressed==1 || pressed==2)){
turn = pressed == 1 ? 'c' : 'h'; //1=CPU 2=Human
stage = 3;
removeWhoGoesFirst();
// If human turn is selected
}else if(stage == 3 && turn == 'h' && pressed > 0 && pressed < 10 && !winner){
setHumanSelection();
}else if(endGame != '0' && pressed == 10){
//Serial.print("turn"); Serial.println(turn);
//Serial.print("endGame"); Serial.println(endGame);
//Serial.print(gameBoard[0][2]); Serial.print(gameBoard[1][2]); Serial.println(gameBoard[2][2]);
//Serial.print(gameBoard[0][1]); Serial.print(gameBoard[1][1]); Serial.println(gameBoard[2][1]);
//Serial.print(gameBoard[0][0]); Serial.print(gameBoard[1][0]); Serial.println(gameBoard[2][0]);
turn = 'u';
difficulty = 0; //1=difficulty 2=whos turn 3=gameplay, 4=reset
stage = 1; //1=difficulty 2=whos turn 3=gameplay, 4=reset
winner = false;
endGame = '0';
resetGameBoard();
blankBoard();
}
}
pressed = 0;
checkForEndGame();
// Apply new Leds and Wait
if(flashOn < flashTimer){
if(flashOn == 0) {
//mx.displayText("A", PA_RIGHT, 2, 1, PA_RANDOM, PA_SLICE)
updateGameBoard();
turnLedsOn();
mx.setColumn(0,0x1A);
AddLed(8, 0x1A, true);
}
flashOn++;
if(flashOn == flashTimer){
flashOff=0;
}
}
// Turn off Flashing Leds and Wait
if(flashOff < flashTimer){
if(flashOff == 0) {
updateGameBoard();
turnSomeOff();
}
flashOff++;
if(flashOff == flashTimer){
flashOn=0;
}
}
}
void setHumanSelection(){
switch (pressed) {
case 1: if(gameBoard[0][2] == 'e'){
gameBoard[0][2] = 'h';
delay(100);
turn = 'c'; } break;
case 2: if(gameBoard[1][2] == 'e'){
gameBoard[1][2] = 'h';
delay(100);
turn = 'c'; } break;
case 3: if(gameBoard[2][2] == 'e'){
gameBoard[2][2] = 'h';
delay(100);
turn = 'c'; } break; // #3 is at 2,2
case 4: if(gameBoard[0][1] == 'e'){
gameBoard[0][1] = 'h';
delay(100);
turn = 'c'; } break;
case 5: if(gameBoard[1][1] == 'e'){
gameBoard[1][1] = 'h';
delay(100);
turn = 'c'; } break;
case 6: if(gameBoard[2][1] == 'e'){
gameBoard[2][1] = 'h';
delay(100);
turn = 'c'; } break;
case 7: if(gameBoard[0][0] == 'e'){
gameBoard[0][0] = 'h';
delay(100);
turn = 'c'; } break; // #7 is at 0,0
case 8: if(gameBoard[1][0] == 'e'){
gameBoard[1][0] = 'h';
delay(100);
turn = 'c'; } break;
case 9: if(gameBoard[2][0] == 'e'){
gameBoard[2][0] = 'h';
delay(100);
turn = 'c'; } break;
default: delay(100);
}
}
int translateIR(){
// Takes command based on IR code received
int tempCode = receiver.decodedIRData.command;
int returned =0 ;
switch (tempCode) {
case 162:
//Serial.print("POWER");
returned = 11;
break;
case 226:
//Serial.print("MENU");
break;
case 34:
//Serial.print("TEST");
break;
case 2:
//Serial.print("PLUS");
break;
case 194:
//Serial.print("BACK");
returned = 10;
break;
case 224:
//Serial.print("PREV.");
break;
case 168:
//Serial.print("PLAY");
break;
case 144:
//Serial.print("NEXT");
break;
case 104:
//Serial.print("num: 0");
break;
case 152:
//Serial.print("MINUS");
break;
case 176:
//Serial.print("keyC");
break;
case 48:
//Serial.print("num1");
returned = 1;
break;
case 24:
//Serial.print("num2");
returned = 2;
break;
case 122:
//Serial.print("num3");
returned = 3;
break;
case 16:
//Serial.print("num4");
returned = 4;
break;
case 56:
//Serial.print("num5");
returned = 5;
break;
case 90:
//Serial.print("num6");
returned = 6;
break;
case 66:
//Serial.print("num7");
returned = 7;
break;
case 74:
//Serial.print("num8");
returned = 8;
break;
case 82:
//Serial.print("num9");
returned = 9;
break;
default:
delay(100);
//Serial.print("DEFAULT");
}
return returned;
}
void checkForEndGame(){
//horiz1, 2, 3
//verti1, 2, 3
//cross1, 2
if(!winner){
int count = 0;
for(int x = 0; x<3; x++){
for(int y = 0; y<3; y++){
if(gameBoard[x][y] == 'e') count++;
}
}
if(count == 0){
endGame = 'd';
}
}
if(endGame != '0' ) {
stage = 4;
}
}