I couldn't really find improvements to the code that other's haven't caught, but I did notice this line:
Code:
printf("Try again?[y/n]: ");
scanf("%s", &tryAgain);
if (tryAgain == 'y') {
printf("Restarting the game!");
}
In order to implement this properly, a good method might be to have the user choice to play the game in a while loop that surrounds the game (which I would move to its own function). It's always a good idea to break unique behaviors into their own functions, it might not matter much in a small game like this, but it is vital once you get larger code going.
I wrote a little game in the same manner as the one you have (number guessing) to show you what I mean:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define MAXGUESS 7
#define MAXRAND 201
void guessing_game(void);
int compare_guess(int,int);
void nlkill(void);
int main()
{
srand(time(NULL));
char playchoice = 'b';
int numbergames = 0;
while(playchoice != 'n'){
printf("Would you like to play %s?\n\n",
(numbergames > 0) ? "again" : "");
printf("[Y] / [N] >");
if(scanf(" %c", &playchoice) != 1){
printf("Please enter numbers only\n\n");
}
playchoice = tolower(playchoice);
if(playchoice == 'y'){
guessing_game();
++numbergames;
}
if(playchoice != 'n' || playchoice != 'y'){
nlkill();
}
}
printf("Thank U 4 being such a great playa!\n\n");
return 0;
}
void nlkill(void)
{
while(getchar() != '\n');
}
void guessing_game(void)
{
int random;
int guesses = 0;
int playerguess;
random = rand()%MAXRAND+1;
while(guesses <= MAXGUESS){
printf("\nGuess a number between 1 and %d\n\n>", MAXRAND);
if(scanf(" %d", &playerguess) != 1){
printf("Please enter numbers only\n");
}
if(compare_guess(playerguess, random) == 1){
printf("Congratulations, you're a big wiener!\n\n");
return;
}
else ++guesses;
}
printf("You a LOSER!\n\n");
}
int compare_guess(int player, int random)
{
if(player < random){
printf("\nThat's the lowest of the low.\n\n");
return 0;
}
else if(player > random){
printf("\nYou are so high right now!\n\n");
return 0;
}
return 1;
}
In the main function you have the outer loop, which asks if the player wants to play. If the player is on their 2nd game or later, a ternary operator will add "again" to "Do you want to play?". You have a bit of checking the return of scanf, and also putting the player choice variable through a function called tolower() which will make it lowercase (so we can compare it). If input is invalid, I flush the input buffer with nlkill() (getchar !='\n').
Then the guessing_game() function is the body of the actual game, it's in its own function so that I can easily see where it is, and know what behaviors it should display. This function then uses the compare function to determine if the player has won.
On a final note, one thing you might want to think about (not really a coding thing), is the conceptual side of design. For instance, in a game like this, if the player uses the most efficient manner of guessing, is there a statistical likelihood they could still lose (would you want them too?). As it is, using a simple binary search, the player is almost certainly going to win.