1
0

feat: Initial commit

This commit is contained in:
2024-03-22 17:37:24 +01:00
parent 4288bd63a1
commit 6732a7a166
120 changed files with 9620 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
// Laboratorio 6 - Esercizio 3 - equipArray.c
// Matteo Schiff - s295565
#ifndef EQUIPARRAY_C_DEFINED
#define EQUIPARRAY_C_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "invArray.h"
#include "equipArray.h"
#include "inv.h"
/* ADT di prima classe collezione di oggetti di equipaggiamento */
typedef struct equipArray_s {
int inUse;
inv_t *items[EQUIP_SLOT];
} *equipArray_t;
/* creatore e disruttore */
equipArray_t equipArray_init() {
equipArray_t new = malloc(sizeof(struct equipArray_s));
new->inUse = 0;
return new;
}
void equipArray_free(equipArray_t equipArray) {
free(equipArray);
}
/* quanti equipaggiamenti sono in uso */
int equipArray_inUse(equipArray_t equipArray) {
return equipArray->inUse;
}
void equipArray_apply_modifier(stat_t *stats, equipArray_t equipArray) {
for (int i = 0; i < equipArray_inUse(equipArray); i++) {
stat_sum(stats, &equipArray->items[i]->stat);
}
}
/* scrittura su file */
void equipArray_print(FILE *fp, equipArray_t equipArray, invArray_t invArray) {
for (int i = 0; i < equipArray->inUse; i++) {
printf(" %d: ", i);
inv_print(fp, equipArray->items[i]);
}
}
void equipArray_insert(equipArray_t equipArray, invArray_t invArray) {
char name[LEN+1];
if (equipArray->inUse >= EQUIP_SLOT) {
puts("Tutti gli slot sono occupati");
return;
}
puts("Inserisci il nome dell'oggetto da equipaggiare: ");
scanf(" %50s", name);
int index = invArray_searchByName(invArray, name);
if (index == -1) {
puts("Oggetto non trovato");
return;
}
inv_t *selected = invArray_getByIndex(invArray, index);
equipArray->items[(equipArray->inUse)++] = selected;
}
void equipArray_remove(equipArray_t equipArray, invArray_t invArray) {
int index;
if (equipArray->inUse <= 0) {
puts("Tutti gli slot sono vuoti");
return;
}
puts("Oggetti equipaggiati: ");
equipArray_print(stdout, equipArray, invArray);
puts("Inserisci l'indice dell'oggetto da rimuovere: ");
scanf(" %d", &index);
if (index < 0 || index >= equipArray->inUse) {
puts("Indice oggetto non valido");
return;
}
equipArray->items[index] = equipArray->items[--(equipArray->inUse)];
}
/* modifica equipaggiamento scegliendo un oggetto da inventario */
void equipArray_update(equipArray_t equipArray, invArray_t invArray) {
char selection;
puts("Vuoi inserire o rimuovere un oggetto? Rispondi (i) o (r): ");
scanf(" %c", &selection);
if (selection == 'i') {
equipArray_insert(equipArray, invArray);
} else {
equipArray_remove(equipArray, invArray);
}
}
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,34 @@
// Laboratorio 6 - Esercizio 3 - equipArray.h
// Matteo Schiff - s295565
#ifndef EQUIPARRAY_H_DEFINED
#define EQUIPARRAY_H_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define EQUIP_SLOT 8
#include "invArray.h"
/* ADT di prima classe collezione di oggetti di equipaggiamento */
typedef struct equipArray_s *equipArray_t;
/* creatore e disruttore */
equipArray_t equipArray_init();
void equipArray_free(equipArray_t equipArray);
/* quanti equipaggiamenti sono in uso */
int equipArray_inUse(equipArray_t equipArray);
void equipArray_apply_modifier(stat_t *stats, equipArray_t equipArray);
/* scrittura su file */
void equipArray_print(FILE *fp, equipArray_t equipArray, invArray_t invArray);
/* modifica equipaggiamento scegliendo un oggetto da inventario */
void equipArray_update(equipArray_t equipArray, invArray_t invArray);
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,122 @@
// Laboratorio 6 - Esercizio 3 - gdr.c
// Matteo Schiff - s295565
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pgList.h"
#include "invArray.h"
#include "pg.h"
#define N_SCELTE 7
#define DBG 0
enum { falso, vero };
typedef int bool;
void stampaMenu(char *scelte[], int *selezione){
int i=0;
printf("\nMENU'\n");
for(i=0;i<N_SCELTE;i++)
printf("%2d > %s\n",i,scelte[i]);
scanf(" %d",selezione);
}
int main(int argc, char **argv) {
char *scelte[] = {
"Uscita",
"Stampa personaggi",
"Stampa inventario",
"Cerca personaggio",
"Aggiungi personaggio",
"Elimina personaggio",
"Modifica equip"
};
char codiceRicerca[LEN];
int selezione;
FILE *fin;
bool fineProgramma;
pgList_t pgList = pgList_init();
invArray_t invArray = invArray_init();
pg_t *pgp, pg;
fin = fopen("pg.txt","r");
pgList_read(fin, pgList);
fclose(fin);
#if DBG
pgList_print(stdout, pgList);
#endif /* DBG */
fin = fopen("inventario.txt","r");
invArray_read(fin, invArray);
fclose(fin);
#if DBG
invArray_print(stdout, invArray);
#endif /* DBG */
fineProgramma = falso;
do {
stampaMenu(scelte, &selezione);
switch(selezione){
case 0: {
fineProgramma = vero;
} break;
case 1: {
pgList_print(stdout, pgList, invArray);
} break;
case 2: {
invArray_print(stdout, invArray);
} break;
case 3: {
printf("Inserire codice personaggio: ");
scanf("%s", codiceRicerca);
pgp = pgList_searchByCode(pgList, codiceRicerca);
if (pgp!=NULL) {
pg_print(stdout, pgp, invArray);
} else {
puts("Personaggio non trovato");
}
} break;
case 4: {
printf("Cod Nome Classe HP MP ATK DEF MAG SPR: ");
if (pg_read(stdin, &pg) != 0) {
pgList_insert(pgList, pg);
}
} break;
case 5: {
printf("Inserire codice personaggio: ");
scanf("%s", codiceRicerca);
pgList_remove(pgList, codiceRicerca);
} break;
case 6: {
printf("Inserire codice personaggio: ");
scanf("%s", codiceRicerca);
pgp = pgList_searchByCode(pgList, codiceRicerca);
if (pgp!=NULL) {
pg_updateEquip(pgp, invArray);
} else {
puts("Personaggio non trovato");
}
} break;
default:{
printf("Scelta non valida\n");
} break;
}
} while(!fineProgramma);
pgList_free(pgList);
return 0;
}

View File

@@ -0,0 +1,71 @@
// Laboratorio 6 - Esercizio 3 - inv.c
// Matteo Schiff - s295565
#ifndef INV_C_DEFINED
#define INV_C_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include "inv.h"
/* funzioni di input/output delle statistiche */
void stat_read(FILE *fp, stat_t *statp) {
fscanf(fp, " %d %d %d %d %d %d", &(statp->hp), &(statp->mp), &(statp->atk), &(statp->def), &(statp->mag), &(statp->spr));
}
int zero_se_soglia(int a, int soglia) {
if (a < soglia)
return 0;
return a;
}
void stat_print(FILE *fp, stat_t *statp, int soglia) {
fprintf(fp, "HP: %d, MP: %d, ATK: %d, DEF: %d, MAG: %d, SPR: %d\n",
zero_se_soglia(statp->hp, soglia),
zero_se_soglia(statp->mp, soglia),
zero_se_soglia(statp->atk, soglia),
zero_se_soglia(statp->def, soglia),
zero_se_soglia(statp->mag, soglia),
zero_se_soglia(statp->spr, soglia));
}
void stat_copy(stat_t *a, stat_t *b) {
a->atk = b->atk;
a->def = b->def;
a->hp = b->hp;
a->mag = b->mag;
a->mp = b->mp;
a->spr = b->spr;
}
void stat_sum(stat_t *a, stat_t *b) {
a->atk += b->atk;
a->def += b->def;
a->hp += b->hp;
a->mag += b->mag;
a->mp += b->mp;
a->spr += b->spr;
}
/* funzioni di input/output di un oggetto dell'inventario */
void inv_read(FILE *fp, inv_t *invp) {
fscanf(fp, " %49s %49s", invp->nome, invp->tipo);
stat_read(fp, &(invp->stat));
}
void inv_print(FILE *fp, inv_t *invp) {
fprintf(fp, " %s (%s) - ", invp->nome, invp->tipo);
stat_print(fp, &(invp->stat),INT_MIN);
}
/* ritorna il campo stat di un oggetto dell'inventario */
stat_t inv_getStat(inv_t *invp) {
return invp->stat;
}
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,42 @@
// Laboratorio 6 - Esercizio 3 - inv.h
// Matteo Schiff - s295565
#ifndef INV_H_DEFINED
#define INV_H_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN 50
#define MIN_STAT 1
/* quasi ADT statistiche */
typedef struct stat_s {
int hp, mp, atk, def, mag, spr;
} stat_t;
/* quasi ADT oggetto di inventario */
typedef struct inv_s {
char nome[LEN];
char tipo[LEN];
stat_t stat;
} inv_t;
/* funzioni di input/output delle statistiche */
void stat_read(FILE *fp, stat_t *statp);
void stat_print(FILE *fp, stat_t *statp, int soglia);
void stat_copy(stat_t *a, stat_t *b);
void stat_sum(stat_t *a, stat_t *b);
/* funzioni di input/output di un oggetto dell'inventario */
void inv_read(FILE *fp, inv_t *invp);
void inv_print(FILE *fp, inv_t *invp);
/* ritorna il campo stat di un oggetto dell'inventario */
stat_t inv_getStat(inv_t *invp);
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,67 @@
// Laboratorio 6 - Esercizio 3 - invArray.c
// Matteo Schiff - s295565
#ifndef INVARRAY_C_DEFINED
#define INVARRAY_C_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "inv.h"
typedef struct invArray_s {
int N;
inv_t *array;
} *invArray_t;
/* creatore e disruttore */
invArray_t invArray_init() {
invArray_t new = malloc(sizeof(struct invArray_s));
new->N = 0;
new->array = NULL;
return new;
}
void invArray_free(invArray_t invArray) {
free(invArray->array);
free(invArray);
}
/* lettura e scrittura su file */
void invArray_read(FILE *fp, invArray_t invArray) {
if (fscanf(fp, " %d", &invArray->N) != 1)
return;
invArray->array = malloc(invArray->N * sizeof(inv_t));
for (int i = 0; i < invArray->N; i++) {
inv_read(fp, &invArray->array[i]);
}
}
void invArray_print(FILE *fp, invArray_t invArray) {
for (int i = 0; i < invArray->N; i++) {
inv_print(fp, &invArray->array[i]);
}
}
/* ritorna puntatore a oggetto selezionato da indice (nel vettore) */
inv_t *invArray_getByIndex(invArray_t invArray, int index) {
if (index >= invArray->N)
return NULL;
return &(invArray->array[index]);
}
/* ritorna indice (nel vettore) a oggetto selezionato da nome */
int invArray_searchByName(invArray_t invArray, char *name) {
for (int i = 0; i < invArray->N; i++) {
if (!strcmp(invArray->array[i].nome, name))
return i;
}
return -1;
}
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,30 @@
// Laboratorio 6 - Esercizio 3 - invArray.h
// Matteo Schiff - s295565
#ifndef INVARRAY_H_DEFINED
#define INVARRAY_H_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "inv.h"
/* ADT di prima classe collezione di oggetti di inventario */
typedef struct invArray_s *invArray_t;
/* creatore e disruttore */
invArray_t invArray_init();
void invArray_free(invArray_t invArray);
/* lettura e scrittura su file */
void invArray_read(FILE *fp, invArray_t invArray);
void invArray_print(FILE *fp, invArray_t invArray);
/* ritorna puntatore a oggetto selezionato da indice (nel vettore) */
inv_t *invArray_getByIndex(invArray_t invArray, int index);
/* ritorna indice (nel vettore) a oggetto selezionato da nome */
int invArray_searchByName(invArray_t invArray, char *name);
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,16 @@
15
Excalibur Spada2M 0 0 120 0 0 50
Filatterio Accessorio -100 20 -30 -30 25 25
ArmillaDiDiamante Accessorio 0 0 25 0 25
Escutcheon Scudo 1000 50 0 75 0 0
MantoElfico Vesti 0 50 0 10 10 10
Pendragon Scudo 250 0 0 45 0 30
Oricalco Reliquia 0 0 0 0 15 15
TalismanoNero Accessorio -500 150 0 0 100 50
AmmazzaDraghi Spada2M 0 0 80 -20 0 -20
Fiocco Accessorio 0 0 1 1 1 1
CorazzaAdamantina ArmaturaPesante 3333 333 -15 33 -15 33
DagaRunica Pugnale 0 50 10 0 35 0
Tempesta Spada1M 0 0 40 0 -5 0
Maximillian ArmaturaLeggera 500 0 0 30 0 15
Ametista Reliquia 0 0 15 15 0 0

View File

@@ -0,0 +1,49 @@
// Laboratorio 6 - Esercizio 3 - pg.c
// Matteo Schiff - s295565
#ifndef PG_C_DEFINED
#define PG_C_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "equipArray.h"
#include "pg.h"
void pg_calc_stats(pg_t *pgp) {
stat_copy(&pgp->eq_stat, &pgp->b_stat);
equipArray_apply_modifier(&pgp->eq_stat, pgp->equip);
}
/* lettura e scrittura su file */
int pg_read(FILE *fp, pg_t *pgp) {
if (fscanf(fp, " %49s %49s %49s", pgp->cod, pgp->nome, pgp->classe) != 3)
return 0;
stat_read(fp, &(pgp->b_stat));
pgp->equip = equipArray_init();
pg_calc_stats(pgp);
return 1;
}
/* non essendo struct dinamica, pulisce chiamando il distruttore di equipArray */
void pg_clean(pg_t *pgp) {
free(pgp->equip);
}
void pg_print(FILE *fp, pg_t *pgp, invArray_t invArray) {
fprintf(fp, "%s (%s)\n Classe: %s\n Statistiche - ", pgp->nome, pgp->cod, pgp->classe);
stat_print(fp, &(pgp->eq_stat), 0);
equipArray_print(fp, pgp->equip, invArray);
}
/* modifica personaggio aggiungendo/togliendo un equipaggiamento selezionato da inventario:
di fatto e' sufficiente chiamare l'opportuna funzione dal modulo equipArray */
void pg_updateEquip(pg_t *pgp, invArray_t invArray) {
equipArray_update(pgp->equip, invArray);
pg_calc_stats(pgp);
}
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,35 @@
// Laboratorio 6 - Esercizio 3 - pg.h
// Matteo Schiff - s295565
#ifndef PG_H_DEFINED
#define PG_H_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "equipArray.h"
/* quasi ADT personaggio; si noti che si tratta di un composto con riferimento
a un equipArray di proprieta' */
typedef struct pg_s {
char cod[LEN];
char nome[LEN];
char classe[LEN];
stat_t b_stat, eq_stat;
equipArray_t equip;
} pg_t;
/* lettura e scrittura su file */
int pg_read(FILE *fp, pg_t *pgp);
/* non essendo struct dinamica, pulisce chiamando il distruttire di equipArray */
void pg_clean(pg_t *pgp);
void pg_print(FILE *fp, pg_t *pgp, invArray_t invArray);
/* modifica personaggio aggiungendo/togliendo un equipaggiamento selezionato da inventario:
di fatto e' sufficiente chiamare l'opportuna funzione dal modulo equipArray */
void pg_updateEquip(pg_t *pgp, invArray_t invArray);
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,10 @@
PG0001 Sephiroth Guerriero 1832 71 116 65 41 49
PG0002 Aerith MagoBianco 976 144 12 39 121 140
PG0003 Vivi MagoNero 1001 136 17 36 131 115
PG0004 Beatrix Templare 1654 99 85 35 70 61
PG0005 Orlandeau Paladino 1701 84 81 68 34 48
PG0006 Basch Capitano 2199 46 32 98 33 89
PG0007 Lulu MagoNero 999 139 21 33 119 117
PG0008 Yuna Evocatrice 1019 100 32 17 97 99
PG0009 DarkCecil CavaliereNero 1901 48 106 90 12 18
PG0010 Kefka Arcimago 1271 89 39 47 89 64

View File

@@ -0,0 +1,131 @@
// Laboratorio 6 - Esercizio 3 - pgList.c
// Matteo Schiff - s295565
#ifndef PGLIST_C_DEFINED
#define PGLIST_C_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pgList.h"
typedef struct node_s *link;
typedef struct node_s {
pg_t val;
link next;
} node;
/* ADT di prima classe collezione di personaggi */
typedef struct pgList_s {
link head;
link tail;
int nPg;
} *pgList_t;
link new_node(pg_t val, link next) {
link t = malloc(sizeof(node));
if (t == NULL) {
printf("ERROR: out of memory");
exit(2);
}
t->val = val;
t->next = next;
return t;
}
void free_node(link p) {
pg_clean(&p->val);
free(p);
}
/* creatore e distruttore */
pgList_t pgList_init() {
pgList_t new = malloc(sizeof(struct pgList_s));
new->head = NULL;
new->tail = NULL;
new->nPg = 0;
return new;
}
void pgList_free(pgList_t pgList) {
link p, x;
for (p = pgList->head, x = pgList->head->next; x != NULL; p = x, x = x->next){
free_node(p);
}
free(pgList);
}
/* lettura e scrittura su file */
void pgList_read(FILE *fp, pgList_t pgList) {
while (!feof(fp))
{
pg_t pgp;
if (pg_read(fp, &pgp) != 0) {
pgList_insert(pgList, pgp);
}
}
}
void pgList_print(FILE *fp, pgList_t pgList, invArray_t invArray) {
link p;
for (p = pgList->head; p != NULL; p = p->next) {
pg_print(fp, &(p->val), invArray);
}
}
/* inserimento di un nuovo personaggio */
void pgList_insert(pgList_t pgList, pg_t pg) {
link new = new_node(pg, NULL);
if (pgList->head == NULL) {
pgList->head = new;
} else {
pgList->tail->next = new;
}
pgList->tail = new;
}
/* cancellazione con rimozione */
void pgList_remove(pgList_t pgList, char* cod) {
link p, x;
if (pgList->head == NULL)
return;
for (x = pgList->head, p = NULL; x != NULL; p = x, x = x->next) {
if (!strcmp(x->val.cod, cod)) {
if (p == NULL) {
pgList->head = x->next;
} else {
p->next = x->next;
// se tolgo l'ultimo elemento devo aggiornare `tail`
if (x->next == NULL)
pgList->tail = p;
}
free_node(x);
break;
}
}
}
/* ricerca per codice, ritornando il puntatore */
pg_t *pgList_searchByCode(pgList_t pgList, char* cod) {
link p;
for (p = pgList->head; p != NULL; p = p->next) {
if (!strcmp(p->val.cod, cod)) {
return &(p->val);
}
}
return NULL;
}
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif

View File

@@ -0,0 +1,33 @@
// Laboratorio 6 - Esercizio 3 - pgList.h
// Matteo Schiff - s295565
#ifndef PGLIST_H_DEFINED
#define PGLIST_H_DEFINED
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pg.h"
/* ADT di prima classe collezione di personaggi */
typedef struct pgList_s *pgList_t;
/* creatore e distruttore */
pgList_t pgList_init();
void pgList_free(pgList_t pgList);
/* lettura e scrittura su file */
void pgList_read(FILE *fp, pgList_t pgList);
void pgList_print(FILE *fp, pgList_t pgList, invArray_t invArray);
/* inserimento di un nuovo personaggio */
void pgList_insert(pgList_t pgList, pg_t pg);
/* cancellazione con rimozione */
void pgList_remove(pgList_t pgList, char* cod);
/* ricerca per codice, ritornando il puntatore */
pg_t *pgList_searchByCode(pgList_t pgList, char* cod);
/* Si possono aggiungere altre funzioni se ritenute necessarie */
#endif