Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte tempLetra[32];
- int filaLatchPin = 3;
- int filaClockPin = 4;
- int filaDataPin = 2;
- int colLatchPin = 6;
- int colClockPin = 7;
- int colDataPin = 5;
- int mandoPin1 = 8;
- int mandoPin2 = 9;
- int mandoPin3 = 10;
- int mandoPin4 = 11;
- int mandoPin5 = 12;
- int mandoPin6 = 13;
- int OjosPin = 8;
- /*equivalencias mando
- 8 -> izquierda
- 9 -> abajo
- 10 -> arriba
- 11 -> derecha
- 12 -> B
- 13 -> A
- COMUNICACION
- Valores
- 0 -> negativo
- 1 -> afirmativo / recibido
- "chat" -> iniciar modo chat.
- */
- char caracteres[37] = "abcdefghijklmnopqrstuvwxyz0123456789"; //36
- byte data = 0;
- byte nulo = B00000000;
- byte num[] = {B00000000, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001};
- byte orden[] = {B00000000,B10000000, B11000000, B11100000, B11110000, B11111000, B11111100, B11111110, B11111111};
- byte forma[] = {B00000010, B00000000, B00000101, B00001110, B00001000, B10001110, B00010111, B01001110, B00100101, B00101110, B00100101, B00101110, B00100111, B00100100, B00100000, B00100100, B00100111, B00100100, B00100101, B00100100, B11111111, B11111111, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000};
- byte datosTablero[] = {B11000000, B00110000, B00001100, B00000011, B11000000, B00110000, B00001100, B00000011};
- byte datosTableroMostrar[] = {B11000000, B00000000,B00110000,B00000000,B00001100, B00000000, B00000011, B00000000, B00000000, B11000000, B00000000,B00110000,B00000000,B00001100, B00000000, B00000011};
- byte espacio[] = {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};
- int letras[] = {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,480,512,544,576,608,640,672,704,736,768,800,832,864,896,928,960,992,1024,1056,1088,1120}; //posiciones en memoria
- byte a[] = {B00000000, B00000000, B00000111, B11100000, B00001111, B11110000, B00011111, B11111000, B00011100, B00111000, B00011000, B00011000, B00011000, B00011000, B00011111, B11111000, B00011111, B11111000, B00011000, B00011000, B00011000, B00011000, B00011000, B00011000, B00011000, B00011000, B00011000, B00011000, B00000000, B00000000, B00000000, B00000000};
- byte b[] = {B00000000, B00000000, B00000000, B00000000, B00011111, B11000000, B00011111, B11100000, B00011000, B01100000, B00011000, B01100000, B00011000, B01100000, B00011111, B11000000, B00011111, B11100000, B00011000, B01100000, B00011000, B01100000, B00011000, B01100000, B00011111, B11000000, B00011111, B11000000, B00000000, B00000000, B00000000, B00000000};
- byte num1[] = {B00000000, B00000000, B00000001, B10000000, B00000011, B10000000, B00000111, B10000000, B00001101, B10000000, B00011001, B10000000, B00000001, B10000000, B00000001, B10000000, B00000001, B10000000, B00000001, B10000000, B00000001, B10000000, B00000001, B10000000, B00000001, B10000000, B00000011, B11000000, B00000000, B00000000, B00000000, B00000000};
- byte num2[] = {B00000000, B00000000, B00000011, B11000000, B00001111, B11100000, B00001100, B00110000, B00000000, B00110000, B00000000, B00110000, B00000000, B00110000, B00000000, B01100000, B00000000, B11000000, B00000001, B10000000, B00000011, B00000000, B00000110, B00000000, B00000111, B11110000, B00000111, B11110000, B00000000, B00000000, B00000000, B00000000};
- byte desconectado[] ={B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000001, B10000000, B00000011, B11110000, B11111111, B10000000, B11111111, B10000000, B00000011, B11110000, B00000001, B10000000, B00000000, B00000000, B00001100, B11101110, B00010010, B10001000, B00010010, B11101110, B00010010, B10001000, B00001100, B10001000, B00000000, B00000000};
- byte conectado[] = {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000001, B10000000, B00000011, B11110000, B11111111, B10000000, B11111111, B10000000, B00000011, B11110000, B00000001, B10000000, B00000000, B00000000, B00001100, B10010000, B00010010, B11010000, B00010010, B10110000, B00010010, B10010000, B00001100, B10010000, B00000000, B00000000};
- //variables de tiempo
- unsigned long tiempoPasado;
- //datos de tablero
- int tablero[8][8]; //0 indexed!
- byte arrayTableroRojo[32] = {0}; //equivalente en binario
- byte arrayTableroVerde[32] = {0}; //equivalente en color
- byte segundoPlano[32];
- //almacen temporal de datos
- byte arrayPalabra[]={};
- //variables de control
- int copiarLetraMemoria = 0;
- int valorSelector = 0;
- int finalizarMenu = 0;
- int finalizarSubMenu = 0;
- //variables de selector
- int datoSelector = 0;
- int unavez = 0;
- //variables de juego
- int turnoJuego = 0; //1 rojo, 2 verde.
- int introJuego = 0;
- int partida = 0;
- int tirada = 0;
- //variables de comunicacion
- int incoming = 0;
- byte lectura = 0;
- boolean paso = false;
- char envio[65];
- int contadorEnviar = 0;
- boolean completado = false;
- boolean subCompletado = false;
- //variables de chat
- int valorLista = 0;
- int contadorSelec = 0;
- char palabra[100]; //100 caracteres max.
- void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
- Wire.beginTransmission(deviceaddress);
- Wire.send((int)(eeaddress >> 8)); // MSB
- Wire.send((int)(eeaddress & 0xFF)); // LSB
- Wire.send(data);
- Wire.endTransmission();
- }
- byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
- byte rdata = 0xFF;
- Wire.beginTransmission(deviceaddress);
- Wire.send((int)(eeaddress >> 8)); // MSB
- Wire.send((int)(eeaddress & 0xFF)); // LSB
- Wire.endTransmission();
- Wire.requestFrom(deviceaddress,1);
- if (Wire.available()) rdata = Wire.receive();
- return rdata;
- }
- void obtenerLetra(char letra[]) //guarda en tempLetra la letra a obtener
- {
- int buscar;
- switch(letra[0])
- {
- case 97:
- buscar = letras[0];
- break;
- case 98:
- buscar = letras[1];
- break;
- case 99:
- buscar = letras[2];
- break;
- case 100:
- buscar = letras[3];
- break;
- case 101:
- buscar = letras[4];
- break;
- case 102:
- buscar = letras[5];
- break;
- case 103:
- buscar = letras[6];
- break;
- case 104:
- buscar = letras[7];
- break;
- case 105:
- buscar = letras[8];
- break;
- case 106:
- buscar = letras[9];
- break;
- case 107:
- buscar = letras[10];
- break;
- case 108:
- buscar = letras[11];
- break;
- case 109:
- buscar = letras[12];
- break;
- case 110:
- buscar = letras[13];
- break;
- case 111:
- buscar = letras[14];
- break;
- case 112:
- buscar = letras[15];
- break;
- case 113:
- buscar = letras[16];
- break;
- case 114:
- buscar = letras[17];
- break;
- case 115:
- buscar = letras[18];
- break;
- case 116:
- buscar = letras[19];
- break;
- case 117:
- buscar = letras[20];
- break;
- case 118:
- buscar = letras[21];
- break;
- case 119:
- buscar = letras[22];
- break;
- case 120:
- buscar = letras[23];
- break;
- case 121:
- buscar = letras[24];
- break;
- case 122: //z
- buscar = letras[25];
- break;
- case 48: //numeros
- buscar = letras[26];
- break;
- case 49:
- buscar = letras[27];
- break;
- case 50:
- buscar = letras[28];
- break;
- case 51:
- buscar = letras[29];
- break;
- case 52:
- buscar = letras[30];
- break;
- case 53:
- buscar = letras[31];
- break;
- case 54:
- buscar = letras[32];
- break;
- case 55:
- buscar = letras[33];
- break;
- case 56:
- buscar = letras[34];
- break;
- case 57://9
- buscar = letras[35];
- break;
- }
- for(int y=0; y<32; y++)
- {
- tempLetra[y] = readEEPROM(eeprom,y+buscar);
- }
- }
- void mostrar(byte horizontal_1_8, byte horizontal_8_16, byte vertical_rojo_1_8, byte vertical_rojo_8_16, byte vertical_verde_1_8, byte vertical_verde_8_16)
- {
- digitalWrite(filaLatchPin, 0);
- digitalWrite(colLatchPin, 0);
- shiftOut(colDataPin, colClockPin, vertical_rojo_1_8); //rojo 1-8
- shiftOut(filaDataPin, filaClockPin, horizontal_1_8); //horizontal
- shiftOut(colDataPin, colClockPin, vertical_rojo_8_16); //rojo 8*16
- shiftOut(filaDataPin, filaClockPin, horizontal_8_16);
- shiftOut(colDataPin, colClockPin, vertical_verde_1_8); //verde 1-8
- shiftOut(colDataPin, colClockPin, vertical_verde_8_16); //verde 1-16
- digitalWrite(colLatchPin, 1);
- digitalWrite(filaLatchPin, 1);
- }
- void barrer(int color,int medida, byte arreglo[]) //0 rojo 1 verde 2 naranja
- {
- int contador =0;
- int ciclos = 0;
- for(int y=0; y<medida; y++) {
- if(ciclos<8)
- {
- if(color == 0)
- {
- mostrar(num[y+1], nulo, arreglo[contador], arreglo[contador+1], nulo, nulo);
- }
- else if(color == 1)
- {
- mostrar(num[y+1], nulo, nulo, nulo, arreglo[contador], arreglo[contador+1]);
- }
- else
- {
- mostrar(num[y+1], nulo, arreglo[contador], arreglo[contador+1], arreglo[contador], arreglo[contador+1]);
- }
- }
- else
- {
- if(color == 0)
- {
- mostrar(nulo, num[y-7], arreglo[contador], arreglo[contador+1], nulo, nulo);
- }
- else if(color == 1)
- {
- mostrar(nulo, num[y-7], nulo, nulo, arreglo[contador], arreglo[contador+1]);
- }
- else
- {
- mostrar(nulo,num[y-7], arreglo[contador], arreglo[contador+1], arreglo[contador], arreglo[contador+1]);
- }
- }
- ciclos+=1;
- contador+=2;
- if(ciclos == 16)
- {
- ciclos = 0;
- }
- }
- }
- void mover(byte arreglo[], int tamano, char direccion[])
- {
- byte data1;
- byte data2;
- unsigned int byte16;
- int contador = 0;
- for(int y=0; y<tamano; y++){
- data1 = arreglo[contador];
- data2 = arreglo[contador+1];
- byte16 = (data1<<8)|(data2);
- if(direccion == "izquierda")
- {
- byte16 = byte16 << 1;
- }
- else
- {
- byte16 = byte16 >> 1;
- }
- data1 = byte16; //autorrecorte
- data2 = byte16 >> 8; //mover datos y autorrecorte
- arreglo[contador] = data2; //invertir los datos al mostrar
- arreglo[contador+1] = data1;
- contador+=2;
- }
- }
- void copiarLetra(byte letra[], byte destino[])
- {
- for(int y=0; y<32; y++)
- {
- destino[y] = letra[y];
- }
- }
- void mostrarPalabra(char palabra[], int medida, int color)
- {
- int tamano = 32*medida;
- byte arreglo[tamano];
- int ciclo = 0;
- for(int y=0; y<=medida; y++)
- {
- switch(palabra[y])
- {
- case 97:
- obtenerLetra("a");
- copiarLetra(tempLetra,arreglo);
- break;
- case 98:
- obtenerLetra("b");
- copiarLetra(tempLetra,arreglo);
- break;
- case 99:
- obtenerLetra("c");
- copiarLetra(tempLetra,arreglo);
- break;
- case 100:
- obtenerLetra("d");
- copiarLetra(tempLetra,arreglo);
- break;
- case 101:
- obtenerLetra("e");
- copiarLetra(tempLetra,arreglo);
- break;
- case 102:
- obtenerLetra("f");
- copiarLetra(tempLetra,arreglo);
- break;
- case 103:
- obtenerLetra("g");
- copiarLetra(tempLetra,arreglo);
- break;
- case 104:
- obtenerLetra("h");
- copiarLetra(tempLetra,arreglo);
- break;
- case 105:
- obtenerLetra("i");
- copiarLetra(tempLetra,arreglo);
- break;
- case 106:
- obtenerLetra("j");
- copiarLetra(tempLetra,arreglo);
- break;
- case 107:
- obtenerLetra("k");
- copiarLetra(tempLetra,arreglo);
- break;
- case 108:
- obtenerLetra("l");
- copiarLetra(tempLetra,arreglo);
- break;
- case 109:
- obtenerLetra("m");
- copiarLetra(tempLetra,arreglo);
- break;
- case 110:
- obtenerLetra("n");
- copiarLetra(tempLetra,arreglo);
- break;
- case 111:
- obtenerLetra("o");
- copiarLetra(tempLetra,arreglo);
- break;
- case 112:
- obtenerLetra("p");
- copiarLetra(tempLetra,arreglo);
- break;
- case 113:
- obtenerLetra("q");
- copiarLetra(tempLetra,arreglo);
- break;
- case 114:
- obtenerLetra("r");
- copiarLetra(tempLetra,arreglo);
- break;
- case 115:
- obtenerLetra("s");
- copiarLetra(tempLetra,arreglo);
- break;
- case 116:
- obtenerLetra("t");
- copiarLetra(tempLetra,arreglo);
- break;
- case 117:
- obtenerLetra("u");
- copiarLetra(tempLetra,arreglo);
- break;
- case 118:
- obtenerLetra("v");
- copiarLetra(tempLetra,arreglo);
- break;
- case 119:
- obtenerLetra("w");
- copiarLetra(tempLetra,arreglo);
- break;
- case 120:
- obtenerLetra("x");
- copiarLetra(tempLetra,arreglo);
- break;
- case 121:
- obtenerLetra("y");
- copiarLetra(tempLetra,arreglo);
- break;
- case 122:
- obtenerLetra("z");
- copiarLetra(tempLetra,arreglo); //800
- break;
- case 20:
- copiarLetra(espacio,arreglo);
- break;
- case 48:
- obtenerLetra("0");
- copiarLetra(tempLetra,arreglo);
- break;
- case 49:
- obtenerLetra("1");
- copiarLetra(tempLetra,arreglo);
- break;
- case 50:
- obtenerLetra("2");
- copiarLetra(tempLetra,arreglo);
- break;
- case 51:
- obtenerLetra("3");
- copiarLetra(tempLetra,arreglo);
- break;
- case 52:
- obtenerLetra("4");
- copiarLetra(tempLetra,arreglo);
- break;
- case 53:
- obtenerLetra("5");
- copiarLetra(tempLetra,arreglo);
- break;
- case 54:
- obtenerLetra("6");
- copiarLetra(tempLetra,arreglo);
- break;
- case 55:
- obtenerLetra("7");
- copiarLetra(tempLetra,arreglo);
- break;
- case 56:
- obtenerLetra("8");
- copiarLetra(tempLetra,arreglo);
- break;
- case 57:
- obtenerLetra("9");
- copiarLetra(tempLetra,arreglo);
- break;
- }
- ciclo = 0;
- for(int a=0; a<2; a++)
- {
- mover(arreglo,32,"derecha");
- }
- for(int b=0; b<14; b++)
- {
- if(ciclo == 0)
- {
- for(int c=0; c<10; c++)
- {
- barrer(color,16,arreglo);
- }
- }
- else{
- for(int d=0; d<3; d++)
- {
- barrer(color,16,arreglo);
- }
- }
- ciclo = 1;
- mover(arreglo,32,"izquierda");
- }
- }
- }
- void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
- int i=0;
- int pinState;
- pinMode(myClockPin, OUTPUT);
- pinMode(myDataPin, OUTPUT);
- digitalWrite(myDataPin, 0);
- digitalWrite(myClockPin, 0);
- for (i=7; i>=0; i--) {
- digitalWrite(myClockPin, 0);
- if ( myDataOut & (1<<i) ) {
- pinState= 1;
- }
- else {
- pinState= 0;
- }
- digitalWrite(myDataPin, pinState);
- digitalWrite(myClockPin, 1);
- digitalWrite(myDataPin, 0);
- }
- digitalWrite(myClockPin, 0);
- }
- void mostrarArrayTablero()
- {
- int contador =0;
- int ciclos = 0;
- for(int y=0; y<16; y++) {
- if(ciclos<8)
- {
- mostrar(num[y+1], nulo, arrayTableroRojo[contador], arrayTableroRojo[contador+1], arrayTableroVerde[contador], arrayTableroVerde[contador+1]);
- }
- else
- {
- mostrar(nulo,num[y-7], arrayTableroRojo[contador], arrayTableroRojo[contador+1], arrayTableroVerde[contador], arrayTableroVerde[contador+1]);
- }
- ciclos+=1;
- contador+=2;
- if(ciclos == 16)
- {
- ciclos = 0;
- }
- }
- }
- void borrarTablero()
- {
- for(int y=0; y<8; y++)
- {
- for(int x=0; x<8; x++)
- {
- escribirArrayTableroSobre(x,y,tablero[x][y]);
- }
- }
- }
- void escribirArrayTableroSobre(int fila, int columna, int valor)
- {
- int lugar = fila*4;
- int par = 2;
- int impar1 = 1;
- int impar3 = 3;
- int constante1;
- int constante2;
- if(columna<=3)
- {
- constante1 = 0;
- constante2 = par;
- }
- else
- {
- constante1 = impar1;
- constante2 = impar3;
- }
- if(valor == 0) //nulo
- {
- arrayTableroRojo[lugar+constante1] = nulo;
- arrayTableroRojo[lugar+constante2] = nulo;
- arrayTableroVerde[lugar+constante1] = nulo;
- arrayTableroVerde[lugar+constante2] = nulo;
- }
- else if(valor == 1) //rojo
- {
- arrayTableroRojo[lugar+constante1] = datosTablero[columna];
- arrayTableroRojo[lugar+constante2] = datosTablero[columna];
- arrayTableroVerde[lugar+constante1] = nulo;
- arrayTableroVerde[lugar+constante2] = nulo;
- }
- else if(valor == 2) //verde
- {
- arrayTableroRojo[lugar+constante1] = nulo;
- arrayTableroRojo[lugar+constante2] = nulo;
- arrayTableroVerde[lugar+constante1] = datosTablero[columna];
- arrayTableroVerde[lugar+constante2] = datosTablero[columna];
- }
- else if(valor == 3) //naranja
- {
- arrayTableroRojo[lugar+constante1] = datosTablero[columna];
- arrayTableroRojo[lugar+constante2] = datosTablero[columna];
- arrayTableroVerde[lugar+constante1] = datosTablero[columna];
- arrayTableroVerde[lugar+constante2] = datosTablero[columna];
- }
- }
- void escribirArrayTablero(int fila, int columna, int valor)
- {
- int lugar = fila*4;
- int par = 2;
- int impar1 = 1;
- int impar3 = 3;
- int constante1;
- int constante2;
- if(columna<=3)
- {
- constante1 = 0;
- constante2 = par;
- }
- else
- {
- constante1 = impar1;
- constante2 = impar3;
- }
- if(valor == 0) //nulo
- {
- arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | nulo;
- arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | nulo;
- arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | nulo;
- arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | nulo;
- }
- else if(valor == 1) //rojo
- {
- arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | datosTablero[columna];
- arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | datosTablero[columna];
- arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | nulo;
- arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | nulo;
- }
- else if(valor == 2) //verde
- {
- arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | nulo;
- arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | nulo;
- arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | datosTablero[columna];
- arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | datosTablero[columna];
- }
- else if(valor == 3) //naranja
- {
- arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | datosTablero[columna];
- arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | datosTablero[columna];
- arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | datosTablero[columna];
- arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | datosTablero[columna];
- }
- }
- void refreshTablero()
- {
- for(int y=0; y<8; y++)
- {
- for(int x=0; x<8; x++)
- {
- escribirArrayTablero(x,y,tablero[x][y]);
- }
- }
- }
- void combinarArrays(byte array1[], byte array2[], byte destino[])
- {
- for(int y=0; y<32; y++)
- {
- destino[y]=array1[y] | array2[y];
- }
- }
- int moverSelector(int direccion)//0 atras, 1 delante
- {
- //borrar cualquier selector anterior
- for(int y=0; y<8; y++)
- {
- borrarSelector(y);
- }
- if(direccion == 0)
- {
- if(valorSelector == 0)
- {
- valorSelector == 7;
- }
- else
- {
- valorSelector--;
- }
- imprimirSelector(valorSelector);
- return valorSelector;
- }
- else
- {
- if(valorSelector == 7)
- {
- valorSelector == 0;
- }
- else
- {
- valorSelector++;
- }
- imprimirSelector(valorSelector);
- return valorSelector;
- }
- }
- void imprimirSelector(int posicion) //imprime el selector
- {
- escribirArrayTablero(0,posicion,3);
- }
- void borrarSelector(int posicion)
- {
- escribirArrayTableroSobre(0,posicion,0);
- }
- void animarCaidaFicha(int color, int columna, int fila) //desde columna(arriba), hasta fila | color 1 rojo 2 verde 3 naranja
- {
- for(int y=0; y<=fila; y++)
- {
- escribirArrayTablero(y,columna,color);
- refreshTablero();
- for(int x=0; x<6; x++)
- {
- mostrarArrayTablero();
- }
- borrarTablero();
- }
- }
- void resetTablero()
- {
- for(int y=0; y<=8; y++)
- {
- for(int x=0; x<=8; x++)
- {
- tablero[x][y] = 0;
- }
- }
- }
- void tirarFichaOnline(int columna, int valor)
- {
- int fila=0;
- for(int y=0; y<8; y++)
- {
- if(tablero[y][columna] == 0)
- {
- fila++;
- }
- }
- if(fila>0)
- {
- fila = fila - 1;
- }
- else
- {
- fila = 0;
- }
- tablero[fila][columna] = valor;
- refreshTablero();
- borrarTablero();
- }
- void tirarFicha(int columna, int valor)
- {
- int fila=0;
- for(int y=0; y<8; y++)
- {
- if(tablero[y][columna] == 0)
- {
- fila++;
- }
- }
- if(fila>0)
- {
- fila = fila - 1;
- }
- else
- {
- fila = 0;
- }
- animarCaidaFicha(valor, columna, fila);
- tablero[fila][columna] = valor;
- refreshTablero();
- }
- int Selector(int valor)
- {
- boolean tirada = true;
- while(tirada == true)
- {
- while((digitalRead(mandoPin1) == LOW) && (digitalRead(mandoPin4) == LOW) && (digitalRead(mandoPin5) == LOW))
- {
- refreshTablero();
- mostrarArrayTablero();
- }
- if(digitalRead(mandoPin5) == HIGH){
- tirarFicha(datoSelector, valor);
- tiempoPasado=millis();
- refreshTablero();
- while ((millis()-tiempoPasado)<150)
- {
- mostrarArrayTablero();
- }
- tirada = false;
- }
- if(digitalRead(mandoPin1) == HIGH) {
- tiempoPasado=millis();
- datoSelector = moverSelector(0);
- refreshTablero();
- while ((millis()-tiempoPasado)<150)
- {
- mostrarArrayTablero();
- }
- }
- if(digitalRead(mandoPin4) == HIGH) {
- tiempoPasado=millis();
- datoSelector = moverSelector(1);
- refreshTablero();
- while ((millis()-tiempoPasado)<150)
- {
- mostrarArrayTablero();
- }
- }
- }
- //se ha acabado el turno
- for(int y = 0; y<25; y++)//mostrar durante un rato el tablero
- {
- mostrarArrayTablero();
- }
- return 1; //acabar con la funcion
- }
- int comprobarColumnaLibre(int columna) //devuelve el numero de fila libre
- {
- for(int y=7; y<=0; y--)
- {
- if(tablero[y][columna] == 0)
- {
- return y;
- }
- }
- }
- void tirarMaquina(int valor) //funcion de inteligencia para la tirada
- {
- unsigned long probabilidad[8]; //filas donde se almacenar\u00e1 la probabilidad
- int accion;
- int tirar;
- int temp;
- //vertical
- for(int y=0; y<8; y++) //columna
- {
- for(int x=0; x<=6; x++) //fila
- {
- if((tablero[x][y]== valor) && (tablero[x+1][y] == valor)) //dos juntas
- {
- probabilidad[y]+=1;
- if((tablero[x-1][y] == 0) && (x>0))
- {
- probabilidad[y]+=5;
- }
- }
- }
- }
- for(int y=0; y<8; y++) //columna
- {
- for(int x=0; x<=5; x++) //fila
- {
- if((tablero[x][y]== valor) && (tablero[x+1][y] == valor) && (tablero[x+2][y] == valor)) //tres juntas
- {
- probabilidad[y]+=6;
- if((tablero[x-1][y] == 0) && (x>0))
- {
- probabilidad[y]+=15;
- }
- }
- }
- }
- //horizontal
- temp = probabilidad[0];
- for(int y=0; y<8; y++) //averiguar el maximo
- {
- Serial.println(probabilidad[y]);
- }
- for(int y=0; y<8; y++) //averiguar la posicion del m\u00e1ximo
- {
- Serial.println(probabilidad[y]);
- if(probabilidad[y] == temp)
- {
- tirar = y;
- }
- }
- //ya tenemos donde tirar
- tirarFicha(0,1);
- for(int y=0; y<15; y++)
- {
- mostrarArrayTablero();
- }
- }
- void animacionRecoger()
- {
- for(int x=0; x<8; x++)
- {
- for(int y=0; y<8; y++)
- {
- escribirArrayTablero(x,y,3);
- refreshTablero();
- for(int z=0; z<3; z++)
- {
- mostrarArrayTablero();
- }
- }
- }
- borrarTablero();
- resetTablero();
- }
- void Ganar(int ganador)
- {
- if(ganador == 1) //la maquina
- {
- mostrarPalabra("has perdido",11,2);
- }
- else
- {
- mostrarPalabra("has ganado",10,2);
- }
- for(int y=0; y<30; y++)
- {
- mostrarArrayTablero();
- }
- animacionRecoger();
- }
- int detectarGanador(int valor) //1 gana 1 (rojo) 2 gana 2 (verde);
- {
- //vertical
- for(int y=0; y<8; y++) //columna
- {
- for(int x=0; x<=4; x++) //fila
- {
- if((tablero[x][y]== valor) && (tablero[x+1][y] == valor) && (tablero[x+2][y] == valor) && (tablero[x+3][y] == valor))
- {
- return valor;
- }
- }
- }
- //horizontal
- for(int y=0; y<=4; y++) //columna
- {
- for(int x=0; x<8; x++) //fila
- {
- if((tablero[x][y] == valor) && (tablero[x][y+1] == valor) && (tablero[x][y+2] == valor) && (tablero[x][y+3] == valor))
- {
- return valor;
- }
- }
- }
- //diagonal normal 1/2
- for(int y=3; y<=7; y++) //fila
- {
- if((tablero[y][0] == valor) && (tablero[y-1][1] == valor) && (tablero[y-2][2] == valor) && (tablero[y-3][3] == valor))
- {
- return valor;
- }
- }
- for(int y=0; y<=4; y++) //columna
- {
- if((tablero[7][y] == valor) && (tablero[6][y+1] == valor) && (tablero[5][y+2] == valor) && (tablero[4][y+3] == valor))
- {
- return valor;
- }
- }
- //diagonal normal 2/2
- for(int y=3; y<=7; y++) //columna
- {
- if((tablero[0][y] == valor) && (tablero[1][y-1] == valor) && (tablero[2][y-2] == valor) && (tablero[3][y-3] == valor))
- {
- return valor;
- }
- }
- for(int y=0; y<=4; y++) //fila
- {
- if((tablero[y][7] == valor) && (tablero[y+1][6] == valor) && (tablero[y+2][5] == valor) && (tablero[y+3][4] == valor))
- {
- return valor;
- }
- }
- //diagonal inversa 1/2
- for(int y=4; y>=0; y--) //fila
- {
- if((tablero[y][0] == valor) && (tablero[y+1][1] == valor) && (tablero[y+2][2] == valor) && (tablero[y+3][3] == valor))
- {
- return valor;
- }
- }
- for(int y=0; y<=4; y++) //columna
- {
- if((tablero[0][y] == valor) && (tablero[1][y+1] == valor) && (tablero[2][y+2] == valor) && (tablero[3][y+3] == valor))
- {
- return valor;
- }
- }
- //diagonal inversa 2/2
- for(int y=3; y<=7; y++) //columna
- {
- if((tablero[7][y] == valor) && (tablero[6][y-1] == valor) && (tablero[5][y-2] == valor) && (tablero[4][y-3] == valor))
- {
- return valor;
- }
- }
- for(int y=7; y<=3; y--)
- {
- if((tablero[7][y] == valor) && (tablero[6][y-1] == valor) && (tablero[5][y-2] == valor) && (tablero[4][y-3] == valor))
- {
- return valor;
- }
- }
- }
- int menu()
- {
- //mostrarPalabra("a jugar b chat",16,0);
- int valor = 0;
- int subvalor = 0;
- while(finalizarMenu==0)
- {
- if(valor == 0)
- {
- barrer(2,16,a);
- if(digitalRead(mandoPin5) == HIGH)
- {
- for(int y=0; y<10; y++)
- {
- barrer(1,16,a);
- }
- mostrarPalabra("jugadores",8,2);
- while(finalizarSubMenu==0)
- {
- if(subvalor==0)
- {
- barrer(2,16,num1);
- if(digitalRead(mandoPin5) == HIGH)
- {
- for(int y=0; y<10; y++)
- {
- barrer(1,16,num1);
- }
- return 1; //un jugador
- }
- }
- else
- {
- barrer(2,16,num2);
- if(digitalRead(mandoPin5) == HIGH)
- {
- for(int y=0; y<10; y++)
- {
- barrer(1,16,num2);
- }
- return 2; //multiplayer
- }
- }
- if(digitalRead(mandoPin1) == HIGH)
- {
- subvalor=0;
- }
- if(digitalRead(mandoPin4) == HIGH)
- {
- subvalor=1;
- }
- }
- //1 jugar1, 2multip, 3 chat
- }
- }
- else
- {
- barrer(2,16,b);
- if(digitalRead(mandoPin5) == HIGH)
- {
- for(int y=0; y<10; y++)
- {
- barrer(1,16,b);
- }
- return 3; //chat
- }
- }
- if(digitalRead(mandoPin1) == HIGH)
- {
- valor = 0;
- }
- if(digitalRead(mandoPin4) == HIGH)
- {
- valor = 1;
- }
- if((digitalRead(mandoPin2) == HIGH) || (digitalRead(mandoPin3) == HIGH))
- {
- mostrarPalabra("a jugar b chat",17,2);
- }
- }
- }
- void copiarToChar()
- {
- int contador = 0; //mapeamos los valores
- for(int y=0; y<8; y++)
- {
- for(int x=0; x<8; x++)
- {
- if(tablero[y][x] == 0)
- {
- envio[contador] = '1';
- }
- else if(tablero[y][x] == 1)
- {
- envio[contador] = '2';
- }
- else if(tablero[y][x] == 2)
- {
- envio[contador] = '3';
- }
- contador++;
- }
- }
- contador = 0;
- }
- void enviarDatos(int dato)
- {
- boolean enviado = false;
- while(enviado == false)
- {
- barrer(0,16,desconectado);
- if(Serial.available()>0)
- {
- Serial.print(dato);
- Serial.flush();
- enviado = true;
- }
- }
- }
- void enviarTablero()
- {
- contadorEnviar=0;
- boolean enviar = false;
- copiarToChar();
- while(enviar == false)
- {
- if(Serial.available()>0)
- {
- if(contadorEnviar>63)
- {
- contadorEnviar = 0;
- enviar = true;
- completado = true;
- }
- Serial.print(envio[contadorEnviar]);
- Serial.flush();
- contadorEnviar++;
- }
- }
- }
- int recibirDatos()
- {
- int dato;
- boolean recibir = true;
- while(recibir == true)
- {
- if(Serial.available()>0)
- {
- dato = Serial.read();
- Serial.flush();
- recibir = false;
- return dato;
- }
- }
- }
- int leerSensor()
- {
- int pin = OjosPin;
- unsigned long Duracion;
- pinMode(pin, OUTPUT);
- digitalWrite(pin, LOW);
- delayMicroseconds(2);
- digitalWrite(pin, HIGH);
- delayMicroseconds(10);
- digitalWrite(pin, LOW);
- pinMode(pin, INPUT);
- Duracion = pulseIn(pin, HIGH);
- tiempoPasado = millis();
- while ((millis()-tiempoPasado)<50)
- {
- barrer(2,16,tempLetra);
- }
- return Duracion/58;
- }
- void seleccionarLetra()
- {
- int contador = 0;
- char recibir[50];
- int dato;
- if(valorLista>36)
- {
- valorLista = 0;
- }
- tiempoPasado = millis();
- while ((millis()-tiempoPasado)<25)
- {
- barrer(2,16,tempLetra);
- }
- if(leerSensor()<20)
- {
- char letra[1] = {int(caracteres[valorLista])};
- obtenerLetra(letra);
- valorLista++;
- }
- if(digitalRead(mandoPin5) == HIGH)
- {
- tiempoPasado = millis();
- while ((millis()-tiempoPasado)<180)
- {
- barrer(1,16,tempLetra);
- }
- palabra[contadorSelec] = caracteres[valorLista-1];
- contadorSelec++;
- }
- if(digitalRead(mandoPin6) == HIGH)
- {
- enviarPalabra();
- }
- if(Serial.available()>0)
- {
- dato = Serial.read();
- Serial.flush();
- if(dato>0)
- {
- for(int y=0; y<dato; y++)
- {
- recibir[y] = Serial.read();
- contador++;
- }
- mostrarPalabra(recibir,sizeof(recibir),2);
- }
- }
- }
- void limpiarPalabra()
- {
- for(int y=0; y<50; y++)
- {
- palabra[y] = '0';
- }
- }
- void enviarPalabra()
- {
- contadorEnviar=0;
- boolean enviar = false;
- copiarToChar();
- while(enviar == false)
- {
- if(Serial.available()>0)
- {
- if(contadorEnviar>99)
- {
- contadorEnviar = 0;
- enviar = true;
- completado = true;
- }
- Serial.print(palabra[contadorEnviar]);
- Serial.flush();
- contadorEnviar++;
- }
- }
- }
- void setup() {
- pinMode(colLatchPin, OUTPUT);
- pinMode(filaLatchPin, OUTPUT);
- pinMode(mandoPin1, INPUT);
- pinMode(mandoPin2, INPUT);
- pinMode(mandoPin3, INPUT);
- pinMode(mandoPin4, INPUT);
- pinMode(mandoPin5, INPUT);
- pinMode(mandoPin6, INPUT);
- Serial.begin(57600);
- Wire.begin();
- partida = menu();
- introJuego = 0;
- limpiarPalabra();
- }
- void loop() {
- switch(partida)
- {
- case 1: //1player
- if(introJuego == 0) //antes de empezar partida
- {
- //mostrarPalabra("juegas con verdes y empiezas",27,1);
- resetTablero();
- borrarTablero();
- refreshTablero();
- introJuego = 1;
- turnoJuego = 1; //te toca a ti
- }
- //comprobar si te toca
- if((turnoJuego == 1) && (tirada == 0))
- {
- tirada = Selector(2);
- }
- //si no te toca tira la maquina
- else
- {
- tirarMaquina(1);
- turnoJuego = 1;
- tirada = 0;
- }
- if(detectarGanador(1)==1)
- {
- Ganar(1);
- menu();
- }
- if(detectarGanador(2)==2)
- {
- Ganar(2);
- menu();
- }
- break;
- case 2: //multiplayer
- if(introJuego == 0)
- {
- while(incoming == 0)
- {
- barrer(0,16,desconectado);
- if(Serial.available()>0)
- {
- incoming = Serial.read();
- Serial.print(incoming,BYTE);
- introJuego = 1;
- turnoJuego = 1; //te toca a ti
- resetTablero();
- borrarTablero();
- refreshTablero();
- Serial.flush();
- }
- }
- for(int y=0; y<50; y++)
- {
- barrer(1,16,conectado);
- }
- }
- enviarDatos(3);
- //inicio del juego online
- if((turnoJuego == 1) && (tirada == 0))
- {
- tirada = Selector(2);
- }
- else
- {
- while(completado == false)
- {
- Serial.flush();
- enviarDatos(1);
- Serial.flush();
- enviarTablero();
- }
- Serial.flush();
- incoming = recibirDatos();
- Serial.flush();
- tirarFichaOnline(incoming,1);
- turnoJuego = 1;
- tirada = 0;
- }
- completado = false;
- subCompletado = false;
- Serial.flush();
- if(detectarGanador(1)==1)
- {
- enviarDatos(4);
- enviarTablero();
- Ganar(1); //pierde arduino
- menu();
- }
- if(detectarGanador(2)==2)
- {
- enviarDatos(5);
- enviarTablero();
- Ganar(2); //gana arduino.
- menu();
- }
- break;
- case 3:
- seleccionarLetra();
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement