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 %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