# Thread: Need help with my code

1. ## Need help with my code

Hi, Im new here and also saw that you guys help people with similars codes like the one I have to do.
Im trying to do a card game (user vs pc) where the first to get 7.5 points win. For that I created a deck that have 4 suits with 10 cards each one. The cards go from 1 to 10, and the value of the cards 1-7 they value the number they have. Cards 8,9 and 10 value 0.5 points. I made this code below but my teacher said that I should avoid system(“pause”) because its possible to create a simple code for this game by using only if…else and loops without using system(“pause”). Im having a hard time trying to figure it out another way to do it, so if anyone here can help me I’ll be very grateful. I would like to know too if it is possible to delete a card once chosen? Like reduce the number of cards each time one is chosen. And how could I do that?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main()
{
srand( time(NULL) );
int key, card, cardpc;
int deck[40] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10};
float points, points_pc;

printf("Press 1 to play or 0 to not!\t");
scanf("%d", &key);

printf("\n");

if (key ==0){ printf("BYE\n"); system("pause");}
else if (key==1){
while(key==1){
card = deck[rand()% 40];
if(card==8 || card==9 || card==10){
printf("You got 0.5 points");
points = points + 0.5;
}
else{
printf("You got %d points",card);
points = points + card;
}
printf("\n");

printf("Your score is: %0.1f \n", points);

if(points>7.5){
printf("YOU LOST!\n");
system("pause");
}
else if (points==7.5) {
printf("YOU WON");
system("pause");
}
cardpc = deck[rand()% 40];
if(cardpc==8 || cardpc==9 || cardpc==10){
printf("The PC got 0.5 points");
points_pc = points_pc + 0.5;
}
else{
printf("The PC got %d points", cardpc);
points_pc = points_pc + cardpc;
}
printf("\n");
printf("The PC score is: %0.1f \n", points_pc);

if(points_pc>7.5){
printf("YOU WON! The PC score is above 7.5!\n");
system("pause");
}
else{
if (points_pc == 7.5){
printf("The PC won");
system("pause");
}
}
printf("Do you want another card? (Press 1 if you want it)\t");
scanf("%d", &key);

printf("\n");
}

}
//if the user passes turn, the pc can still continue playing
while(points_pc<points){
cardpc = deck[rand()% 40];
if(cardpc==8 || cardpc==9 || cardpc==10){
printf("The PC got 0.5 points");
points_pc = points_pc + 0.5;
}
else{
printf("The PC got %d points", cardpc);
points_pc = points_pc + cardpc;
}
printf("\n");
printf("The PC score is: %0.1f \n", points_pc);

if(points_pc==points){
printf("The PC won! \n");
}
else{
if(points_pc>7.5){
printf("You won! The PC score is above 7.5\n");
}
else{
if(points_pc>points){
printf("The PC won! \n");
}
}
}
}
printf("\n");

if (points_pc > points){
printf("The PC won\n");
}
system("pause");

return 0;
}```

2. It will have no effect on the execution of your code, but the most important thing you can do right now is to format your code properly as it will greatly improve the readability of your code. Once your code is more readable, it becomes much easier to understand, and when it is easier to understand, it will also be easier to improve, and such improvements could have a positive effect on your code when it is compiled and executed.

What I mean, is formatting something like this:
Code:
```if (key ==0){ printf("BYE\n"); system("pause");}
else if (key==1){
while(key==1){
card = deck[rand()% 40];
if(card==8 || card==9 || card==10){
printf("You got 0.5 points");
points = points + 0.5;
}
else{
printf("You got %d points",card);
points = points + card;
}```
into something like this:
Code:
```if (key == 0) {
printf("BYE\n");
system("pause");
} else if (key == 1) {
while (key == 1) {
card = deck[rand() % 40];
if (card == 8 || card == 9 || card == 10) {
printf("You got 0.5 points");
points = points + 0.5;
} else {
printf("You got %d points",card);
points = points + card;
}```
In this case I'm using 4 spaces per indent level with K&R style brace placement. The exact number of spaces you use, or whether you use a literal tab character instead, and which brace placement style you use, or whether or not you put spaces in between certain operators etc, are all matters of subjective style. If a style isn't imposed on you by, choose what you like and be consistent. The point is to make it easy to see what structures are nested in what other structures, where they start and begin, and to avoid having to keep adjusting to subjective style differences within the same piece of code.

Originally Posted by BatteryLife
I made this code below but my teacher said that I should avoid system(“pause”) because its possible to create a simple code for this game by using only if…else and loops without using system(“pause”). Im having a hard time trying to figure it out another way to do it, so if anyone here can help me I’ll be very grateful.
I agree with your teacher: it doesn't look like you need to pause anything. Just remove those system function calls and see how it goes.

Originally Posted by BatteryLife
I would like to know too if it is possible to delete a card once chosen? Like reduce the number of cards each time one is chosen. And how could I do that?
Yes, it is possible. You're using an array, so you cannot quite "delete" an element. But what you can do is to find a way to mark an element as "deleted", e.g., by setting it to 0, then if the card chosen has been deleted, you choose again. It would be somewhat easier if you swapped the deleted card with a card at the end of the deck so that you can avoid having to choose again, but this also means you need to tweak how you generate a random number.

3. ## system("pause")

I put the system("pause") beacuse If I don't put here, for example:
Code:
```}else if (points_pc == 7.5){
printf("The PC won");
system("pause") ;
}
```
Code:
```printf("Do you want another card? (Press 1 if you want it)\t");
scanf("%d", &key);
```

even though the game is clearly over

4. Originally Posted by BatteryLife
even though the game is clearly over
Ah, but then if the game is clearly over, why pause? Exit the game instead. To do this, you need to structure your code clearly so that there's a well defined way of ending the game loop when the game is over.

5. I made some changes in my code and created an array to mark a card as deleted when chosen, but I don't think that is working at all and I don't know why (I have tried playing with 6 cards to see what happens when all the cards are chosen, but the game does not stop). Can anyone here help me pls?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main()
{
srand( time(NULL) );
int key, card, cardpc, n;
int deck[40] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10};
int draw[40] = {0}; //array to "delete" a card. Mark a card as "deleted", e.g., by setting it to 1,
// then if the card chosen has been deleted, it choose again.
float points, points_pc;
points=0;
points_pc=0;

printf("Press 1 to play or 0 to not!\t");
scanf("%d", &key);

printf("\n");

if (key ==0){
printf("BYE\n");

}else if (key==1){
while(key==1){
n = deck[rand()% 40];
card = deck[n];
if (draw[n] == 1){
card = deck[rand()% 40];
}
if(card==8 || card==9 || card==10){
printf("You got 0.5 points");
points = points + 0.5;
}else{
printf("You got %d points",card);
points = points + card;
}
printf("\n");
printf("Your score is: %0.1f \n", points);

draw[n]=1;

cardpc = deck[rand()% 40];
cardpc = deck[n];
if (draw[n] == 1){
cardpc = deck[rand()% 40];
}
if(cardpc==8 || cardpc==9 || cardpc==10){
printf("The PC got 0.5 points");
points_pc = points_pc + 0.5;
}else{
printf("The PC got %d points", cardpc);
points_pc = points_pc + cardpc;
}
printf("\n");
printf("The PC score is: %0.1f \n", points_pc);

draw[n]=1;

printf("Do you want another card? (Press 1 if you want it)\t");
scanf("%d", &key);

printf("\n");
}
//if the user passes turn, the pc can still continue playing
while(points_pc<points){
cardpc = deck[rand()% 40];
cardpc = deck[n];
if (draw[n] == 1){
cardpc = deck[rand()% 40];
}
if(cardpc==8 || cardpc==9 || cardpc==10){
printf("The PC got 0.5 points");
points_pc = points_pc + 0.5;
}else{
printf("The PC got %d points", cardpc);
points_pc = points_pc + cardpc;
}
printf("\n");
printf("The PC score is: %0.1f \n", points_pc);

draw[n]=1;

}
printf("\n");

}
//RESULTS
if(points_pc==7.5){
printf("The PC won! \n");
}else if(points_pc>7.5){
printf("You won! The PC score is above 7.5\n");
}else if(points_pc>points){
printf("The PC won! \n");
}else if(points>7.5){
printf("YOU LOST!\n");
}else if (points==7.5) {
printf("YOU WON");
}

return 0;
}```

6. A couple of thoughts.
1. Indentation - seriously.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main()
{
srand(time(NULL));
int key, card, cardpc, n;
int deck[40] = {
1, 1, 1, 1,
2, 2, 2, 2,
3, 3, 3, 3,
4, 4, 4, 4,
5, 5, 5, 5,
6, 6, 6, 6,
7, 7, 7, 7,
8, 8, 8, 8,
9, 9, 9, 9,
10, 10, 10, 10
};
int draw[40] = { 0 };
//array to "delete" a card. Mark a card as "deleted", e.g., by setting it to 1,
// then if the card chosen has been deleted, it choose again.
float points, points_pc;
points = 0;
points_pc = 0;

printf("Press 1 to play or 0 to not!\t");
scanf("%d", &key);

printf("\n");

if (key == 0) {
printf("BYE\n");
} else if (key == 1) {
while (key == 1) {
n = deck[rand() % 40];
card = deck[n];
if (draw[n] == 1) {
card = deck[rand() % 40];
}
if (card == 8 || card == 9 || card == 10) {
printf("You got 0.5 points");
points = points + 0.5;
} else {
printf("You got %d points", card);
points = points + card;
}
printf("\n");
printf("Your score is: %0.1f \n", points);

draw[n] = 1;
cardpc = deck[rand() % 40];
cardpc = deck[n];

if (draw[n] == 1) {
cardpc = deck[rand() % 40];
}

if (cardpc == 8 || cardpc == 9 || cardpc == 10) {
printf("The PC got 0.5 points");
points_pc = points_pc + 0.5;
} else {
printf("The PC got %d points", cardpc);
points_pc = points_pc + cardpc;
}

printf("\n");
printf("The PC score is: %0.1f \n", points_pc);

draw[n] = 1;
printf("Do you want another card? (Press 1 if you want it)\t");
scanf("%d", &key);
printf("\n");
}

//if the user passes turn, the pc can still continue playing
while (points_pc < points) {
cardpc = deck[rand() % 40];
cardpc = deck[n];
if (draw[n] == 1) {
cardpc = deck[rand() % 40];
}
if (cardpc == 8 || cardpc == 9 || cardpc == 10) {
printf("The PC got 0.5 points");
points_pc = points_pc + 0.5;
} else {
printf("The PC got %d points", cardpc);
points_pc = points_pc + cardpc;
}
printf("\n");
printf("The PC score is: %0.1f \n", points_pc);
draw[n] = 1;
}
printf("\n");
}

//RESULTS
if (points_pc == 7.5) {
printf("The PC won! \n");
} else if (points_pc > 7.5) {
printf("You won! The PC score is above 7.5\n");
} else if (points_pc > points) {
printf("The PC won! \n");
} else if (points > 7.5) {
printf("YOU LOST!\n");
} else if (points == 7.5) {
printf("YOU WON");
}

return 0;
}```
If your code is a visual mess, there's no hope of figuring out how one bit of code relates to another.

2. Functions - use them liberally.
Having a single function over 100 lines long means you can't focus on one specific issue at a time.

Small functions you can determine "yeah, this one works" means you can stop worrying about it so much.
I mean, you don't worry about how printf/scanf work, you just use them.
You need to make your own functions so you can have the same mind set about them as well.

7. Thanks for the mess tip. I know I could use functions, but teacher told me to do the game all in function int main(), thats why I do it the way I did. To solve my problem you think that I could do an statment to say: if all cards have already been chosen( the array 'draw' is all set to 1) the game ends. ?? And how could I do it?

8. Originally Posted by BatteryLife
Thanks for the mess tip. I know I could use functions, but teacher told me to do the game all in function int main(), thats why I do it the way I did. To solve my problem you think that I could do an statment to say: if all cards have already been chosen( the array 'draw' is all set to 1) the game ends. ?? And how could I do it?
You need a different teacher or School.

Tim S.

9. > I know I could use functions, but teacher told me to do the game all in function int main(),
So do it using functions to make it work, then inline it all to make a mess for teacher.

I can't even begin to describe how strange your attempt to draw a card is.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int drawCard(int deck[], int drawn[] ) {
int n;
do {
// keep guessing until we find a card
// that hasn't been drawn already
n = rand() % 40;
} while ( drawn[n] );
drawn[n] = 1;     // it's drawn now
return deck[n];   // return the card
}

float score(const char *player, int card, float points ) {
if (card == 8 || card == 9 || card == 10) {
printf("%s got 0.5 points", player);
points = points + 0.5;
} else {
printf("%s got %d points", player, card);
points = points + card;
}
printf("\n");
printf("%s score is: %0.1f \n", player, points);
return points;
}

int main()
{
srand(time(NULL));
int key, card, cardpc, n;
int deck[40] = {
1, 1, 1, 1,
2, 2, 2, 2,
3, 3, 3, 3,
4, 4, 4, 4,
5, 5, 5, 5,
6, 6, 6, 6,
7, 7, 7, 7,
8, 8, 8, 8,
9, 9, 9, 9,
10, 10, 10, 10
};
int draw[40] = { 0 };
//array to "delete" a card. Mark a card as "deleted", e.g., by setting it to 1,
// then if the card chosen has been deleted, it choose again.
float points, points_pc;
points = 0;
points_pc = 0;

printf("Press 1 to play or 0 to not!\t");
scanf("%d", &key);

printf("\n");

if (key == 0) {
printf("BYE\n");
} else if (key == 1) {
while (key == 1) {
card = drawCard(deck,draw);
points = score("You", card, points);

card = drawCard(deck,draw);
points_pc = score("The PC", card, points_pc);

printf("Do you want another card? (Press 1 if you want it)\t");
scanf("%d", &key);
printf("\n");
}

//if the user passes turn, the pc can still continue playing
while (points_pc < points) {
card = drawCard(deck,draw);
points_pc = score("The PC", card, points_pc);
}
printf("\n");
}

//RESULTS
if (points_pc == 7.5) {
printf("The PC won! \n");
} else if (points_pc > 7.5) {
printf("You won! The PC score is above 7.5\n");
} else if (points_pc > points) {
printf("The PC won! \n");
} else if (points > 7.5) {
printf("YOU LOST!\n");
} else if (points == 7.5) {
printf("YOU WON");
}

return 0;
}```