Carddealing funktion

Hello! Im currently trying to make a CardGame but it doesn't work the way I want.
I want the deal funktion to deal cards from the carddeck to individual players in the player array. But my problem is, that im unsure if it works at all(the carddealing funktion) because when I try output one of the players deck to the console nothing is showed(no compiler errors).

#include <iostream>
#include "Card.h"
#include <sstream>
#include "Player.h"
#include "CardGame.h"
#include <ctime>
#include <stdlib.h>


using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(nullptr)));
CardGame beta;
beta.shuffle();
beta.deal();
beta.showResult();
}

#define CardGame_H
static const int nrP = 4;
static const int deckSize = 32;
class CardGame
{
public:
CardGame();
void shuffle();
void deal();
std::string showPlayers();
//void printDeck();
//~CardGame();
private:

std::array<Player, nrP> players;
std::array<Card, deckSize> deck;
};
#endif CardGame_H

#include "CardGame.h"
#include "Player.h"
#include "Card.h"
#include <sstream>
#include <string>
#include <vector>
#include <iostream>
#include <ctime>
#include <stdlib.h>

using namespace std;

CardGame::CardGame()

{
players = { 0, 1, 2, 3 };
//player1 = 1;

int kortIndex = 0;
for (size_t suitCount = 0; suitCount <= 3; ++suitCount) {
for (size_t faceCount = 0; faceCount <= 7; ++faceCount) {
Face face1 = static_cast<Face>(faceCount);
Suit suit1 = static_cast<Suit>(suitCount);
deck[kortIndex] = Card(suit1, face1);
++kortIndex;
}

}

}

void CardGame::shuffle()
{
for (size_t kortNr = 0; kortNr < deckSize; ++kortNr) {
int randomCard = rand() % 32;
Card temp = deck[randomCard];
deck[randomCard] = deck[kortNr];
deck[kortNr] = temp;
}
}

void CardGame::deal()
{

for (int count = 0; count < 8; count++) {
players[0].push_back(deck[count]);
}

for (int count = 8; count < 16; count++) {
players[1].push_back(deck[count]);
}

std::string CardGame::showResult()
{
stringstream out1;
stringstream out2;
stringstream out3;
stringstream out4;

//out1 << player1.toString();
//players[0].toString();
//out2 << players[1].toString();
//out3 << players[2].toString();
//out4 << players[3].toString();
string outstream;
return outstream = out1.str(); // + out2.str() + out3.str() + out4.str();
}
for (int count = 16; count = 24; count++) {
players[2].push_back(deck[count]);
}

for (int count = 24; count = 32; count++) {
players[3].push_back(deck[count]);
}

}


From the player class:

#pragma once
#include "Card.h"
#include <sstream>
#include <string>
#include <vector>

#ifndef Player_H
#define Player_H
class Player
{
public:
Player(int = 0);
void push_back(Card);
int size();
std::string toString();
//~Player();
private:
int id;
int sumOfPoints;
std::vector<Card>playerDeck;
};
#endif Player_H


#include "Player.h"
#include "Card.h"
#include <sstream>
#include <string>
#include <vector>


using namespace std;

Player::Player(int id1)
: id(id1)
{
sumOfPoints = 0;

}
void Player::push_back(Card card1)
{
playerDeck.push_back(card1);
}

std::string Player::toString()
{
stringstream out;

out << playerDeck[0].toString() << endl << playerDeck[1].toString() << endl << playerDeck[2].toString()
<< endl << playerDeck[3].toString() << endl << playerDeck[4].toString() << endl << playerDeck[5].toString() <<
endl << playerDeck[6].toString() << playerDeck[7].toString() << endl;
string outstream;
return outstream = out.str();
}


show result appears to assign values, not show any results.
it also returns an uninitialized variable.

actually the weird for loops don't seem to be in the function, but maybe they were or something?

Its really unclear what this function is doing, but it does not look right.
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>

struct card
{
    char suit ;
    char value ;

    // ...
};

std::ostream& operator << ( std::ostream& stm, const std::vector<card>& cards )
{
    stm << "[ " ;
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( card c : cards ) stm << c.suit << c.value << ' ' ;
    return stm << ']' ;
}

// https://cal-linux.com/tutorials/vectors.html
std::vector<card> make_standard_deck()
{
    static const std::string suits = "SHDC" ;
    static const std::string values = "23456789TJQKA" ;

    std::vector<card> deck ;
    for( char s : suits ) for( char v : values ) deck.push_back( { s, v } ) ;
    return deck ;
}

std::vector<card>& shuffle( std::vector<card>& cards )
{
    // http://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 rng( std::random_device{}() ) ;

    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::shuffle( std::begin(cards), std::end(cards), rng ) ;
    return cards ;
}

std::vector<card> deal( std::vector<card>& deck, std::size_t ncards )
{
    if( ncards > deck.size() ) return {} ; // deck is too small, return empty vector

    // form a hand of the last ncards cards in the deck
    std::vector<card> hand { std::rbegin(deck), std::rbegin(deck)+ncards } ;
    // and remove the cards that were dealt out from the deck
    deck.resize( deck.size() - ncards ) ;
    return hand ;
}

struct player
{
    static constexpr std::size_t hand_sz = 13 ;

    int id ;
    std::vector<card> hand{} ;
};

void deal_to( std::vector<card>& deck, player& p )
{ p.hand = deal( deck, player::hand_sz ) ; }

int main()
{
    auto deck = make_standard_deck() ;
    std::cout << "deck: " << deck << '\n' ;

    shuffle(deck) ;
    std::cout << "(shuffled) deck: " << deck << '\n' ;

    player p1{1}, p2{2} ;
    deal_to( deck, p1 ) ;
    deal_to( deck, p2 ) ;

    std::cout << "\nplayer #" << p1.id << " : " << p1.hand << '\n'
              << "player #" << p2.id << " : " << p2.hand << '\n'
              << "(remaining) deck: " << deck << '\n' ;
}

http://coliru.stacked-crooked.com/a/dc7e663953cdb505
Topic archived. No new replies allowed.