int play_hangman (in, out) int in, out; { char *cuvant_complet, cuvant_partial[MAXLEN]; char ghicit[MAXLEN], outbuf[MAXLEN]; int Vieti = MaxVieti; int Stare_joc = 'I'; int i, ghicit_bine, lungime_cuvant; char HostName[MAXLEN]; gethostname (HostName, MAXLEN); sprintf (outbuf, "Jucam \"HangMan\" pe masina %s \n\r\n\r", HostName); send (out, outbuf, strlen (outbuf), 0); /* Ia aleator un cuvant din lista */ cuvant_complet = cuvinte[rand () % NUMAR_DE_CUVINTE]; lungime_cuvant = strlen (cuvant_complet); /* Initial, nici o litera ghicita */ for (i = 0; i < lungime_cuvant; i++) cuvant_partial[i] = '-'; cuvant_partial[i] = '\0'; sprintf (outbuf, " %s %d\n\r", cuvant_partial, Vieti); send (out, outbuf, strlen (outbuf), 0); while (Stare_joc == 'I') { /* Cere ghicirea unei litere */ while (recv (in, ghicit, MAXLEN, 0) < 0) { if (errno != EINTR) ERR ("recv", 4); } ghicit_bine = 0; for (i = 0; i < lungime_cuvant; i++) { if (ghicit[0] == cuvant_complet[i]) { ghicit_bine = 1; cuvant_partial[i] = cuvant_complet[i]; } } if (!ghicit_bine) Vieti--; if (strcmp (cuvant_complet, cuvant_partial) == 0) Stare_joc = 'W'; /*Castiga userul */ else if (Vieti == 0) { Stare_joc = 'L'; /* Pierde userul */ strcpy (cuvant_partial, cuvant_complet); /* Arata-i cuvantul */ } sprintf (outbuf, " %s %d\n\r", cuvant_partial, Vieti); send (out, outbuf, strlen (outbuf), 0); } switch (Stare_joc) { case 'W': send (out, "\n\rAi castigat. Felicitari! \n\r\n\r", 31, 0); break; case 'L': send (out, "\n\rAi pierdut. Mai incearca.\n\r\n\r", 30, 0); } } /* Programul 3.15 Sursa func›iei play_hangman.c */