Salve ragazzi, qualcuno gentilmente mi aiuterebbe a risolvere questo segmentation fault, nel codice è spiegato tutto quello che il programma fa tramite commenti
Ma in poche parole devo creare una classifica per il gioco del tris e questo codice parte quando la partita termina (il progetto non prevede persistenza dei risultati)
Ma in poche parole devo creare una classifica per il gioco del tris e questo codice parte quando la partita termina (il progetto non prevede persistenza dei risultati)
C:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
typedef struct {
char* nickname;
int vittorie;
int pareggi;
int sconfitte;
} score;
typedef struct nodo{
score giocatore;
struct nodo* next;
} nodo;
void setPunteggio (char* player1, int risultato);
void stampaClassifica(nodo* head);
nodo * getPosizione (score player, nodo* head);
void setLista(score player, nodo* head);
nodo* cercaGiocatore(char* player, nodo* head);
int main () {
/*
QUANDO TERMINA LA PARTITA I GIOCATORI CHIAMANO RISPETTIVAMENTE LA FUNCTION setPunteggio
QUESTA CHIAMA A SUA VOLTA LA FUNCTION cercaGiocatore PER VERIFICARE SE IL GIOCATORE SIA GIÀ PRESENTE IN LISTA
DOPODICHÈ RIEMPIRÀ O AGGIORNERÀ LA STRUTTURA score E AL TERMINE CHIAMERÀ LA FUNCTION setLista PASSANDOGLI LO score APPENA
CREATO, QUESTO VERRA INSERITO NELLA LISTA IN MODO ORDINATO TENENDO CONTO PRIMA DEL NUMERO DI VITTORIE POI DEL NUMERO DI PAREGGI E POI QUELLO DI SCONFITTE, SE
IL GIOCATORE DOVESSE GIÀ ESISTERE ALL INTERNO DELLA LISTA VERRANNO SOLO AGGIORNATI I DATI DEL SUO PUNTEGGIO
*/
printf("main");
setPunteggio("madg96", 1);
setPunteggio("ciruzz", 3);
}
nodo * getPosizione (score player, nodo* head){
int flag = 0;
while(head != NULL && flag == 0){
if(head -> giocatore.vittorie > player.vittorie ) {
head = head -> next;
} else if (head -> giocatore.vittorie == player.vittorie){
if(head -> giocatore.pareggi > player.pareggi){
head = head -> next;
} else if(head -> giocatore.pareggi == player.pareggi) {
if (head -> giocatore.sconfitte < player.sconfitte){
head = head -> next;
}else {
return head;
flag = 1;
}
} else {
return head;
flag = 1;
}
} else {
return head;
flag = 1;
}
}
return head;
}
void setLista(score player, nodo* head){
if(head == NULL) {
head = (nodo*) malloc (sizeof(nodo));
head -> giocatore = player;
head -> next = NULL;
} else {
nodo* curr = NULL;
nodo* newNode = NULL;
curr = getPosizione(player, head);
newNode =(nodo*) malloc (sizeof(nodo));
newNode -> giocatore = player;
newNode -> next = curr; // forse *curr o &curr
curr = newNode;
}
}
void setPunteggio(char* player, int risultato){
nodo * getterHead, head;
score * p;
printf("entro\n");
getterHead = cercaGiocatore(player, &head);
if(getterHead == NULL){//IL GIOCATORE NON È PRESENTE NELLA LISTA QUINDI BISOGNA ALLOCARE SPAZIO NELLA LISTA E NELLA STRUTTURA
//head= (nodo*) malloc(sizeof(nodo)); //ALLOCO SPAZIO NELLA LISTA
p = (score*) malloc (sizeof(player)); // ALLOCO SPAZIO PER UNA NUOVA ISTANZA DELLA STRUTTURA
if(risultato == 1){//IL GIOCATORE HA VINTO E SETTO 1 ALLE VITTORIE
p -> nickname = player;
p -> vittorie = 1;
p -> pareggi = 0;
p -> sconfitte = 0;
setLista(*p, &head); //MANDO L ISTANZA DELLA STRUCT A SET LISTA PER INSERIRLO IN MODO ORDINATO
}else if (risultato == 2) { //IL GIOCATORE HA PAREGGIATO E SETTO 1 AI PAREGGI
p -> nickname = player;
p -> vittorie = 0;
p -> pareggi = 1;
p -> sconfitte = 0;
setLista(*p, &head);
} else if (risultato == 3) { //IL GIOCATORE HA PERSO E SETTO 1 ALLE SCONFITTE
p -> nickname = player;
p -> vittorie = 0;
p -> pareggi = 0;
p -> sconfitte = 1;
setLista(*p, &head);
}
} else { // NEL CASO IN CUI IL GIOCATORE SIA GIÀ PRESENTE IN LISTA TROVO LA SUA ISTANZA E AGGIUNGO 1 IN BASE AL RISULTATO OTTENUTO
if(risultato == 1 ){// QUESTI IF ELSE FUNZIONANO ALLO STESSO MODO DI QUELLI SOPRA, IN BASE AL RISULTATO AGGIUNGONO PUNTI AL GIOCATORE
getterHead -> giocatore.vittorie += 1;
} else if ( risultato == 2){
getterHead -> giocatore.pareggi += 1;
} else if ( risultato == 3){
getterHead -> giocatore.sconfitte += 1;
}
}
//stampaClassifica(&head);
}
nodo* cercaGiocatore(char* player, nodo* head){
int trovato = 0;
while(head != NULL && trovato == 0){
if(strcmp(head -> giocatore.nickname, player) == 0) {
trovato = 1;
return head;
}
head = head -> next;
}
return NULL;
}