Advertisement
Guest User

Combates

a guest
Jan 20th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 37.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. template <class T>
  12. class nodo
  13. {
  14.   public:
  15.     T info;
  16.     nodo<T>* next, *prev;
  17.     nodo()
  18.     {
  19.       this->next = NULL;
  20.       this->prev = NULL;
  21.     }
  22.     nodo(T info)
  23.     {
  24.       this->info = info;
  25.       this->next = NULL;
  26.       this->prev = NULL;
  27.     }
  28. };
  29. template <class T>
  30. class lista : public nodo<T>
  31. {
  32.   protected:
  33.     nodo<T>* primero, *ultimo;
  34.     int size = 0;
  35.     nodo<T>* crear_nodo(T info)
  36.     {
  37.       nodo<T>* nuevo;
  38.       nuevo = new nodo<T>(info);
  39.       return nuevo;
  40.     }
  41.   public:
  42.     bool vacia()
  43.     {
  44.       return ((primero == NULL && ultimo == NULL) || getsize() == 0);
  45.     }
  46.     int imprimir()
  47.     {
  48.       printf("Tamanio de la lista: %d\n", getsize());
  49.       nodo<T>* temp;
  50.       temp = primero;
  51.       for(int i = 1; i <= getsize(); ++i)
  52.       {
  53.         cout << "Elemento N" << i << ": " << temp->info << "\n";
  54.         temp = temp->next;
  55.       }
  56.       return 1;
  57.     }
  58.     int insertar_primero(T info)
  59.     {
  60.       nodo<T>* aux;
  61.       aux = crear_nodo(info);
  62.       if(primero == NULL || vacia())
  63.       {
  64.         primero = aux;
  65.         ultimo = primero;
  66.       }
  67.       else
  68.       {    
  69.         aux->next = primero;
  70.         primero->prev = aux;
  71.         primero = aux; // primero = primero->prev;
  72.       }
  73.       size = size + 1;
  74.       return 1;
  75.     }
  76.     int insertar_ultimo(T info)
  77.     {
  78.       nodo<T>* aux;
  79.       aux = crear_nodo(info);
  80.       if(ultimo == NULL || primero == NULL || vacia())
  81.         insertar_primero(info);
  82.       else
  83.       {    
  84.         aux->prev = ultimo;
  85.         ultimo->next = aux;
  86.         ultimo = aux; // ultimo = ultimo->next;
  87.         size = size + 1;
  88.       }
  89.       return 1;
  90.     }
  91.     int insertar_ordenado(T info)
  92.     {
  93.       if(vacia())
  94.       {
  95.         insertar_primero(info);
  96.       }
  97.       else{
  98.         nodo<T>* aux, *nuevo;
  99.         //int pos = 1;
  100.         aux = primero;
  101.         nuevo = crear_nodo(info);
  102.         if(info < primero->info){
  103.           insertar_primero(info);
  104.         }else{
  105.           while((aux->next != NULL) && (info > (aux->next)->info))
  106.           {
  107.             //printf("%d > %d\n", info, (aux->next)->info);
  108.             aux = aux->next;
  109.             //pos = pos + 1;
  110.           }
  111.           //insertar_pos(pos, info);
  112.           if(aux == primero){
  113.             insertar_primero(info);
  114.             //printf("INSERTE PRIMERO\n");
  115.           }
  116.           else{
  117.             if(aux == ultimo){
  118.               insertar_ultimo(info);
  119.               //printf("INSERTE ULTIMO\n");
  120.             }
  121.             else{
  122.               nuevo->next = aux->next;
  123.               nuevo->prev = aux;
  124.               (aux->next)->prev = nuevo;
  125.               aux->next = nuevo;
  126.               this->size = this->size + 1;
  127.               //printf("INSERTE_ORDENADO\n");
  128.             }  
  129.           }
  130.         }
  131.       }
  132.       return 1;
  133.     }
  134.     int insertar_pos(int pos, T info)
  135.     {
  136.       if(primero == NULL)
  137.         insertar_primero(info);
  138.       else{
  139.         if(pos-1 == getsize())
  140.           insertar_ultimo(info);  
  141.         else{
  142.           nodo<T>* aux, *temp;
  143.           aux = crear_nodo(info);
  144.           temp = primero;
  145.           for(int i = 1; i <= pos-1; i++)
  146.           {
  147.             temp = temp->next;
  148.           }
  149.           (temp->prev)->next = aux;
  150.           temp->prev = aux;
  151.           aux->prev = temp->prev;
  152.           aux->next = temp;
  153.           temp->prev = aux;
  154.           this->size = this->size + 1;
  155.         }
  156.       }
  157.       return 1;
  158.     }
  159.     int insertar(int pos, T info)
  160.     {
  161.       if(pos == 1)
  162.         insertar_primero(info);
  163.       else{
  164.         if(pos == this->size)
  165.           insertar_ultimo(info);
  166.         else
  167.           insertar_pos(pos, info);
  168.       }
  169.       return 1;
  170.     }
  171.     int eliminar_primero()
  172.     {
  173.       nodo<T>* aux;
  174.       aux = primero;
  175.       primero = aux->next;
  176.       if(primero != NULL)
  177.         primero->prev = NULL;
  178.       delete aux;
  179.       this->size = this->size - 1;
  180.       return 1;
  181.     }
  182.     int eliminar_ultimo()
  183.     {
  184.       nodo<T>* aux;
  185.       aux = ultimo;
  186.       ultimo = aux->prev;
  187.       if(ultimo != NULL)
  188.         ultimo->next = NULL;
  189.       delete aux;
  190.       this->size = this->size - 1;
  191.       return 1;
  192.     }
  193.     int eliminar(int pos)
  194.     {
  195.       if(pos == 1)
  196.         eliminar_primero();
  197.       else{
  198.         if(pos == getsize())
  199.           eliminar_ultimo();
  200.         else{
  201.           nodo<T>* aux;
  202.           aux = primero;
  203.           for(int i = 1; i < pos; ++i){
  204.             aux = aux->next;
  205.           }
  206.           (aux->prev)->next = aux->next;
  207.           (aux->next)->prev = aux->prev;
  208.           delete aux;
  209.           this->size = this->size - 1;
  210.         }
  211.       }
  212.       return 1;
  213.     }
  214.     T extraer_primero()
  215.     {
  216.       //printf("Extraer_primero\n");
  217.       //system("pause");
  218.       T aux;
  219.       aux = primero->info;
  220.       eliminar_primero();
  221.       //printf("Extraje %d\n", aux);
  222.       return aux;
  223.     }
  224.     T extraer_ultimo()
  225.     {
  226.       T aux;
  227.       aux = ultimo->info;
  228.       eliminar_ultimo();
  229.       //printf("Extraje %d\n", aux);
  230.       return aux;
  231.     }
  232.     T extraer(int pos)
  233.     {
  234.       T aux;
  235.       if(pos == 1)
  236.         aux = extraer_primero();
  237.       else{
  238.         if(pos == getsize())
  239.           aux = extraer_ultimo();
  240.         else{
  241.           nodo<T>* nodoaux;
  242.           nodoaux = primero;
  243.           for(int i = 1; i < pos; i++)
  244.           {
  245.             nodoaux = nodoaux->next;
  246.           }
  247.           aux = nodoaux->info;
  248.           eliminar(pos);
  249.         }
  250.       }
  251.       //printf("Extraje %d\n", aux);
  252.       return aux;
  253.     }
  254.     int ordenar()
  255.     {
  256.       T aux;
  257.       lista<T> l;
  258.       while(!vacia())
  259.       {
  260.         aux = extraer_primero();
  261.         l.insertar_ordenado(aux);
  262.       }
  263.       while(!l.vacia())
  264.       {
  265.         aux = l.extraer_primero();
  266.         insertar_ordenado(aux);
  267.       }
  268.       return 1;
  269.     }
  270.     int vaciar()
  271.     {
  272.       while(!vacia()){
  273.         eliminar_primero();
  274.       }
  275.       return 1;
  276.     }
  277.     T mostrar(int pos)
  278.     {
  279.       nodo<T>* aux;
  280.       aux = primero;
  281.       if(pos == 1)
  282.       {
  283.         //cout << "Mostrar " << (aux->info).getnombre() << "\n";
  284.         return aux->info;
  285.       }
  286.       for(int i = 1; i <= pos && !(vacia()) && aux->next != NULL; ++i)
  287.       {
  288.         aux = aux->next;
  289.       }
  290.       //cout << "Mostrar " << (aux->info).getnombre() << "\n";
  291.       return aux->info;
  292.     }
  293.     T* mostrarp(int pos)
  294.     {
  295.       nodo<T>* aux;
  296.       aux = primero;
  297.       T* pT;
  298.       if(pos == 1)
  299.       {
  300.         cout << "Mostrar 1 " << (aux->info).getnombre() << "\n";
  301.         pT = &(aux->info);
  302.         return pT;
  303.       }
  304.       for(int i = 1; i <= pos && !(vacia()) && aux->next != NULL; ++i)
  305.       {
  306.         aux = aux->next;
  307.       }
  308.       cout << "Mostrar 2 " << (aux->info).getnombre() << "\n";
  309.      
  310.       pT = &(aux->info);
  311.       return pT;
  312.     }
  313.     T* buscar(T info)
  314.     {
  315.       nodo<T>* aux;
  316.       aux = primero;
  317.       for(int i = 1; i <= getsize() && !(vacia()) && aux != NULL; ++i)
  318.       {
  319.         if(aux->info == info)
  320.         {
  321.           T* pT;
  322.           pT = &(aux->info);
  323.           return pT;
  324.         }
  325.         aux = aux->next;
  326.       }
  327.       return NULL;
  328.     }
  329.     bool pertenece(T info)
  330.     {
  331.       T* aux;
  332.       return (aux = this->buscar(info));
  333.     }
  334.     int getsize()
  335.     {
  336.       //int tamano = this->size;
  337.       //return tamano;
  338.       return size;
  339.     }
  340. };
  341.  string Borrar_substr(string &str, string del)
  342. //Borra todas las ocurrencias del string del que se encuentren dentro del string str
  343. {
  344.   while(str.find(del) != string::npos)
  345.   {
  346.     str.erase(str.begin() + str.find(del));
  347.   }
  348.   return str;
  349. }
  350.  
  351. int Buscar_substr(string &str, string find, int start)
  352. //Busca la poscicion de la ultima cadena consecutiva
  353. {
  354.   int posfind;
  355.   posfind = str.find(find, start);
  356.   while((posfind != string::npos) && (posfind + 1 == str.find(find, posfind + 1)))
  357.   {
  358.     posfind = str.find(find, posfind + 1);
  359.   }
  360.   return posfind;
  361. }
  362.  
  363. string Picar_substr(string origen, string &str , int &start, int &end, string principio, string fin)
  364. //Crea en str un substr de origen segun ciertos criterios
  365. {
  366.     start = Buscar_substr(origen, principio, end);
  367.     end = origen.find(fin, start + 1);
  368.     if(fin != "final")
  369.       str = origen.substr(start+1, end - start - 1);
  370.     else
  371.       str = origen.substr(start+1);
  372.     cout << str << "|\n";
  373.     Borrar_substr(str, " ");
  374.     cout << str << "|\n";
  375.     return str;
  376. }
  377. template <class T>
  378. bool Existe_T(T lista, string nombre)
  379. //Retorna true si existe el elemento con el nombre nombre en la lista
  380. {
  381.   bool existe;
  382.   existe = false;
  383.   if(lista.vacia())
  384.   {
  385.     //cout << "NO Existe\n";
  386.     return false;
  387.   }
  388.   for(int i = 1; i <= lista.getsize(); ++i)
  389.   {
  390.     //cout << "Entre\n";
  391.     if(nombre == (lista.mostrar(i)).getnombre())
  392.     {
  393.       existe = true;
  394.       //cout << "Existe1\n";
  395.       return existe;
  396.       break;
  397.     }
  398.   }
  399.   //cout << "Existe1.1\n";
  400.   return existe;
  401. }
  402. template <class T>
  403. int Buscar_T_pos(T lista, string nombre)
  404. //Busca el elemento con el nombre nombre en la lista
  405. {
  406.   if(lista.vacia())
  407.   {
  408.     return 0;
  409.   }
  410.   for(int i = 1; i <= lista.getsize(); ++i)
  411.   {
  412.     if(nombre == (lista.mostrar(i)).getnombre())
  413.     {
  414.       return i;
  415.     }
  416.   }
  417.   return -1;
  418. }
  419. int Error(int codigo, string &mensaje)
  420. {
  421.   /*
  422.   -Codigos de Error:
  423.     0: Comando inválido
  424.     1: La contextura no existe
  425.     2: El Luchador ya existe
  426.     3: El Luchador no existe
  427.     4: El Jugador ya existe
  428.     5: EL nivel de habilidad es invalido
  429.     6: El Jugador no existe
  430.     7: Imposible realizar combate: El Jugador <nombrejugador> no tiene luchadores
  431.     8: El número de jugadores tiene que ser 2
  432.     9: El número de jugadores tiene que ser 4
  433.     10: El número minimo de jugadores tiene que ser 3
  434.   */
  435.   switch (codigo)
  436.   {
  437.     case 0:
  438.       cout << "Comando invalido: " << mensaje << "\n";
  439.       return 0;
  440.     case 1:
  441.       cout << "La contextura " << mensaje << "no existe\n";
  442.       return 1;
  443.     case 2:
  444.       cout << "El Luchador " << mensaje << " ya existe\n";
  445.       return 2;
  446.     case 3:
  447.       cout << "El Luchador " << mensaje << " no existe\n";
  448.       return 3;
  449.     case 4:
  450.       cout << "El Jugador " << mensaje << " ya existe\n";
  451.       return 4;
  452.     case 5:
  453.       cout << "EL nivel de habilidad " << mensaje << " es invalido\n";
  454.       return 5;
  455.     case 6:
  456.       cout << "El Jugador " << mensaje << " no existe\n";
  457.       return 6;
  458.     case 7:
  459.       cout << "Imposible realizar combate: El Jugador " << mensaje << " no tiene luchadores\n";
  460.       return 7;
  461.     case 8:
  462.       //Podría juntar el Error 8 y 9 en uno génerico:
  463.       //cout << "El número de jugadores tiene que ser << mensaje <<"\n";
  464.       //return 8; Con mensaje = al numero minimo de jugadores requeridos.
  465.       //Pero no quise c: (Dime que opinas).
  466.       cout << "El número de jugadores tiene que ser 2\n";
  467.       return 8;
  468.     case 9:
  469.       cout << "El número de jugadores tiene que ser 4\n";
  470.       return 9;
  471.     case 10:
  472.       cout << "El número minimo de jugadores tiene que ser 3\n";
  473.       return 10;
  474.     default:
  475.       cout << "Comando invalido: " << mensaje << "\n";
  476.       return -1;
  477.   }
  478. }
  479. template <class T>
  480. int Picar_sublista(T lista, string &participantes, string &comando)
  481.   {
  482.     /*
  483.     if(this->buscar(jugador)){
  484.         //El Jugador no existe
  485.         Error(0, comando);
  486.     }else{
  487.     */
  488.         if(participantes.find("[") != 0 || participantes.find("]") != participantes.size() - 1
  489.         || ((participantes.find(",") + 1 == participantes.find(",", participantes.find(",") + 1)
  490.         || participantes.find(",") < participantes.find("[") || participantes.find(",") > participantes.find("]"))
  491.         && participantes.find(",")!= string::npos && participantes.find(",", participantes.find(",") + 1) != string::npos))
  492.       //Comando invalido
  493.         {
  494.           Error(0, comando);
  495.         }
  496.         else
  497.         //Comando valido
  498.         {
  499.           cout << "Voy a picar\n";
  500.           int start, end;
  501.           start = 0;
  502.           end = 0;
  503.           while(participantes.find(",",end+1) != string::npos || participantes.find("]",end+1) != string::npos)
  504.           {
  505.             string jugadoraux;
  506.             start = end;
  507.             if(start == 0)
  508.             {
  509.               if(participantes.find(",",start) != string::npos)
  510.               //Caso mas de un luchador
  511.               {
  512.                 Picar_substr(participantes, jugadoraux, start, end, "[", ",");
  513.               }
  514.               else
  515.               //Caso un luchador
  516.               {
  517.                 Picar_substr(participantes, jugadoraux, start, end, "[", "]");
  518.               }
  519.             }
  520.             else
  521.             {
  522.               if(participantes.find(",",end+1) != string::npos)
  523.               //Caso mas de un luchador
  524.               {
  525.                 Picar_substr(participantes, jugadoraux, start, end, ",", ",");
  526.               }
  527.               else
  528.               //Caso ultimo luchador
  529.               {
  530.                 Picar_substr(participantes, jugadoraux, start, end, ",", "]");
  531.               }
  532.             }
  533.             if(J.buscar(jugadoraux))
  534.             //Apila la direccion de memoria del luchador
  535.             {
  536.               Jugador *aux;
  537.               cout << "Jugador a buscar: " << jugadoraux << "\n";
  538.               aux = J.buscar(jugadoraux);
  539.               if((aux->pila.vacia()))
  540.               //Si la pila del jugador esta vacia muestra un error
  541.               {
  542.                 Error(7, jugadoraux);
  543.                 return 7;
  544.               }
  545.               else
  546.               //Jufador con luchadores
  547.               {          
  548.                 cout << "Insertare: " << jugadoraux << " en la lista de particpantes\n";
  549.                 lista.insertar_primero(aux);
  550.                 cout << "Inserte: " << jugadoraux << " en la lista de particpantes\n";
  551.                 //cout << "pop: " << (luchadores.pop())->getnombre() << "\n";
  552.                 //luchadores.push(pluchador);
  553.               }
  554.             }
  555.             else
  556.             //Luchador no existe
  557.             {
  558.               Error(6, jugadoraux);
  559.               return 6;
  560.             }      
  561.           }
  562.         }  
  563.     //}
  564.   }
  565.  /*
  566. struct ventaja{// Estructura de los movimientos
  567.     string contex = "";
  568.     ventaja *next = NULL;
  569. };
  570.  
  571. class lisven{
  572. public:
  573. ventaja * inicio = NULL;
  574. lisven(){ inicio = NULL;}
  575.  
  576.  
  577.     void insertar(string c){
  578.     if(c == "-1"){
  579.         printf(" contextura invalida");
  580.     }
  581.         if(inicio = NULL){
  582.             inicio = new ventaja;
  583.         }else{
  584.             ventaja * actual = inicio;
  585.             while(actual->next!=NULL && actual->contex != c){//||
  586.                 actual = actual->next;
  587.             }
  588.             if(actual->contex == c){
  589.                 free(actual);
  590.             }else{
  591.                 actual->next = new ventaja;
  592.                 actual->next->contex = c;
  593.             }
  594.         }
  595.     }
  596.  
  597.     ventaja * ret(){
  598.         return inicio;
  599.     }  
  600. };
  601. */
  602. struct Movimiento{// Estructura de los movimientos
  603.     string combo;
  604.     int danno;
  605.     lista<string*> ventajas;
  606.     Movimiento * next = NULL;
  607. };
  608.  
  609.  
  610.  
  611. class lismovimientos{//Lista de los movimientos con sus respectivos metodos
  612. public:
  613.     Movimiento * inicio = NULL;
  614.  
  615.  
  616.     void sobreescribir(Movimiento * a, int d, string &v, string &comando){
  617.         a->danno = d;
  618.         aplicarventajas(a, v, comando);
  619.     }
  620.     void aplicarventajas(Movimiento *&pmove, string &ventajas2, string &comando){
  621.         if( ventajas2.find("[") != 0 || ventajas2.find("]") != ventajas2.size() - 1
  622.         || ((ventajas2.find(",") + 1 == ventajas2.find(",", ventajas2.find(",") + 1)  
  623.         || ventajas2.find(",") < ventajas2.find("[") || ventajas2.find(",") > ventajas2.find("]"))
  624.         && ventajas2.find(",") != string::npos && ventajas2.find(",", ventajas2.find(",") + 1) != string::npos)
  625.         )
  626.       //Comando invalido
  627.       {
  628.         printf(" pos ']': %d size: %d", ventajas2.find("]"), ventajas2.size());
  629.         Error(0, comando);
  630.       }
  631.       else
  632.       //Comando valido
  633.       {
  634.         int start, end;
  635.         start = 0;
  636.         end = 0;
  637.         while(ventajas2.find(",",end+1) != string::npos || ventajas2.find("]",end+1) != string::npos)
  638.         {
  639.           string contextura;
  640.           start = end;
  641.           if(start == 0)
  642.           {
  643.             if(ventajas2.find(",",start) != string::npos)
  644.             //Caso mas de una ventaja
  645.             {
  646.               Picar_substr(ventajas2, contextura, start, end, "[", ",");
  647.             }
  648.             else
  649.             //Caso una ventaja o ninguna
  650.             {
  651.               Picar_substr(ventajas2, contextura, start, end, "[", "]");
  652.             }
  653.           }
  654.           else
  655.           {
  656.             if(ventajas2.find(",",end+1) != string::npos)
  657.             //Caso mas de una ventaja
  658.             {
  659.               Picar_substr(ventajas2, contextura, start, end, ",", ",");
  660.             }
  661.             else
  662.             //Caso ultima ventaja
  663.             {
  664.               Picar_substr(ventajas2, contextura, start, end, ",", "]");
  665.             }
  666.           }
  667.           if(contexturas.pertenece(contextura))
  668.           //Almacena la direccion de memoria de la contextura
  669.           {
  670.             string* pcontextura;
  671.             pcontextura = contexturas.buscar(contextura);
  672.             pmove->ventajas.insertar_ultimo(pcontextura);
  673.           }
  674.           else{
  675.             if(ventajas2.find("]") - ventajas2.find("[") == 1)
  676.             //Sin ventajas
  677.             {
  678.               pmove->ventajas.vaciar();
  679.               continue;
  680.             }
  681.             else
  682.             //Contextura invalida
  683.             {
  684.               Error(1, contextura);
  685.             }
  686.           }      
  687.         }
  688.       }
  689.     }
  690.     void insertar(string c, int d, string &v, string &comando){
  691.         if(inicio == NULL){
  692.             inicio = new Movimiento();
  693.             inicio->combo = c;
  694.             inicio->danno = d;
  695.             this->aplicarventajas(inicio, v, comando);
  696.         }else{
  697.             Movimiento * actual = inicio;
  698.             while(actual->next!=NULL && actual->combo != c){//||
  699.                 actual = actual->next;
  700.             }
  701.             if(actual->combo == c){
  702.                 sobreescribir(actual, d, v, comando);
  703.             }else{
  704.                 actual->next = new Movimiento;
  705.                 (actual->next)->combo = c;
  706.                 (actual->next)->danno = d;
  707.                 this->aplicarventajas(actual->next, v, comando);
  708.             }
  709.         }
  710.     }
  711. };
  712.  
  713. struct Luchador{//Estructura de Luchador
  714.     string nombre;
  715.     string *contextura;
  716.     lismovimientos lsmovimientos;
  717.     Luchador * next = NULL;
  718. };
  719.  
  720. class listadeluchadores{// Lista de luchadores con sus respectivos metodos
  721. public:
  722.     bool estado = false;
  723.     Luchador * inicio = NULL;
  724.  
  725.     void insertar(string a, string b){
  726.         if(!contexturas.pertenece(b)){
  727.                 Error(1, b);
  728.                 return;
  729.         }
  730.         cout << "toy aqui\n";
  731.         if(!estado){
  732.             cout << "entre a inicio papu\n";
  733.             inicio = new Luchador;
  734.             inicio->nombre = a;
  735.             inicio->contextura = contexturas.buscar(b);
  736.             estado = true;
  737.         }else{
  738.             cout << inicio->nombre <<" " <<inicio->contextura<<" inicio \n";
  739.             cout << "entre al peo papu\n";
  740.             Luchador * actual = inicio;
  741.             cout << actual->nombre <<" " <<actual->contextura<<"\n";
  742.             while(actual->next != NULL){
  743.                 cout << actual->nombre <<" " <<actual->contextura<<"\n";
  744.                 actual = actual->next;
  745.             }
  746.             cout << actual->nombre <<" " <<actual->contextura<<"\n";
  747.             actual->next=new Luchador;
  748.             (actual->next)->nombre = a;
  749.             (actual->next)->contextura = contexturas.buscar(b);
  750.         }
  751.     }
  752.  
  753.     Luchador * buscar(string busqueda){
  754.         if(inicio == NULL){
  755.             return NULL;
  756.         }else{
  757.             Luchador * actual = inicio;
  758.             while(actual->next != NULL){
  759.                 if(actual->nombre == busqueda){
  760.                     return actual;
  761.                 }
  762.                 actual = actual->next;
  763.             }
  764.             if(actual->nombre == busqueda){
  765.                 return actual;
  766.             }else{
  767.                 return NULL;
  768.             }
  769.         }
  770.     }
  771.  
  772.     void buscar(){
  773.         Luchador * actual = inicio;
  774.         while(actual->next != NULL){
  775.             actual = actual->next;
  776.             cout << actual->nombre <<" " <<actual->contextura<<"\n";
  777.         }
  778.     }
  779.  
  780. };
  781.  
  782. //lismovimientos M;
  783. listadeluchadores L;
  784.  
  785. struct nodopilaluchadores{//Estructura de apuntadores que apuntan a los odos de la lista de luchadores
  786.     Luchador * per;
  787.     nodopilaluchadores * next;
  788. };
  789.  
  790.  
  791. class piladeluchadores{//Pila de luchadores, donde los nodos que apuntan a los luchadores son los que son apilados
  792. public:
  793.     bool estado = false;
  794.     nodopilaluchadores * inicio = NULL;
  795.   bool vacia()
  796.   {
  797.     return (inicio == NULL)
  798.   }
  799.     void insertar(Luchador *luchador){
  800.         if(!estado){
  801.             inicio = new nodopilaluchadores;
  802.             inicio->per = luchador;
  803.         }else{
  804.             nodopilaluchadores * aux = inicio;
  805.             inicio = new nodopilaluchadores;
  806.             inicio->per = luchador;
  807.             inicio->next = aux;
  808.         }
  809.     }
  810.  
  811.     /*Luchador * retluch(){//Esta funcion retorna el tope de la pila (especificamente el nodo al que apunta el nodo que esta en el tope)
  812.         if(inicio == NULL){
  813.             return NULL;
  814.         }
  815.         return inicio->per;
  816.     }*/
  817.  
  818.     nodopilaluchadores * rettop(){//Esta funcion a diferencia de la otra, la cual esta comentada, retorna el nodo que esta en el tope de la pila de nodos apuntadores.
  819.         if(inicio == NULL){
  820.             return NULL;
  821.         }
  822.         return inicio;
  823.     }
  824.     void Desapilar(){
  825.         if(inicio->next == NULL){
  826.             inicio=NULL;
  827.             return;
  828.         }
  829.         inicio = inicio->next;
  830.     }
  831.  
  832.     void rein(){
  833.         while(inicio != NULL){
  834.             Desapilar();
  835.         }
  836.     }
  837.  
  838.     void enpilar(nodopilaluchadores * &a){
  839.       //Esta funcion utiliza las primitivas de la pila para hacer que
  840.       //la pila ~(sea pro, y tenga trastornos de identidad para que se comporte como nosotros queremos, mientras sigue siendo una pila c:)~ nunca este vacia
  841.         if(inicio == NULL){
  842.             inicio=a;
  843.             inicio->next = NULL;
  844.             return;
  845.         }
  846.         nodopilaluchadores * z = rettop();
  847.         Desapilar();
  848.         enpilar(a);
  849.         z->next = inicio;
  850.         inicio = z;
  851.     }
  852. };
  853.  
  854. class Jugador{
  855.   //Esto, es una clase, a diferencia de otros nodos que son estructuras ya que esto nos permite agilizar el calculo de daño e implmentar los diferentes metodos de los Jugadores.
  856. public:
  857.     string nombre;
  858.     int lvl;
  859.     piladeluchadores pila;
  860.     int acumulado;
  861.     int acumuladotot;
  862.     Jugador * next = NULL;
  863.  
  864.     void calcular(Luchador * ali, Luchador * riv){//Esta accion actualiza el daño que realiza el judador por combate
  865.         acumulado = 0;
  866.         bool r = true;
  867.         if(ali->lsmovimientos.inicio != NULL){
  868.             Movimiento * actual = ali->lsmovimientos.inicio;
  869.             ventaja * ven;
  870.             while(actual->next != NULL){
  871.                 ven = actual->lista.ret();
  872.                 while(r && ven->next !=NULL ){
  873.                     if(ven->contex == ""){//debes arreglar esto
  874.                         acumulado = acumulado + actual->danno + lvl;
  875.                         r = false;
  876.                     }else if(ven->contex != riv->contextura){//y esto
  877.                         acumulado = acumulado + (actual->danno/2) + lvl;
  878.                         r = false;
  879.                     }else if(ven->contex == riv->contextura){//y esto
  880.                         acumulado = acumulado + (actual->danno*2) + lvl;
  881.                         r = false;
  882.                     }
  883.                     actual = actual->next;
  884.                 }
  885.             }
  886.             if(ven->contex == "" && r){
  887.                 acumulado = acumulado + actual->danno + lvl;//y esto
  888.                 r = false;
  889.             }else if(ven->contex != riv->contextura && r){
  890.                 acumulado = acumulado + (actual->danno/2) + lvl;//y esto
  891.                 r = false;
  892.             }else if(ven->contex == riv->contextura && r){
  893.                 acumulado = acumulado + (actual->danno*2) + lvl;//y esto
  894.                 r = false;
  895.             }
  896.         }
  897.     }
  898.  
  899.     void acum(){//acumula los valores totales de daño para todos contra todos
  900.         acumuladotot = acumuladotot + acumulado;
  901.     }
  902.     void rein(){//reinicia los valores totales
  903.         acumuladotot = 0;
  904.     }
  905. };
  906.  
  907. class listadejugadores{
  908. public:
  909.     Jugador * inicio = NULL;
  910.  
  911.     bool vacio(){
  912.         return inicio==NULL;
  913.     }
  914.  
  915.     void insertar(string n, string& habilidad){//crea e insterta nodos a la lista de jugadores
  916.       int l;
  917.       l = atoi(habilidad.c_str());
  918.       if(l >= 0 && l <= 100)
  919.       //Habilidad permitida
  920.       {
  921.         if(inicio == NULL){
  922.             inicio = new Jugador;
  923.             inicio->nombre = n;
  924.             inicio->lvl = l;
  925.             inicio->acumuladotot = 0;
  926.             inicio->acumulado = 0;
  927.         }else{
  928.             Jugador * actual = inicio;
  929.             while(actual->next != NULL){
  930.                 actual = actual->next;
  931.             }
  932.             actual->next = new Jugador;
  933.             (actual->next)->nombre = n;
  934.             (actual->next)->lvl = l;
  935.             (actual->next)->acumuladotot = 0;
  936.             (actual->next)->acumulado = 0
  937.         }
  938.       }else{
  939.         //Muy OP no vale
  940.         Error(5, habilidad);
  941.       }
  942.     }
  943.  
  944.  
  945.     Jugador * buscar(string busqueda){
  946.         if(inicio == NULL){
  947.             return NULL;
  948.         }else{
  949.             Jugador * actual = inicio;
  950.             while(actual->next != NULL){
  951.                 if(actual->nombre == busqueda){
  952.                     return actual;
  953.                 }
  954.                 actual = actual->next;
  955.             }
  956.             if(actual->nombre == busqueda){
  957.                 return actual;
  958.             }else{
  959.                 return NULL;
  960.             }
  961.      
  962.        }
  963.     }
  964.  
  965.  
  966.     void imprimir(){
  967.         Jugador * actual = inicio;
  968.         cout << actual->nombre <<" " <<actual->lvl<<"\n";
  969.         while(actual->next != NULL){
  970.             cout << actual->nombre <<" " <<actual->lvl<<"\n";
  971.             actual = actual->next;
  972.         }
  973.     }
  974.  
  975.     void asignar(string &jugador, string &luchadores2, string &comando){
  976.       if(this->buscar(jugador)){
  977.         //El Jugador no existe
  978.         Error(6, jugador);
  979.       }else{
  980.         if(luchadores2.find("[") != 0 || luchadores2.find("]") != luchadores2.size() - 1
  981.         || ((luchadores2.find(",") + 1 == luchadores2.find(",", luchadores2.find(",") + 1)
  982.         || luchadores2.find(",") < luchadores2.find("[") || luchadores2.find(",") > luchadores2.find("]"))
  983.         && luchadores2.find(",")!= string::npos && luchadores2.find(",", luchadores2.find(",") + 1) != string::npos))
  984.       //Comando invalido
  985.       {
  986.         Error(0, comando);
  987.       }
  988.         else
  989.         //Comando valido
  990.         {
  991.           cout << "Voy a asignar\n";
  992.           int start, end;
  993.           start = 0;
  994.           end = 0;
  995.           while(luchadores2.find(",",end+1) != string::npos || luchadores2.find("]",end+1) != string::npos)
  996.           {
  997.             string luchadoraux;
  998.             start = end;
  999.             if(start == 0)
  1000.             {
  1001.               if(luchadores2.find(",",start) != string::npos)
  1002.               //Caso mas de un luchador
  1003.               {
  1004.                 Picar_substr(luchadores2, luchadoraux, start, end, "[", ",");
  1005.               }
  1006.               else
  1007.               //Caso un luchador
  1008.               {
  1009.                 Picar_substr(luchadores2, luchadoraux, start, end, "[", "]");
  1010.               }
  1011.             }
  1012.             else
  1013.             {
  1014.               if(luchadores2.find(",",end+1) != string::npos)
  1015.               //Caso mas de un luchador
  1016.               {
  1017.                 Picar_substr(luchadores2, luchadoraux, start, end, ",", ",");
  1018.               }
  1019.               else
  1020.               //Caso ultimo luchador
  1021.               {
  1022.                 Picar_substr(luchadores2, luchadoraux, start, end, ",", "]");
  1023.               }
  1024.             }
  1025.             if(L.buscar(luchadoraux))
  1026.             //Apila la direccion de memoria del luchador
  1027.             {
  1028.               Jugador *aux;
  1029.               aux = J.buscar(jugador);
  1030.               Luchador* pluchador;
  1031.               pluchador = L.buscar(luchadoraux);
  1032.               cout << "Luchador a buscar: " << luchadoraux << "\n";
  1033.               cout << "Pusheare: " << pluchador->nombre << " en la pila del jugador: " << jugador << "\n";
  1034.               aux->pila.insertar(pluchador);
  1035.               cout << "Pushee: " << pluchador->nombre << " en la pila del jugador: " << jugador << "\n";
  1036.               //cout << "pop: " << (luchadores.pop())->getnombre() << "\n";
  1037.               //luchadores.push(pluchador);
  1038.             }
  1039.             else
  1040.             //Luchador no existe
  1041.             {
  1042.               Error(3, luchadoraux);
  1043.             }      
  1044.           }
  1045.         }  
  1046.       }
  1047.     }
  1048. };
  1049.  
  1050.  
  1051.  
  1052. listadejugadores J;
  1053.  
  1054. void print1v1(string a, string b){
  1055.     Jugador * J1 = J.buscar(a);
  1056.     Jugador * J2 = J.buscar(b);
  1057.     if(J1->acumulado > J2->acumulado){
  1058.         printf("El ganador es ");
  1059.         cout << J1->nombre;
  1060.         printf(" realizando %d de daño\n", J1->acumulado);
  1061.     }else{
  1062.         printf("El ganador es ");
  1063.         cout << J2->nombre;
  1064.         printf(" realizando %d de daño\n", J2->acumulado);
  1065.     }
  1066.     free(J1);
  1067.     free(J2);
  1068. }
  1069.  
  1070. void comb1v1(lista<Jugador*> Particpantes, comando){
  1071.     nodopilaluchadores * aux = J1->pila.rettop();
  1072.     Luchador * Luch1 = aux->per;
  1073.     J1->pila.enpilar(aux);
  1074.     free(aux);
  1075.     aux = J2->pila.rettop();
  1076.     Luchador * Luch2 = aux->per;
  1077.     J2->pila.enpilar(aux);
  1078.     free(aux);
  1079.     J1->calcular(Luch1, Luch2);
  1080.     J2->calcular(Luch2, Luch1);
  1081.     free(J1);
  1082.     free(J2);
  1083. }
  1084.  
  1085. void comb2v2(lista<Jugador*> Particpantes, comando){
  1086.     Jugador * J1 = J.buscar(a);
  1087.     Jugador * J2 = J.buscar(b);
  1088.     Jugador * J3 = J.buscar(c);
  1089.     Jugador * J4 = J.buscar(d);
  1090.     comb1v1(J1, J3);
  1091.     comb1v1(J2, J4);
  1092.     if(J1->acumulado + J2->acumulado > J3->acumulado + J4->acumulado){
  1093.         printf("Los ganadores son ");
  1094.         cout << J1->nombre;
  1095.         printf(" realizando %d de daño y ", J1->acumulado);
  1096.         cout << J2->nombre;
  1097.         printf(" realizando %d de daño\n", J2->acumulado);
  1098.     }else{
  1099.         printf("Los ganadores son ");
  1100.         cout << J3->nombre;
  1101.         printf(" realizando %d de daño", J3->acumulado);
  1102.         cout << J4->nombre;
  1103.         printf(" realizando %d de daño\n", J4->acumulado);
  1104.     }
  1105. }
  1106.  
  1107. void reintot(){
  1108.     Jugador * actual = J.inicio;
  1109.     while(actual->next != NULL){
  1110.         actual->rein();
  1111.     }
  1112.     actual->rein();
  1113. }
  1114.  
  1115. void tvt(lista<Jugador*> Particpantes, comando){
  1116.     Jugador * actual = J.inicio;
  1117.     while(actual->next != NULL){
  1118.         Jugador * rival = actual->next;
  1119.         while(rival->next != NULL){
  1120.             comb1v1(actual, rival);
  1121.             rival->next;
  1122.         }
  1123.     rival->next;
  1124.     actual->acum();
  1125.     }
  1126.     actual=J.inicio;
  1127.     Jugador * primero = new Jugador;
  1128.     Jugador * segundo = new Jugador;
  1129.     Jugador * tercero = new Jugador;
  1130.     while(actual->next !=NULL){
  1131.         if(actual->acumuladotot > primero->acumuladotot && actual->acumuladotot > segundo->acumuladotot && actual->acumuladotot > tercero->acumuladotot){
  1132.         tercero = segundo;
  1133.         segundo = primero;
  1134.         free(primero);
  1135.         primero = actual;
  1136.         }else if(actual->acumuladotot < primero->acumuladotot && actual->acumuladotot > segundo->acumuladotot && actual->acumuladotot > tercero->acumuladotot){
  1137.             tercero = segundo;
  1138.             free(segundo);
  1139.             segundo = actual;
  1140.         }else if(actual->acumuladotot < primero->acumuladotot && actual->acumuladotot < segundo->acumuladotot && actual->acumuladotot > tercero->acumuladotot){
  1141.             free(tercero);
  1142.             tercero = actual;
  1143.         }
  1144.     }
  1145.     printf("El ganador en 1er puesto es ");
  1146.     cout << primero->nombre;
  1147.     printf(" realizando %d de daño, 2do puesto es ", primero->acumuladotot);
  1148.     cout << segundo->nombre;
  1149.     printf(" realizando %d de daño y 3er Puesto es ", segundo->acumuladotot);
  1150.     cout << tercero->nombre;
  1151.     printf(" %d de daño\n", tercero->acumuladotot);
  1152.     reintot();
  1153. }
  1154. void Combate(string &modalidad, lista<Jugador*> LParticipantes, string &comando)
  1155. {
  1156.   if(modalidad == "1vs1"){
  1157.       if(Particpantes.getsize() != 2)
  1158.     //Numero de Participantes invalido
  1159.     {
  1160.       Error(8, comando);
  1161.     }
  1162.     else
  1163.     {
  1164.       comb1v1(LParticipantes, comando);
  1165.       print1v1(J1, J2);
  1166.     }  
  1167.   }
  1168.   else if(modalidad == "2vs2"){
  1169.     if(Particpantes.getsize() != 4)
  1170.     //Numero de Participantes invalido
  1171.     {
  1172.       Error(9, comando);
  1173.     }
  1174.     else
  1175.     {
  1176.       comb2v2(LParticipantes);
  1177.     }    
  1178.   }
  1179.   else if(modalidad == "Todos"){
  1180.     if(Particpantes.getsize() < 3)
  1181.     //Numero de Participantes invalido
  1182.     {
  1183.       Error(10, comando);
  1184.     }
  1185.     else{
  1186.       int cont = 2;
  1187.       Jugador * aux = J.inicio;
  1188.       while(aux->next != NULL && cont < 3){
  1189.         cont++;
  1190.       }
  1191.       if(cont >= 3){
  1192.         tvt();
  1193.       }
  1194.     }
  1195.   }
  1196. }
  1197. int Crear_contexturas(lista<string> &contexturas)
  1198. {
  1199.  
  1200.   contexturas.insertar_primero("ligero");
  1201.   contexturas.insertar_primero("equilibrado");
  1202.   contexturas.insertar_primero("pesado");
  1203.   //contexturas.imprimir();
  1204. }
  1205.  
  1206. lista<string> contexturas;
  1207.  
  1208. void Entrada()
  1209. {
  1210. ifstream Entrada;
  1211. Entrada.open("Entrada.in");//Aqui se abre el archivo
  1212. if (Entrada.fail()){
  1213.     Entrada.open("Entrada.in.txt");
  1214.     if (Entrada.fail()){
  1215.         Entrada.open("Entrada.txt");
  1216.         if (Entrada.fail()){
  1217.             printf("Entrada.in no fue encontrada \n");
  1218.             return -1;
  1219.         }
  1220.     }
  1221. }
  1222. string comando;
  1223. while(!Entrada.eof() && getline(Entrada,comando)){
  1224.   //Imprimir_Todo(lcontexturas, lluchadores, ljugadores);
  1225.   //system("pause");
  1226.     int posws, pos;
  1227.     posws = 0; //Poscicion white space
  1228.     pos = 0;
  1229.     posws = comando.find(" ", posws); //Busca el primer " "
  1230.     //if (posws!=string::npos)
  1231.       cout << comando << "\n";
  1232.     // " - Tamanno: " << prueba.size() << " - Espacio: " << posws << "\n";
  1233.     pos = posws;
  1234.     string instruccion;
  1235.     instruccion = comando.substr(0, posws);
  1236.     cout << instruccion << "|\n";
  1237.  
  1238.  
  1239.     if(instruccion == "Luchador"){
  1240.      
  1241.       string nombre;
  1242.       Picar_substr(comando, nombre, pos, posws, " ", " ");
  1243.       if(L.buscar(nombre))
  1244.       //El Luchador ya existe
  1245.       {
  1246.         Error(2, nombre);
  1247.       }
  1248.       else
  1249.       //Crear Luchador
  1250.       {  
  1251.         string contextura;
  1252.         Picar_substr(comando, contextura, pos, posws, " ", "final");
  1253.         L.insertar(nombre, contextura);
  1254.         cout << "Mi menusito sirve\n";
  1255.       }
  1256.     }else if(instruccion == "Movimiento"){
  1257.         /*string per = "";
  1258.         int danno = 0;
  1259.         string aux = "", x = " ";
  1260.         line >> per;
  1261.         L.buscar();
  1262.         line >> aux >> danno >> per;
  1263.         for(int i = 0; i < per.size(); ++i){
  1264.           if(per[i] != ']' || per[i] != '[' || per[i] != ',' ){
  1265.             x += per[i];
  1266.           }
  1267.         }
  1268.         for(int i = 0; i < per.size(); i++){
  1269.             L.buscar(aux)->lista.insertar(v);
  1270.         }*/
  1271.       string luchador;
  1272.       Picar_substr(comando, luchador, pos, posws, " ", " ");
  1273.       if(L.buscar(luchador))
  1274.       //Asignar movimiento
  1275.       {
  1276.         Luchador *pluchador;
  1277.         pluchador = L.buscar(luchador);
  1278.         //cout << "Existe\n";
  1279.         string combo;
  1280.         Picar_substr(comando, combo, pos, posws, " ", " ");
  1281.         string damage;
  1282.         Picar_substr(comando, damage, pos, posws, " ", " ");
  1283.         string ventajas;
  1284.         Picar_substr(comando, ventajas, pos, posws, " ", "final");
  1285.         int damage2;
  1286.         damage2 = atoi(damage.c_str());
  1287.         pluchador->lsmovimientos.insertar(combo, damage2, ventajas, comando);
  1288.       }
  1289.       else
  1290.       //El Luchador no existe
  1291.       {
  1292.         Error(3, luchador);
  1293.         cout << "woa morir1\n";
  1294.       }
  1295.     }else if(instruccion == "Jugador"){
  1296.  
  1297.       string nombre;
  1298.       Picar_substr(instruccion, nombre, pos, posws, " ", " ");
  1299.       if(J.buscar(nombre))
  1300.       //El Jugador ya existe
  1301.       {
  1302.         Error(4, nombre);
  1303.       }
  1304.       else
  1305.       //Crear Jugador
  1306.       {  
  1307.         string habilidad;
  1308.         Picar_substr(instruccion, habilidad, pos, posws, " ", "final");
  1309.         int habilidad2;
  1310.         J.insertar(nombre, habilidad);
  1311.  
  1312.         cout << "woa morir 2\n";
  1313.         }
  1314.       }else if(instruccion == "Asignar"){
  1315.  
  1316.       string jugador;
  1317.       Picar_substr(instruccion, jugador, pos, posws, " ", " ");
  1318.       if(L.buscar(jugador))
  1319.       //Asignar luchadores
  1320.       {
  1321.         string luchadoresasignar;
  1322.         Picar_substr(instruccion, luchadoresasignar, pos, posws, " ", "final");
  1323.         J.asignar(jugador, luchadoresasignar, comando);
  1324.       }
  1325.       else
  1326.       //El Jugador no existe
  1327.       {
  1328.         Error(6, jugador);
  1329.       }
  1330.     }else if(instruccion == "Combate"){
  1331.       string modalidad;
  1332.       Picar_substr(comando, modalidad, pos, posws, " ", " ");
  1333.       string participantes;
  1334.       Picar_substr(comando, participantes, pos, posws, " ", "final");
  1335.  
  1336.       lista<Jugador*> LParticipantes;
  1337.       Picar_sublista(LParticipantes, participantes, comando);
  1338.       Combate(modalidad, LParticipantes, comando);
  1339.     }else{
  1340.       //Comando invalido
  1341.       Error(0, comando);
  1342.     }
  1343. }
  1344. Entrada.close();
  1345. }
  1346.  
  1347. int main (){
  1348.   Crear_contexturas(contexturas);
  1349.   Entrada();
  1350. return 0;
  1351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement