Пример #1
0
/**
 * Menu to manage a planet
 * @param pl Pointer to the planet to manage
 * @return Returns 0 if planet pointer should be NULL 1 otherwise
 **/
int planetMenu( pPlanet pl )
{
	int choice = 0;
	char selection[30];
	size_t length = 0;
	pCountry pc = NULL;

	if ( pl == NULL ) {
		return 0;
	}

	while ( 1 ) {
		bzero(selection, 30);

		printf("\nPlanet: @s\n", pl->name);
		printf("1) Display planet information\n");
		printf("2) Set Period\n");
		printf("3) Set Orbit Speed\n");
		printf("4) Set Aphelion\n");
		printf("5) Set Perihelion\n");
		printf("6) Set Mean Radius\n");
		printf("7) Set Equatorial Radius\n");
		printf("8) Set Mass\n");
		printf("9) Set Gravity\n");
		printf("10) Set population\n");
		printf("11) Add Country\n");
		printf("12) Select country\n");
		printf("13) Delete Planet and exit menu\n");
		printf("14) Exit menu\n");
		printf("Selection: ");

		receive_until(selection, '\n', 3);
		choice = atoi( selection );

		switch ( choice ) {
			case 1:
				printPlanetInfo( pl );
				break;
			case 2:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->period = atof(selection);
				break;
			case 3:
				printf("\n-> ");
				receive_until(selection, '\n', 10);
				pl->orbitspeed = atof(selection);
				break;
			case 4:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->aphelion = atof(selection);
				break;
			case 5:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->perihelion = atof(selection);
				break;
			case 6:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->radius = atof(selection);
				break;
			case 7:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->eradius = atof(selection);
				break;
			case 8:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->mass = atof(selection);
				break;
			case 9:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->gravity = atof(selection);
				break;
			case 10:
				printf("\n-> ");
				receive_until( selection, '\n', 10);
				pl->population = atoi(selection);
				break;
			case 11:
				if ( pl->country_count >= COUNTRYMAX ) {
					printf("Too many countries\n");
					continue;
				}

				printf("\nNew Name: ");
				receive_until(selection, '\n', 19);

				choice = 0;
				while ( choice < COUNTRYMAX ) {
					if ( pl->countries[choice] == NULL ) {
						break;
					}
					choice++;
				}

				if ( choice == COUNTRYMAX ) {
					printf("!!No country slots\n");
					continue;
				}

				if ( allocate( sizeof(Country), 0, (void**)(&pc) ) != 0 ) {
					printf("!!Failed to allocate structure\n");
					continue;
				}

				initCountry( pc );

				pl->countries[choice] = pc;

				length = 0;
	
				while ( isalnum( selection[length] ) ) {
					pc->name[length] = selection[length];
					length++;
				}
				pc->name[length] = '\x00';

				pl->country_count++;
				break;
			case 12:
				printf("\nCountries:\n");
				for ( choice = 0; choice < pl->country_count; choice++ ) {
					if ( pl->countries[choice] != NULL ) {
						printf("@d) @s\n", choice + 1, pl->countries[choice]);
					}
				}

				bzero(selection, 30);
				printf("\n-> ");
				receive_until( selection, '\n', 3 );

				choice = atoi(selection);

				if ( choice < 1 || choice > COUNTRYMAX) {
					printf("Invalid choice...\n");
					continue;
				} else if ( pl->countries[ choice-1] == NULL ) {
					printf("Invalid choice...\n");
					continue;
				}

				if ( countryMenu( pl->countries[choice-1]) == 0 ) {
					pl->countries[choice-1] = NULL;
					pl->country_count--;
				}
				break;
			case 13:
				freePlanet( pl );
				return 0;
				break;
			case 14:
				return 1;
				break;
			default:
				printf("Invalid...\n");
				break;
		};
	}

end:
	return 1;
}
Пример #2
0
int main() {

    FILE *in = fopen("input.dat", "r");

    Country* nk = (Country*) malloc(sizeof(Country));
    initCountry(nk, NULL);

    int nkSize;
    fscanf(in, "%d", &nkSize);

    int i, unitStrength;
    for (i = 0; i < nkSize; i++) {
        fscanf(in, "%d ", &unitStrength);
        addUnit(nk, unitStrength);
    }

    int countryNo;
    fscanf(in, "%d\n", &countryNo);
    Country* allies = (Country*) malloc(countryNo * sizeof(Country));

    char line[200];
    char *tok, *newName;

    int maxArmySize = -1, minArmySize = 2147e6;
    Country *strongestCountry = NULL, *weakestCountry = NULL;
    Country* canWinAlone = NULL;

    for (i = 0; i < countryNo; i++) {
        fgets(line, 200, in);

        tok = strtok(line, " ");

        // country name + initialization

        newName = (char*) malloc (strlen(tok) * sizeof(char));
        strcpy(newName, tok);
        initCountry(allies+i, newName);

        // unit assignments
        tok = strtok(NULL, " \n");

        while (tok != NULL) {
            addUnit(allies+i, atoi(tok));
            tok = strtok(NULL, " \n");
        }

        if ((allies+i)->armySize > maxArmySize) {
            maxArmySize =  (allies+i)->armySize;
            strongestCountry = allies+i;
        }

        if ((allies+i)->armySize < minArmySize) {
            minArmySize = (allies+i) -> armySize;
            weakestCountry = allies+i;
        }

        // strictly considering the sentinels
        // without Kim's 1 HP
        if ((allies+i)->armySize >= nk->armySize) {
            canWinAlone = allies+i;
        }
        //printUnits(allies+i);

        // simulate one on one battle
    }
    if (canWinAlone == NULL) {
        oneOnOne(strongestCountry, nk);
    }

    //printUnits(allies);
    // attack simulation
    int noMoreArmies = 0;
    char result = '0';

    while (!noMoreArmies && result != 'w') {
        noMoreArmies = 1;

        // suppose there are no more armies left
        for (i = 0; i < countryNo; i++) {

            if ((allies + i)->army->first != NULL) {
                noMoreArmies = 0;

                // only Kim remains
                // army i deals the winning blow just to kim
                // no sentinels were left
                if (result == 'K') {
                    result = 'w';
                    break;
                }

                result = fightBattle(allies + i, nk);

                // army i dealt the winning blow to the sentinel and kim
                if (result == 'w') {
                    break;
                }
            }
        }
    }

    FILE *out = fopen("output.dat", "w");

    if (noMoreArmies) {
        fprintf(out, "The allies have lost!");
    } else {
        fprintf(out, "The tyrant was killed!\n");
        fprintf(out, "The last hit was done by: %s.\n", (allies+i)->name);
        fprintf(out, "The strongest country was: %s.\n", strongestCountry->name);
        fprintf(out, "The weakest country was: %s.\n", weakestCountry->name);
        if(canWinAlone != NULL) {
            // just the sentinels, not necessarily including Kim
            fprintf(out, "%s could have defeated all the sentinels.\n", canWinAlone->name);
        } else {
            fprintf(out, "No country could have defeated all the sentinels.\n");
            fprintf(out, "%s could have brought down the first %d sentinels ",
                   strongestCountry->name,
                   strongestCountry->lastDefeatedSentinel);
            fprintf(out, "and would have chipped off %d points from sentinel %d.\n",
                   strongestCountry->lastDmg,
                   strongestCountry->lastDefeatedSentinel + 1);
        }

    }

    //printf("%s\n", strongestCountry->name);
    //printf("%s\n", weakestCountry->name);


    fclose(in);
    return 0;
}