Exemplo n.º 1
0
static void
digit_help(char *s, long flag)
{
  long n = atoi(s);
  if (n < 0 || n > MAX_SECTION+4)
    pari_err(e_SYNTAX,"no such section in help: ?",s,s);
  if (n == MAX_SECTION+1)
    community();
  else if (flag & h_LONG)
    external_help(s,3);
  else
    commands(n);
  return;
}
Exemplo n.º 2
0
int sagent::handle_input(ACE_HANDLE fd)
{
  ACE_TRACE("sagent::handle_input");

  transaction tr(iv_snmp_session_); // this section needs a better design
  tr.handle_input(fd);
  char rcv_com_str[MAX_COMM_STR_LEN];
  if (tr.result(pdu_, rcv_com_str) < 0)
     return 0;
  OctetStr community(rcv_com_str);
  const ACE_INET_Addr &ta = tr.get_from_addr();
  ACE_TCHAR buf_tmp[MAXHOSTNAMELEN + 1];
  ta.addr_to_string (buf_tmp, MAXHOSTNAMELEN);
  UdpAddress ra (ACE_TEXT_ALWAYS_CHAR (buf_tmp));
  tgt_.set_address(ra);


  // process msg here by calling subclass's implementation
  switch (pdu_.get_type()){
  case sNMP_PDU_GET:
    tgt_.set_read_community(community);
    this->handle_get(pdu_, tgt_);
    break;

  case sNMP_PDU_GETNEXT:
    tgt_.set_read_community(community);
    this->handle_get_next(pdu_, tgt_);
    break;

  case sNMP_PDU_SET:
    tgt_.set_write_community(community);
    this->handle_set(pdu_, tgt_);
    break;

  default:
    ACE_ASSERT(0);
  }
  return 0;
}
Exemplo n.º 3
0
// Run a Monte-Carlo simulation of a game with 6 players.
// Simulate 1,000,000 games.
// Record the winning hole cards of each game.
// Then count the winning frequency of each hole cards.
void simulate(int num_players, int num_simulations)
{
	std::mt19937 engine;
	//std::uniform_int_distribution<int> d(0, 51);
	auto gen = [&](int n) -> int {
		return std::uniform_int_distribution<int>(0, n - 1)(engine);
	};

	// Keep track of the number of occurrences and winning of each combination
	// of hole cards.
	struct hole_stat_t
	{
		char type[4];  // e.g. "AKs"
		int num_occur[MAX_PLAYERS]; // number of occurrence given n opponents
		int num_win[MAX_PLAYERS];   // number of winning given n opponents
		double odds(int num_players) const 
		{
			return (double)(num_occur[num_players] - num_win[num_players])
                / num_win[num_players];
		}
	};
	hole_stat_t stat[HOLE_CARD_COMBINATIONS];
	for (int i = 0; i < HOLE_CARD_COMBINATIONS; i++)
	{
		format_hole_index(stat[i].type, i);
		memset(stat[i].num_occur, 0, sizeof(stat[i].num_occur));
		memset(stat[i].num_win, 0, sizeof(stat[i].num_win));
	}

	// Initialize a deck of cards.
	Hand deck[52];
	for (int i = 0; i < 52; i++)
	{
        Card card((Rank)(i / 4), (Suit)(i % 4));
        deck[i] = Hand(card);
	}

	for (int i = 0; i < num_simulations; i++)
	{
		// Shuffle the deck.
		std::random_shuffle(deck + 0, deck + 52, gen);

		// Store the winning hand and hole cards.
        HandStrength win_strength;

		// Use the first five cards as community cards.
        Hand community(deck, 5);

		// Use each of the next two cards as hole cards for the players.
		for (int j = 0; j < num_players; j++)
		{
            Hand hole;
            hole += deck[5 + j * 2];
			hole += deck[5 + j * 2 + 1];

			// Update the occurrence of this combination of hole cards.
			stat[compute_hole_index(hole)].num_occur[j]++;

			// Find the best 5-card combination from these 7 cards.
            HandStrength strength = EvaluateHand(community + hole);

			// Update the winning hand statistics for a game with j+1 players.
			if (j == 0 || strength > win_strength)
			{
                win_strength = strength;
				stat[compute_hole_index(hole)].num_win[j]++;
			}
		}

		// Note that we do not process tie here. This needs to be fixed.
	}

#if 1
	printf("r1 r2 s Hole");
	for (int n = 2; n <= num_players; n++)
	{
		printf(" %6d", n);
	}
	printf("\n");
	for (int hole = 0; hole < 169; hole++)
	{
		char t = stat[hole].type[2];
		printf("%2d %2d %c %s ", hole / 13, hole % 13, 
			(t == ' ')? 'p' : t, stat[hole].type);
		for (int n = 2; n <= num_players; n++)
		{
			double prob = (double)stat[hole].num_win[n-1] / stat[hole].num_occur[n-1];
			printf(" %.4lf", prob);
		}
		printf("\n");
	}
	//printf("----------------\n");
#endif

#if 0
	// Display a rectangular table.
	printf("O\\S");
	for (int r2 = Rank_Ace; r2 >= Rank_Duce; r2--)
	{
		printf("%5c", format_rank((Rank)r2));
	}
	printf("\n");
	for (int r1 = Rank_Ace; r1 >= Rank_Duce; r1--)
	{
		// Label
		printf("%c: ", format_rank((Rank)r1));

		// Off suit (r1, r2) for r1 <= r2.
		for (int r2 = Rank_Ace; r2 >= Rank_Duce; r2--)
		{
			int index = r1 * 13 + r2;
			printf((r1 == r2)? " *%3.1lf" : "%5.1lf", stat[index].odds());
		}

		// Pair (r1, r1).

		// Same-suit (r1, r2) for r1 > r2.
		printf("\n");
	}
#endif

#if 0
	// Sort the statistics by winning count (strongest hand first).
	std::sort(stat, stat + HOLE_CARD_COMBINATIONS,
		[](const hole_stat_t &s1, const hole_stat_t &s2) -> bool 
	{
		double p1 = (double)s1.num_win / (s1.num_occur + 0.0001);
		double p2 = (double)s2.num_win / (s2.num_occur + 0.0001);
		return p1 > p2;
	});
	
	// Display statistics.
	for (int i = 0; i < HOLE_CARD_COMBINATIONS; i++)
	{
		double percentage = 100.0 * stat[i].num_win / (stat[i].num_occur + 0.0001);
		// printf("%s %.2lf%%\n", stat[i].type, percentage);
		printf("%s %.1lf\n", stat[i].type, (100-percentage)/percentage);
	}
#endif

}