简体   繁体   中英

passing value from an object to a two dimensional array

I am writing code for a simple poker game. The problem I'm having is the proper use of pointers. I did look at the link below, as well as did some google research, but I am missing something and I know its something simple. would appreciate help.

Trouble deciphering function parameters: pointer and pointer to pointer

Ok i have afunction that randomly generates the face and the value of the card. I want to pass this into an array as one string... so basically if wFace[column] is king and wSuit[row] is Heart. i want to pass king hearts to an array..player1... I can't figure out how to do that..here is what I tried..but it only collects the first value of face..it doesnt get the suit value.

When i try the code below the error I am getting is the following 73 35 E:\\C++lab\\pokerGame2.cpp [Error] incompatible types in assignment of 'const char*' to 'char* [5]'

[code]

void deal(const int wDeck[][13], const char *wFace[],
           const char *wSuit[], char *wplayer1[][5], const char *wplayer2[5]  )      
{   
    int card;
    int row;
    int column;
    int s;


    for(card =1; card <= 10; card++){

        if(card<=5){
            for(row = 0; row <= 3; row++){

            for(column = 0; column <= 12; column++){

                if(wDeck[row][column] ==card){
                    wplayer1[card] = wFace[column];
                    for(s=0; s<1; s++){
                        wplayer1[s] = wSuit[row];
                    }
                    //wplayer1[card][card]= (wFace[column], wSuit[row]);

                    cout<<wFace[column]<<" of "<<wSuit[row]<<"\n";
                    cout<<"player1 cards "<< wplayer1[card][s];
                }       
            }
        } 
    }
        else if(card>5 && card <=10){ 
            for(row = 0; row <= 3; row++){

            for(column = 0; column <= 12; column++){

                {if(wDeck[row][column] ==card){
                    wplayer2[card]= wFace[column], wSuit[row];
                     cout<<wFace[column]<<" of "<<wSuit[row]<<"\n";

                        }


                }
            }
        } 
    }

}

enter code here

[/code]

Welcome to Stack Overflow .

I've answered your previous question , where you gave the complete code so I think I do understand what you want in this post. Before I answer this question, please allow me to explain why people are so serious about the format .

Format itself is not the culprit, the content is.

If you cannot break down your question to a clean concept, it will be hard to describe it with a clean format. Digest your problem first, and then ask a clean question.

Please refer to

  1. The SSCCE - Short, Self Contained, Correct (Compilable), Example , and

  2. How to Ask

It takes time to learn how to ask a good question, but trust me, it's worth your time.

Besides the fact that digesting the problem is itself a learning process, sometimes you could even find the solution yourself when trying to break it down.

Remember that Stack Ovedrflow is more like a Q&A site than a conventional forum. People here are stricter about the format. We want to see a clean, understandable question so that we can quickly answer it with less discussion (remember it's not like the conventional forum). The discussion can occur in chat rooms, or simply in the comments but the thread remains a clean form of Q&A.


Now let's go back to your question.

The problem is not "how to pass two value into 2 dem array" but "why should you pass that kind of data?"

Since you are using C++ , you can use class to format your data.

Take a look at a simple example below:

#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;

const char* FaceString[13] = {"Ace", "2", "3", "4",
                              "5", "6", "7", "8",
                              "9", "10", "Jack", "Queen", "King"};

const char* SuitString[4] = {"Spades", "Hearts", "Diamonds", "Clubs"};

class Card
{
public:
    int face;
    int suit;

    void toString() {
        cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
    }

};

int main()
{    
    // Create a deck of cards
    Card deck[52];

    for (int i = 0; i < 52; i++) {    
        deck[i].face = i % 13;
        deck[i].suit = i % 4;    
    }

    // Shuffle
    srand (unsigned(time(0)));
    random_shuffle(&deck[0], (&deck[0]+52));   

    // Display cards
    for (int j = 0; j < 52; j++) {  
        cout<< "Card_" << (j+1) << ": ";
        deck[j].toString();
    }

    return 0;
}

The class Card is an abstraction (generalization) of poker cards.

In main() I create a deck of card by Card deck[52] and assign values (face and suit) to each of them by

 for (int i = 0; i < 52; i++) { 
        deck[i].face = i % 13;
        deck[i].suit = i % 4;
    }

In this case, you can simply shuffle one-dimensional and organized objects array deck , and pass a whole object (a poker card with it's own face and suit parameter) as value.

I use random_shuffle() in std library to shuffle the cards, and I leave dealing to you.

This will make your program cleaner and more understandable. I think this example is enough to provide you a direction of how to solve your problem.

Stack Overflow is a great Q&A site, it's a tremendous loss for you if you just give it up.

Keep it up, you'll get better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM