vendredi 4 décembre 2020

card game - how can I make this statement?

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.

After someone helped me here card game - what to put between the square brackets [ ]? I made some changes in my code and now I have it like below. The thing is that i don't know how to make an statement to say: if all cards have already been chosen( that is, the array 'draw' is all set to 1) the game ends. Can anyone here help me pls?

#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 < 7.5) {
      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) && (points > 7.5)){
    printf("You both lost! \n");
  } else if ((points == 7.5) || (points_pc > 7.5)) {
    printf("You won! The PC score is above 7.5\n");
  } else if ((points_pc == 7.5) || (points > 7.5)) {
    printf("The PC won");
  }

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire