Example #1
0
double simulatedAnnealing(char *key, char *ciphertext, char *plaintext, int messageLen) {
	int count, iter;
	float annealStep;
	char newKey[26], oldKey[26];
	double prob, delta, maxScore, score, bestScore;
	
	// Copy over key so we don't screw up our master copy. Decipher ciphertext using key and score it
	strcpy(oldKey,key);
	decipher(oldKey, ciphertext, plaintext, messageLen);
	maxScore = scoreText(plaintext,messageLen);
	bestScore = maxScore;
	iter = 0;

	// For each step, find our best key
	for (annealStep = ANNEALING_TEMP; annealStep >= 0; annealStep -= ANNEALING_STEP_SIZE) {
		for (count = 0; count < MAX_ITERATIONS; count++) { 
			strcpy(newKey, oldKey);
			alterKey(newKey);
			decipher(newKey, ciphertext, plaintext, messageLen);
			score = scoreText(plaintext, messageLen);
			// our difference between our current maxScore and step score
			delta = score - maxScore;
			// We did work in the positive direction (hopefully...)
			if (delta >= 0) {
				maxScore = score;
				strcpy(oldKey, newKey);
			} else if (annealStep > 0) {
				// the work we did is a side-grade 
				prob = exp(delta / annealStep);
				if (prob > 1.0 * rand() / RAND_MAX) {
					maxScore = score;
					strcpy(oldKey, newKey);				
				}
			}
			// This is our best score so far
			if (maxScore > bestScore){
				bestScore = maxScore;
				strcpy(key, oldKey);
				outputStats(iter, bestScore, key);
			} 
			iter++;
		}
	}
	
	return bestScore;
}
Example #2
0
void
GameState::printScore()
{
	SDL_Color color = { 255, 255, 255, 255 };
	std::string scoreText("Score: " + std::to_string(score_));
	SDL_Rect destination;
	destination.x = 20;
	destination.y = 20;
	renderText(color,destination,scoreText);
}
Example #3
0
void OrganismCellWorld::render(float alpha, sf::RenderTarget& renderTarget)
{
    CellWorld<OrganismCell>::render(alpha, renderTarget);

    sf::Sprite posCursorSpr(posCursorTex);
    sf::Sprite negCursorSpr(negCursorTex);

    posCursorSpr.setOrigin(3.f, 3.f);
    negCursorSpr.setOrigin(3.f, 3.f);

    posCursorSpr.setPosition(
        floor(posCursor.x),
        floor(posCursor.y));
    negCursorSpr.setPosition(
        floor(negCursor.x),
        floor(negCursor.y));

    renderTarget.draw(posCursorSpr);
    renderTarget.draw(negCursorSpr);

    sf::Sprite foodHighSpr(foodHighTex);
    sf::Sprite foodLowSpr(foodLowTex);
    foodHighSpr.setOrigin(3.f, 3.f);
    foodLowSpr.setOrigin(3.f, 3.f);
    for (auto& foodObj : food)
    {
        if (foodObj.ticksLeft > FOOD_TICKS / 2)
        {
            foodHighSpr.setPosition(static_cast<sf::Vector2f>(foodObj.getPos()));
            renderTarget.draw(foodHighSpr);
        }
        else
        {
            foodLowSpr.setPosition(static_cast<sf::Vector2f>(foodObj.getPos()));
            renderTarget.draw(foodLowSpr);
        }
    }

    sf::Sprite vortSpr(vortexTex);
    vortSpr.setOrigin(3.f, 3.f);
    for (auto& vortObj : vortices)
    {
        vortSpr.setPosition(static_cast<sf::Vector2f>(vortObj.getPos()));
        renderTarget.draw(vortSpr);
    }

    bool gameOver = (score >= scoreMax) || aliveCount < 200;

    if (!gameOver)
    {
        sf::Text scoreText(std::to_string(score) + "/" + std::to_string(scoreMax) +  " EATEN", scoreFont);
        scoreText.setCharacterSize(8);
        scoreText.setPosition(1.f, -2.f);
        renderTarget.draw(scoreText);
    }
    else
    {
        sf::Text winText;
        winText.setFont(scoreFont);
        winText.setCharacterSize(8);

        if (score >= scoreMax)
        {
            winText.setString("CANCER WINS!");
        }
        else
        {
            winText.setString("ANTI-CANCER WINS!");
        }

        winText.setOrigin(floor(winText.getLocalBounds().width * 0.5f), floor(winText.getLocalBounds().height * 0.5f));
        winText.setPosition(width * 0.5f, height * 0.5f);
        renderTarget.draw(winText);
    }
}