Exemple #1
0
int AI::betterMemory()
{
    int position = 0;
    int nextPos = 0;
    int lastPos = 0;
    bool ok = false;
    Direction lastDir = AI::UP;
    bool keepPosAndDir = false;

    do
    {
        // Check if an active ship is available
        if(shipActive())
        {
            if(!keepPosAndDir)
            {
                // Get information about the last turn
                lastPos = _alreadyTried.last();
                lastDir = lastDirection();
                keepPosAndDir = false;
            }

            // Get next position
            if(lastHit())
            {
                nextPos = positionInDirection(lastDir, lastPos);
            }
            else
            {
                if(_currentHitsOnShip >= 2)
                {
                    if(!keepPosAndDir)
                        lastDir = oppositeDirection(lastDir);
                    nextPos = positionInDirection(lastDir, _shipFirstHit);
                }
                else
                {
                    if(!keepPosAndDir)
                        lastDir = nextDirection(lastDir);
                    nextPos = positionInDirection(lastDir, _shipFirstHit);
                }
            }

            // If the position is not valid, try an other one
            if(nextPos < 0)
            {
                if(_currentHitsOnShip >= 2)
                    lastDir = oppositeDirection(lastDir);
                else
                    lastDir = nextDirection(lastDir);

                lastPos = _shipFirstHit;
                keepPosAndDir = true;
                continue;
            }

            // If position was already tried, try an other one
            if(_alreadyTried.contains(nextPos))
            {
                lastDir = nextDirection(lastDir);
                lastPos = _shipFirstHit;
                keepPosAndDir = true;
                continue;
            }
            else
            {
                ok = true;
            }

            // Set the next position
            position = nextPos;
        }
        else
        {
            // If no active ship is available, pick random position
            position = simpleMemory();
            _shipSunken = false;
            ok = true;
        }

    }while(!ok);

    _alreadyTried.append(position);
    setLastDirection(lastDir);

    return position;
}
Exemple #2
0
void readFromFile(FILE *file, FILE *fileOutput)
{
    int N;
    fscanf (file, "%d", &N);
    printf ("N=%d\n", N);

    int totalLifeOfSentinels = 0;
    int value;
    int i;
    for(i=0; i<N; i++)
    {
        fscanf(file, "%d", &value);
        totalLifeOfSentinels = totalLifeOfSentinels + value;
        addLast(value);
    }

    printf("%d\n", totalLifeOfSentinels);
    printAll(fileOutput);

    int countryNumber;
    fscanf(file, "%d", &countryNumber);
    printf ("countryNumber=%d\n", countryNumber);

    /*!*****************************************************************************
    *                             DECLARATION ZONE
    ********************************************************************************/
    char line[1000]; //for each line
    value = 0;
    int ok = 0;
    int lifeOfaCountry = 0;
    int totalLifeOfCountries = 0;
    int maxLife = 0, minLife = 9999;
    char mostPowerfulCountry[100], weakestCountry[100],  thePowerfulOfThemAll[100];
    node *strongestCountry; //
    /*!*****************************************************************************+
    *                           END OF THE DECLARATION ZONE
    ********************************************************************************/

    fgets(line, sizeof(line), file);
    int k;
    for(k=0; k<countryNumber; k++)
    {
        fgets(line, sizeof(line), file);
        char *tok = strtok(line,  " \n");
        printf("\n%s: ", tok);
        addCountry(tok);

        listOfLists *currentCountry = head; // move pointer to current country

        while (currentCountry->next != NULL)
        {
            currentCountry = currentCountry->next;
        }

        while((tok = strtok(NULL, " ")) != NULL) //I skip the counties, and tok will be first - the first value-element of China and so on
        {
            printf("%s ", tok);
            value = atoi(tok);
            lifeOfaCountry = lifeOfaCountry + value;
            addWaves(fileOutput,value,currentCountry);
        }

        totalLifeOfCountries = totalLifeOfCountries + lifeOfaCountry;

        if(maxLife<lifeOfaCountry)
        {
            maxLife=lifeOfaCountry;
            strcpy(mostPowerfulCountry,currentCountry->countryName);
            strongestCountry = currentCountry->nextWave;
        }
        if(minLife>lifeOfaCountry)
        {
            minLife=lifeOfaCountry;
            strcpy(weakestCountry,currentCountry->countryName);
        }
        if(lifeOfaCountry>totalLifeOfSentinels)
        {
            ok = 1;
            strcpy(thePowerfulOfThemAll, head->countryName);

        }
        lifeOfaCountry=0;
    }

    fprintf(fileOutput, "\nThe strongest country was: %s\n", mostPowerfulCountry);
    fprintf(fileOutput, "The weakest country was: %s\n", weakestCountry);

    if(ok==1)
    {
        fprintf(fileOutput, "%s could have defeated all the sentinels.\n",thePowerfulOfThemAll);
    }

    else
    {
        fprintf(fileOutput, "No country could have defeated all the sentinels\n");
        sentinelsDown(fileOutput, maxLife, mostPowerfulCountry);
    }

    if(totalLifeOfSentinels<totalLifeOfCountries)
    {
        fprintf(fileOutput,"The tyrant was killed!\n");
        lastHit(fileOutput, totalLifeOfSentinels );
    }

    else
    {
        fprintf(fileOutput,"The tyrant was not killed!\n");
    }
}