簡體   English   中英

紙牌游戲 - 我怎么能做出這樣的聲明?

[英]card game - how can I make this statement?

我正在嘗試做一個紙牌游戲(用戶對電腦),首先獲得 7.5 分的人獲勝。 為此,我創建了一套有 4 套套裝,每套 10 張牌的套牌。 卡片 go 從 1 到 10,卡片 1-7 的值是他們所擁有的數字。 卡片 8,9 和 10 價值 0.5 分。

在有人幫我打牌之后 - 在方括號 [ ] 之間放什么? 我對我的代碼進行了一些更改,現在我得到了如下所示。 問題是我不知道如何發表聲明說:如果所有卡片都已被選中(即數組'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;
}

牌組可以洗牌。 然后遍歷甲板並增加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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM