【问题标题】:How can I show the player's cards in the form of arrays in the playing cards written in C programming?C语言编写的扑克牌中,如何以数组的形式显示玩家的牌?
【发布时间】:2021-04-22 12:53:04
【问题描述】:

大家好。我目前正在用 C 程序编写卡片代码。但我无法处理一些问题:(游戏的名称是Bastra(或Pishti)。我需要将4张牌面朝上放在桌子上并将4张牌分成4名玩家。当我们运行代码时,这个发生了,但我需要将玩家的卡片定义为一个数组。卡片是随机写在屏幕上的,但我不能在接下来的步骤中使用它,因为它不是数组。一个这样的我需要的部分是例如Card board_cards [3];Card player_1_cards [3];Card player_2_cards [3]; 等等。我想我需要在显示部分执行此操作,但我不知道如何执行此操作。如果您能帮助我,我会很高兴:)

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

#define MAX 9
#define MAX_CARDS 52
#define MAX_RANKS 13
#define MAX_SUITS 4
#define Empty NULL
//structure definition
struct card { 
    char *rank;    
    char suit[MAX];  
} ;

typedef struct card Card;
enum name {Spades, Clubs, Diamonds, Hearts};
enum number {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King};
char * suits[] = {"Spades", "Clubs", "Diamonds", "Hearts"};
char * ranks[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

void initialize(Card []);
void shuffle(Card []);
void display(const Card[]);

void cleaning_player_card(int player, int card_index, Card player_deck[3],Card the_card);
void cleaning_board(int board_card_index, Card board_deck[], Card board_card);
int arr1 ,arr2,arr3, arr4;
int main(void)
{
    Card deck[MAX_CARDS] = {""};
    initialize(deck);

    printf("BOARD:\n");
    shuffle(deck);
    display(deck);

    printf("\nPlayer one ... \n");
    shuffle(deck);
    display(deck);

    printf("\nPlayer two  ... \n");
    shuffle(deck);
    display(deck);

    printf("\nPlayer three  ... \n");
    shuffle(deck);
    display(deck);

    printf("\nPlayer four  ... \n");
    shuffle(deck);
    display(deck);
    return 0;
}

/*
  initialize the deck of cards to string values
*/

void initialize(Card deck[]){
    int i = 0;
    for (i = 0; i < MAX_CARDS; i++){
        deck[i].rank = ranks[i%MAX_RANKS];               
        strncpy(deck[i].suit, suits[i/MAX_RANKS], MAX);  
}
}

void shuffle(Card deck[]){
    int card_p = 0; //index of card 
    int i = 0; //counter
    Card temp = {" "}; //temp holding place for card
    srand(time(NULL)); //seed the random numbers with current time
    for (i = 0; i < 52; i++) {
        card_p = rand() % 52; 
        temp = deck[i];
        deck[i] = deck[card_p];
        deck[card_p] = temp;
    } 

}

void display(const Card deck[]) {
    int i = 0;
    for(i = 0; i < 4; i++) {
      const char *ranks[]={deck[i].rank};
      const char *suits[]={deck[i].suit};
        printf("%s of ", *ranks);
        printf("%s", *suits);
        
        printf("\n");
    }
}

【问题讨论】:

    标签: arrays c multidimensional-array enums typedef


    【解决方案1】:

    我会使用二维数组,例如cards[MAX_CARDS][MAX_SUITS]。

    【讨论】:

      【解决方案2】:

      我研究了您的代码,但有些地方我不明白。 然后我运行了你的代码。 我看到不止一个玩家拥有一张牌。

      例如;

      BOARD:
      Six of Spades    
      Seven of Spades
      Eight of Spades   
      Three of Spades
      
      Player one ...
      King of Spades
      Jack of Clubs
      Seven of Clubs  1
      Eight of Spades   
      
      Player two  ...
      Nine of Diamonds
      Two of Diamonds
      Four of Diamonds  2
      Seven of Clubs    1
      
      Player three  ...
      Six of Diamonds
      Five of Spades
      Six of Hearts   3
      Four of Diamonds  2
      
      Player four  ...
      Nine of Clubs
      Four of Hearts
      Ace of Hearts
      Six of Hearts  3
      

      我决定重新排列代码。

      代码是--->

      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include <string.h>
      /*
      We will store data in memory in the form of a char variable.
      
      For example;
              MEMORY      PRINT
              0-1         Spades of Two
              3-10        Hearts of Jack
              2-12        Diamonds of King
              1-0         Clubs of Ace
      
      */
          
      typedef struct{
          char name;
          char value;
      }card;
      
      
      /*
      transformer function -->
      converts the data in memory to STRINGS that we can understand. 
      And then it prints on the screen.
      */
      
      void transformer(card elements){ 
          const char *namee[4]={"Spades", "Clubs", "Diamonds", "Hearts"};
          const char *valuee[13]={"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
          char var_name[10],var_value[10];
          strcpy(var_name,namee[elements.name]);
          strcpy(var_value,valuee[elements.value]);
          printf("%s of %s\n",var_name,var_value);
      }
      
      void print_1d_arr( card *arr, int arr_size){
          for (int i=0;i<arr_size;++i){
              transformer(arr[i]);
          }
          printf("\n");
      }
      
      void print_2d_arr( card **arr, int arr_x, int arr_y){
          for (int i=0;i<arr_x;++i){
              printf("PLAYER %d\n",i+1);
              print_1d_arr(arr[i],arr_y);
          }
      }
      
      card *make_1d_arr( int arr_size){
          card *son=(card *)malloc( sizeof(card)*arr_size );
          return son;
      }
      
      card **make_2d_arr( int arr_x, int arr_y){
          card **son=(card **)malloc( sizeof(card*)*arr_x );
          for (int i=0;i<arr_x;++i){
              son[i]=(card *)malloc( sizeof(card)*arr_y );
          }
          return son;
      }
      
      /*
      in_arr function -->
      This function checks whether the data we are looking for is in the list(ARRAY).
      Returns 1 if present, 0 otherwise.
      */
      
      int in_arr(card *arr, card elements, int size_arr){
          for (int i=0;i<size_arr;++i){
              if (arr[i].name==elements.name && arr[i].value==elements.value){
                  return 1;
              }
          }
          return 0;
      }
      
      /*
      mix function --->
      This function will mix all the cards.
      And It will give us a list of mixed cards.
      */
      
      card *mix(){
          card *son,var_forever;
          son=make_1d_arr(52);
          char var_name, var_value, count_size;
          count_size=0;
          while (1){
              if (count_size==52){
                  return son;
              }
              var_name=rand()%4;
              var_value=rand()%13;
              var_forever.name=var_name;
              var_forever.value=var_value;
              if (in_arr(son,var_forever,count_size)){
                  continue;
              }
              son[count_size]=var_forever;
              ++count_size;
          }
      }
      
      /*
      player_arr function --->
      This function will give us the cards that players have.
      For example;
              MEMORY              PRINT
              [ [ 1-1 , 3-10 ]        PLAYER 1
                [ 2-0, 0-5 ] ]        Clubs of Two
                                      Hearts of Jack
          
                                      PLAYER 2
                                      Diamonds of Ace
                                      Spades of Six
      
      Note;This function does not have print function.
      
      x--->
      
      Imagine our mixed deck like this:
          0-1 , 3-10 , 2-5 , 1-5 , 3-3 , 2-11 ,........., 2-4 , 3-3 
          For example, two people play the game 
          And let's distribute 3 cards to each player.
          Cards to be given to the first player=0-1 , 3-10 , 2-5 
          Cards to be given to the second player = 1-5 , 3-3 , 2-11
      
      If you know Python, this function is as follows in Python
      Python Code;
      
      def playe_arr(number_of_players, TNOCEPH):
          all_deck = mix()
          total_cards = number_of_players*TNOCEPH
          son=[]
          if total_cards>52:
              print("ERROR ---> the total number of cards is greater than 52")
              return
          a=0
          b=TNOCEPH
          for i in range(number_of_players):
              player_variable=all_deck[a:b]
              son.append(player_variable)
              a+=TNOCEPH
              b+=TNOCEPH
          return son
      */
      
      card **player_arr(int number_of_players, int TNOCEPH){ // NOCEP == the number of cards each player has
          srand(time(NULL));
          card *all_deck=mix();
          int total_cards=number_of_players*TNOCEPH; //number of cards to be distributed
          int x=0;
          int y=0;
          card **son=make_2d_arr(number_of_players,TNOCEPH);
          if (total_cards>52){
              printf("ERROR --->the total number of cards is greater than 52\n");
              return NULL;
          }
          for (int i=0;i<total_cards;++i){ //Please look up (x)
              if (y==TNOCEPH){
                  y=0;
                  ++x;
              }
              son[x][y]=all_deck[i];
              ++y;
          }
          return son;
      }
      
      /*
      finish function --->
      Combine all functions.
      */
      
      void finish(){
          int NOP, TNOCEPH;
          printf("Number of players = ");scanf("%d",&NOP);
          printf("The number of cards each player has = ");scanf("%d",&TNOCEPH);
          card **son=player_arr(NOP,TNOCEPH);
          print_2d_arr(son,NOP,TNOCEPH);
      }
      
      int main(){
          finish();
          return 0;
      }
      

      我希望它对你有用。可莱凝胶;)

      【讨论】:

      • Çook çok teşekkür ederim.
      猜你喜欢
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      相关资源
      最近更新 更多