#include "LedControl.h"
int cubo = 0;
int alto = 0;
int lado = 0;
int fase = 0;
int boton;
const int buno = 11;
const int bdos = 12;
const int btre = 10;
const int bcua = 9;
int anteriorboton = 0;
int marca = 5;
int puesto[8] = {10, 10, 10, 10, 10, 10, 10, 10};
int numero = 0;
int tapado = 2;
LedControl lc = LedControl(4, 2, 3, 2);
void cero() {
lc.setLed(cubo, 0 + alto, 1 + lado, false);
lc.setLed(cubo, 1 + alto, 0 + lado, false);
lc.setLed(cubo, 1 + alto, 2 + lado, false);
lc.setLed(cubo, 1 + alto, 1 + lado, false);
lc.setLed(cubo, 2 + alto, 1 + lado, false);
lc.setLed(cubo, 0 + alto, 2 + lado, false);
lc.setLed(cubo, 2 + alto, 0 + lado, false);
lc.setLed(cubo, 0 + alto, 0 + lado, false);
lc.setLed(cubo, 2 + alto, 2 + lado, false);
}
void as() {
cero();
lc.setLed(cubo, 0 + alto, 1 + lado, true);
lc.setLed(cubo, 1 + alto, 0 + lado, true);
lc.setLed(cubo, 1 + alto, 2 + lado, true);
lc.setLed(cubo, 1 + alto, 1 + lado, true);
lc.setLed(cubo, 2 + alto, 1 + lado, true);
}
void rey() {
cero();
lc.setLed(cubo, 0 + alto, 2 + lado, true);
lc.setLed(cubo, 1 + alto, 1 + lado, true);
lc.setLed(cubo, 2 + alto, 0 + lado, true);
lc.setLed(cubo, 0 + alto, 0 + lado, true);
lc.setLed(cubo, 2 + alto, 2 + lado, true);
}
void dama () {
cero();
lc.setLed(cubo, 0 + alto, 0 + lado, true);
lc.setLed(cubo, 0 + alto, 2 + lado, true);
lc.setLed(cubo, 2 + alto, 0 + lado, true);
lc.setLed(cubo, 2 + alto, 2 + lado, true);
}
void jota() {
cero();
lc.setLed(cubo, 0 + alto, 2 + lado, true);
lc.setLed(cubo, 1 + alto, 1 + lado, true);
lc.setLed(cubo, 2 + alto, 0 + lado, true);
}
void rojo() {
cero();
lc.setLed(cubo, 0 + alto, 0 + lado, true);
lc.setLed(cubo, 2 + alto, 2 + lado, true);
}
void negro() {
cero();
lc.setLed(cubo, 1 + alto, 1 + lado, true);
}
void crun() {
lc.setLed(cubo, 0, 3, true);
lc.setLed(cubo, 1, 3, true);
lc.setLed(cubo, 2, 3, true);
lc.setLed(cubo, 3, 3, true);
lc.setLed(cubo, 3, 2, true);
lc.setLed(cubo, 3, 1, true);
lc.setLed(cubo, 3, 0, true);
}
void crune() {
lc.setLed(cubo, 4, 0, false);
lc.setLed(cubo, 4, 1, false);
lc.setLed(cubo, 4, 2, false);
lc.setLed(cubo, 4, 3, false);
lc.setLed(cubo, 4, 4, false);
lc.setLed(cubo, 4, 5, false);
lc.setLed(cubo, 4, 6, false);
lc.setLed(cubo, 4, 7, false);
lc.setLed(cubo, 3, 0, false);
lc.setLed(cubo, 3, 1, false);
lc.setLed(cubo, 3, 2, false);
lc.setLed(cubo, 3, 3, false);
lc.setLed(cubo, 3, 4, false);
lc.setLed(cubo, 3, 5, false);
lc.setLed(cubo, 3, 6, false);
lc.setLed(cubo, 3, 7, false);
lc.setLed(cubo, 0, 3, false);
lc.setLed(cubo, 0, 4, false);
lc.setLed(cubo, 1, 3, false);
lc.setLed(cubo, 1, 4, false);
lc.setLed(cubo, 2, 3, false);
lc.setLed(cubo, 2, 4, false);
lc.setLed(cubo, 3, 3, false);
lc.setLed(cubo, 3, 4, false);
lc.setLed(cubo, 4, 3, false);
lc.setLed(cubo, 4, 4, false);
lc.setLed(cubo, 5, 3, false);
lc.setLed(cubo, 5, 4, false);
lc.setLed(cubo, 6, 3, false);
lc.setLed(cubo, 6, 4, false);
lc.setLed(cubo, 7, 3, false);
lc.setLed(cubo, 7, 4, false);
}
void crdo() {
lc.setLed(cubo, 4, 0, true);
lc.setLed(cubo, 4, 1, true);
lc.setLed(cubo, 4, 2, true);
lc.setLed(cubo, 4, 3, true);
lc.setLed(cubo, 5, 3, true);
lc.setLed(cubo, 6, 3, true);
lc.setLed(cubo, 7, 3, true);
}
void crtr() {
lc.setLed(cubo, 4, 7, true);
lc.setLed(cubo, 4, 6, true);
lc.setLed(cubo, 4, 5, true);
lc.setLed(cubo, 4, 4, true);
lc.setLed(cubo, 5, 4, true);
lc.setLed(cubo, 6, 4, true);
lc.setLed(cubo, 7, 4, true);
}
void crcu() {
lc.setLed(cubo, 3, 4, true);
lc.setLed(cubo, 3, 5, true);
lc.setLed(cubo, 3, 6, true);
lc.setLed(cubo, 3, 7, true);
lc.setLed(cubo, 2, 4, true);
lc.setLed(cubo, 1, 4, true);
lc.setLed(cubo, 0, 4, true);
}
void azar() {
numero = 0;
for (int i = 1; i < 9; i++) {
if (numero < 5) {
puesto[i] = random(1, 7);
numero++;
} else {
puesto[i] = 7;
}
if (i == tapado) {
i = 4;
}
}
}
void pon() {
switch (puesto[marca]) {
case 1: negro(); break;
case 2: rojo(); break;
case 3: jota(); break;
case 4: dama(); break;
case 5: rey(); break;
case 6: as(); break;
case 10: cero(); break;
case 11: negro(); break;
case 12: rojo(); break;
case 13: jota(); break;
case 14: dama(); break;
case 15: rey(); break;
case 16: as(); break;
case 17: cero(); break;
}
}
void imprime() {
marca = 1; cubo = 0; alto = 0; lado = 5; pon();
marca ++; lado = 0; pon();
marca ++; lado = 5; alto = 5; pon();
marca ++; lado = 0; pon();
marca ++; cubo = 1; alto = 0; lado = 5; pon();
marca ++; lado = 0; pon();
marca ++; lado = 5; alto = 5; pon();
marca ++; lado = 0; pon();
}
void reimprime() {
switch (marca) {
case 1: cubo = 0; alto = 0; lado = 5; pon(); break;
case 2: cubo = 0; alto = 0; lado = 0; pon(); break;
case 3: cubo = 0; alto = 5; lado = 5; pon(); break;
case 4: cubo = 0; alto = 5; lado = 0; pon(); break;
case 5: cubo = 1; alto = 0; lado = 5; pon(); break;
case 6: cubo = 1; alto = 0; lado = 0; pon(); break;
case 7: cubo = 1; alto = 5; lado = 5; pon(); break;
case 8: cubo = 1; alto = 5; lado = 0; pon(); break;
}
}
void botonn() {
if (digitalRead (buno) == HIGH) {
boton = 1;
}
if (digitalRead (bdos) == HIGH) {
boton = 2;
}
if (digitalRead (btre) == HIGH) {
boton = 3;
}
if (anteriorboton != boton && boton != 0) {
} else {
boton = 0;
}
anteriorboton = boton;
}
void cambio() {
if (cubo == 1 && puesto[marca] < 9) {
numero = random(1, 7);
if (numero != puesto[marca]) {
puesto[marca] = numero + 10; reimprime();
}else { cambio();
}
}
if (cubo == 0 && puesto[1] < 11 && puesto[2] < 11 && puesto[3] < 11 && puesto[4] < 11 ) {
for (int i = 1; i < 5; i++) {
if (puesto[i] != 10 ) {
numero = random(1, 7);
if (numero == puesto[i]) {
i--;
} else {
puesto[i] = numero + 10;
marca = i;
reimprime();
cubo = 1;
}
}
}
}
}
void poncruz() {
if (digitalRead (bcua) == HIGH) {
cubo = 1;
} else {
cubo = 0;
}
if (cubo == 0 && puesto[1] > 8 && puesto[2] > 8 && puesto[3] > 8 && puesto[4] > 8) {
cubo = 3;
}
if (cubo == 1 && puesto[5] > 8 && puesto[6] > 8 && puesto[7] > 8 && puesto[8] > 8) {
cubo = 3;
}
if (boton == 1 ) {
marca++;
}
if (puesto[marca] > 6 ) {
marca++;
}
if (marca > 4 && cubo == 0) {
marca = 1;
}
if (marca > 8 ) {
marca = 5;
}
if (marca < 5 && cubo == 1) {
marca = 5;
}
if (marca < 1 ) {
marca = 1;
}
if (marca == 5 || marca == 1) {
crcu();
}
if (marca == 8 || marca == 4) {
crdo();
}
if (marca == 7 || marca == 2) {
crun();
}
if (marca == 6 || marca == 3) {
crtr();
}
crune();
}
void mover() {
for (int i = 1; i < 5; i++) {
if (puesto[marca] != 10 && cubo == 1 && puesto[i] == 10 ) {
puesto[i] = puesto[marca];
puesto[marca] = 10; reimprime(); marca = i; reimprime();
}
}
for (int i = 5; i < 9; i++) {
if (puesto[marca] != 10 && puesto[marca] < 7 && cubo == 0 && puesto[i] == 10 ) {
puesto[i] = puesto[marca];
puesto[marca] = 10; reimprime(); marca = i; reimprime();
}
}
}
void setup() {
randomSeed(analogRead(A1));
lc.shutdown(0, false); lc.setIntensity(0, 8); lc.setScanLimit(0, 2);
//for (int i = 1; i < 10; i=i+2) { dados[i] = i; //random(1, 7); }
}
void loop() {
botonn();
if (fase == 0) {
azar();
fase = 1;
}
if (fase == 1) {
imprime();
numero++;
fase = 2;
}
if (fase == 2) {
if (boton != 0) {
fase = 3;
lado = 0;
alto = 0;
}
}
if (fase == 3) {
poncruz();
if (boton == 3) {
cambio();
}
if (boton == 2) {
mover();
}
}
}