Advertisement
Guest User

Borja Rossell

a guest
Dec 7th, 2009
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 33.87 KB | None | 0 0
  1. byte tempLetra[32];
  2.  
  3. int filaLatchPin = 3;
  4. int filaClockPin = 4;
  5. int filaDataPin = 2;
  6.  
  7. int colLatchPin = 6;
  8. int colClockPin = 7;
  9. int colDataPin = 5;
  10.  
  11. int mandoPin1 = 8;
  12. int mandoPin2 = 9;
  13. int mandoPin3 = 10;
  14. int mandoPin4 = 11;
  15. int mandoPin5 = 12;
  16. int mandoPin6 = 13;
  17.  
  18. int OjosPin = 8;
  19.  
  20. /*equivalencias mando
  21. 8 -> izquierda
  22. 9 -> abajo
  23. 10 -> arriba
  24. 11 -> derecha
  25. 12 -> B
  26. 13 -> A
  27.  
  28. COMUNICACION
  29. Valores
  30. 0 -> negativo
  31. 1 -> afirmativo / recibido
  32.  
  33. "chat" -> iniciar modo chat.
  34. */
  35. char caracteres[37] = "abcdefghijklmnopqrstuvwxyz0123456789"; //36
  36. byte data = 0;
  37. byte nulo = B00000000;
  38. byte num[] = {B00000000, B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001};
  39. byte orden[] = {B00000000,B10000000, B11000000, B11100000, B11110000, B11111000, B11111100, B11111110, B11111111};
  40. 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};
  41. byte datosTablero[] = {B11000000, B00110000, B00001100, B00000011, B11000000, B00110000, B00001100, B00000011};
  42. byte datosTableroMostrar[] = {B11000000, B00000000,B00110000,B00000000,B00001100, B00000000, B00000011, B00000000, B00000000, B11000000, B00000000,B00110000,B00000000,B00001100, B00000000, B00000011};
  43.  
  44. 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};
  45. 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
  46.  
  47.  
  48. 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};
  49. 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};
  50. 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};
  51. 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};
  52. 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};
  53. 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};
  54. //variables de tiempo
  55. unsigned long tiempoPasado;
  56.  
  57. //datos de tablero
  58. int tablero[8][8];  //0 indexed!
  59. byte arrayTableroRojo[32] = {0}; //equivalente en binario
  60. byte arrayTableroVerde[32] = {0}; //equivalente en color
  61. byte segundoPlano[32];
  62.  
  63. //almacen temporal de datos
  64. byte arrayPalabra[]={};
  65.  
  66. //variables de control
  67. int copiarLetraMemoria = 0;
  68. int valorSelector = 0;
  69. int finalizarMenu = 0;
  70. int finalizarSubMenu = 0;
  71.  
  72. //variables de selector
  73. int datoSelector = 0;
  74. int unavez = 0;
  75.  
  76. //variables de juego
  77. int turnoJuego = 0; //1 rojo, 2 verde.
  78. int introJuego = 0;
  79. int partida = 0;
  80. int tirada = 0;
  81.  
  82. //variables de comunicacion
  83. int incoming = 0;
  84. byte lectura = 0;
  85. boolean paso = false;
  86. char envio[65];
  87. int contadorEnviar = 0;
  88. boolean completado = false;
  89. boolean subCompletado = false;
  90.  
  91. //variables de chat
  92. int valorLista = 0;
  93. int contadorSelec = 0;
  94. char palabra[100]; //100 caracteres max.
  95.  
  96. void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
  97.   Wire.beginTransmission(deviceaddress);
  98.   Wire.send((int)(eeaddress >> 8));   // MSB
  99.   Wire.send((int)(eeaddress & 0xFF)); // LSB
  100.   Wire.send(data);
  101.   Wire.endTransmission();
  102. }
  103.  
  104. byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
  105.   byte rdata = 0xFF;
  106.   Wire.beginTransmission(deviceaddress);
  107.   Wire.send((int)(eeaddress >> 8));   // MSB
  108.   Wire.send((int)(eeaddress & 0xFF)); // LSB
  109.   Wire.endTransmission();
  110.   Wire.requestFrom(deviceaddress,1);
  111.   if (Wire.available()) rdata = Wire.receive();
  112.   return rdata;
  113. }
  114.  
  115. void obtenerLetra(char letra[]) //guarda en tempLetra la letra a obtener
  116. {
  117.   int buscar;
  118.  
  119.   switch(letra[0])
  120.   {
  121.      case 97:
  122.      buscar = letras[0];
  123.      break;
  124.      case 98:
  125.      buscar = letras[1];
  126.      break;
  127.      case 99:
  128.      buscar = letras[2];
  129.      break;
  130.      case 100:
  131.      buscar = letras[3];
  132.      break;
  133.      case 101:
  134.      buscar = letras[4];
  135.      break;
  136.      case 102:
  137.      buscar = letras[5];
  138.      break;
  139.      case 103:
  140.      buscar = letras[6];
  141.      break;
  142.      case 104:
  143.      buscar = letras[7];
  144.      break;
  145.      case 105:
  146.      buscar = letras[8];
  147.      break;
  148.      case 106:
  149.      buscar = letras[9];
  150.      break;
  151.      case 107:
  152.      buscar = letras[10];
  153.      break;
  154.      case 108:
  155.      buscar = letras[11];
  156.      break;
  157.      case 109:
  158.      buscar = letras[12];
  159.      break;
  160.      case 110:
  161.      buscar = letras[13];
  162.      break;
  163.      case 111:
  164.      buscar = letras[14];
  165.      break;
  166.      case 112:
  167.      buscar = letras[15];
  168.      break;
  169.      case 113:
  170.      buscar = letras[16];
  171.      break;
  172.      case 114:
  173.      buscar = letras[17];
  174.      break;
  175.      case 115:
  176.      buscar = letras[18];
  177.      break;
  178.      case 116:
  179.      buscar = letras[19];
  180.      break;
  181.      case 117:
  182.      buscar = letras[20];
  183.      break;
  184.      case 118:
  185.      buscar = letras[21];
  186.      break;
  187.      case 119:
  188.      buscar = letras[22];
  189.      break;
  190.      case 120:
  191.      buscar = letras[23];
  192.      break;
  193.      case 121:
  194.      buscar = letras[24];
  195.      break;
  196.      case 122: //z
  197.      buscar = letras[25];
  198.      break;
  199.      case 48: //numeros
  200.      buscar = letras[26];
  201.      break;
  202.      case 49:
  203.      buscar = letras[27];
  204.      break;
  205.      case 50:
  206.      buscar = letras[28];
  207.      break;
  208.      case 51:
  209.      buscar = letras[29];
  210.      break;
  211.      case 52:
  212.      buscar = letras[30];
  213.      break;
  214.      case 53:
  215.      buscar = letras[31];
  216.      break;
  217.      case 54:
  218.      buscar = letras[32];
  219.      break;
  220.      case 55:
  221.      buscar = letras[33];
  222.      break;
  223.      case 56:
  224.      buscar = letras[34];
  225.      break;
  226.      case 57://9
  227.      buscar = letras[35];
  228.      break;
  229.   }
  230.  
  231.   for(int y=0; y<32; y++)
  232.   {
  233.      tempLetra[y] = readEEPROM(eeprom,y+buscar);
  234.   }
  235. }
  236.  
  237. 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)
  238. {
  239.     digitalWrite(filaLatchPin, 0);
  240.     digitalWrite(colLatchPin, 0);
  241.     shiftOut(colDataPin, colClockPin, vertical_rojo_1_8); //rojo 1-8
  242.     shiftOut(filaDataPin, filaClockPin, horizontal_1_8); //horizontal
  243.     shiftOut(colDataPin, colClockPin, vertical_rojo_8_16); //rojo 8*16
  244.     shiftOut(filaDataPin, filaClockPin, horizontal_8_16);
  245.     shiftOut(colDataPin, colClockPin, vertical_verde_1_8); //verde 1-8
  246.     shiftOut(colDataPin, colClockPin, vertical_verde_8_16); //verde 1-16
  247.     digitalWrite(colLatchPin, 1);
  248.     digitalWrite(filaLatchPin, 1);
  249.    }
  250.    
  251. void barrer(int color,int medida, byte arreglo[]) //0 rojo 1 verde 2 naranja
  252.    {
  253.      
  254.      int contador =0;
  255.      int ciclos = 0;
  256.      for(int y=0; y<medida; y++) {
  257.      
  258.        if(ciclos<8)
  259.        {
  260.         if(color == 0)
  261.         {
  262.           mostrar(num[y+1], nulo, arreglo[contador], arreglo[contador+1], nulo, nulo);
  263.         }
  264.         else if(color == 1)
  265.         {
  266.           mostrar(num[y+1], nulo, nulo, nulo, arreglo[contador], arreglo[contador+1]);
  267.         }
  268.         else
  269.         {
  270.           mostrar(num[y+1], nulo, arreglo[contador], arreglo[contador+1], arreglo[contador], arreglo[contador+1]);
  271.         }
  272.        }
  273.        else
  274.        {
  275.          if(color == 0)
  276.         {
  277.           mostrar(nulo, num[y-7], arreglo[contador], arreglo[contador+1], nulo, nulo);
  278.         }
  279.         else if(color == 1)
  280.         {
  281.           mostrar(nulo, num[y-7], nulo, nulo, arreglo[contador], arreglo[contador+1]);
  282.         }
  283.         else
  284.         {
  285.           mostrar(nulo,num[y-7], arreglo[contador], arreglo[contador+1], arreglo[contador], arreglo[contador+1]);
  286.         }
  287.        }
  288.        ciclos+=1;
  289.        contador+=2;
  290.        if(ciclos == 16)
  291.        {
  292.          ciclos = 0;
  293.        }
  294.      }
  295.    }
  296.    
  297. void mover(byte arreglo[], int tamano, char direccion[])
  298. {
  299.   byte data1;
  300.   byte data2;
  301.   unsigned int byte16;
  302.   int contador = 0;
  303.  
  304.   for(int y=0; y<tamano; y++){
  305.    
  306.     data1 = arreglo[contador];
  307.     data2 = arreglo[contador+1];
  308.     byte16 = (data1<<8)|(data2);
  309.      if(direccion == "izquierda")
  310.     {
  311.      byte16 = byte16 << 1;
  312.     }
  313.     else
  314.     {
  315.       byte16 = byte16 >> 1;
  316.     }
  317.    data1 = byte16; //autorrecorte
  318.    data2 = byte16 >> 8; //mover datos y autorrecorte
  319.    
  320.    arreglo[contador] = data2;  //invertir los datos al mostrar
  321.    arreglo[contador+1] = data1;
  322.    contador+=2;
  323.    
  324.   }
  325. }
  326.  
  327. void copiarLetra(byte letra[], byte destino[])
  328. {
  329.   for(int y=0; y<32; y++)
  330.   {
  331.     destino[y] = letra[y];
  332.    
  333.   }
  334.  
  335. }
  336.  
  337. void mostrarPalabra(char palabra[], int medida, int color)
  338. {
  339.   int tamano = 32*medida;
  340.   byte arreglo[tamano];
  341.   int ciclo = 0;
  342.  
  343.   for(int y=0; y<=medida; y++)
  344.   {
  345.    switch(palabra[y])
  346.    {
  347.      case 97:
  348.      obtenerLetra("a");
  349.      copiarLetra(tempLetra,arreglo);
  350.      break;
  351.      case 98:
  352.      obtenerLetra("b");
  353.      copiarLetra(tempLetra,arreglo);
  354.      break;
  355.      case 99:
  356.      obtenerLetra("c");
  357.      copiarLetra(tempLetra,arreglo);
  358.      break;
  359.      case 100:
  360.      obtenerLetra("d");
  361.      copiarLetra(tempLetra,arreglo);
  362.      break;
  363.      case 101:
  364.      obtenerLetra("e");
  365.      copiarLetra(tempLetra,arreglo);
  366.      break;
  367.      case 102:
  368.      obtenerLetra("f");
  369.      copiarLetra(tempLetra,arreglo);
  370.      break;
  371.      case 103:
  372.      obtenerLetra("g");
  373.      copiarLetra(tempLetra,arreglo);
  374.      break;
  375.      case 104:
  376.      obtenerLetra("h");
  377.      copiarLetra(tempLetra,arreglo);
  378.      break;
  379.      case 105:
  380.      obtenerLetra("i");
  381.      copiarLetra(tempLetra,arreglo);
  382.      break;
  383.      case 106:
  384.      obtenerLetra("j");
  385.      copiarLetra(tempLetra,arreglo);
  386.      break;
  387.      case 107:
  388.      obtenerLetra("k");
  389.      copiarLetra(tempLetra,arreglo);
  390.      break;
  391.      case 108:
  392.      obtenerLetra("l");
  393.      copiarLetra(tempLetra,arreglo);
  394.      break;
  395.      case 109:
  396.      obtenerLetra("m");
  397.      copiarLetra(tempLetra,arreglo);
  398.      break;
  399.      case 110:
  400.      obtenerLetra("n");
  401.      copiarLetra(tempLetra,arreglo);
  402.      break;
  403.      case 111:
  404.      obtenerLetra("o");
  405.      copiarLetra(tempLetra,arreglo);
  406.      break;
  407.      case 112:
  408.      obtenerLetra("p");
  409.      copiarLetra(tempLetra,arreglo);
  410.      break;
  411.      case 113:
  412.      obtenerLetra("q");
  413.      copiarLetra(tempLetra,arreglo);
  414.      break;
  415.      case 114:
  416.      obtenerLetra("r");
  417.      copiarLetra(tempLetra,arreglo);
  418.      break;
  419.      case 115:
  420.      obtenerLetra("s");
  421.      copiarLetra(tempLetra,arreglo);
  422.      break;
  423.      case 116:
  424.      obtenerLetra("t");
  425.      copiarLetra(tempLetra,arreglo);
  426.      break;
  427.      case 117:
  428.      obtenerLetra("u");
  429.      copiarLetra(tempLetra,arreglo);
  430.      break;
  431.      case 118:
  432.      obtenerLetra("v");
  433.      copiarLetra(tempLetra,arreglo);
  434.      break;
  435.      case 119:
  436.      obtenerLetra("w");
  437.      copiarLetra(tempLetra,arreglo);
  438.      break;
  439.      case 120:
  440.      obtenerLetra("x");
  441.      copiarLetra(tempLetra,arreglo);
  442.      break;
  443.      case 121:
  444.      obtenerLetra("y");
  445.      copiarLetra(tempLetra,arreglo);
  446.      break;
  447.      case 122:
  448.      obtenerLetra("z");
  449.      copiarLetra(tempLetra,arreglo); //800
  450.      break;
  451.      case 20:
  452.      copiarLetra(espacio,arreglo);
  453.      break;
  454.      case 48:
  455.      obtenerLetra("0");
  456.      copiarLetra(tempLetra,arreglo);
  457.      break;
  458.      case 49:
  459.      obtenerLetra("1");
  460.      copiarLetra(tempLetra,arreglo);
  461.      break;
  462.      case 50:
  463.      obtenerLetra("2");
  464.      copiarLetra(tempLetra,arreglo);
  465.      break;
  466.      case 51:
  467.      obtenerLetra("3");
  468.      copiarLetra(tempLetra,arreglo);
  469.      break;
  470.      case 52:
  471.      obtenerLetra("4");
  472.      copiarLetra(tempLetra,arreglo);
  473.      break;
  474.      case 53:
  475.      obtenerLetra("5");
  476.      copiarLetra(tempLetra,arreglo);
  477.      break;
  478.      case 54:
  479.      obtenerLetra("6");
  480.      copiarLetra(tempLetra,arreglo);
  481.      break;
  482.      case 55:
  483.      obtenerLetra("7");
  484.      copiarLetra(tempLetra,arreglo);
  485.      break;
  486.      case 56:
  487.      obtenerLetra("8");
  488.      copiarLetra(tempLetra,arreglo);
  489.      break;
  490.      case 57:
  491.      obtenerLetra("9");
  492.      copiarLetra(tempLetra,arreglo);
  493.      break;
  494.     }
  495.    ciclo = 0;
  496.    for(int a=0; a<2; a++)
  497.      {
  498.      mover(arreglo,32,"derecha");
  499.      }
  500.    for(int b=0; b<14; b++)
  501.    {
  502.      if(ciclo == 0)
  503.      {
  504.      for(int c=0; c<10; c++)
  505.      {
  506.       barrer(color,16,arreglo);
  507.      }
  508.      }
  509.      else{
  510.        for(int d=0; d<3; d++)
  511.      {
  512.       barrer(color,16,arreglo);
  513.      }
  514.      }
  515.      ciclo = 1;
  516.      mover(arreglo,32,"izquierda");
  517.    }
  518.   }
  519. }
  520.  
  521. void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  522.  
  523.   int i=0;
  524.   int pinState;
  525.   pinMode(myClockPin, OUTPUT);
  526.   pinMode(myDataPin, OUTPUT);
  527.  
  528.   digitalWrite(myDataPin, 0);
  529.   digitalWrite(myClockPin, 0);
  530.  
  531.    for (i=7; i>=0; i--)  {
  532.     digitalWrite(myClockPin, 0);
  533.     if ( myDataOut & (1<<i) ) {
  534.       pinState= 1;
  535.     }
  536.     else { 
  537.       pinState= 0;
  538.     }
  539.     digitalWrite(myDataPin, pinState);
  540.     digitalWrite(myClockPin, 1);
  541.     digitalWrite(myDataPin, 0);
  542.   }
  543.   digitalWrite(myClockPin, 0);
  544.  
  545. }
  546.  
  547.  
  548. void mostrarArrayTablero()
  549. {
  550.  int contador =0;
  551.      int ciclos = 0;
  552.      for(int y=0; y<16; y++) {
  553.      
  554.        if(ciclos<8)
  555.        {
  556.         mostrar(num[y+1], nulo, arrayTableroRojo[contador], arrayTableroRojo[contador+1], arrayTableroVerde[contador], arrayTableroVerde[contador+1]);
  557.         }
  558.        else
  559.        {
  560.          mostrar(nulo,num[y-7], arrayTableroRojo[contador], arrayTableroRojo[contador+1], arrayTableroVerde[contador], arrayTableroVerde[contador+1]);
  561.        }
  562.        ciclos+=1;
  563.        contador+=2;
  564.        if(ciclos == 16)
  565.        {
  566.          ciclos = 0;
  567.        }
  568.      }
  569.    }
  570.  
  571. void borrarTablero()
  572. {
  573.   for(int y=0; y<8; y++)
  574.   {
  575.     for(int x=0; x<8; x++)
  576.     {
  577.      escribirArrayTableroSobre(x,y,tablero[x][y]);
  578.     }
  579.   }
  580. }
  581.  
  582. void escribirArrayTableroSobre(int fila, int columna, int valor)
  583. {
  584. int lugar = fila*4;
  585.  
  586. int par = 2;
  587. int impar1 = 1;
  588. int impar3 = 3;
  589.  
  590. int constante1;
  591. int constante2;
  592.  
  593. if(columna<=3)
  594. {
  595.   constante1 = 0;
  596.   constante2 = par;
  597. }
  598. else
  599. {
  600.   constante1 = impar1;
  601.   constante2 = impar3;
  602. }
  603.  
  604.   if(valor == 0) //nulo
  605.   {
  606.     arrayTableroRojo[lugar+constante1] = nulo;
  607.     arrayTableroRojo[lugar+constante2] = nulo;
  608.    
  609.     arrayTableroVerde[lugar+constante1] = nulo;
  610.     arrayTableroVerde[lugar+constante2] = nulo;
  611.   }
  612.   else if(valor == 1) //rojo
  613.   {
  614.     arrayTableroRojo[lugar+constante1] = datosTablero[columna];
  615.     arrayTableroRojo[lugar+constante2] = datosTablero[columna];
  616.    
  617.     arrayTableroVerde[lugar+constante1] = nulo;
  618.     arrayTableroVerde[lugar+constante2] = nulo;
  619.   }
  620.   else if(valor == 2) //verde
  621.   {
  622.     arrayTableroRojo[lugar+constante1] = nulo;
  623.     arrayTableroRojo[lugar+constante2] = nulo;
  624.    
  625.     arrayTableroVerde[lugar+constante1] = datosTablero[columna];
  626.     arrayTableroVerde[lugar+constante2] = datosTablero[columna];
  627.   }
  628.   else if(valor == 3) //naranja
  629.   {
  630.     arrayTableroRojo[lugar+constante1] = datosTablero[columna];
  631.     arrayTableroRojo[lugar+constante2] = datosTablero[columna];
  632.    
  633.     arrayTableroVerde[lugar+constante1] = datosTablero[columna];
  634.     arrayTableroVerde[lugar+constante2] = datosTablero[columna];
  635.   }
  636. }
  637.  
  638. void escribirArrayTablero(int fila, int columna, int valor)
  639. {
  640.  int lugar = fila*4;
  641.  
  642. int par = 2;
  643. int impar1 = 1;
  644. int impar3 = 3;
  645.  
  646. int constante1;
  647. int constante2;
  648.  
  649. if(columna<=3)
  650. {
  651.   constante1 = 0;
  652.   constante2 = par;
  653. }
  654. else
  655. {
  656.   constante1 = impar1;
  657.   constante2 = impar3;
  658. }
  659.  
  660.   if(valor == 0) //nulo
  661.   {
  662.     arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | nulo;
  663.     arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | nulo;
  664.    
  665.     arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | nulo;
  666.     arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | nulo;
  667.   }
  668.   else if(valor == 1) //rojo
  669.   {
  670.     arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | datosTablero[columna];
  671.     arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | datosTablero[columna];
  672.    
  673.     arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | nulo;
  674.     arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | nulo;
  675.   }
  676.   else if(valor == 2) //verde
  677.   {
  678.     arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | nulo;
  679.     arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | nulo;
  680.    
  681.     arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | datosTablero[columna];
  682.     arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | datosTablero[columna];
  683.   }
  684.   else if(valor == 3) //naranja
  685.   {
  686.     arrayTableroRojo[lugar+constante1] = arrayTableroRojo[lugar+constante1] | datosTablero[columna];
  687.     arrayTableroRojo[lugar+constante2] = arrayTableroRojo[lugar+constante2] | datosTablero[columna];
  688.    
  689.     arrayTableroVerde[lugar+constante1] = arrayTableroVerde[lugar+constante1] | datosTablero[columna];
  690.     arrayTableroVerde[lugar+constante2] = arrayTableroVerde[lugar+constante2] | datosTablero[columna];
  691.   }
  692. }
  693.  
  694. void refreshTablero()
  695. {
  696.   for(int y=0; y<8; y++)
  697.   {
  698.     for(int x=0; x<8; x++)
  699.     {
  700.      escribirArrayTablero(x,y,tablero[x][y]);
  701.     }
  702.   }
  703. }
  704.  
  705. void combinarArrays(byte array1[], byte array2[], byte destino[])
  706. {
  707.  for(int y=0; y<32; y++)
  708. {
  709.   destino[y]=array1[y] | array2[y];
  710. }
  711. }
  712.  
  713. int moverSelector(int direccion)//0 atras, 1 delante
  714. {
  715.   //borrar cualquier selector anterior
  716.   for(int y=0; y<8; y++)
  717.   {
  718.     borrarSelector(y);
  719.   }
  720.  
  721.   if(direccion == 0)
  722.   {
  723.     if(valorSelector == 0)
  724.     {
  725.       valorSelector == 7;
  726.     }
  727.     else
  728.     {
  729.       valorSelector--;
  730.     }
  731.     imprimirSelector(valorSelector);
  732.     return valorSelector;
  733.   }
  734.   else
  735.   {
  736.     if(valorSelector == 7)
  737.     {
  738.       valorSelector == 0;
  739.     }
  740.     else
  741.     {
  742.       valorSelector++;
  743.     }
  744.     imprimirSelector(valorSelector);
  745.     return valorSelector;
  746.   }
  747.    
  748. }
  749.  
  750. void imprimirSelector(int posicion) //imprime el selector
  751. {
  752.   escribirArrayTablero(0,posicion,3);
  753.  
  754. }
  755.  
  756. void borrarSelector(int posicion)
  757. {
  758.   escribirArrayTableroSobre(0,posicion,0);
  759. }
  760.  
  761. void animarCaidaFicha(int color, int columna, int fila) //desde columna(arriba), hasta fila | color  1 rojo 2 verde 3 naranja
  762. {
  763.   for(int y=0; y<=fila; y++)
  764.   {
  765.     escribirArrayTablero(y,columna,color);
  766.     refreshTablero();
  767.     for(int x=0; x<6; x++)
  768.     {
  769.     mostrarArrayTablero();
  770.     }
  771.     borrarTablero();
  772.   }
  773.  
  774. }
  775.  
  776. void resetTablero()
  777. {
  778.  for(int y=0; y<=8; y++)
  779.  {
  780.    for(int x=0; x<=8; x++)
  781.  {
  782.    tablero[x][y] = 0;
  783.  }
  784.  }
  785. }
  786. void tirarFichaOnline(int columna, int valor)
  787. {
  788.   int fila=0;
  789.   for(int y=0; y<8; y++)
  790.   {
  791.     if(tablero[y][columna] == 0)
  792.     {
  793.       fila++;
  794.     }
  795.    }
  796.    if(fila>0)
  797.    {
  798.      fila = fila - 1;
  799.    }
  800.    else
  801.    {
  802.      fila = 0;
  803.    }
  804.    tablero[fila][columna] = valor;
  805.    refreshTablero();
  806.    borrarTablero();
  807.    
  808.   }
  809. void tirarFicha(int columna, int valor)
  810. {
  811.   int fila=0;
  812.   for(int y=0; y<8; y++)
  813.   {
  814.     if(tablero[y][columna] == 0)
  815.     {
  816.       fila++;
  817.     }
  818.    }
  819.    if(fila>0)
  820.    {
  821.      fila = fila - 1;
  822.    }
  823.    else
  824.    {
  825.      fila = 0;
  826.    }
  827.    animarCaidaFicha(valor, columna, fila);
  828.    tablero[fila][columna] = valor;
  829.    refreshTablero();
  830.    
  831.  
  832. }
  833.  
  834.  
  835. int Selector(int valor)
  836. {
  837.  
  838.   boolean tirada = true;
  839.  
  840.  while(tirada == true)
  841.  {
  842.  while((digitalRead(mandoPin1) == LOW) && (digitalRead(mandoPin4) == LOW) && (digitalRead(mandoPin5) == LOW))
  843.   {
  844.     refreshTablero();
  845.     mostrarArrayTablero();
  846.    }
  847.   if(digitalRead(mandoPin5) == HIGH){
  848.      tirarFicha(datoSelector, valor);
  849.     tiempoPasado=millis();
  850.     refreshTablero();
  851.     while ((millis()-tiempoPasado)<150)
  852.     {
  853.     mostrarArrayTablero();
  854.     }  
  855.     tirada = false;
  856. }
  857.     if(digitalRead(mandoPin1) == HIGH) {
  858.     tiempoPasado=millis();
  859.     datoSelector = moverSelector(0);
  860.     refreshTablero();
  861.     while ((millis()-tiempoPasado)<150)
  862.     {
  863.     mostrarArrayTablero();
  864.     }  
  865. }
  866.     if(digitalRead(mandoPin4) == HIGH) {
  867.     tiempoPasado=millis();
  868.     datoSelector = moverSelector(1);
  869.     refreshTablero();
  870.     while ((millis()-tiempoPasado)<150)
  871.     {
  872.     mostrarArrayTablero();
  873.     }  
  874. }
  875.  }
  876.  //se ha acabado el turno
  877.  for(int y = 0; y<25; y++)//mostrar durante un rato el tablero
  878.  {
  879.    mostrarArrayTablero();
  880.  }
  881.  return 1; //acabar con la funcion
  882. }
  883.  
  884. int comprobarColumnaLibre(int columna) //devuelve el numero de fila libre
  885. {
  886.   for(int y=7; y<=0; y--)
  887.   {
  888.     if(tablero[y][columna] == 0)
  889.     {
  890.       return y;
  891.     }
  892.   }
  893. }
  894.  
  895.  
  896. void tirarMaquina(int valor) //funcion de inteligencia para la tirada
  897. {
  898.   unsigned long probabilidad[8]; //filas donde se almacenar\u00e1 la probabilidad
  899.   int accion;
  900.   int tirar;
  901.   int temp;
  902.   //vertical
  903.   for(int y=0; y<8; y++) //columna
  904.   {
  905.     for(int x=0; x<=6; x++) //fila
  906.     {
  907.       if((tablero[x][y]== valor) && (tablero[x+1][y] == valor)) //dos juntas
  908.       {
  909.         probabilidad[y]+=1;
  910.         if((tablero[x-1][y] == 0) && (x>0))
  911.         {
  912.           probabilidad[y]+=5;
  913.         }
  914.       }
  915.       }
  916.   }
  917. for(int y=0; y<8; y++) //columna
  918.   {
  919.     for(int x=0; x<=5; x++) //fila
  920.     {
  921.       if((tablero[x][y]== valor) && (tablero[x+1][y] == valor) && (tablero[x+2][y] == valor)) //tres juntas
  922.       {
  923.         probabilidad[y]+=6;
  924.         if((tablero[x-1][y] == 0) && (x>0))
  925.         {
  926.           probabilidad[y]+=15;
  927.         }
  928.       }
  929.       }
  930.     }
  931.   //horizontal
  932.  temp = probabilidad[0];
  933.  for(int y=0; y<8; y++) //averiguar el maximo
  934.  {
  935.    
  936.   Serial.println(probabilidad[y]);
  937.  }
  938.  for(int y=0; y<8; y++) //averiguar la posicion del m\u00e1ximo
  939.  {
  940.    Serial.println(probabilidad[y]);
  941.    if(probabilidad[y] == temp)
  942.    {
  943.      tirar = y;
  944.    }
  945.  }
  946.  
  947.  //ya tenemos donde tirar
  948.  tirarFicha(0,1);
  949.  for(int y=0; y<15; y++)
  950.  {
  951.    mostrarArrayTablero();
  952.  }
  953. }
  954. void animacionRecoger()
  955. {
  956.   for(int x=0; x<8; x++)
  957.   {
  958.     for(int y=0; y<8; y++)
  959.     {
  960.       escribirArrayTablero(x,y,3);
  961.       refreshTablero();
  962.       for(int z=0; z<3; z++)
  963.       {
  964.         mostrarArrayTablero();
  965.       }
  966.     }
  967.   }
  968.   borrarTablero();
  969.   resetTablero();
  970. }
  971.  
  972. void Ganar(int ganador)
  973. {
  974.  if(ganador == 1) //la maquina
  975. {
  976.  mostrarPalabra("has perdido",11,2);
  977. }
  978. else
  979. {
  980.   mostrarPalabra("has ganado",10,2);
  981. }
  982. for(int y=0; y<30; y++)
  983. {
  984.   mostrarArrayTablero();
  985. }
  986. animacionRecoger();
  987. }
  988.  
  989. int detectarGanador(int valor) //1 gana 1 (rojo) 2 gana 2 (verde);
  990. {
  991.   //vertical
  992.   for(int y=0; y<8; y++) //columna
  993.   {
  994.     for(int x=0; x<=4; x++) //fila
  995.     {
  996.       if((tablero[x][y]== valor) && (tablero[x+1][y] == valor) && (tablero[x+2][y] == valor) && (tablero[x+3][y] == valor))
  997.       {
  998.         return valor;
  999.       }
  1000.     }
  1001.   }
  1002.   //horizontal
  1003.   for(int y=0; y<=4; y++) //columna
  1004.   {
  1005.     for(int x=0; x<8; x++) //fila
  1006.     {
  1007.       if((tablero[x][y] == valor) && (tablero[x][y+1] == valor) && (tablero[x][y+2] == valor) && (tablero[x][y+3] == valor))
  1008.       {
  1009.         return valor;
  1010.       }
  1011.     }
  1012.   }
  1013.   //diagonal normal 1/2
  1014.  for(int y=3; y<=7; y++) //fila
  1015.  {
  1016.    if((tablero[y][0] == valor) && (tablero[y-1][1] == valor) && (tablero[y-2][2] == valor) && (tablero[y-3][3] == valor))
  1017.    {
  1018.      return valor;
  1019.    }
  1020.  }
  1021.  for(int y=0; y<=4; y++) //columna
  1022.  {
  1023.    if((tablero[7][y] == valor) && (tablero[6][y+1] == valor) && (tablero[5][y+2] == valor) && (tablero[4][y+3] == valor))
  1024.    {
  1025.      return valor;
  1026.    }
  1027.  }
  1028.  //diagonal normal 2/2
  1029.  for(int y=3; y<=7; y++) //columna
  1030.  {
  1031.    if((tablero[0][y] == valor) && (tablero[1][y-1] == valor) && (tablero[2][y-2] == valor) && (tablero[3][y-3] == valor))
  1032.    {
  1033.      return valor;
  1034.    }
  1035.  }
  1036.  for(int y=0; y<=4; y++) //fila
  1037.  {
  1038.    if((tablero[y][7] == valor) && (tablero[y+1][6] == valor) && (tablero[y+2][5] == valor) && (tablero[y+3][4] == valor))
  1039.    {
  1040.      return valor;
  1041.    }
  1042.  }
  1043.  
  1044.  //diagonal inversa 1/2
  1045.  for(int y=4; y>=0; y--) //fila
  1046.  {
  1047.    if((tablero[y][0] == valor) && (tablero[y+1][1] == valor) && (tablero[y+2][2] == valor) && (tablero[y+3][3] == valor))
  1048.    {
  1049.      return valor;
  1050.    }
  1051.  }
  1052.  for(int y=0; y<=4; y++) //columna
  1053.  {
  1054.    if((tablero[0][y] == valor) && (tablero[1][y+1] == valor) && (tablero[2][y+2] == valor) && (tablero[3][y+3] == valor))
  1055.    {
  1056.      return valor;
  1057.    }
  1058.  }
  1059.  
  1060.  //diagonal inversa 2/2
  1061.  for(int y=3; y<=7; y++) //columna
  1062.  {
  1063.    if((tablero[7][y] == valor) && (tablero[6][y-1] == valor) && (tablero[5][y-2] == valor) && (tablero[4][y-3] == valor))
  1064.    {
  1065.      return valor;
  1066.    }
  1067.  }
  1068.  
  1069. for(int y=7; y<=3; y--)
  1070. {
  1071.   if((tablero[7][y] == valor) && (tablero[6][y-1] == valor) && (tablero[5][y-2] == valor) && (tablero[4][y-3] == valor))
  1072.    {
  1073.      return valor;
  1074.    }
  1075. }
  1076. }
  1077.  
  1078. int menu()
  1079. {
  1080.   //mostrarPalabra("a  jugar  b  chat",16,0);
  1081.   int valor = 0;
  1082.   int subvalor = 0;
  1083.   while(finalizarMenu==0)
  1084.   {
  1085.     if(valor == 0)
  1086.     {
  1087.       barrer(2,16,a);
  1088.       if(digitalRead(mandoPin5) == HIGH)
  1089.       {
  1090.         for(int y=0; y<10; y++)
  1091.         {
  1092.         barrer(1,16,a);
  1093.         }
  1094.         mostrarPalabra("jugadores",8,2);
  1095.         while(finalizarSubMenu==0)
  1096.         {
  1097.           if(subvalor==0)
  1098.           {
  1099.             barrer(2,16,num1);
  1100.             if(digitalRead(mandoPin5) == HIGH)
  1101.             {
  1102.               for(int y=0; y<10; y++)
  1103.               {
  1104.                 barrer(1,16,num1);
  1105.               }
  1106.               return 1; //un jugador
  1107.             }
  1108.           }
  1109.           else
  1110.           {
  1111.             barrer(2,16,num2);
  1112.             if(digitalRead(mandoPin5) == HIGH)
  1113.             {
  1114.               for(int y=0; y<10; y++)
  1115.               {
  1116.                 barrer(1,16,num2);
  1117.               }
  1118.               return 2; //multiplayer
  1119.             }
  1120.           }
  1121.           if(digitalRead(mandoPin1) == HIGH)
  1122.           {
  1123.             subvalor=0;
  1124.           }
  1125.           if(digitalRead(mandoPin4) == HIGH)
  1126.           {
  1127.             subvalor=1;
  1128.           }
  1129.         }
  1130.          //1 jugar1, 2multip, 3 chat
  1131.       }
  1132.     }
  1133.     else
  1134.     {
  1135.       barrer(2,16,b);
  1136.       if(digitalRead(mandoPin5) == HIGH)
  1137.       {
  1138.         for(int y=0; y<10; y++)
  1139.         {
  1140.         barrer(1,16,b);
  1141.         }
  1142.         return 3; //chat
  1143.       }
  1144.     }
  1145.     if(digitalRead(mandoPin1) == HIGH)
  1146.     {
  1147.      valor = 0;
  1148.      
  1149.     }  
  1150.     if(digitalRead(mandoPin4) == HIGH)
  1151.     {
  1152.      valor = 1;
  1153.      
  1154.      
  1155.     }
  1156.      if((digitalRead(mandoPin2) == HIGH) || (digitalRead(mandoPin3) == HIGH))
  1157.      {
  1158.        mostrarPalabra("a  jugar  b  chat",17,2);
  1159.      }
  1160.     }  
  1161.   }
  1162.  
  1163.   void copiarToChar()
  1164.   {
  1165.      int contador = 0; //mapeamos los valores
  1166.     for(int y=0; y<8; y++)
  1167.     {
  1168.       for(int x=0; x<8; x++)
  1169.     {
  1170.       if(tablero[y][x] == 0)
  1171.       {
  1172.         envio[contador] = '1';
  1173.       }
  1174.       else if(tablero[y][x] == 1)
  1175.       {
  1176.         envio[contador] = '2';
  1177.       }
  1178.       else if(tablero[y][x] == 2)
  1179.       {
  1180.         envio[contador] = '3';
  1181.       }
  1182.       contador++;
  1183.     }
  1184.     }
  1185.     contador = 0;
  1186.    
  1187.   }
  1188.  
  1189.  void enviarDatos(int dato)
  1190.  {
  1191.    boolean enviado = false;
  1192.    while(enviado == false)
  1193.    {
  1194.     barrer(0,16,desconectado);
  1195.    if(Serial.available()>0)
  1196.    {
  1197.      Serial.print(dato);
  1198.      Serial.flush();
  1199.      enviado = true;
  1200.    }
  1201.  }
  1202.  }
  1203.   void enviarTablero()
  1204.   {
  1205.     contadorEnviar=0;
  1206.     boolean enviar = false;
  1207.    
  1208.      copiarToChar();
  1209.    while(enviar == false)
  1210.   {
  1211.     if(Serial.available()>0)
  1212.   {
  1213.     if(contadorEnviar>63)
  1214.     {
  1215.       contadorEnviar = 0;
  1216.       enviar = true;
  1217.       completado = true;
  1218.     }
  1219.     Serial.print(envio[contadorEnviar]);
  1220.     Serial.flush();
  1221.     contadorEnviar++;
  1222.    
  1223.   }
  1224.   }
  1225.   }
  1226.  
  1227. int recibirDatos()
  1228. {
  1229.   int dato;
  1230.   boolean recibir = true;
  1231.   while(recibir == true)
  1232.   {
  1233.   if(Serial.available()>0)
  1234.   {
  1235.     dato = Serial.read();
  1236.     Serial.flush();
  1237.     recibir = false;
  1238.     return dato;
  1239.   }
  1240. }
  1241. }
  1242.  
  1243. int leerSensor()
  1244. {
  1245.   int pin = OjosPin;
  1246.  unsigned long Duracion;
  1247.  pinMode(pin, OUTPUT);
  1248.   digitalWrite(pin, LOW);
  1249.   delayMicroseconds(2);
  1250.   digitalWrite(pin, HIGH);
  1251.   delayMicroseconds(10);
  1252.   digitalWrite(pin, LOW);
  1253.  
  1254.   pinMode(pin, INPUT);
  1255.   Duracion = pulseIn(pin, HIGH);
  1256.   tiempoPasado = millis();
  1257.   while ((millis()-tiempoPasado)<50)
  1258.     {
  1259.     barrer(2,16,tempLetra);
  1260.     }  
  1261.   return Duracion/58;
  1262. }
  1263.  
  1264. void seleccionarLetra()
  1265. {
  1266.   int contador = 0;
  1267.   char recibir[50];
  1268.  int dato;
  1269.   if(valorLista>36)
  1270.   {
  1271.     valorLista = 0;
  1272.   }
  1273.   tiempoPasado = millis();
  1274.   while ((millis()-tiempoPasado)<25)
  1275.     {
  1276.     barrer(2,16,tempLetra);
  1277.     }  
  1278.   if(leerSensor()<20)
  1279.   {
  1280.     char letra[1] = {int(caracteres[valorLista])};
  1281.     obtenerLetra(letra);
  1282.     valorLista++;
  1283.   }
  1284.  
  1285.   if(digitalRead(mandoPin5) == HIGH)
  1286.   {
  1287.     tiempoPasado = millis();
  1288.   while ((millis()-tiempoPasado)<180)
  1289.     {
  1290.     barrer(1,16,tempLetra);
  1291.     }  
  1292.     palabra[contadorSelec] = caracteres[valorLista-1];
  1293.     contadorSelec++;
  1294.   }
  1295.   if(digitalRead(mandoPin6) == HIGH)
  1296.   {
  1297.     enviarPalabra();
  1298.   }
  1299.   if(Serial.available()>0)
  1300.   {
  1301.     dato = Serial.read();
  1302.     Serial.flush();
  1303.     if(dato>0)
  1304.   {
  1305.     for(int y=0; y<dato; y++)
  1306.     {
  1307.       recibir[y] = Serial.read();
  1308.       contador++;
  1309.     }
  1310.     mostrarPalabra(recibir,sizeof(recibir),2);
  1311.   }
  1312.   }
  1313.  
  1314.  
  1315. }
  1316.  
  1317. void limpiarPalabra()
  1318. {
  1319.   for(int y=0; y<50; y++)
  1320.   {
  1321.     palabra[y] = '0';
  1322.   }
  1323. }
  1324. void enviarPalabra()
  1325. {
  1326.   contadorEnviar=0;
  1327.     boolean enviar = false;
  1328.    
  1329.      copiarToChar();
  1330.    while(enviar == false)
  1331.   {
  1332.     if(Serial.available()>0)
  1333.   {
  1334.     if(contadorEnviar>99)
  1335.     {
  1336.       contadorEnviar = 0;
  1337.       enviar = true;
  1338.       completado = true;
  1339.     }
  1340.     Serial.print(palabra[contadorEnviar]);
  1341.     Serial.flush();
  1342.     contadorEnviar++;
  1343.    
  1344.   }
  1345.   }
  1346.   }
  1347.  
  1348. void setup() {
  1349.  
  1350.   pinMode(colLatchPin, OUTPUT);
  1351.   pinMode(filaLatchPin, OUTPUT);
  1352.  
  1353.   pinMode(mandoPin1, INPUT);
  1354.   pinMode(mandoPin2, INPUT);
  1355.   pinMode(mandoPin3, INPUT);
  1356.   pinMode(mandoPin4, INPUT);
  1357.   pinMode(mandoPin5, INPUT);
  1358.   pinMode(mandoPin6, INPUT);
  1359.  
  1360.  
  1361.   Serial.begin(57600);
  1362.   Wire.begin();
  1363.  
  1364.   partida = menu();
  1365.   introJuego = 0;
  1366.   limpiarPalabra();
  1367.  
  1368.  
  1369. }
  1370.  
  1371. void loop() {
  1372.  
  1373. switch(partida)
  1374. {
  1375.   case 1: //1player
  1376.   if(introJuego == 0) //antes de empezar partida
  1377.   {
  1378.     //mostrarPalabra("juegas con verdes y empiezas",27,1);
  1379.     resetTablero();
  1380.     borrarTablero();
  1381.     refreshTablero();
  1382.     introJuego = 1;
  1383.     turnoJuego = 1; //te toca a ti
  1384.   }
  1385.   //comprobar si te toca
  1386.   if((turnoJuego == 1) && (tirada == 0))
  1387.   {
  1388.   tirada = Selector(2);
  1389.   }
  1390.   //si no te toca tira la maquina
  1391.   else
  1392.   {
  1393.    tirarMaquina(1);
  1394.    turnoJuego = 1;
  1395.    tirada = 0;
  1396.    
  1397.   }
  1398.   if(detectarGanador(1)==1)
  1399.   {
  1400.     Ganar(1);
  1401.     menu();
  1402.   }
  1403.   if(detectarGanador(2)==2)
  1404.   {
  1405.     Ganar(2);
  1406.     menu();
  1407.   }
  1408.   break;
  1409.  
  1410.   case 2: //multiplayer
  1411.  
  1412.   if(introJuego == 0)
  1413.   {
  1414.    
  1415.    while(incoming == 0)
  1416.    {
  1417.      barrer(0,16,desconectado);
  1418.      if(Serial.available()>0)
  1419.    {
  1420.      incoming = Serial.read();
  1421.      Serial.print(incoming,BYTE);
  1422.      introJuego = 1;
  1423.      turnoJuego = 1; //te toca a ti
  1424.      resetTablero();
  1425.      borrarTablero();
  1426.      refreshTablero();
  1427.      Serial.flush();
  1428.    }
  1429.    }
  1430.    for(int y=0; y<50; y++)
  1431.    {
  1432.    barrer(1,16,conectado);
  1433.    }
  1434.   }
  1435.    
  1436.      enviarDatos(3);
  1437.    //inicio del juego online
  1438.    if((turnoJuego == 1) && (tirada == 0))
  1439.    {
  1440.      
  1441.      tirada = Selector(2);
  1442.    
  1443.    }
  1444.    else
  1445.    {
  1446.      while(completado == false)
  1447.      {
  1448.      Serial.flush();
  1449.      enviarDatos(1);
  1450.      Serial.flush();
  1451.      enviarTablero();
  1452.      }
  1453.      Serial.flush();
  1454.    
  1455.      incoming = recibirDatos();
  1456.      Serial.flush();
  1457.      tirarFichaOnline(incoming,1);
  1458.      
  1459.      turnoJuego = 1;
  1460.      tirada = 0;
  1461.    }
  1462.    completado = false;
  1463.    subCompletado = false;
  1464.    Serial.flush();
  1465.    if(detectarGanador(1)==1)
  1466.   {
  1467.     enviarDatos(4);
  1468.     enviarTablero();
  1469.     Ganar(1); //pierde arduino
  1470.     menu();
  1471.   }
  1472.   if(detectarGanador(2)==2)
  1473.   {
  1474.     enviarDatos(5);
  1475.     enviarTablero();
  1476.     Ganar(2); //gana arduino.
  1477.     menu();
  1478.   }
  1479.  
  1480.   break;
  1481.  
  1482.   case 3:
  1483.   seleccionarLetra();
  1484.   break;
  1485. }
  1486.  
  1487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement