Thread: help me in this question

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    23

    help me in this question

    Your task is to write a C program which allows a user to play the game of twenty-one against the computer. Fortunately twenty-one is a very simple game.
    In the game of twenty-one, you are given numbers until you choose to stop. Each number you are given is chosen at random from the integers between 1 and 10 inclusive. Your score is the sum of these numbers. The player with the highest score wins. However, if your score is larger than 21 you are said to bust which means you loose immediately.
    The user of your program should play first. If the user busts, he looses and the game stops there. Otherwise the computer plays next. When the computer finishes playing your program should indicate who wins.
    The computer player should follow a simple strategy. It should stop taking numbers when and only when it has a score of at least 15

    and the output like this
    Code:
    $ a.out
    Your score is: 0
    Do you want to take another number [y or n]? y
    You get the number: 9
    Your score is: 9
    Do you want to take another number [y or n]? y
    You get the number: 6
    Your score is: 15
    Do you want to take another number [y or n]? y
    You get the number: 6
    Your score is: 21
    Do you want to take another number [y or n]? n
    Now the computer plays
    The computer gets the number: 6
    The computer gets the number: 7
    The computer gets the number: 5
    The computer's score is: 18
    You win.
    THIS HOW I AM trying to do
    Code:
    #include <stdlib.h>
    int main()
    {
    	int c;
    	int l=0;
    	int m=0;
    	int i=1;
    	int y=0;
    	int n=1;
    	
    	printf("you score is &#37;i \n",l);
    	c=getchar();
    
    	while (i!=0)
    		{
    		printf("Do you want to take another number [y or n] ?");
    		scanf("%c\n",&c);
    		if (c='n')
    			break;				
    		m = rand()%10+1;
    		printf("you get the number: %i \n",m);
    		l = l + m;
    		printf("Your score is: %i\n ",l);
    		if(l>21){
    			printf("YOU bust\n");
    			y=16;
    			l=-1;
    			n=0;
    			break;
    			
    			}
    
    		}
    		
    		if(n!=0)
    			printf("Now the computer plays\n");
    
    
    			
    	
    
    	while(y<15)
    		{
    
    			m=rand()%10+1;
    			printf("the computer gets the number :%i\n",m);
    			y=y+m;
    			
    			if(y>21){
    			printf("The computer's score is:%i\n",y);
    			printf("Computer bust\n");
    			y=-1;
    			n=0;
    			break;
    			}
    			
    	}
    	if(n!=0)
    		printf("The computer's score is:%i\n",y);
    		
    		if(l>y)
    			printf("YOU WIN\n");
    		
    		else if(y>l)
    			printf("COMPUTER WIN\n");
    		else
    			printf("Draw\n");
    	return 0;
    }
    but not work in yes or no
    always break
    and some times the number repeat the same
    Last edited by thaer89; 11-05-2007 at 01:12 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    c='n'
    You probably mean "c == 'n'?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    if dont know
    i need
    when input "n"
    the while break

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you know the difference between one and two equal signs in C? If not, perhaps you should turn to that page in your book?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    i dont have a book
    please help me
    but i remmber == mean if to nmber equal

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what reference do you use when you need to look something up? Or do you rely on forums for even the most simple question?

    By the way, I'm 90% certain that the compiler would tell you that you are doing something strange here if you enable -Wall on the compile line - that will probably give you other warnings too - fix them.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    the compiler dont show my any error
    i dont know why dont work
    i think because
    i use compiler in windows

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    this last thing i can do
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    char c;
    int main()
    
    {
    
    	int m;
    
    	int l=0;
    	int i=1;
    	int y=0;
    	int n=1;
    
    	printf("you score is %i \n",l);
    
    	while (i!=0)
    		{
    
    		printf("Do you want to take another number [y or n] ?");
    		scanf("%c\n",&c);
    
    		if(c=='n')
    		{
    		break;
    		}
    		m=rand()%10+1;
    		printf("you get the number: %i \n",m);
    		l = l + m;
    
    		printf("Your score is: %i\n ",l);
    
    
    		if(l>21){
    			printf("YOU bust\n");
    			y=16;
    			l=-1;
    			n=0;
    			break;
    
    			}
    
    		}
    
    		if(n!=0)
    			printf("Now the computer plays\n");
    
    
    
    
    
    	while(y<15)
    		{
    
    			m = rand()%10+1;
    			printf("the computer gets the number :%i\n",m);
    			y=y+m;
    
    			if(y>21){
    			printf("The computer's score is:%i\n",y);
    			printf("Computer bust\n");
    			y=-1;
    			n=0;
    			break;
    			}
    
    	}
    	if(n!=0)
    		printf("The computer's score is:%i\n",y);
    
    		if(l>y)
    			printf("YOU WIN\n");
    
    		else if(y>l)
    			printf("COMPUTER WIN\n");
    		else
    			printf("Draw\n");
    	return 0;
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A few suggestions for improvement:

    It would help a lot if you use longer names than a single letter - names like "compScore" or "compScore" is much easier to read than y and "userScore" instead of l - by the way, l is a TERRIBLE name for a variable - it is very easily confused with 1, which is of course not a good thing.

    Code:
    			y=16;
    is a terrble way to ensure that your while-loop further down isn't entered. Use the flag (n) in combination with the y - which also means that you don't need the break inside the second while-loop, because you are setting n there too. [Again, n should probably have a better name].

    Code:
    	while (i!=0)
    Is this supposed to be a "while(1)" statement - since i is never changed as far as I can see.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    ok thank you very much
    i will make your advise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM