EDN Admin
Well-known member
Hi there. Well, from the Deitel book, on chapter 21, subchapter 6 "High efficiency playcards shuffle and deal Simulation", I cannot figure out what is the backstage operation running for each command.
a) I cannot understand how the DeckOfCards constructor has a direct access to the struct Card, without having a relative command typed inside the class DeckOfCards, i.e. { friend struct Card }
b) for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere deck.face = face[i % 13];<br/>
<span style="white-spacere //cout<<face[i % 13]<<", ";<br/>
<span style="white-spacere deck.suit = suit[i / 13];<br/>
<span style="white-spacere //cout<<suit[i / 13]<<", ";<br/>
<span style="white-spacere }
What is going on here??????? What do these assignments perform and mean?
c) for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere int j = rand() % 52;<br/>
//cout<<j<<", "<<((i + 1) % 2 ?t : n);<span style="white-spacere
<br/>
<span style="white-spacere Card temp = deck;<br/>
<span style="white-spacere deck = deck[j];<br/>
<span style="white-spacere deck[j] = temp;<br/>
<span style="white-spacere }
Why these switches are being selected? What is the puprose of such a permutation?
Thank you community a priori!!!!
<br/>
- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
#include <iostream>
using std::cout;
using std::endl;<br/>
using std::left;<br/>
using std::right;<br/>
<br/>
#include <iomanip><br/>
using std::setw;<br/>
<br/>
#include <cstdlib><br/>
using std::rand;<br/>
using std::srand;<br/>
<br/>
#include <ctime><br/>
using std::time;<br/>
<br/>
#include "header1.h"<br/>
<br/>
DeckOfCards:eckOfCards()<br/>
{<br/>
<span style="white-spacere static char *face[4] =<br/>
<span style="white-spacere {<br/>
<span style="white-spacere "Heart", "Diamonds", <br/>
<span style="white-spacere "Clubs", "Spades"<br/>
<span style="white-spacere },<br/>
<span style="white-spacere *suit[13] =<br/>
<span style="white-spacere {<br/>
<span style="white-spacere "Ace", "Deuce", "Three", "Four", "Five",<br/>
<span style="white-spacere "Six", "Seven", "Eight", "Nine", "Ten",<br/>
<span style="white-spacere "JacK", "Queen", "King"<br/>
<span style="white-spacere };<br/>
<span style="white-spacere for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere deck.face = face[i % 13];<br/>
<span style="white-spacere //cout<<face[i % 13]<<", ";<br/>
<span style="white-spacere deck.suit = suit[i / 13];<br/>
<span style="white-spacere //cout<<suit[i / 13]<<", ";<br/>
<span style="white-spacere }<br/>
<span style="white-spacere srand(time(0));<br/>
}<br/>
void DeckOfCards::shuffle()<br/>
{<br/>
<span style="white-spacere cout<<"nn- - - - - - - - - - - - - - - - - - - - - - - - nn";<br/>
<span style="white-spacere for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere int j = rand() % 52;<br/>
//cout<<j<<", "<<((i + 1) % 2 ?t : n);<span style="white-spacere
<br/>
<span style="white-spacere Card temp = deck;<br/>
<span style="white-spacere deck = deck[j];<br/>
<span style="white-spacere deck[j] = temp;<br/>
<span style="white-spacere }<br/>
}<br/>
void DeckOfCards::deal() const<br/>
{<br/>
<span style="white-spacere cout<<"nn- - - - - - - - - - - - - - - - - - - - - - - - nn";<br/>
<span style="white-spacere for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere cout<<setw(8)<<right<<deck.face<<" of "<br/>
<span style="white-spacere <<left<<setw(8)<<deck.suit<br/>
<span style="white-spacere << ((i + 1) % 2 ? t : n);<br/>
<span style="white-spacere }<br/>
}
View the full article
a) I cannot understand how the DeckOfCards constructor has a direct access to the struct Card, without having a relative command typed inside the class DeckOfCards, i.e. { friend struct Card }
b) for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere deck.face = face[i % 13];<br/>
<span style="white-spacere //cout<<face[i % 13]<<", ";<br/>
<span style="white-spacere deck.suit = suit[i / 13];<br/>
<span style="white-spacere //cout<<suit[i / 13]<<", ";<br/>
<span style="white-spacere }
What is going on here??????? What do these assignments perform and mean?
c) for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere int j = rand() % 52;<br/>
//cout<<j<<", "<<((i + 1) % 2 ?t : n);<span style="white-spacere
<br/>
<span style="white-spacere Card temp = deck;<br/>
<span style="white-spacere deck = deck[j];<br/>
<span style="white-spacere deck[j] = temp;<br/>
<span style="white-spacere }
Why these switches are being selected? What is the puprose of such a permutation?
Thank you community a priori!!!!
<br/>
- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
#include <iostream>
using std::cout;
using std::endl;<br/>
using std::left;<br/>
using std::right;<br/>
<br/>
#include <iomanip><br/>
using std::setw;<br/>
<br/>
#include <cstdlib><br/>
using std::rand;<br/>
using std::srand;<br/>
<br/>
#include <ctime><br/>
using std::time;<br/>
<br/>
#include "header1.h"<br/>
<br/>
DeckOfCards:eckOfCards()<br/>
{<br/>
<span style="white-spacere static char *face[4] =<br/>
<span style="white-spacere {<br/>
<span style="white-spacere "Heart", "Diamonds", <br/>
<span style="white-spacere "Clubs", "Spades"<br/>
<span style="white-spacere },<br/>
<span style="white-spacere *suit[13] =<br/>
<span style="white-spacere {<br/>
<span style="white-spacere "Ace", "Deuce", "Three", "Four", "Five",<br/>
<span style="white-spacere "Six", "Seven", "Eight", "Nine", "Ten",<br/>
<span style="white-spacere "JacK", "Queen", "King"<br/>
<span style="white-spacere };<br/>
<span style="white-spacere for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere deck.face = face[i % 13];<br/>
<span style="white-spacere //cout<<face[i % 13]<<", ";<br/>
<span style="white-spacere deck.suit = suit[i / 13];<br/>
<span style="white-spacere //cout<<suit[i / 13]<<", ";<br/>
<span style="white-spacere }<br/>
<span style="white-spacere srand(time(0));<br/>
}<br/>
void DeckOfCards::shuffle()<br/>
{<br/>
<span style="white-spacere cout<<"nn- - - - - - - - - - - - - - - - - - - - - - - - nn";<br/>
<span style="white-spacere for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere int j = rand() % 52;<br/>
//cout<<j<<", "<<((i + 1) % 2 ?t : n);<span style="white-spacere
<br/>
<span style="white-spacere Card temp = deck;<br/>
<span style="white-spacere deck = deck[j];<br/>
<span style="white-spacere deck[j] = temp;<br/>
<span style="white-spacere }<br/>
}<br/>
void DeckOfCards::deal() const<br/>
{<br/>
<span style="white-spacere cout<<"nn- - - - - - - - - - - - - - - - - - - - - - - - nn";<br/>
<span style="white-spacere for(int i = 0; i < 52; i++)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere cout<<setw(8)<<right<<deck.face<<" of "<br/>
<span style="white-spacere <<left<<setw(8)<<deck.suit<br/>
<span style="white-spacere << ((i + 1) % 2 ? t : n);<br/>
<span style="white-spacere }<br/>
}
View the full article