Feb 10, 2018 at 12:30am UTC
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();
}
Feb 10, 2018 at 1:02am UTC
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.