int main (void) { rand_no(); int i,j,k; for(i = 0; i < 20; i++){ if( i == 10){ printf("ni ge da ben dan hahaha"); return 0; } printf("Please input the 4 NO. you guess:\n"); scanf("%s",gue); k = right_or_n(); if( k == 1 ){ continue; } y_or_n(); if(x == 0 && y == 0) printf("No num. is here"); if(x == 4){ printf("You done!\n"); break; } else{ for (j = 0; j < x; j++) printf("A"); for(j = 0; j < y; j++) printf("B"); printf("\n"); } } return 0; }
void DumbBot::generate_random_unit_list( MapAndUnits::UNIT_SET &units ) { MapAndUnits::UNIT_SET::iterator unit_iterator; int unit_index; m_random_unit_map.clear(); for ( unit_iterator = units.begin(); unit_iterator != units.end(); unit_iterator++ ) { unit_index = rand_no( RAND_MAX ); m_random_unit_map.insert( RANDOM_UNIT_MAP::value_type( unit_index, *unit_iterator ) ); } }
int main(int argc, char *argv[]) { int i; int r; int val; if (argc != 2) { fprintf(stderr, "USAGE: %s val\n", argv[0]); exit(1); } val = atoi(argv[1]); srand(getpid()); while (1) { r = rand_no(val); printf("%d\n", r); } }
void DumbBot::generate_movement_orders( void ) { RANDOM_UNIT_MAP::iterator unit_iterator; DESTINATION_MAP destination_map; MapAndUnits::UNIT_AND_ORDER *unit; MapAndUnits::COAST_SET *adjacent_coasts; MapAndUnits::COAST_SET::iterator coast_iterator; DESTINATION_MAP::iterator destination_iterator; DESTINATION_MAP::iterator next_destination_iterator; bool try_next_province; WEIGHTING next_province_chance; bool selection_is_ok; bool order_unit_to_move; MapAndUnits::UNITS::iterator current_unit_iterator; RANDOM_UNIT_MAP::iterator random_unit_iterator; int occupying_unit_order; MOVING_UNIT_MAP moving_unit_map; MOVING_UNIT_MAP::iterator moving_unit_iterator; // Put our units into a random order. This is one of the ways in which // DumbBot is made non-deterministic - the order the units are considered // in can affect the orders selected generate_random_unit_list( m_map_and_units->our_units ); unit_iterator = m_random_unit_map.begin(); log_debug("%s %d", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->current_season ].c_str(), m_map_and_units->current_year ); while ( unit_iterator != m_random_unit_map.end() ) { // Build the list of possible destinations for this unit, indexed by the // weight for the destination destination_map.clear(); unit = &( m_map_and_units->units[ unit_iterator->second ] ); log_debug("Selecting destination for %s", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ unit->coast_id.province_index ].province_token ].c_str() ); // Put all the adjacent coasts into the destination map adjacent_coasts = &( m_map_and_units->game_map[ unit_iterator->second ].coast_info[ unit->coast_id.coast_token ].adjacent_coasts ); for ( coast_iterator = adjacent_coasts->begin(); coast_iterator != adjacent_coasts->end(); coast_iterator++ ) { destination_map.insert( DESTINATION_MAP::value_type( m_destination_value[ *coast_iterator ], *coast_iterator ) ); } // Put the current location in (we can hold rather than move) destination_map.insert( DESTINATION_MAP::value_type( m_destination_value[ unit->coast_id ] + 1, unit->coast_id ) ); do { // Pick a destination destination_iterator = destination_map.begin(); log_debug("Destination selected : %s %s", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ destination_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ destination_iterator->second.coast_token ].c_str() ); try_next_province = true; while ( try_next_province == true ) { // Consider the next destination in the map next_destination_iterator = destination_iterator; next_destination_iterator++; // If there is no next destination, give up. if ( next_destination_iterator == destination_map.end() ) { try_next_province = false; } else { // Randomly determine whether to use the current or next destination, // based on their relative weights (the higher the difference, the // more chance of picking the better one). if ( destination_iterator->first == 0 ) { next_province_chance = 0; } else { next_province_chance = ( ( destination_iterator->first - next_destination_iterator->first ) * m_alternative_difference_modifier ) / destination_iterator->first; } if ( ( rand_no( 100 ) < m_play_alternative ) && ( rand_no( 100 ) >= next_province_chance ) ) { // Pick the next one (and go around the loop again to keep considering // more options destination_iterator = next_destination_iterator; log_debug("New destination selected : %s %s (%d%% chance)", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ destination_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ destination_iterator->second.coast_token ].c_str(), m_play_alternative - ( next_province_chance * m_play_alternative / 100 ) ); } else { // Use this one try_next_province = false; } } } // Check whether this is a reasonable selection selection_is_ok = true; order_unit_to_move = true; // If this is a hold order if ( destination_iterator->second.province_index == unit->coast_id.province_index ) { log_debug("Ordered to hold"); // Hold order m_map_and_units->set_hold_order( unit_iterator->second ); } else { // If there is a unit in this province already current_unit_iterator = m_map_and_units->units.find( destination_iterator->second.province_index ); if ( m_map_and_units->our_units.find( destination_iterator->second.province_index ) != m_map_and_units->our_units.end() ) { log_debug("Province occupied"); // If it is not yet ordered if ( current_unit_iterator->second.order_type == MapAndUnits::NO_ORDER ) { log_debug("Occupying unit unordered"); // Find this unit in the random unit map for ( random_unit_iterator = m_random_unit_map.begin(); random_unit_iterator != m_random_unit_map.end(); random_unit_iterator++ ) { if ( random_unit_iterator->second == destination_iterator->second.province_index ) { occupying_unit_order = random_unit_iterator->first; } } // Move this unit to after the one it's waiting for. We can't decide // whether to move there or not, so give up on this unit for now if ( occupying_unit_order >= 0 ) { m_random_unit_map.insert( RANDOM_UNIT_MAP::value_type( occupying_unit_order - 1, unit_iterator->second ) ); order_unit_to_move = false; } } // If it is not moving else if ( ( current_unit_iterator->second.order_type != MapAndUnits::MOVE_ORDER ) && ( current_unit_iterator->second.order_type != MapAndUnits::MOVE_BY_CONVOY_ORDER ) ) { log_debug("Occupying unit not moving"); // If it needs supporting if ( m_competition_value[ destination_iterator->second.province_index ] > 1 ) { log_debug("Supporting occupying unit"); // Support it m_map_and_units->set_support_to_hold_order( unit_iterator->second, destination_iterator->second.province_index ); order_unit_to_move = false; } else { log_debug("Destination not accepted"); // This is not an acceptable selection selection_is_ok = false; // Make sure it isn't selected again destination_map.erase( destination_iterator ); } } } if ( selection_is_ok ) { // If there is a unit already moving to this province moving_unit_iterator = moving_unit_map.find( destination_iterator->second.province_index ); if ( moving_unit_iterator != moving_unit_map.end() ) { log_debug("Unit already moving here"); // If it may need support if ( m_competition_value[ destination_iterator->second.province_index ] > 0 ) { log_debug("Supporting moving unit"); // Support it m_map_and_units->set_support_to_move_order( unit_iterator->second, moving_unit_iterator->second, moving_unit_iterator->first ); order_unit_to_move = false; } else { log_debug("Destination not accepted"); // This is not an acceptable selection selection_is_ok = false; // Make sure it isn't selected again destination_map.erase( destination_iterator ); } } } if ( ( selection_is_ok ) && ( order_unit_to_move ) ) { log_debug("Ordered to move"); m_map_and_units->set_move_order( unit_iterator->second, destination_iterator->second ); moving_unit_map[ destination_iterator->second.province_index ] = unit_iterator->second; } } } while ( selection_is_ok == false ); // Unit is now ordered, so delete it from the random unit map m_random_unit_map.erase( unit_iterator ); // Move onto the next one unit_iterator = m_random_unit_map.begin(); } check_for_wasted_holds( moving_unit_map ); }
void DumbBot::generate_build_orders( int build_count ) { DESTINATION_MAP build_map; MapAndUnits::PROVINCE_SET::iterator home_centre_iterator; MapAndUnits::PROVINCE_COASTS *province_coasts; MapAndUnits::PROVINCE_COASTS::iterator build_coast_iterator; MapAndUnits::COAST_ID coast_id; int builds_remaining = build_count; DESTINATION_MAP::iterator build_iterator; DESTINATION_MAP::iterator next_build_iterator; bool try_next_build; WEIGHTING next_build_chance; int build_province; log_debug("%s %d", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->current_season ].c_str(), m_map_and_units->current_year ); // Put all the coasts of all the home centres into a map for ( home_centre_iterator = m_map_and_units->open_home_centres.begin(); home_centre_iterator != m_map_and_units->open_home_centres.end(); home_centre_iterator++ ) { province_coasts = &( m_map_and_units->game_map[ *home_centre_iterator ].coast_info ); for ( build_coast_iterator = province_coasts->begin(); build_coast_iterator != province_coasts->end(); build_coast_iterator++ ) { coast_id.province_index = *home_centre_iterator; coast_id.coast_token = build_coast_iterator->first; build_map.insert( DESTINATION_MAP::value_type( m_destination_value[ coast_id ], coast_id ) ); } } // For each build, while we have a vacant home centre while ( ( build_map.empty() == false ) && ( builds_remaining > 0 ) ) { // Select the best location build_iterator = build_map.begin(); log_debug("Build selected : %s %s", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ build_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ build_iterator->second.coast_token ].c_str() ); try_next_build = true; // Consider the next location while ( try_next_build ) { next_build_iterator = build_iterator; next_build_iterator++; if ( next_build_iterator == build_map.end() ) { // There isn't one, so stick with this one try_next_build = false; } else { // Determine randomly if ( build_iterator->first == 0 ) { next_build_chance = 0; } else { next_build_chance = ( ( build_iterator->first - next_build_iterator->first ) * m_alternative_difference_modifier ) / build_iterator->first; } if ( ( rand_no( 100 ) < m_play_alternative ) && ( rand_no( 100 ) >= next_build_chance ) ) { // Try the next one build_iterator = next_build_iterator; log_debug("New build selected : %s %s (%d%% chance)", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ build_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ build_iterator->second.coast_token ].c_str(), m_play_alternative - ( next_build_chance * m_play_alternative / 100 ) ); } else { // Stick with this one try_next_build = false; } } } // Order the build m_map_and_units->set_build_order( build_iterator->second ); build_province = build_iterator->second.province_index; build_iterator = build_map.begin(); // Remove all other build options for this province while ( build_iterator != build_map.end() ) { if ( build_iterator->second.province_index == build_province ) { build_iterator = build_map.erase( build_iterator ); } else { build_iterator++; } } builds_remaining--; } // If not all builds ordered, order some waives. if ( builds_remaining > 0 ) { log_debug("Waiving %d builds", builds_remaining ); m_map_and_units->set_total_number_of_waive_orders( builds_remaining ); } }
void DumbBot::generate_remove_orders( int remove_count ) { MapAndUnits::UNIT_SET::iterator unit_iterator; DESTINATION_MAP remove_map; MapAndUnits::UNIT_AND_ORDER *unit; int remove_counter; DESTINATION_MAP::reverse_iterator remove_iterator; DESTINATION_MAP::iterator remove_forward_iterator; DESTINATION_MAP::reverse_iterator next_remove_iterator; bool try_next_remove; WEIGHTING next_remove_chance; // Put all the units into a removal map, ordered by value of their location for ( unit_iterator = m_map_and_units->our_units.begin(); unit_iterator != m_map_and_units->our_units.end(); unit_iterator++ ) { unit = &( m_map_and_units->units[ *unit_iterator ] ); remove_map[ m_destination_value[ unit->coast_id ] ] = unit->coast_id; } // For each required removal for ( remove_counter = 0; remove_counter < remove_count; remove_counter++ ) { // Start with the best option (which is the end of the list) remove_iterator = remove_map.rbegin(); log_debug("Removal selected : %s %s", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ remove_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ remove_iterator->second.coast_token ].c_str() ); try_next_remove = true; // Determine whether to try the next option while ( try_next_remove ) { next_remove_iterator = remove_iterator; next_remove_iterator++; // No next option - so don't try it. if ( next_remove_iterator == remove_map.rend() ) { try_next_remove = false; } else { // Decide randomly if ( remove_iterator->first == 0 ) { next_remove_chance = 0; } else { next_remove_chance = ( ( remove_iterator->first - next_remove_iterator->first ) * m_alternative_difference_modifier ) / remove_iterator->first; } if ( ( rand_no( 100 ) < m_play_alternative ) && ( rand_no( 100 ) >= next_remove_chance ) ) { // Try the next one remove_iterator = next_remove_iterator; log_debug("New removal selected : %s %s (%d%% chance)", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ remove_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ remove_iterator->second.coast_token ].c_str(), m_play_alternative - ( next_remove_chance * m_play_alternative / 100 ) ); } else { // Stick with this one try_next_remove = false; } } } // Order the removal m_map_and_units->set_remove_order( remove_iterator->second.province_index ); remove_forward_iterator = remove_iterator.base(); remove_forward_iterator--; remove_map.erase( remove_forward_iterator ); } }
void DumbBot::generate_retreat_orders( void ) { RANDOM_UNIT_MAP::iterator unit_iterator; DESTINATION_MAP destination_map; MapAndUnits::UNIT_AND_ORDER *unit; MapAndUnits::COAST_SET::iterator coast_iterator; DESTINATION_MAP::iterator destination_iterator; DESTINATION_MAP::iterator next_destination_iterator; bool try_next_province; WEIGHTING next_province_chance; bool selection_is_ok; MOVING_UNIT_MAP moving_unit_map; MOVING_UNIT_MAP::iterator moving_unit_iterator; // Put the units into a list in random order generate_random_unit_list( m_map_and_units->our_dislodged_units ); unit_iterator = m_random_unit_map.begin(); log_debug("%s %d", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->current_season ].c_str(), m_map_and_units->current_year ); while ( unit_iterator != m_random_unit_map.end() ) { destination_map.clear(); unit = &( m_map_and_units->dislodged_units[ unit_iterator->second ] ); log_debug("Selecting destination for %s", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ unit->coast_id.province_index ].province_token ].c_str() ); // Put all the adjacent coasts into the destination map for ( coast_iterator = unit->retreat_options.begin(); coast_iterator != unit->retreat_options.end(); coast_iterator++ ) { destination_map.insert( DESTINATION_MAP::value_type( m_destination_value[ *coast_iterator ], *coast_iterator ) ); } do { // Pick a destination destination_iterator = destination_map.begin(); if ( destination_iterator == destination_map.end() ) { // No retreat possible. Disband unit m_map_and_units->set_disband_order( unit_iterator->second ); selection_is_ok = true; log_debug("Disbanding unit"); } else { log_debug("Destination selected : %s %s", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ destination_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ destination_iterator->second.coast_token ].c_str() ); // Determine whether to try the next option instead try_next_province = true; while ( try_next_province == true ) { next_destination_iterator = destination_iterator; next_destination_iterator++; // If there is no next option, don't try it... if ( next_destination_iterator == destination_map.end() ) { try_next_province = false; } else { // Randomly decide whether to move on if ( destination_iterator->first == 0 ) { next_province_chance = 0; } else { next_province_chance = ( ( destination_iterator->first - next_destination_iterator->first ) * m_alternative_difference_modifier ) / destination_iterator->first; } if ( ( rand_no( 100 ) < m_play_alternative ) && ( rand_no( 100 ) >= next_province_chance ) ) { // Move on destination_iterator = next_destination_iterator; log_debug("New destination selected : %s %s (%d%% chance)", TokenTextMap::instance()->m_token_to_text_map[ m_map_and_units->game_map[ destination_iterator->second.province_index ].province_token ].c_str(), TokenTextMap::instance()->m_token_to_text_map[ destination_iterator->second.coast_token ].c_str(), m_play_alternative - ( next_province_chance * m_play_alternative / 100 ) ); } else { // Stick with this province try_next_province = false; } } } // Check whether this is a reasonable selection selection_is_ok = true; // If there is a unit already moving to this province moving_unit_iterator = moving_unit_map.find( destination_iterator->second.province_index ); if ( moving_unit_iterator != moving_unit_map.end() ) { log_debug("Unit already moving here"); // This is not an acceptable selection selection_is_ok = false; // Make sure it isn't selected again destination_map.erase( destination_iterator ); } if ( selection_is_ok ) { log_debug("Ordered to RETREAT"); // Order the retreat m_map_and_units->set_retreat_order( unit_iterator->second, destination_iterator->second ); moving_unit_map[ destination_iterator->second.province_index ] = unit_iterator->second; } } } while ( selection_is_ok == false ); // Unit is now ordered, so delete it from the random unit map m_random_unit_map.erase( unit_iterator ); // Move onto the next one unit_iterator = m_random_unit_map.begin(); } }