• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

My First C game, Please critique

Newbie Spellweaver
Joined
Jul 25, 2006
Messages
26
Reaction score
0
I just started learning C starting last week Wednesday. So, I feel this is a pretty decent start eh? thanks

Code:
/* Random Guess 2-Player Version by: FoxFire. */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    
    /* Define the Variables Required. */
    int random_number;
    int player_guess;
    int player1_counter = 0;
    int player2_counter = 0;
    int player1_turn;
    int player2_turn;
    
    /*  Initialize RNG */
    srand(time(NULL));
    
    /* Generate random number */
    random_number = rand() % 100 + 1;
    
    /* Welcome Message */
    printf( "This is a 2 Player Guessing Game! Its 1 - 100! Good Luck!\n\n" );
    
    /* Game Loop */
    do {
        printf ( "Player 1, Please enter your guess: \n\n");
        scanf ("%d", &player_guess);
        getchar();
        
                  if(player_guess < 1 || player_guess > 100) {
                  printf( "Please enter a number between 1 and 100. \n\n" );
                  continue;
                  }    
            
                  /* Increment guess_counter */
                  player1_counter++;
                  
        
        {
                   /* Process Guess */
       
        if(player_guess > random_number) {
            printf( "You guessed too High.\n\n" );
            player1_turn = 1;
            }
         else if(player_guess < random_number) {
            printf( "You guessed too Low.\n\n" );
            player1_turn = 1;
            }
         else if(player_guess == random_number) {
               system("cls");
               printf( "Congratulations, Player 1 Guessed the Number, Player 1 Wins! \n\n");
               break;
               }
                     
         } while(player1_turn != 1);
               
             
               
      /*Player2 turn */
        do {
             printf( "Player 2, Please enter your guess: \n\n" );
             scanf( "%d", &player_guess);
             getchar();
             
             /* Check Input */
        
        if(player_guess > 100 || player_guess < 1) {
            printf( "Invalid Input. Try Again.\n\n" );
            continue;
            }
            
            /* Increment guess_counter */
        player2_counter++;
        
        
        /* Process Guess */
        if(player_guess > random_number) {
            printf( "You guessed too High.\n\n" );
            player2_turn = 1;
            }
         else if(player_guess < random_number) {
            printf( "You guessed too Low.\n\n" );
            player2_turn = 1;
            }
         else if(player_guess == random_number) {
               system("cls");
               printf( "Congratulations, Player 2 Guessed the Number, Player 2 Wins! \n\n");
               break;
               }
               
        } while(player2_turn != 1);  
    
              
    } while(player_guess != random_number);
    
    
    printf( "Player 1 guessed %d times.\n\n", player1_counter);
    printf( "Player 2 guessed %d times. \n\n\n\n", player2_counter);
    printf( "Press enter to Quit.");
    getchar();
}


If anyone can show me a better way to do some of the things I did, please let me know.

TheRightHandMan
 
Divine Celestial
Loyal Member
Joined
Jul 7, 2004
Messages
853
Reaction score
5
1. Fix your indenting.

2. Extraneous '{' on line 40.

3. Remove redundant checking. If a number is neither greater than or less than x, then it must be equal (if you don't believe this, think about it).

Also simplified the inner loop greatly.
Code:
/* Random Guess 2-Player Version by: FoxFire. */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    
    /* Define the Variables Required. */
    int random_number;
    int player_guess;
    int clpr=0;
    int cnt[2];

    /*  Initialize RNG */
    srand(time(0));
    
    /* Generate random number */
    random_number = rand() % 100 + 1;
    
    /* Welcome Message */
    printf( "This is a 2 Player Guessing Game! Its 1 - 100! Good Luck!\n\n" );
    
    /* Game Loop */
    for(;;) {
        printf ( "Player %d, Please enter your guess: \n\n",cplr+1);
    reinput:
        scanf ("%d\n", &player_guess);
        if(player_guess < 1 || player_guess > 100) {
         printf( "Please enter a number between 1 and 100. \n\n" );
         goto reinput;
        }    
        /* Increment guess_counter */
        cnt[clpr]++;

        /* check */
        if(player_guess == random_number) {
         printf( "Congratulations, Player %d Guessed the Number, Player %d Wins! \n\n",clpr+1,clpr+1);
         break;
        }
        printf( "You guessed too %s.\n\n",(player_guess>random_number)?"High":"Low");
        clpr ^=1; /* flip between player 1 and 2 */
    }
    clpr=1;
    do
     printf( "Player %d guessed %d times.\n\n",2-clpr,cnt[2-clpr]);
    while(clpr--);
    printf( "Press enter to Quit.");
    getchar();
}
 
Junior Spellweaver
Joined
Apr 9, 2007
Messages
101
Reaction score
1
Where Do i put this in C#?? what sort of thinng lol
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
This topic is old, just like the other 2 you replied on. The author probably forgot about the topic and thus its useless to bump this topic.
 
Newbie Spellweaver
Joined
Sep 28, 2007
Messages
13
Reaction score
0
Errm. What the hell. You posted the C++ and your asking us to judge it. We are meant to play it not judge it.

_Hacker
 
Back
Top