Guest User

pokemon.c

a guest
Sep 18th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include "Pokemon.h"
  5.  
  6.  
  7. /*Função responsável por zerar a bolsa pokemon*/
  8. void limpaBag(tPokeBag* p) {
  9.  
  10. }
  11.  
  12. /*Iniciar a bolsa pokemon*/
  13. void initBag(tPokeBag* p) {
  14. p->inicio = NULL;
  15. p->numElem = 0;
  16. }
  17.  
  18. /*Tamanho da bolsa*/
  19. int tamBag(tPokeBag p) {
  20.  
  21. return p.numElem;
  22. }
  23.  
  24. /*Função que imprime na tela os pokemon da bolsa pokemon*/
  25. void imprimeBag(tPokeBag p) {
  26. int count;
  27. if (p.inicio == NULL)
  28. printf("Bolsa de Pokemon vazia!!\n");
  29. else {
  30. printf("Imprimindo a Bolsa Pokemon\n");
  31. tPokemon* pos = p.inicio;
  32. while (pos != NULL) {
  33. printf("Pokemon %d|| CP %d || HP %d \n", pos->ID, pos->cp, pos->hp);
  34. pos = pos->prox;
  35. }
  36. }
  37. }
  38.  
  39. /*Função que insere os pokemon ordenadamente por numero, onde o primeiro de cada numero é o pokemon mais recente*/
  40. bool insereOrdenadoBag(tPokeBag* p, int valor) {
  41. tPokemon* novoPoke;
  42. tPokemon* posAtual;
  43. novoPoke = (tPokemon*) malloc (sizeof(tPokemon));
  44. if (novoPoke != NULL) {
  45. novoPoke->ID = valor;
  46. novoPoke->cp = rand () % 3000;
  47. novoPoke->hp = rand () % 500;
  48. if (p->inicio == NULL) {
  49. novoPoke->prox = NULL;
  50. p->inicio = novoPoke;
  51. }
  52. else if (valor < p->inicio->ID || valor == p->inicio->ID) {
  53. novoPoke->prox = p->inicio;
  54. p->inicio->ant = novoPoke;
  55. p->inicio = novoPoke;
  56. }
  57. else {
  58. posAtual = p->inicio;
  59. while (posAtual->prox != NULL && posAtual->prox->ID < valor)
  60. posAtual = posAtual->prox;
  61. novoPoke->prox = posAtual->prox;
  62. novoPoke->ant = posAtual;
  63. if (posAtual->prox != NULL)
  64. posAtual->prox->ant = novoPoke;
  65. posAtual->prox = novoPoke;
  66. }
  67. p->numElem++;
  68. printf("Pokemon adicionado corretamente na Bolsa\n");
  69. return true;
  70. }
  71. return false;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment