Thread: Problem with computer speed?!

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Problem with computer speed?!

    I wrote a solution for the game of craps.
    Here is how it is played :

    Each round has two phases: "come-out" and "point". To start a round, the shooter makes one or more "come-out" rolls. A come-out roll of 2, 3 or 12 (called "craps", the shooter is said to "crap out") ends the round with players losing their "pass line" bets. A come-out roll of 7 or 11 (a "natural") results in a win for "pass line" bets. The shooter continues to make come-out rolls until he rolls 4, 5, 6, 8, 9, or 10, which number becomes the "point". The dealers then move an "On" button to the point number signifying the second phase of the round. If the shooter rolls the point number, the result is a win for bets on the pass line. If the shooter rolls a seven (a "seven-out"), the pass line loses, the don't pass wins, and the round ends.


    and here is the code :

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>
    
    //function declarations
    int roll_dice(void);
    bool play_game(void);
    
    int main (int argc, const char * argv[]) {
        // insert code here...
    	bool result;
    	
    	int wins = 0;
    	int losses = 0;
    	
    	char ch;
    	
    	
    	
    	do {
    		result = play_game();
    		if (result == true)
    		{
    			printf("\nYou win!");
    			wins++;
    			printf("\n\nPlay again?");
    			ch = getchar();
    		}
    		else
    		{
    			printf("\nYou lose!");
    			losses++;
    			printf("\n\nPlay again?");
    			ch = getchar();
    		}
    	} while (ch != 'n');
    	
    	printf("\n\nWins: %d\tLosses: %d", wins, losses);
    	
    }
    
    int roll_dice(void)
    {
    	srand((unsigned)time(NULL));
    	
    	int firstDice;
    	int secondDice;
    	
    	firstDice = rand() % 6 + 1;
    	secondDice = rand() % 6 + 1;
    	
    	return (firstDice + secondDice);
    }
    
    bool play_game(void)
    {
    	int result = roll_dice();
    	int point = 0;
    	int newDice;
    	
    	printf("You rolled: %d", result);
    	
    	if (result == 7 || result == 11) 
    	{
    		return true;
    	}
    	else if (result == 2 || result == 3 || result == 12)
    	{
    		return false;
    	}
    	else {
    		point = result;
    		printf("\nYour point is: %d", point);
    		
    		do {
    			newDice = roll_dice();
    			printf("\nYou rolled: %d", newDice);
    		}
    		while (newDice != 7 && newDice != point);
    		
    		if (newDice == 7)
    			return false;
    		else
    			return true;
    	}
    
    }
    The problem is with the point case.When I run it I always get the same roll as the previous one and the user always win.
    If I debug the program I get other rolls which is the result that I want.
    Does it have to do with the speed of the machine?
    Thank you.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    srand() once, at the start of main, and forget about it. If you reseed your RNG with the same seed over and over, you get the same sequence of numbers. This problem will only manifest if the program runs at computer-speeds, but not at user-speeds (e.g. when you're debugging).

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    Thnx a lot for your help.I am trying now to solve the problem with the newline character and the getchar() command.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    getchar() is a function, not a command.

    After you've read all the input you were interested in, you could call the following function.
    Code:
    void
    flush_stdin( void )
    {
        char ch;
        while ( ( ch = getchar() ) != '\n' && ch != EOF );
    }
    This will get rid of all characters up to the first newline or until EOF (which would mean that there are no more characters in the buffer), whichever comes first.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    Thank you msh.Your code worked!
    Many thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Major Computer Problem
    By Olidivera in forum Tech Board
    Replies: 10
    Last Post: 07-15-2005, 11:15 AM
  3. Please help with serial communication problem - Long
    By spdylude in forum Windows Programming
    Replies: 3
    Last Post: 04-06-2005, 09:41 AM
  4. Computer Problems
    By fac7 in forum Tech Board
    Replies: 13
    Last Post: 03-23-2004, 01:22 AM
  5. Ping problem
    By patricksky in forum Tech Board
    Replies: 1
    Last Post: 09-02-2002, 07:42 PM