Esempio n. 1
0
int main(void)
{
	int i = 0;
	for (; i < 5; ++i) {
		// the second assert should abort the program
		check_and_set();
	}

	return 0;
}
Esempio n. 2
0
/*
static char
game (char **board)
{
  int round, i, j, l, ref;
  char buffer[BUFFER_SIZE];
  for (round = 0; round < (m * n); round++)
    {
      i = 0;
      system ("clear");
      printf ("Number of stone to align to win (k) = %d\n\n", k);
      board_print (board);
      do
	{
	  printf ("\nPlayer '%c' give your move 'line column' (eg: 1 2),"
		  " press 'Q' to quit:\n ",
		  (round % 2 == 0 ? PLAYER1 : PLAYER2));
	  fgets (buffer, BUFFER_SIZE, stdin);
	  clean (buffer);
	  char *clean = strtok (buffer, ",. - ");
	  while (clean != NULL)
	    {
	      if (isdigit (clean[0]))
		{
		  if (l == 0)
		    i = str2index (clean);
		  else if (l == 0)
		    j = str2index (clean);
		  else
		    printf (" Too much argument \n"
			    "The first two parameters will be used\n");
		  l++;
		}
	      if (isalpha (clean[0]))
		if (clean[0] == 'q' || clean[0] == 'Q')
		  return (EXIT_SUCCESS);
	      clean = strtok (NULL, " ,.-");
	    }
	  if (next >= 2)
	    ref =
	      board_set ((round % 2 == 0 ? PLAYER1 : PLAYER2), i, j, board);
	  else
	    printf ("Not enought argument. Try again please.\n");
	}
      while ((ctrl != 0) || (next < 2));
      if (is_winner (board) != NONE)
	return (is_winner (board));
    }
  return NONE;
}
*/
int
main (int argc, char **argv)
{
  int optc;
  static struct option long_opts[] = {
    /* These options do not set a flag.
       We distinguish them by their indices. */
    {"verbose", no_argument, NULL, 'v'},
    {"set-m", required_argument, NULL, 'm'},
    {"set-n", required_argument, NULL, 'n'},
    {"set-k", required_argument, NULL, 'k'},
    {"all-ai", no_argument, NULL, '0'},
    {"player1-ai", no_argument, NULL, '1'},
    {"player2-ai", no_argument, NULL, '2'},
    {"contest", required_argument, NULL, 'c'},
    {"version", no_argument, NULL, 'V'},
    {"help", no_argument, NULL, 'h'},
    {NULL, 0, NULL, 0},
  };

  while ((optc =
	  getopt_long (argc, argv, "m:n:k:012c:vVh", long_opts, NULL)) != -1)
    {

      switch (optc)
	{
	case 'm':
	case 'n':
	case 'k':
	  check_and_set (optc, atoi (optarg));
	  break;

	case '0':
	  printf ("set both players to be an AI\n");
	  break;

	case '1':
	  printf ("set first player as an AI\n");
	  break;

	case '2':
	  printf ("set second player as an AI\n");
	  break;

	case 'c':
	  printf ("enable 'contest mode'\n");
	  break;

	case 'v':
	  verbose = true;
	  break;

	case 'V':
	  version ();
	  break;

	case 'h':
	  usage (EXIT_SUCCESS);
	  break;

	default:
	  usage (EXIT_FAILURE);
	}
    }
  board = board_alloc ();
  //char bo = game(board);
  board_free (board);
  return (EXIT_SUCCESS);
}
Esempio n. 3
0
 /**
  * Add the given Id to the set.
  *
  * @param id The Id to set.
  */
 void set(T id) override final {
     (void)check_and_set(id);
 }
Esempio n. 4
0
int trace_host(int socket, struct RequestData* data, int ttl, int time_limit,
    struct TraceResults* results){
    
    int nr_of_packets = results -> nr_of_packets;
    
    // przygotuj miejsce na odpowiedź
    unsigned char response[IP_MAXPACKET + 1];
    struct sockaddr_in resp_address;
    socklen_t addr_len = sizeof(struct sockaddr_in);
    
    // obsługa czasu
    struct timeval start, end;
    gettimeofday(&start, NULL);
    
    // wyślij nr_of_packets pakietów i wyczyść informacje o ip w results
    for(int j = 0; j < nr_of_packets; ++j){
        send_request(socket, ttl, data);
        results -> ips[j][0] = '\0';
    }
    
    // j oznacza liczbę otrzymanych odpowiedzi
    for(int j = 0; j < nr_of_packets;){
        
        gettimeofday(&end, NULL);
        int interval = tv_diff_to_ms(&start, &end); // czas jaki upłynął od wysłania pakietów
        if(interval > time_limit){ // przekroczono czas oczekiwania
            return j;
        }
        
        // pp_recvfrom sprawdza czy jakieś pakiety oczekują w kolejce na pobranie
        // jeśli nie zwraca -1
        if(pp_recvfrom(socket, response, IP_MAXPACKET, 0, &resp_address, &addr_len) < 0){
            continue;
        }
        
        struct ip* ip_resp = (struct ip*) response;
        struct icmp* icmp_resp = (struct icmp*) (response + ip_resp -> ip_hl * 4);
        
        // sprawdź czy masz do czynienia z pakietem typu ICMP TIME EXCEEDED
        if(icmp_resp -> icmp_type == ICMP_TIME_EXCEEDED && icmp_resp -> icmp_code == ICMP_EXC_TTL){
            
            // jeśli tak, to otrzymaliśmy również oryginalny pakiet ICMP ECHO
            struct ip* original = (struct ip*) (response + ip_resp -> ip_hl * 4 + ICMP_HEADER_LEN);
            icmp_resp = (struct icmp*) (response + ip_resp -> ip_hl * 4 + ICMP_HEADER_LEN + original -> ip_hl * 4);
            
            // jeśli wszystko jest ok, zwiększ liczbę otrzymanych odpowiedzi
            if(original -> ip_p == IPPROTO_ICMP && 
                check_and_set(icmp_resp, data, nr_of_packets, interval, &resp_address, results)){
                    ++j;
            }
        }
        
        // jak wyżej
        if (icmp_resp -> icmp_type == ICMP_ECHOREPLY &&
            check_and_set(icmp_resp, data, nr_of_packets, interval, &resp_address, results)){
                ++j;
        }
    }
    
    return nr_of_packets;
}