示例#1
0
void process_input(char *recvbuf, int recv_buf_cnt, int* csock) 
{
	int x = atoi(recvbuf);
	int recvbuf_len = 1024;
	int recv_byte_cnt;
	FILE *fp = fopen("thisbin.bin", "rb+");
	if (x == 1){
		if ((recv_byte_cnt = recv(*csock, recvbuf, recvbuf_len, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			free(csock);
			return;
		}
		printf("\nReceived bytes %d\nReceived string \"%s\"\n", recv_byte_cnt, recvbuf);
		createcategory(fp, recvbuf, csock);
	}
	if (x == 2){
		char *name = (char *)malloc(sizeof(char) * 10);
		char *specification = (char *)malloc(sizeof(char) * 20);
		char *pid = (char *)malloc(sizeof(char) * 3);
		char *token = (char *)malloc(sizeof(char) * 20);
		char s[2] = ",";
		display_categories(fp, csock);
		memset(recvbuf, '\0', sizeof(recvbuf));
		if ((recv_byte_cnt = recv(*csock, recvbuf, recvbuf_len, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			free(csock);
			return;
		}
		printf("\nReceived bytes %d\nReceived string \"%s\"\n", recv_byte_cnt, recvbuf);

		int p = atoi(recvbuf);
		display_persons(fp, p, csock);
		if ((recv_byte_cnt = recv(*csock, recvbuf, recvbuf_len, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			free(csock);
			return;
		}
		printf("\nReceived bytes %d\nReceived string \"%s\"\n", recv_byte_cnt, recvbuf);
		token = strtok(recvbuf, s);
		while (token != NULL)
		{
			printf(" %s\n", token);
			if (count == 1)
				strcpy(name, token);
			if (count == 2)
				strcpy(specification, token);
			if (count == 3)
				strcpy(pid, token);
			token = strtok(NULL, s);
			count++;
		}
		createperson(fp, p, atoi(pid), name, specification, csock);
	}
	if (x == 3){
		char *name = (char *)malloc(sizeof(char) * 10);
		char *d = (char *)malloc(sizeof(char) * 3);
		char *m = (char *)malloc(sizeof(char) * 3);
		char *y = (char *)malloc(sizeof(char) * 5);
		char *token = (char *)malloc(sizeof(char) * 20);
		char s[2] = ",";
		count = 1;
		display_categories(fp, csock);
		memset(recvbuf, '\0', sizeof(recvbuf));
		if ((recv_byte_cnt = recv(*csock, recvbuf, recvbuf_len, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			free(csock);
			return;
		}
		printf("\nReceived bytes %d\nReceived string \"%s\"\n", recv_byte_cnt, recvbuf);

		int c = atoi(recvbuf);
		display_persons(fp, c, csock);
		if ((recv_byte_cnt = recv(*csock, recvbuf, recvbuf_len, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			free(csock);
			return;
		}
		printf("\nReceived bytes %d\nReceived string \"%s\"\n", recv_byte_cnt, recvbuf);

		int  p= atoi(recvbuf);
		if ((recv_byte_cnt = recv(*csock, recvbuf, recvbuf_len, 0)) == SOCKET_ERROR){
			fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
			free(csock);
			return;
		}
		printf("\nReceived bytes %d\nReceived string \"%s\"\n", recv_byte_cnt, recvbuf);

		display_appointments(fp, c, p, csock);
		token = strtok(recvbuf, s);
		while (token != NULL)
		{
			printf("\n in while loop %d", count);
			printf(" %s\n", token);
			if (count == 1)
				strcpy(name, token);
			if (count == 2)
				strcpy(d, token);
			if (count == 3)
				strcpy(m, token);
			if (count == 4)
				strcpy(y, token);
			token = strtok(NULL, s);
			count++;
		}
		getappointment(fp, c, p, name, atoi(d), atoi(m), atoi(y), csock);
	}
	fclose(fp);
}
int main(int argc, char *argv[])
{
	srand(time(NULL));
    // An array of 4 players, may need to be a pointer if you want it set dynamically
    player players[4];
 
    // Input buffer and and commands
    char buffer[BUFFER_LEN] = { 0 };

    // Display the game introduction and prompt for players namesc
    printf("Welcome to Jeopardy: CSCI-3020 Edition! \n \n");
    // initialize each of the players in the array
    for (int i = 0; i < 4; i++){ 
        printf("Enter the name of player %d. \n", i);
        scanf("%s", players[i].name);
        printf("\n");
        players[i].score = 0;
    }

    initialize_game();
    int *turnOrder = (int*)malloc(4*sizeof(int));
    generateTurnOrder(turnOrder);

    printf("Turn Order: \n");
    printf("--------------- \n");
    for (int i = 0; i <= 3; i++){
    	printf("%d \n", turnOrder[i]);
    }
    printf("---------------\n");
    
    player selectedPlayer;
    char *category = (char*)calloc(256,sizeof(char));
    int value;
    // Perform an infinite loop getting command input from users until game ends
    while (fgets(buffer, BUFFER_LEN, stdin) != NULL)
    {

    	for(int i = 0; i < 4; i++){
    		selectedPlayer = players[turnOrder[i] - 1];

    		printf("%s's turn. \n", selectedPlayer.name);
    		display_categories();
    		printf("\nSelect a category: \n");
    		scanf("%s", category);
    		printf("\nSelect a value: \n");
    		scanf("%d", &value);

    		display_question(category, value);


    	}
        // Call functions from the questions and players source files
	
        // Execute the game until all questions are answered
       
        // Display the final results and exit
    }



    free(turnOrder);
    return EXIT_SUCCESS;
}