使用用户输入信息初始化结构

发布于 2024-08-30 14:10:24 字数 2017 浏览 10 评论 0原文

我正在尝试制作一个适用于扑克(德州扑克)起手牌的程序;每手牌都有一个从 1 到 169 的值,我希望能够输入每张牌以及它们是否适合,并使这些值对应于一系列结构。这是到目前为止的代码,我似乎无法让它工作(我是一个初级程序员)。哦,顺便说一句,我正在使用 Visual Studio 2005

#include "stdafx.h"
#include <iostream>


int main()
{
    using namespace std;


    struct FirstCard
    {
        struct SecondCard
        {

            int s;  //suited
            int n;  //non-suited

        };

        SecondCard s14;
        SecondCard s13;
        SecondCard s12;
        SecondCard s11;
        SecondCard s10;
        SecondCard s9;
        SecondCard s8;
        SecondCard s7;
        SecondCard s6;
        SecondCard s5;
        SecondCard s4;
        SecondCard s3;
        SecondCard s2;


    };

    FirstCard s14; //ace
    FirstCard s13; //king
    FirstCard s12; //queen
    FirstCard s11; //jack
    FirstCard s10;
    FirstCard s9;
    FirstCard s8;
    FirstCard s7;
    FirstCard s6;
    FirstCard s5;
    FirstCard s4;
    FirstCard s3;
    FirstCard s2;


    s14.s14.n = 169; // these are the values that each combination
    s13.s13.n = 168; // will evaluate to, would eventually have 
    s12.s12.n = 167; // hand combinations all the way down to 1
    s11.s11.n = 166;
    s14.s13.s = 165;
    s14.s13.s = 164;
    s10.s10.n = 163; //10, 10, nonsuited
    s14.s13.n = 162;
    s14.s11.s = 161;
    s13.s12.s = 160;// king, queen, suited
    s9.s9.n = 159;
    s14.s10.s = 158;
    s14.s12.n = 157;
    s13.s11.s = 156;
    s8.s8.n = 155;
    s12.s11.s = 154;
    s13.s10.s = 153;
    s14.s9.s = 152;
    s14.s11.n = 151;

    cout << "enter first card: " << endl;

    cin >> somthing?//no idea what to put here, but this would somehow
            //read out the user input (a number from 2 to 14)
            //and assign it to the corresponding struct

    cout << firstcard.secondcard.suited_or_not << endl; //this would change depending
                                                            //on what the user inputs
    system("Pause");

} 

I am trying to make a program that works with poker (texas holdem) starting hands; each hand has a value from 1 to 169, and i want to be able to input each card and whether they are suited or not, and have those values correspond to a series of structs. Here is the code so far, i cant seem to get it to work (im a beginning programmer). oh and im using visual studio 2005 by the way

#include "stdafx.h"
#include <iostream>


int main()
{
    using namespace std;


    struct FirstCard
    {
        struct SecondCard
        {

            int s;  //suited
            int n;  //non-suited

        };

        SecondCard s14;
        SecondCard s13;
        SecondCard s12;
        SecondCard s11;
        SecondCard s10;
        SecondCard s9;
        SecondCard s8;
        SecondCard s7;
        SecondCard s6;
        SecondCard s5;
        SecondCard s4;
        SecondCard s3;
        SecondCard s2;


    };

    FirstCard s14; //ace
    FirstCard s13; //king
    FirstCard s12; //queen
    FirstCard s11; //jack
    FirstCard s10;
    FirstCard s9;
    FirstCard s8;
    FirstCard s7;
    FirstCard s6;
    FirstCard s5;
    FirstCard s4;
    FirstCard s3;
    FirstCard s2;


    s14.s14.n = 169; // these are the values that each combination
    s13.s13.n = 168; // will evaluate to, would eventually have 
    s12.s12.n = 167; // hand combinations all the way down to 1
    s11.s11.n = 166;
    s14.s13.s = 165;
    s14.s13.s = 164;
    s10.s10.n = 163; //10, 10, nonsuited
    s14.s13.n = 162;
    s14.s11.s = 161;
    s13.s12.s = 160;// king, queen, suited
    s9.s9.n = 159;
    s14.s10.s = 158;
    s14.s12.n = 157;
    s13.s11.s = 156;
    s8.s8.n = 155;
    s12.s11.s = 154;
    s13.s10.s = 153;
    s14.s9.s = 152;
    s14.s11.n = 151;

    cout << "enter first card: " << endl;

    cin >> somthing?//no idea what to put here, but this would somehow
            //read out the user input (a number from 2 to 14)
            //and assign it to the corresponding struct

    cout << firstcard.secondcard.suited_or_not << endl; //this would change depending
                                                            //on what the user inputs
    system("Pause");

} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2024-09-06 14:10:24

首先,不要通过尝试和错误来学习所有内容,而是考虑阅读一本好的入门书(在此列出)。每本值得一读的书都涉及 C++ 的基本输入以及如何通过使用容器等方式处理重复代码。

话虽这么说:我对扑克一无所知,但你绝对需要阅读有关容器的内容 - 如果你重复定义变量,则必须有某种方法来抽象它。

根据您所拥有的,假设我们将值分组在一个结构中:

struct Values {
    unsigned suited;
    unsigned unsuited;
    Values() : suited(0), unsuited(0) {} // some default
};

然后我们可以使用允许我们通过编号访问它们的容器:

typedef std::map<unsigned, Values> SecondCardMap;
typedef std::map<unsigned, SecondCardMap> CardMap;

这样我们就可以通过它们的编号方便​​地插入和修改结构:

CardMap cards;
cards[14][14].unsuited = 169;
cards[13][13].unsuited = 168;

现在您只需必须从用户处读取索引:

unsigned first, second;
std::cout << "enter first: ";
std::cin >> first;
std::cout << "enter second: ";
std::cin >> second;

并且可以根据以下内容访问值:

cards[first][second].unsuited = 1234;

请注意,上述内容没有解决任何错误处理等问题。

First off, instead of learning all by trial and error, consider reading a good introductory book (list here). Every book worth reading address basic input in C++ and how to handle repetitive code by e.g. using containers.

That being said: I don't know anything about poker, but you definitely need to read about containers - if you repeatedly define variables, there has to be some way to abstract it.

Going from what you have, lets say we group the values in one struct:

struct Values {
    unsigned suited;
    unsigned unsuited;
    Values() : suited(0), unsuited(0) {} // some default
};

Then we can use containers that allows us to access them by their number:

typedef std::map<unsigned, Values> SecondCardMap;
typedef std::map<unsigned, SecondCardMap> CardMap;

With that we can insert and modify the structs conveniently via their numbers:

CardMap cards;
cards[14][14].unsuited = 169;
cards[13][13].unsuited = 168;

Now you'd only have to read in indices from the user:

unsigned first, second;
std::cout << "enter first: ";
std::cin >> first;
std::cout << "enter second: ";
std::cin >> second;

And could access the values according to that:

cards[first][second].unsuited = 1234;

Note that the above doesn't address any error handling etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文