【问题标题】:card game - how can I make this statement?纸牌游戏 - 我怎样才能做出这样的声明?
【发布时间】:2020-12-04 23:29:58
【问题描述】:

我正在尝试做一个纸牌游戏(用户对电脑),首先获得 7.5 分的人获胜。为此,我创建了一套有 4 套套装,每套 10 张牌的套牌。卡片从 1 到 10,卡片的价值从 1 到 7,他们看重他们拥有的数字。卡片 8,9 和 10 值 0.5 分。

在有人在这里帮助我card game - what to put between the square brackets [ ]? 之后,我对我的代码进行了一些更改,现在我得到了如下所示。问题是我不知道如何发表声明说:如果所有卡片都已被选中(即数组'draw'全部设置为1),游戏结束。有人可以帮我吗?

#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) {
        n = deck[rand() % 40];
        card = deck[n];
      }
      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;
      n = deck[rand() % 40];
      cardpc = deck[n];

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

      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) {
      n = deck[rand() % 40];
      cardpc = deck[n];
      if (draw[n] == 1) {
        n = deck[rand() % 40];
        cardpc = deck[n];

      }
      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;
}

【问题讨论】:

  • 考虑使用不同的数据结构。虽然在这种情况下,拥有一个记录数组中剩余卡片数量的计数器似乎很容易,并在该计数器变为零时退出。
  • @WilliamPursell 谢谢!我以前没有考虑过。
  • 要添加,同一张卡可以多次随机选择,因为如果之前已经选择过一张卡,只有一次重试(draw[n] == 1)。这是预期的@Lusvi 吗?
  • @Agus 为什么可以多次选择同一张卡?我认为通过做我所做的没关系...你能告诉我我有什么问题吗?
  • n = deck[rand() % 40]; card = deck[n]; if (draw[n] == 1) {n = deck[rand() % 40]; card = deck[n];} -> 这里说if (draw[n] == 1)你会做同样的过程,但这并不能保证当你再次调用rand() % 40时它会产生不同的价值

标签: arrays c if-statement


【解决方案1】:

牌组可以洗牌。然后遍历卡片组并递增n

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

void shuffle ( int *deck, int size) {
    while ( size) {
        int index = rand ( ) % size;
        --size;
        int temp = deck[index];
        deck[index] = deck[size];
        deck[size] = temp;
    }
}

int main() {
    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 key, card, cardpc, n;
    float points, points_pc;

    points = 0;
    points_pc = 0;

    srand(time(NULL));

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

    shuffle ( deck, 40);

    printf("\n");

    n = 0;

    if (key == 0) {
        printf("BYE\n");
    } else if (key == 1) {
        while (key == 1) {
            card = deck[n];
            ++n;
            if ( n >= 40) {
                printf ( "no cards left\n");
                break;
            }
            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);

            cardpc = deck[n];
            ++n;
            if ( n >= 40) {
                printf ( "no cards left\n");
                break;
            }

            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);

            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 ( n < 40 && points_pc < 7.5) {
            cardpc = deck[n];
            ++n;

            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);
        }
        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;
}

【讨论】:

  • 感谢您的帮助。对此,我真的非常感激。你能告诉我为什么card = deck[n]吗?不要知道这个 n 是从哪里来的
  • 哦,别忘了。我明白了
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2022-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多