DEV Community

Leo
Leo

Posted on • Updated on

El Torneo Pokémon

El programa está comentado y se ejecuta en Español.

@imnotleo


The program is commented and runs in Spanish.

@imnotleo


Image description

Image description

Image description

Image description

Image description

Image description

main.cpp

/*
AUTOR:    Leobardo Vázquez 
CURSO:    Estructura de Datos
PROGRAMA: El Torneo Pokémon
FECHA:    12 de Mayo del 2024
*/
#include <iostream>
#include "heap.hpp"

int main() {

  srand((unsigned) time(nullptr));
  int n;
  cin>> n;

  heap k(n);
  heap o(n);

  for (int i = 0; i < n; i++){
    int x = rand() % (4*n) + 1;
    k.ins(x);
  }

  for (int i = 0; i < n; i++){
    int x = rand() % (4*n) + 1;
    o.ins(x);
  }

  cout << "Ketchum: ";
    k.print();
  cout << "Oak:     ";
    o.print();

  cout << "\n\nInicia el Torneo\n\n";
  int K = k.tout();
  int O = o.tout();

  while ( K != 0 && O != 0 ) {

    cout << "k:" << K << " Vs. o:" << O << " | ";

    if (K > O) {
      cout << "Ketchum wins";
      K -= O;
      if (!o.empty()) O = o.tout();
      else O = 0;
    } else if (O > K) {
      cout << "Oak wins";
      O -= K;
      if (!k.empty()) K = k.tout();
      else K = 0;
    } else if (K == O){
      cout << "Nobody wins";
      if (!k.empty() && !o.empty()) { 
        K = k.tout();
        O = o.tout();
      }else if (!o.empty()) {
          O = o.tout();
          K = 0;
      }else if (!k.empty()) {
          K = k.tout();
          O = 0;
      }else {
          K = 0;
          O = 0;
      }
    }
    cout << endl;
  }

  if ( K == 0 && O == 0 ) cout << "\n\nNo one has pokes left!\nNobody wins, it's a tie!";
  else if (K == 0) cout << "\n\nKetchum is out of pokes!\nOak wins the tournment!";
  else cout << "\n\nOak is out of pokes!\nKetchum wins the tournment!";

  return 0;
}

//https://dev.to/imnotleo/torneo-pokemon-1o3l
Enter fullscreen mode Exit fullscreen mode

heap.cpp


#include "heap.hpp"

  heap::heap(int c){

    n = c;
    s = 0;

    a = new int[n];
  }

  heap::~heap(){

    delete[] a;
  }

 void heap::ins(int x){

   assert(!full());

  a[s] = x;
  int i = s++;
   while (i > 0 && a[i] < a[parent(i)]) {
     int c = a[i];
     a[i] = a[parent(i)];
     a[parent(i)] = c;

     i = parent(i);
   }

 }

int heap::tout(){
  assert(!empty());

  int x = a[0];
  int i = 0;
  a[0] = a[--s];
  while (i <= parent(s-1) && a[i] > a[minChild(i)]){

    int m = minChild(i);
    int c = a[i];
    a[i] = a[m];
    a[m] = c;
    i = m;
  }

    return x;
}

int heap::minChild(int i) {

  assert(i <= parent(s-1));

  if (right(i) > s-1) return left(i);
  if (a[left(i)] < a[right(i)]) { return left(i); }
  else return right(i);

}

void heap::print(){

  cout << "[ ";
  for (int i = 0; i < s; i++) cout << a[i] <<" ";
  cout << "]\n";
}
//https://dev.to/imnotleo/torneo-pokemon-1o3l
Enter fullscreen mode Exit fullscreen mode

heap.hpp


#ifndef heap_hpp
#define heap_hpp

#include <stdio.h>
#include <iostream>
#include <cassert>
using namespace std;

class heap {

  int parent(int i) { return (i - 1) / 2; }
  int left(int i) { return 2 * i + 1; }
  int right(int i) { return 2 * i + 2; }

  int n; // capacidad
  int s; // tamaño

  int *a; // arreglo

public:

  heap(int);
  ~heap();

void ins(int);
int tout();
int minChild(int);

int capacity() const { return n; }
int size() const { return s; }

bool empty() const { return s == 0; }
bool full() const { return s == n; }

void print();
};

#endif /* stack_hpp */
//https://dev.to/imnotleo/torneo-pokemon-1o3l

Enter fullscreen mode Exit fullscreen mode

Top comments (0)