-
-
Save Wiguwbe/935e0fb3723c9100cdbf81ec44d1a299 to your computer and use it in GitHub Desktop.
elimination tournament, with linked lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <string.h> | |
struct equipa { | |
char *nome; | |
// TODO mais campos | |
}; | |
typedef struct equipa equipa_t; | |
struct lista { | |
equipa_t *equipa; | |
struct lista *next; | |
}; | |
#define L_ALLOC (struct lista*)malloc(sizeof(struct lista)) | |
#define NUM_EQUIPAS 8 | |
static int chosen = 0; | |
static equipa_t equipas[] = { | |
{"porto"}, | |
{"benfica"}, | |
{"sporting"}, | |
{"braga"}, | |
{"famalicao"}, | |
{"leiria"}, | |
{"vitoria"}, | |
{"santa clara"} | |
}; | |
struct lista * sorteio(void) | |
{ | |
// gera 8 items na lista com as equipas | |
struct lista *head = L_ALLOC; | |
struct lista *atual = head; | |
struct lista *bak; | |
if(head==NULL) { | |
fprintf(stderr, "memoria rebentada\n"); | |
exit(1); | |
} | |
srandom(time(NULL)); | |
int choice, bit, test; | |
for(int i=0; i < NUM_EQUIPAS; i++) { | |
while(1) { | |
choice = random() % 8; | |
bit = 1 << choice; | |
test = chosen ^ bit; | |
if(test > chosen) { | |
chosen = test; | |
break; | |
} | |
} | |
atual->equipa = equipas + choice; | |
atual->next = L_ALLOC; | |
if(atual->next == NULL) { | |
fprintf(stderr, "memoria rebentada\n"); | |
exit(1); | |
} | |
bak = atual; | |
atual = atual->next; | |
} | |
// remover ultima | |
free(atual); | |
bak->next = NULL; | |
return head; | |
} | |
static void _pl(struct lista *l) { | |
// for debug, print list | |
struct lista *p = l; | |
printf("---\n"); | |
while(p) { | |
printf("%p (%p), %s\n", p, p->next, p->equipa->nome); | |
p = p->next; | |
} | |
} | |
equipa_t * jogar_torneio(struct lista *torneio) | |
{ | |
struct lista *a, *b; | |
int golos_a, golos_b; | |
int ronda = 0; | |
// enquanto houver um `next`, a lista tem mais do que 1 equipa | |
while(torneio->next != NULL) { | |
//_pl(torneio); | |
// primeira equipa | |
a = torneio; | |
while(a != NULL) { | |
// equipa a seguir (par) | |
b = a->next; | |
do { | |
golos_a = random() % 4; | |
golos_b = random() % 4; | |
} while(golos_a == golos_b); | |
// imprime o jogo | |
for(int i=0;i<ronda;i++) { | |
putchar('\t'); | |
} | |
printf( | |
"%s %d - %d %s\n", | |
a->equipa->nome, | |
golos_a, | |
golos_b, | |
b->equipa->nome | |
); | |
// remove-se sempre o `b`, para facilitar a vida | |
// mantém-se o vencedor no `a` | |
if(golos_b > golos_a) { | |
// troca os lados | |
a->equipa = b->equipa; | |
} | |
a->next = b->next; | |
free(b); | |
// proxima iteracao, com o `b` já removido, o "salto" é só de 1 (next) | |
a = a->next; | |
} | |
ronda++; | |
} | |
return torneio->equipa; | |
} | |
int main() | |
{ | |
struct lista *torneio = sorteio(); | |
equipa_t *vencedora = jogar_torneio(torneio); | |
printf("vencedora: %s\n", vencedora->nome); | |
// a memoria é libertada no final do programa (automaticamente) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment