Hi im trying to design a game, like 21, but it is up to 42.
the game will start and the user will choose whether they want another generated random number. they will keep being asked until they bust (at over 42) or they enter n and the game ends.
once the player has had their turn, the computer will then play the game and the computer will either bust or stop after a given amount of turns the computer chooses.
the person or player closest to 42 will win...... I have designed the players code.. but im struggling with then allowing the computer to play.. any help would be great.. thank you
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
char answer=0; // y or n
int score=0;
int randomnumber=0;
int computerscore=0;
int gameover=0;
int arandomnumber=0;
int brandomnumber=0;
int crandomnumber=0;
int score2=0;
char answer2=0;
//game starts
printf("Do you want to play the game? Y or N?:\n ");
answer=getch();
printf("%d", answer);
while(answer == 'y' || answer == 'Y')
{
srand(time(NULL));
randomnumber=rand()%21+1;
printf("Your number is: %d\n\n", randomnumber);
score=score+randomnumber;
printf("Your score is: %d\n\n", score);
if(answer == 'n'){
printf("game over");
}
if (score>42){
printf("You Bust\n\n");
gameover=1;
break;
}
printf("Do you want another number? Y or N?: \n\n");
answer=getch();
printf("%d\n", answer);
}
printf("now the computer plays\n\n");
{
srand(time(NULL));
arandomnumber=rand()%21+1;
printf("PC number is: %d\n\n", arandomnumber);
score2=score2+arandomnumber;
printf("The computers score is: %d\n\n", score2);
}
if (score<score2){
printf("The computer won!");
}
if (score>score2){
printf("Well done you won!");
}
system("pause");
return 0;
}