Thread: Need help with my code

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.

    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    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") ;
               }
    
    it reads the follow line
    Code:
    printf("Do you want another card? (Press 1 if you want it)\t");
    scanf("%d", &key);
    

    even though the game is clearly over

    Last edited by BatteryLife; 12-01-2020 at 02:59 AM. Reason: beacuse I wrote it wrong before

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    4
    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. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by BatteryLife View Post
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread