如何初始化一副纸牌的 3D 数组

How to initialize 3D array for deck of cards

本文关键字:3D 数组 一副 初始化      更新时间:2023-10-16
// deck of cards
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i, j, k;
    char arr[4][13][14] =
    {
        {
            { heart one, heart two, heart three, heart four, heart five, heart six, heart seven, heart eight, heart nine, heart ten, heart jack, heart queen, heart king, heart ace }
        },
        {
            { diamond one, diamond two, diamond three, diamond four, diamond five, diamond six, diamond seven, diamond eight, diamond nine, diamond ten, diamond jack, diamond queen, diamond king, diamond ace }
        },
        {
            { club one, club two, club three, club four, club five, club six, club seven, club eight, club nine, club ten, club jack, club queen, club king, club ace }
        },
        { 
            { spade one,spade two, spade three, spade four, spade five, spade six, spade seven, spade,eight, spade nine, spade ten, spade jack, spade queen, spade king, spade ace }
        },
    };
    clrscr();
    printf(":::3D Array:::nn");
    for(i=0; i<4;i++)
    {
        for(j=0;j<13;j++)
        {
            for(k=0;k<14;k++)
            {
                printf("%dt",arr[i][j][k]);
            }
            printf("n");
        }
        printf("n");
    }
    return 0;
}

我收到一个错误,其中没有指定心形、钻石、铁锹、球杆。 但是我设置了字符类型,有人可以给我一些关于如何解决这个问题的指示吗?我想要一副牌的 3D 数组,4 行(花色(,13 列(二,三,...,ace(和 14 个数据位置(最长的是钻石 8,例如,取 13 个元素(。请帮忙!

您可以自下而上地开始构建声明以帮助您更好地理解声明语法。

您将如何声明 14 char s 的数组?

char card[14] = "heart two";

现在,您将如何创建其中 13 个的数组?

char suite[13][14] = {"heart two", "heart three", "heart four" ...};

现在,您将如何创建其中 4 个的数组?

char deck[4][13][14] =
{
   {"heart two", "heart three", "heart four" ...},
   {"diamond two", "diamond three", "diamond four" ...},
   {"club two", "club three", "club four" ...},
   {"spade two", "spade three", "spade four" ...}
};

我猜你想要这样的东西。此外,一副牌中没有 1。

const char *arr[4][13]=
{
    {"heart two", "heart three",...},
    {"diamond two", "diamond three",...},
    {"club two", "club three",...},
    {"spade two", "spade three",...}
};