Example #1
0
int main(int argc, char *argv[])
{
  int i;

  for (i = 0; i < NUM_TUNES; ++i)
    unhash(unhash_table[i], i);

  strcpy(guess_this, "AABBC");

  num_possible = NUM_TUNES;
  for (i = 0; i < NUM_TUNES; ++i)
    is_possible[i] = 1;

  for (i = 1; i < argc; ++i)
	{
		int g = next_guess(i);
    int result = ((argv[i][0]-'0') << 3) + (argv[i][1]-'0');
		eliminate(g, result);
	}

  if (num_possible < 1)
	{
		printf("ACK!");
	}
	else if (num_possible == 1)
	{
		for (i = 0; i < NUM_TUNES; ++i)
			if (is_possible[i])
				break;
		printf("%.5s 1\n", unhash_table[i]);
	}
	else
	{
	  printf("%.5s %d\n", unhash_table[next_guess(i)], num_possible);
	}

  return 0;
}
Example #2
0
/* New game handler, send and recieve messages with a client */
void 
*new_game(void *new_conection)
{
    int len;
    char guess[NUM_CHOOSEN + 1];     
    guess_out_t *output; 
    char *code = malloc(sizeof(char)*sizeof(NUM_CHOOSEN));;

    char *time_str = malloc(sizeof(char) * TIME_LEN);

	/* Get data from strcuture */
	data_t *client_data = (data_t*)new_conection;
	int new_s = client_data->client_socket;
	char *ip4 = client_data->ip4;

	pthread_mutex_lock(&mutex);
	fprintf(logfile, "%s (%s) (sock_id = %d) client connected\n", time_format(time_str), ip4, new_s);
    pthread_mutex_unlock(&mutex);	

	/* Generate a code for each client if it was not provided */
	if(client_data->code != NULL){		
		strcpy(code, client_data->code);	
	} else {					
		code = generate_code(code);		
	}	

	pthread_mutex_lock(&mutex);
	fprintf(logfile, "%s (0.0.0.0) Server secret for (%s)(sock_id = %d) = %s\n", time_format(time_str), ip4, new_s, code);
	pthread_mutex_unlock(&mutex);

	/*Get and send instructions for the game */
	char* instructions = game_instructions();
	write(new_s, instructions, strlen(instructions) + 1);

	/*Read guesses from the user until the number of guesses has been exhausted */
	int num_guesses = 0;
	while (len=recv(new_s,&guess,sizeof(guess),0)){
		if(num_guesses < MAX_GUESSES){

			/* Write guess into log file */
			fprintf(logfile, "%s From: (%s)(socket_id = %d) guess = %s\n", time_format(time_str), ip4, new_s, guess);

			/*Get the output of the guess*/
			output = next_guess(code, guess, num_guesses);
			
			/*Send Message to player*/
			write(new_s, output->send_msg, output->send_len);	
				
			/* Write outcome into log file */	
			pthread_mutex_lock(&mutex);
			if(strstr(output->log_msg, "SUCCESS")){
				correct_guesses++;
				fprintf(logfile, "%s To: (%s)(socket_id = %d) %s\n", time_format(time_str), ip4, new_s, 
					                                   output->log_msg);
			} else if(strstr(output->log_msg, "FAILURE")){
				fprintf(logfile, "%s To: (%s)(socket_id = %d) %s\n", time_format(time_str), ip4, new_s, 
					                                   output->log_msg);
			} else if(strstr(output->log_msg, "INVALID")){
				fprintf(logfile, "%s To: (%s)(socket_id = %d) %s\n", time_format(time_str), ip4, new_s, 
					                                   output->log_msg);
			} else {
				fprintf(logfile, "%s To: (%s)(socket_id = %d) server's hint: %s\n", time_format(time_str), ip4, new_s, 
					                                                  output->log_msg);
			} 
			pthread_mutex_unlock(&mutex);

			num_guesses = output->num_guesses;					
		} else {
			break;
		}
	}
	close (new_s);
} 
Example #3
0
File: client.c Project: raphi011/os
/**
 * @brief Program entry point
 * @param argc The argument counter
 * @param argv The argument vector
 * @return EXIT_SUCCESS on success, EXIT_PARITY_ERROR in case of an parity
 * error, EXIT_GAME_LOST in case client needed to many guesses,
 * EXIT_MULTIPLE_ERRORS in case multiple errors occured in one round
 */
int main(int argc, char *argv[])
{
    char *server_arg;
    char *port_arg;
    struct addrinfo hints, *serv;
    int sockfd;
    int error;
    int ret = 0;


    if (argc > 0) {
        progname = argv[0];
    }
    
    if (argc < 3) {
        bail_out(EXIT_FAILURE, "Usage: %s <server-hostname> <server-port>", progname);
    }

    server_arg = argv[1];
    port_arg = argv[2];

     
    memset(&hints, 0, sizeof hints); 
    hints.ai_family = AF_INET; 
    hints.ai_socktype = SOCK_STREAM;
    error = getaddrinfo(server_arg, port_arg, &hints, &serv);
    
    if (error != 0) {
        bail_out(EXIT_FAILURE, "getaddrinfo error");
    }
    
    sockfd = socket(serv->ai_family, serv->ai_socktype, serv->ai_protocol);    
    
    if (sockfd < 0) {
        bail_out(EXIT_FAILURE, "socket error");
    } 

    /* Connect the socket to the address */
    if (connect(sockfd, serv->ai_addr, serv->ai_addrlen) != 0) {
        bail_out(EXIT_FAILURE, "connect error");
    }

    for (int round = 0; ; round++) {
        static uint8_t buffer;
        bool parity_error;
        bool game_lost;
        bool game_won;

        uint8_t color[2]; 
        
        next_guess(color);
    
        if (write(sockfd, &color, sizeof(color)) < 0) {
            bail_out(EXIT_FAILURE, "write error");
        }

        if (read_from_server(sockfd, &buffer, READ_BYTES) == NULL) {
            bail_out(EXIT_FAILURE, "read_from_server");
        }
        
        /* read status flags and evaluate if we've won */
        parity_error = buffer & (1 << PARITY_ERR_BIT);
        game_lost = buffer & (1 << GAME_LOST_ERR_BIT);
        game_won = (buffer & SLOTS) == SLOTS;

        if (parity_error) { 
            (void)fprintf(stderr, "Parity error");
            ret = EXIT_PARITY_ERROR;
        } 
        if (game_lost) {
            (void)fprintf(stderr, "Game lost"); 
            if (ret == EXIT_PARITY_ERROR) {
                ret = EXIT_MULTIPLE_ERRORS;
            } else {
                ret = EXIT_GAME_LOST; 
            }
        }   

        if (game_won) {
            (void)printf("Won after %d round(s)", round+1);
            break;
        } else if (ret) {
            break;
        }
    } 

    freeaddrinfo(serv);

    return ret;
}