/* This function will ask the user to enter 4 guesses. Return Value : The count of the correct guesses compared to the 4 general variables (rand1, rand2, rand3 and rand4). */ int tahmin_gir(){ printf("tahminleriniz 4 sayi olarak giriniz :\n"); int n1,n2,n3,n4; scanf("%d%d%d%d",&n1,&n2,&n3,&n4); int retVal=0; retVal+=checkGuess(n1); retVal+=checkGuess(n2); retVal+=checkGuess(n3); retVal+=checkGuess(n4); return retVal; }
/*********************************************************** synopsis: handle the keyboard events BACKSPACE & ESCAPE - clear letters RETURN - check guess SPACE - shuffle a-z - select the first instance of that letter in the shuffle box and move to the answer box inputs: event - the key that has been pressed node - the top of the answers list letters - the letter sprites outputs: n/a ***********************************************************/ static void handleKeyboardEvent(SDL_Event *event, struct node* head, struct sprite** letters) { struct sprite* current = *letters; char keyedLetter; keyedLetter = event->key.keysym.sym; if (!gamePaused){ switch(keyedLetter){ case SDLK_BACKSPACE: case SDLK_ESCAPE: /* clear has been pressed */ clearGuess = 1; break; case SDLK_RETURN: /* enter has been pressed */ checkGuess(answer, head); break; case ' ': /* shuffle has been pressed */ shuffleRemaining = 1; Mix_PlayChannel(-1, getSound("shuffle"),0); break; default: /* loop round until we find the first instance of the * selected letter in SHUFFLE */ while (current!=NULL&¤t->box!=CONTROLS){ if (current->box == SHUFFLE){ if (current->letter == keyedLetter){ current->toX = nextBlankPosition(ANSWER, ¤t->index); current->toY = ANSWER_BOX_Y; current->box = ANSWER; Mix_PlayChannel(-1, getSound("click-shuffle"), 0); break; } } current=current->next; } } } }
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; }
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); }
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
/************************************************ 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
int main() { //Variable to hold number of games const int games(10); //Variables used in the above functions int answer(0), count(0), guess(0), lowest(INT_MAX), highest(0); //The game will repeat 10 times for(int n = 0; n < games; n++){ //Reset variables for next game answer = count = guess = 0; //string to validate user input std::string input(""); //Set secret number for game secretNum(answer); //Continues until user guesses correct answer while(!checkGuess(guess, answer)){ //validate user input and make sure cin is emptied while(true){ std::cout << "Guess a number between 100 and 999." << std::endl; std::getline(std::cin,input); std::stringstream inputSS(input); if((inputSS >> guess) && (guess > 99) && (guess < 1000)){ break; } std::cout << "You've entered invalid input. Enter an integer between 100 - 999.\n"; } count++; if(checkGuess(guess, answer)){ countResponse(count); } else{ guessResponse(guess, answer); } } //Track the highest and lowest number of guesses lowest = (lowest > count) ? count : lowest; highest = (highest < count) ? count : highest; if(count == 1){ std::cout << "You were able to guess the secret number after " << count << " try." << std::endl; } else{ std::cout << "You were able to guess the secret number in " << count << " tries." << std::endl; } } std::cout << "Highest count of guesses to find the secret number: " << highest << std::endl; std::cout << "Lowest count of guesses to find the secret number: " << lowest << std::endl; return 0; }
int checkAnswer(char* choice, int i, int j){ if(strncmp(choice,"Q", 1) != 0){ return checkGuess(choice, i, j); } return 0; }
/*********************************************************** synopsis: checks where the mouse click occurred - if it's in a defined hotspot then perform the appropriate action Hotspot Action ----------------------------------------------------- A letter set the new x,y of the letter and play the appropriate sound ClearGuess set the clearGuess flag checkGuess pass the current answer to the checkGuess routine solvePuzzle set the solvePuzzle flag shuffle set the shuffle flag and play the appropriate sound newGame set the newGame flag quitGame set the quitGame flag inputs: button - mouse button that has ben clicked x, y - the x,y coords of the mouse screen - the SDL_Surface to display the image head - pointer to the top of the answers list letters - pointer to the letters sprites outputs: n/a ***********************************************************/ static void clickDetect(int button, int x, int y, SDL_Surface *screen, struct node* head, struct sprite** letters) { struct sprite* current = *letters; if (!gamePaused) { while (current!=NULL&¤t->box!=CONTROLS){ if (x>= current->x && x<= current->x+current->w && y>= current->y && y<=current->y + current->h){ if (current->box == SHUFFLE){ current->toX = nextBlankPosition(ANSWER, ¤t->index); current->toY = ANSWER_BOX_Y; current->box = ANSWER; Mix_PlayChannel(-1, getSound("click-shuffle"), 0); } else{ current->toX = nextBlankPosition(SHUFFLE, ¤t->index); current->toY = SHUFFLE_BOX_Y; current->box = SHUFFLE; Mix_PlayChannel(-1, getSound("click-answer"), 0); } break; } current=current->next; } if (IsInside(hotbox[BoxClear], x, y)) { /* clear has been pressed */ clearGuess = 1; } /* check the other hotspots */ if (IsInside(hotbox[BoxEnter], x, y)) { /* enter has been pressed */ checkGuess(answer, head); } if (IsInside(hotbox[BoxSolve], x, y)) { /* solve has been pressed */ solvePuzzle = 1; } if (IsInside(hotbox[BoxShuffle], x, y)) { /* shuffle has been pressed */ shuffleRemaining = 1; Mix_PlayChannel(-1, getSound("shuffle"),0); } } if (IsInside(hotbox[BoxNew], x, y)) { /* new has been pressed */ startNewGame = 1; } if (IsInside(hotbox[BoxQuit], x, y)) { /* new has been pressed */ quitGame = 1; } }