Anteprima
Vedrai una selezione di 1 pagina su 5
Programma server del gioco del misterMind Pag. 1
1 su 5
Disdici quando vuoi 162x117
Disdici quando
vuoi
Acquista con carta
o PayPal
Scarica i documenti
tutte le volte che vuoi
Sintesi
Parte server del gioco del mistermind.Permette di giocare al mistermind se si aggiunge un client! ottimo esempio fatto con c++ in borland
Estratto del documento

// Inizializzo vett[] ha 49 numero ascii che quivale a 1 cosi facendo

genera numeri random fino a 9

vitt[i]=49+rand()%9;

for (int j=0;j<i;j++) {

if (vitt[i]==vitt[j]) {

i--;

break;

}

}

}

vitt[4]='\0';

// INVIA MEX DI BENVENUTO.

if ( send(fd_new,BENVENUTO, strlen(BENVENUTO)+1, 0) == SOCKET_ERROR )

error("Server: sending failure ");

// clock t tipo dichiarato in time.h

clock_t start,end;

start=clock();

while (1) {

char msg1[]="Tentativo numero: \0\0\0";

char num[3];

// tent variabile x il incremento dei tentativi la itoa serve x la

conversione da int a stringa

tent++;

itoa(tent,num,10);

// strcat servere x la concatenazione di due stringhe

strcat(msg1,num);

Sleep(1000);

if ( send(fd_new,msg1, strlen(msg1)+1, 0) == SOCKET_ERROR )

error("Server: sending failure ");

//riceviamo in buf il tentativo del client

if ( recv(fd_new, buf, sizeof(buf), 0) == SOCKET_ERROR )

error("Server: receiving failure ");

// controllo che sia un tentativo valido

// controllo che la stringa sia lunga 4 e contenga solo numeri

if (strlen(buf)!=4 || atoi(buf)==0) {

tent--;

if ( send(fd_new,NON_VALIDO,strlen(NON_VALIDO)+1, 0) == SOCKET_ERROR

) error("Server: sending failure ");

continue;

}

int controllo=0;

// controllo che non ci siano zeri

for (int i=0;i<4;i++) {

if (buf[i]=='0') {

tent--;

controllo=1;

break;

}

}

if (controllo==1) {

if ( send(fd_new,NON_VALIDO,strlen(NON_VALIDO)+1, 0) == SOCKET_ERROR

) error("Server: sending failure ");

continue;

}

// controllo che non ci siano ripetizoni.

for (int i=0;i<3 && controllo==0;i++) {

for (int j=i+1;j<4;j++) {

if (buf[i]==buf[j]) {

tent--;

controllo=1;

break;

}

}

}

if (controllo==1) {

if ( send(fd_new,NON_VALIDO,strlen(NON_VALIDO)+1, 0) == SOCKET_ERROR

) error("Server: sending failure ");

continue;

}

int ast=0,cerchi=0;

// controlla il risultato del tentativo.

for (int i=0;i<4;i++) {

for (int j=0;j<4;j++) {

if (vitt[i]==buf[j]) {

if (i==j)

ast++;

else cerchi++;

}

}

}

strcpy(buf,"Risultato: \0");

//prima cosa inserisco nel buffer gli O e poi da dove sono rimasto metto

le X e infene il carattere terminatore.

int len=strlen(buf);

for (int i=len;i<len+cerchi;i++)

buf[i]='O';

len=len+cerchi;

for (int i=len;i<len+ast;i++)

buf[i]='X';

buf[len+ast]='\0';

// invio il messaggio della sequenaza al client

if ( send(fd_new,buf,strlen(buf)+1, 0) == SOCKET_ERROR )

error("Server: sending failure ");

//controlla se l'utente ha vinto se cosi stampa il mex di vittoria

if (ast==4) {

end=clock();

//clock restituisce il tempo passato dall'inizio del programma in

cicli di clock, CLOCKS_PER_SEC una macro che restituisce

//il numero di cicli al secondo

int secondi=(int)(end-start)/(int)CLOCKS_PER_SEC;

strcpy(buf,VITTORIA);

char secondiStr[10];

itoa(secondi,secondiStr,10);

strcat(buf,secondiStr);

Sleep(1000);

if ( send(fd_new,buf, strlen(buf)+1, 0) == SOCKET_ERROR )

error("Server: sending failure ");

break;

}

if (tent==10) {

strcpy(buf,SCONFITTA);

strcat(buf,vitt);

Sleep(1000);

if ( send(fd_new,buf, strlen(buf)+1, 0) == SOCKET_ERROR )

error("Server: sending failure ");

break;

}

}

if (closesocket(fd_new) == SOCKET_ERROR)

error("Server: closing socket ");

if (closesocket(fd) == SOCKET_ERROR)

error("Server: closing socket ");

}

/**********************************************

* Windows Sockets Initialization *

**********************************************/

int InitializeWinsock(WORD wVersionRequested) {

WSADATA wsaData;

int err;

err = WSAStartup(wVersionRequested, &wsaData);

// ritorna errore se, ad esempio, l'applicazione supporta al massimo

// la versione 1.1 e la DLL supporta da 2.0 in su (le versioni non si

sovrappongono)

if (err!=0) return 0; // Tell the user that we couldn't find a usable

winsock.dll

// WSAStartup returns in wHighVersion (struct wsaData) the highest version

it supports

// and in wVersion the minimum of its high version and wVersionRequested.

// wVersion is the The version of the Windows Sockets specification

// that the Windows Sockets DLL expects the caller to use.

// Se WSAStartup ritorna un risultato accettabile, l'applicazione deve

ancora

// verificare che il risultato sia compatibile con la sua richiesta. Ad

esempio,

// con wVersionRequested=1.1 e DLL version 1.0, wVersion=1.0. Se

l'applicazione

// vuole assolutamente usare la DLL 1.1, deve ancora verificare di non

trovarsi

// in questo caso

// Tell the user that we couldn't find a usable winsock.dll.

if (LOBYTE(wsaData.wVersion )!=1 || HIBYTE(wsaData.wVersion)!=1) return 0;

//Everything is fine: proceed

return 1;

}

/**********************************************

* Error Handling Functions *

**********************************************/

// define to save space

#define PERR(X) fprintf(stderr, X); break

void error(char* string) {

int err;

fprintf(stderr, "%s", string);

err= WSAGetLastError();

Dettagli
Publisher
5 pagine
27 download