Ciao sto cercando di scrivere un programma per mischiare le carte di Scopa ma il problema è che non mi scambia in maniera random le carte lasciandole in ordine
Sapete darmi una mano?
carte.cpp
#include "Carte.h"
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void createDeck(vector<Card> &deck)
{
string face[NUM_OF_FACE] = {"Ace","Two","Three","Four","Five","Six","Seven","Prince","Knight","King"};
string suit[NUM_OF_SUIT] = {"Diamonds","Spades","Clubs","Hearts"};
for(size_t x = 0;x < NUM_OF_CARDS;++x)
{
Card temp = {face[x % NUM_OF_FACE],suit[x / NUM_OF_FACE]};
deck.push_back(temp);
}
}
void createHand(vector<Card> &hand)
{
for(size_t x = 0;x < CARDS_ON_HAND;++x)
{
Card temp{"",""};
hand.push_back(temp);
}
}
void shuffle(vector <Card> &deck)
{
for(size_t x = 0;x < NUM_OF_CARDS;++x)
{
srand(time(NULL));
unsigned short int y = rand() % NUM_OF_CARDS ;
//Test whether the number X and Y aren'eguals and if true swap the cards
if(x != y)
{
swap(deck[x],deck[y]);
cout << "X: " << deck[x].face << " of " << deck[x].suit << endl;
cout << "Y: " << deck[y].face << " of " << deck[y].suit << endl;
}
}
}
bool Deal(vector<Card> &deck,vector<Card> &hand)
{
for(size_t x = 0;x < CARDS_ON_HAND;++x)
{
hand[x] = deck[0];
deck.erase(deck.begin());
}
if(hand.size() != 3)
{
return false;
}
return true;
}
void print(const vector<Card> deck)
{
for(size_t x = 0;x < deck.size();++x)
{
cout << x << ": ";
cout << deck[x].face;
cout <<" of";
cout << deck[x].suit << endl;
}
}
void swap(Card &x,Card &y)
{
Card temp {"",""};
temp.face = x.face;
temp.suit = x.suit;
x.face = y.face;
x.suit = y.suit;
y.face = temp.face;
y.suit = temp.suit;
}
Main
#include <iostream>
#include <cstdlib>
#include "Carte.h"
using namespace std;
int main()
{
vector<Card> deck,playerHand;
createDeck(deck);
shuffle(deck);
createHand(playerHand);
Deal(deck,playerHand);
print(playerHand);
cout << "Deck: " << endl;
print(deck);
return EXIT_SUCCESS;
}
Carte.h
#ifndef CARTE_H
#define CARTE_H
#include <vector>
#include <string>
typedef struct {
std::string face;
std::string suit;
}Card;
//Constants value
const int NUM_OF_FACE = 10;
const int NUM_OF_SUIT = 4;
const int NUM_OF_CARDS = 40;
const int CARDS_ON_HAND = 3;
//Functions
void shuffle(std::vector<Card> &x);//Shuffle the cards of the deck
bool Deal(std::vector<Card> &x,std::vector<Card> &y);//Deal the player cards
void createDeck(std::vector<Card> &x);//Create the vector where there are hold the cards in the deck
void createHand(std::vector<Card> &x);//Create the vector that containt the cards in hand
void print(const std::vector<Card> x);//Print the cards
void swap(Card &x,Card &y);
#endif