Example #1
0
int main(void)
{
	srand(time(NULL));

dash:
	getWord();
	if( (strstr(word, "-")) != NULL)
	{
		goto dash;
	} 

	initializeBlanks();

	while( ((strstr(blanks, "_")) != NULL) && (guesses > 0))
	{
		printf("Guesses so far: %s\n", attempts);
		printf("%d guess%s left.\n", guesses, (guesses > 1) ? "es" : "");
		printf("%s\n", blanks);
		getGuess();
		printf("\n");
	}

	if(guesses == 0)
	{
		printf("Too many misses. Game over. The word was %s.\n", word);
		return 0;
	}
	if( (strstr(blanks, "_")) == NULL)
	{
		printf("%s\n", word);
		printf("You won!\n");
	}
}
void ClosePointDitherGenerator::putColor(int x,int y,double r,double g,double b){
    double oldR=r+(r*ditherMap[x%4][y%4]);
    double oldG=g+(g*ditherMap[x%4][y%4]);
    double oldB=b+(b*ditherMap[x%4][y%4]);
    ColorData c=getGuess(oldR,oldG,oldB);
    printf("\033[%i;%im%s",c.fg,
                           c.bg,
                           diether[(int)c.diether]);
}
void ClosePointGenerator::generateColors(){
    diether[0]=" ";
    diether[1]="/";
    diether[2]="%";
    diether[3]="#";
    diether[0]=" ";
    diether[1]="\u2591";
    diether[2]="\u2592";
    diether[3]="\u2593";
    initColorPoints();
    for(int r=0;r<16;r++)
    for(int g=0;g<16;g++)
    for(int b=0;b<16;b++){
        colordata[r][g][b]=getGuess(r/15.0,g/15.0,b/15.0);
    }
}
void DirectCollocationInternal::evaluate(int nfdir, int nadir){
  // get NLP variable bounds and initial guess
  getGuess(nlp_solver_.input(NLP_X_INIT).data());
  getVariableBounds(nlp_solver_.input(NLP_LBX).data(),nlp_solver_.input(NLP_UBX).data());
       
  // get NLP constraint bounds
  getConstraintBounds(nlp_solver_.input(NLP_LBG).data(), nlp_solver_.input(NLP_UBG).data());
       
  //Solve the problem
  nlp_solver_.solve();
  
  // Save the optimal solution
  setOptimalSolution(nlp_solver_.output(NLP_X_OPT).data());

  // Save the optimal cost
  output(OCP_COST).set(nlp_solver_.output(NLP_COST));
}
void DirectMultipleShootingInternal::evaluate(){
  // get NLP variable bounds and initial guess
  getGuess(nlp_solver_.input(NLP_SOLVER_X0).data());
  getVariableBounds(nlp_solver_.input(NLP_SOLVER_LBX).data(),nlp_solver_.input(NLP_SOLVER_UBX).data());
       
  // get NLP constraint bounds
  getConstraintBounds(nlp_solver_.input(NLP_SOLVER_LBG).data(), nlp_solver_.input(NLP_SOLVER_UBG).data());
       
  //Solve the problem
  nlp_solver_.solve();
  
  // Save the optimal solution
  setOptimalSolution(nlp_solver_.output(NLP_SOLVER_X).data());

  // Save the optimal cost
  output(OCP_COST).set(nlp_solver_.output(NLP_SOLVER_F));
}
Example #6
0
int main() {
    
    int key[SIZE];
    int guess[SIZE];
    
    srand(time(0));
    
    // Set up the game.
    fillKey(key, SIZE);    
    int correct = 0, newCorrect;
    
    while (1) {
    
        getGuess(guess, SIZE);
        newCorrect = numCorrect(guess, key, SIZE);
        
        // No new correct digits were discovered, you lose.
        if (newCorrect <= correct) {
        
            printf("Sorry, you did not improve the number of correct digits.\n");
            printf("You lose.\n");
            printKey(key, SIZE);
            break;              
                       
        }
        
        // Print out which digits were correct.
        else {
             
            printNumCorrect(guess, key, SIZE);   
            correct = newCorrect;  
        }
        
        // Winning case!
        if (correct == SIZE) {
            printf("Congratulations, you won the car!\n");
            break;
        }
    
    }
    
    system("PAUSE");
    return 0;
}
Example #7
0
int main() {
	int goal, guess, numberOfGuesses;

	// Generate random integer between 1 and 100 inclusively.
	srand(time(NULL));
	goal = rand() % 100 + 1;

	numberOfGuesses = 0;
	guess = 0;


	printf("I have thought of a number, 1-100.\n");

	/*
	   Main program loop.
	   - Increment numberofGuesses and accept a guess.
	   - Check that value is in the correct range. If not, start over.
	   - Check valid guess and print appropriate message with checkGuess.
	   - End loop and allow progrtam to exit if user guesses goal.
	*/
	while (1)
	{

		numberOfGuesses++;	
		guess = getGuess();

		if (guess < 1 || guess > 100)
		{
			printf("%d is not 1-100.\n", guess);
			continue;
		}

		// checkGuess returns true (1) if guess == goal and false (0) otherwise.
		if(checkGuess(goal, guess, numberOfGuesses))
			break;
	}

	return 0;
}
Example #8
0
int main(void){
	srand(time(NULL));	
	char secretNum[MAXSIZE];
	char theGuess[MAXSIZE];
	int temp = rand() % TOTAL;
	asChars(secretNum, temp);
	int numGuess=0;
	int* remaining = malloc(TOTAL * sizeof(int));
	int i;
	for(i =0;i < TOTAL; i++){
		remaining[i] = 1;
	}
	while(true){
		if( numGuess == 0){
                                fprintf(stdout, "Suggested Guess: 1122\n");
                        }
		while(getGuess(theGuess)){
			//if( numGuess == 0){
			//	fprintf(stdout, "Suggested Guess: 1122\n");
			//}
			fprintf(stdout, "Input %d digits\n", MAXSIZE-1);
		}
		numGuess++;
		int result = checkNums(secretNum, theGuess);
		printResult(result);		
		if(result == (OUTCOMES -1)){
			if( numGuess == 1){
				fprintf(stdout, "You win!  It took you %d guess.\n", numGuess);
			}
			else {
				fprintf(stdout, "You win!  It took you %d guesses.\n", numGuess);
			}
			break;
		}
		stillValid(remaining, theGuess, result);
		playSelf(theGuess, remaining);
	}	
	free(remaining);	
}
Example #9
0
int main (int argc, char* argv[]){
	char theSecret[MAXSIZE], theGuess[MAXSIZE], knownStr[MAXSIZE], filePath[MAXFILE], statsPath[MAXFILE], theStats[MAXFILE];

	int numGuess = 0;
	FILE *filePointer = NULL, *statsPointer = NULL;
	getFilePath(filePath, argc == 2 ? argv[1] : ".words");
	filePointer = fopen(filePath, "r");
	int games, wins, losses;
	float average;
	getFilePath(statsPath, ".hangman");
	statsPointer = fopen(statsPath, "a+");
	getStats(statsPointer, theStats);
	char* remainder;
	games = strtol(theStats, &remainder,10);
	strcpy(theStats, remainder);
	wins = strtol(theStats, &remainder,10);
	strcpy(theStats, remainder);
	losses = games - wins;
	average = strtof(theStats, &remainder);
	char winString[5], losString[7];
	wins == 1 ? strcpy(winString, "Win") : strcpy(winString, "Wins");
	losses == 1 ? strcpy(losString, "Loss") : strcpy(losString, "Losses");
	printf("Game %d  %s: %d %s: %d Average:%.1f\n", games, winString, wins, losString, losses, average); 
	if (!filePointer || !statsPointer){
                printf("ERROR: file does not exist in home directory\n");
                exit(0);
        }
	getLine(filePointer, theSecret);
	int size=(int)strlen(theSecret)-1;
	int wordLen = size < MAXSIZE ? size : MAXSIZE;
	setTo_(knownStr, wordLen);
	while(true){
		printf("%d  %s: ",numGuess, knownStr);
		getGuess(theGuess);//{//no body to loop
		//}
		int check = checkGuess(theSecret, knownStr, theGuess);
		if(!check){
			numGuess++;
		}else if (check == 1){
			char misses[7];
			numGuess == 1 ? strcpy(misses, "miss") : strcpy(misses, "misses");
			printf("   %s\n", theSecret);
			printf("You win! You had %d %s.\n", numGuess, misses);
			wins++;
			games++;
			average = (average*(games-1) + numGuess)/games;
			break;
		}
		printGallows(numGuess);
		if( numGuess == 6){
			printf("  %s\n", theSecret);
			printf("You lose\n");
			losses++;
			games++;
			average = (average*(games-1) + numGuess)/games;
			break;
		}
	}
	fprintf(statsPointer, "\n%d %d %f", games, wins, average);
	fclose(filePointer);
	fclose(statsPointer);
}
Example #10
0
int main( int argc, char **argv )
{
	/*	Set up the code for debugging.
		This allows the user to see the code.
	*/
	int newCode[CODELENGTH];
	genCode(newCode);

	/* If there's 2 inputs, then there's a flag. */
	if( argc == 2 )
	{
		int debugMode;

		printf( "Debugging Information.\n"
				"The code is: " );

		/* Display the code. */
		for( debugMode=0; debugMode<CODELENGTH; debugMode++ )
		{
			printf( " %d", newCode[debugMode]);
		}

	}

	/*	Get user guesses.
		Display an error message if the user inputs a value
		that's not between 1-6.
	*/
	printf( "\nGuess the code by entering 4 numbers between 1-6.\n"
			"	Example: 5 3 2 6\n"
			"You may use repeating variables if you wish.\n");

	int newGuess[CODELENGTH];
	getGuess(newGuess);

	/* Check validity of guess. */
	int g, state;
	state = FALSE;

	for( g=0; g<CODELENGTH; g++ )
	{
		if( newGuess[g] > 1 && newGuess[g] < 7 )
		{
			state = TRUE;
		}
		else
		{
			state = FALSE;
		}
	}

	/*	If the guess is valid, then continue.
		If invalid, prompt error message.
		Otherwise, pass the code and guess to checkGuess for comparison.
	*/
	if( state == FALSE )
	{
		printf( "Invalid guess! Your guess must be a number between 1-6.\n"
				"Please try again.");
	}
	else
	{
		checkGuess( newCode, newGuess );
	}

}//end main
Example #11
0
/************************************************
	Compare user guesses with code generator

 	If guess isn't correct:
 	-> provide the user feedback
 	-> allow another chance to guess.

 	If guess is correct:
 	-> congratulate
 	-> include number of guesses it took to reach correct code
 	-> quit program.
************************************************/
int checkGuess (int code[], int guess[])
{
	int x, y, match, nearMatch, numGuess, newGuess[CODELENGTH];
	match = nearMatch = numGuess = 0;

	for( y=0; y<CODELENGTH; y++ )
	{
		if( code[y] == guess[y] )
		{
			match++;
		}
	}

	/*	If user successfully solves the puzzle,
		congratulate, count guesses and exit program.
	*/
	if( match == CODELENGTH )
	{
		printf( "Congratulations!\n"
		 		"It has taken you %d guesses to solve the code.", numGuess);

		exit(-1);

	}//end if (match...

	else
	{
		for( x=0; x<CODELENGTH; x++ )
		{
			printf( "Your guess ");

			if( code[x] == guess[x] )
			{
				match++;
			}

    		/* Use a counter to find the number of near matches*/
			if( code[x] != guess[x] &&
		  	   (code[x] == guess[x + 1] ||
		    	code[x] == guess[x + 2] ||
		  		code[x] == guess[x + 3] ||
		  		code[x] == guess[x - 1] ||
		  		code[x] == guess[x - 2] ||
		  		code[x] == guess[x - 3] )
		 	  )

			{
				nearMatch++;
			}

			/*	If the user incorrectly guesses, give another chance.
				Also keep track of number of guesses
			*/
			printf( "is incorrect. Please try again!\n"
					"Match: %d\n"
					"Near match: %d\n", match, nearMatch );

			getGuess(guess);

			numGuess++;

			checkGuess( code, guess );


		}//end for( x=0...

	}//end else

}//end int checkGuess