コード例 #1
0
action_c NullActionAbstraction::get_actions(const Game *game,
                                            const State &state) {
  action_c actions(MAX_ABSTRACT_ACTIONS);
  int num_actions = 0;
  bool error = false;
  for (int a = 0; a < NUM_ACTION_TYPES; ++a) {
    Action action;
    action.type = (ActionType)a;
    action.size = 0;
    if (action.type == a_raise) {
      int32_t min_raise_size;
      int32_t max_raise_size;
      if (raiseIsValid(game, &state, &min_raise_size, &max_raise_size)) {
        if (num_actions + (max_raise_size - min_raise_size + 1) >
            MAX_ABSTRACT_ACTIONS) {
          error = true;
          break;
        }
        for (int s = min_raise_size; s <= max_raise_size; ++s) {
          actions[num_actions] = action;
          actions[num_actions].size = s;
          ++num_actions;
        }
      }
    } else if (isValidAction(game, &state, 0, &action)) {
      /* If you hit this assert, there are too many abstract actions allowed.
       * Either coarsen the betting abstraction or increase
       * MAX_ABSTRACT_ACTIONS
       * in constants.hpp
       */
      if (num_actions >= MAX_ABSTRACT_ACTIONS) {
        error = true;
        break;
      }
      actions[num_actions] = action;
      ++num_actions;
    }
  }

  actions.resize(num_actions);
  //std::cout << "actions: \n";
  //for(unsigned i = 0; i < actions.size(); ++i)
      //std::cout << ActionsStr[actions[i].type] << " ";
  //std::cout << "\n";

  /* If you hit this assert, there are too many abstract actions allowed.
   * Either coarsen the betting abstraction or increase MAX_ABSTRACT_ACTIONS
   * in constants.hpp
   */
   assert( !error );

  return actions;
}
コード例 #2
0
action_c PotRelationActionAbstraction::get_actions(const Game *game,
                                                   const State &state) {
  action_c actions(MAX_ABSTRACT_ACTIONS);
  int num_actions = 0;
  bool error = false;
  for (int a = 0; a < NUM_ACTION_TYPES; ++a) {
    Action action;
    action.type = (ActionType)a;
    action.size = 0;
    if (action.type == a_raise) {
      int32_t min_raise_size;
      int32_t max_raise_size;
      int32_t pot_size = state.spent[0] + state.spent[1];
      if (raiseIsValid(game, &state, &min_raise_size, &max_raise_size)) {
        for (int s = 0; s < fractions.size(); ++s) {
          int32_t raise_size = state.maxSpent + fractions[s] * pot_size;
          if (raise_size > max_raise_size)
            raise_size = max_raise_size;
          if (raise_size < min_raise_size)
            raise_size = min_raise_size;

          assert(raise_size > 0);
          actions[num_actions] = action;
          actions[num_actions].size = raise_size;
          ++num_actions;

          if (raise_size >= max_raise_size)
            break;
        }
        // allin
      }
    } else if (isValidAction(game, &state, 0, &action)) {
      /* If you hit this assert, there are too many abstract actions allowed.
       * Either coarsen the betting abstraction or increase
       * MAX_ABSTRACT_ACTIONS
       * in constants.hpp
       */
      if (num_actions >= MAX_ABSTRACT_ACTIONS) {
        error = true;
        break;
      }
      actions[num_actions] = action;
      ++num_actions;
    }
  }

  actions.resize(num_actions);
  assert(!error);

  return actions;
}
コード例 #3
0
int FcpaActionAbstraction::get_actions( const Game *game,
                                        const State &state,
                                        Action actions
                                        [ MAX_ABSTRACT_ACTIONS ] ) const
{
    assert( MAX_ABSTRACT_ACTIONS >= 4 );

    int num_actions = 0;
    for( int a = 0; a < NUM_ACTION_TYPES; ++a ) {
        Action action;
        action.type = ( ActionType ) a;
        action.size = 0;
        if( action.type == a_raise ) {
            int32_t min_raise_size;
            int32_t max_raise_size;
            if( raiseIsValid( game, &state, &min_raise_size, &max_raise_size ) ) {
                /* Check for pot-size raise being valid.  First, get the pot size. */
                int32_t pot = 0;
                for( int p = 0; p < game->numPlayers; ++p ) {
                    pot += state.spent[ p ];
                }
                /* Add amount needed to call.  This gives the size of a pot-sized raise */
                uint8_t player = currentPlayer( game, &state );
                int amount_to_call = state.maxSpent - state.spent[ player ];
                pot += amount_to_call;
                /* Raise size is total amount of chips committed over all rounds
                 * after making the raise.
                 */
                int pot_raise_size = pot + ( state.spent[ player ] + amount_to_call );
                if( pot_raise_size < max_raise_size ) {
                    actions[ num_actions ] = action;
                    actions[ num_actions ].size = pot_raise_size;
                    ++num_actions;
                }
                /* Now add all-in */
                actions[ num_actions ] = action;
                actions[ num_actions ].size = max_raise_size;
                ++num_actions;
            }

        } else if( isValidAction( game, &state, 0, &action ) ) {
            /* Fold and call */
            actions[ num_actions ] = action;
            ++num_actions;
        }
    }

    return num_actions;
}
コード例 #4
0
int NullActionAbstraction::get_actions( const Game *game,
                                        const State &state,
                                        Action actions
                                        [ MAX_ABSTRACT_ACTIONS ] ) const
{
    int num_actions = 0;
    bool error = false;
    for( int a = 0; a < NUM_ACTION_TYPES; ++a ) {
        Action action;
        action.type = ( ActionType ) a;
        action.size = 0;
        if( action.type == a_raise ) {
            int32_t min_raise_size;
            int32_t max_raise_size;
            if( raiseIsValid( game, &state, &min_raise_size, &max_raise_size ) ) {
                if( num_actions + ( max_raise_size - min_raise_size + 1 )
                        > MAX_ABSTRACT_ACTIONS ) {
                    error = true;
                    break;
                }
                for( int s = min_raise_size; s <= max_raise_size; ++s ) {
                    actions[ num_actions ] = action;
                    actions[ num_actions ].size = s;
                    ++num_actions;
                }
            }
        } else if( isValidAction( game, &state, 0, &action ) ) {
            /* If you hit this assert, there are too many abstract actions allowed.
             * Either coarsen the betting abstraction or increase MAX_ABSTRACT_ACTIONS
             * in constants.hpp
             */
            if( num_actions >= MAX_ABSTRACT_ACTIONS ) {
                error = true;
                break;
            }
            actions[ num_actions ] = action;
            ++num_actions;
        }
    }

    /* If you hit this assert, there are too many abstract actions allowed.
     * Either coarsen the betting abstraction or increase MAX_ABSTRACT_ACTIONS
     * in constants.hpp
     */
    assert( !error );

    return num_actions;
}
コード例 #5
0
ファイル: improved_player.c プロジェクト: Michael-Z/poker-2
int main( int argc, char **argv )
{
  int sock, len, r;
  int32_t min, max;
  uint16_t port;
  Game *game;
  MatchState state;
  Action action;
  struct sockaddr_in addr;
  struct hostent *hostent;
  FILE *file, *toServer, *fromServer;
  char line[ MAX_LINE_LEN ];

  if( argc < 4 ) {

    fprintf( stderr, "usage: player game server port\n" );
    exit( EXIT_FAILURE );
  }

  file = fopen( argv[ 1 ], "r" );
  if( file == NULL ) {

    fprintf( stderr, "ERROR: could not open game %s\n", argv[ 1 ] );
    exit( EXIT_FAILURE );
  }
  game = readGame( file );
  if( game == NULL ) {

    fprintf( stderr, "ERROR: could not read game %s\n", argv[ 1 ] );
    exit( EXIT_FAILURE );
  }
  fclose( file );

  hostent = gethostbyname( argv[ 2 ] );
  if( hostent == NULL ) {

    fprintf( stderr, "ERROR: could not look up address for %s\n", argv[ 2 ] );
    exit( EXIT_FAILURE );
  }

  if( sscanf( argv[ 3 ], "%"SCNu16, &port ) < 1 ) {

    fprintf( stderr, "ERROR: invalid port %s\n", argv[ 3 ] );
    exit( EXIT_FAILURE );
  }

  if( ( sock = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) {

    fprintf( stderr, "ERROR: could not open socket\n" );
    exit( EXIT_FAILURE );
  }

  addr.sin_family = AF_INET;
  addr.sin_port = htons( port );
  memcpy( &addr.sin_addr, hostent->h_addr_list[ 0 ], hostent->h_length );

  if( connect( sock, (struct sockaddr *)&addr, sizeof( addr ) ) < 0 ) {

    fprintf( stderr, "ERROR: could not open connect to %s:%"PRIu16"\n",
	     argv[ 2 ], port );
    exit( EXIT_FAILURE );
  }

  toServer = fdopen( sock, "w" );
  fromServer = fdopen( sock, "r" );
  if( toServer == NULL || fromServer == NULL ) {

    fprintf( stderr, "ERROR: could not get socket streams\n" );
    exit( EXIT_FAILURE );
  }

  if( fprintf( toServer, "VERSION:%"PRIu32".%"PRIu32".%"PRIu32"\n",
	       VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION ) != 14 ) {

    fprintf( stderr, "ERROR: could not get send version to server\n" );
    exit( EXIT_FAILURE );
  }
  fflush( toServer );

  while( fgets( line, MAX_LINE_LEN, fromServer ) ) {

    /* ignore comments */
    if( line[ 0 ] == '#' || line[ 0 ] == ';' ) {
      continue;
    }

    len = readMatchState( line, game, &state );
    if( len < 0 ) {

      fprintf( stderr, "ERROR: could not read state %s", line );
      exit( EXIT_FAILURE );
    }

    if( stateFinished( &state.state ) ) {
      /* ignore the game over message */

      continue;
    }

    if( currentPlayer( game, &state.state ) != state.viewingPlayer ) {
      /* we're not acting */

      continue;
    }

	int bucket = computeHandValue(game,&state.state,state.viewingPlayer,min,max);
    
	/* add a colon (guaranteed to fit because we read a new-line in fgets) */
    line[ len ] = ':';
    ++len;

    //if( ( random() % 2 ) && raiseIsValid( game, &state.state, &min, &max ) ) {
      /* raise */
    if ((bucket >= 3) && raiseIsValid(game, &state.state, &min, &max)) {  
	  action.type = raise;
      action.size = min + random() % ( max - min + 1 );
    } else if (bucket >=2) {
      /* call */

      action.type = call;
      action.size = 0;
    }
	else{
		action.type = fold;
		action.size = 0;
	}

    if( !isValidAction( game, &state.state, 0, &action ) ) {

      fprintf( stderr, "ERROR: chose an invalid action\n" );
      exit( EXIT_FAILURE );
    }

    r = printAction( game, &action, MAX_LINE_LEN - len - 1,
		     &line[ len ] );
    if( r < 0 ) {

      fprintf( stderr, "ERROR: line too long after printing action\n" );
      exit( EXIT_FAILURE );
    }
    len += r;
    line[ len ] = '\n';
    ++len;

    if( fwrite( line, 1, len, toServer ) != len ) {

      fprintf( stderr, "ERROR: could not get send response to server\n" );
      exit( EXIT_FAILURE );
    }
    fflush( toServer );
  }

  return EXIT_SUCCESS;
}
コード例 #6
0
action_c PotRelationActionAbstraction::get_actions(const Game *game,
                                                   const State &state){
  action_c actions(MAX_ABSTRACT_ACTIONS);
  int num_actions = 0;
  bool error = false;
  for (int a = 0; a < NUM_ACTION_TYPES; ++a) {
    Action action;
    action.type = (ActionType)a;
    action.size = 0;
    if (action.type == a_raise) {
      int32_t min_raise_size;
      int32_t max_raise_size;
      int32_t pot_size = state.spent[0] + state.spent[1];
      if (raiseIsValid(game, &state, &min_raise_size, &max_raise_size)) {
          //std::cout << "min_raise: " << min_raise_size << "\tmax_size: " << max_raise_size << "\tmax_spent:" << state.maxSpent<< "\n";
      //std::cout << "min: " << min_raise_size << ", max: " << max_raise_size << ", pot: " << pot_size << ",max spent: " << state.maxSpent << "\n";
        for (int s = 0; s < fractions.size(); ++s) {
          int32_t raise_size = state.maxSpent + fractions[s] * pot_size;
          if( raise_size > max_raise_size )
              raise_size = max_raise_size;
          if( raise_size < min_raise_size )
              raise_size = min_raise_size;

          //std::cout << "raise idx: " << s << "\t size: " << raise_size << "\n";

          //std::cout << "attempting to raise: " << raise_size << "\n";

          assert(raise_size > 0);
          actions[num_actions] = action;
          actions[num_actions].size = raise_size;
          ++num_actions;

          if( raise_size >= max_raise_size )
              break;
        }
        // allin
          //actions[num_actions] = action;
          //actions[num_actions].size = 
          //++num_actions;
      }
    } else if (isValidAction(game, &state, 0, &action)) {
      /* If you hit this assert, there are too many abstract actions allowed.
       * Either coarsen the betting abstraction or increase
       * MAX_ABSTRACT_ACTIONS
       * in constants.hpp
       */
      if (num_actions >= MAX_ABSTRACT_ACTIONS) {
        error = true;
        break;
      }
      actions[num_actions] = action;
      ++num_actions;
    }
  }
  //std::cout << "actions: " << num_actions << "\n";

  actions.resize(num_actions);
  assert( !error );
  //for(unsigned i = 0; i < actions.size(); ++i){
      //std::cout << i  << "\n";
      //if(actions[i].type == a_raise)
        //assert(actions[i].size > 0);
  //}

  return actions;
}