codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define COLONNE 5
#define RIGHE 4
void visualizza_mat(char b[RIGHE][COLONNE],int n,int m);
void assegna(int,int,int,int,int); //prototipo della function
struct giocatore {
char nome[10];
char cognome[10];
};
typedef struct giocatore gt;
void main()
{
int totT=0,totS=0,j,r,i,c,simulatore=0,tennista=0,palla,n_lanci=50000,vittorie=0,sconfitte=0,perc_respinte=0,perc_respinte2=0;
gt gioc ;
char campo2[RIGHE][COLONNE]={
{'B','B','B','B','B'},
{'B','G',' ','G','B'},
{'B','A',' ','A','B'},
{'B','G',' ','G','B'}};
printf("Benvenuto nel programma di simulazione del gioco del tennis vuoi giocare?\n");
visualizza_mat(campo2,RIGHE,COLONNE);
printf("\n-- Inserisci il tuo nome -- \n");
scanf("%s",gioc.nome);
printf("-- Inserisci il tuo cognome -- \n");
scanf("%s",gioc.cognome);
srand(time(NULL));
perc_respinte=(n_lanci*80)/100;
perc_respinte2=(500*80)/100;
printf(">> IL GIOCO PUO' INIZIARE <<\n\n");
for(i=0;i<100;i++){
totT=totT+tennista;
totS=totS+simulatore;
tennista=0;
simulatore=0;
for(j=0;j<500;j++) { //INIZIO for
char campo2[RIGHE][COLONNE]={
{'B','B','B','B','B'},
{'B','G',' ','G','B'},
{'B','A',' ','A','B'},
{'B','G',' ','G','B'}};
palla=1+rand()%10;
do{ //INIZIO REPEAT UNTIL
r=rand()%4;
c=rand()%5;
} //FINE REPEAT UNTIL
while ((c == 2) && (r == 1 || r == 2 || r == 3));
fflush(stdin);
campo2[r][c]='X';
visualizza_mat(campo2,RIGHE,COLONNE);printf("%d comb: r=%d c=%d pall=%d\n",j,r+1,c+1,palla);
assegna(r,c,palla,tennista,simulatore); //chiamata alla function
}
printf("%dTennista:%d\n",i,tennista);
if(tennista>perc_respinte2)
vittorie++;
else
sconfitte++;
} //FINE for
printf("\n-- Il giocatore ha totalizzato %d punti --\n",totT);
printf("\n-- Il simulatore ha totalizzato %d punti --\n",totS);
printf("\n-- Le partite vinte sono: %d quelle perse invece sono: %d --\n",vittorie,sconfitte);
printf("\nPer vincere il giocatore deve totalizzare un numero di punti maggiore della \npercentuale di lanci respinti che risulta essere: %d, di conseguenza :\n",perc_respinte);
if(totT>perc_respinte)
printf("\n-- Hai vinto tu giocatore!! --\n");
else
printf("\n-- Mi dispiace hai perso, ha vinto il simulatore --\n");
}
void visualizza_mat(char b[RIGHE][COLONNE],int n,int m)
{
int i,j;
printf("\n");
printf("ECCO IL CAMPO DI TENNIS: \n");
for(i=0;i<RIGHE;i++)
{
for(j=0;j<COLONNE;j++)
printf(" %c",b[i][j]);
printf("\n");
}
}
void assegna(int r ,int c ,int palla, int tennista, int simulatore){
if((r==2 && c ==1) || (r==2 && c==3))
tennista++;
else if((r==1 && c==1) || (r==1 && c==3) || (r==3 && c==1) || (r==3 && c==3)){
if(palla<3)
tennista++;
else
simulatore++;
}
if(((r==0) && (c==0 || c==1 || c==2 || c==3 || c==4)) || (r==1 && c==0) || (r==1 && c==4) || (r==2 && c==0) || (r==2 && c==4) || (r==3 && c==0) || (r==3 && c==4))
tennista++;
}
per i valori di output, ossia tennista e simulatore si devono usare i puntatori?? mmm perchè cosi facendo quando poi torna nel main il valore di tennista vale sempre 0.. :\