What is going on ??????????????????????????????????????????????????

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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-space:pre {<br/>
<span style="white-space:pre deck.face = face[i % 13];<br/>
<span style="white-space:pre //cout<<face[i % 13]<<", ";<br/>
<span style="white-space:pre deck.suit = suit[i / 13];<br/>
<span style="white-space:pre //cout<<suit[i / 13]<<", ";<br/>
<span style="white-space:pre }

What is going on here??????? What do these assignments perform and mean?
c) for(int i = 0; i < 52; i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre int j = rand() % 52;<br/>
//cout<<j<<", "<<((i + 1) % 2 ?t : n);<span style="white-space:pre
<br/>
<span style="white-space:pre Card temp = deck;<br/>
<span style="white-space:pre deck = deck[j];<br/>
<span style="white-space:pre deck[j] = temp;<br/>
<span style="white-space:pre }

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::DeckOfCards()<br/>
{<br/>
<span style="white-space:pre static char *face[4] =<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre "Heart", "Diamonds", <br/>
<span style="white-space:pre "Clubs", "Spades"<br/>
<span style="white-space:pre },<br/>
<span style="white-space:pre *suit[13] =<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre "Ace", "Deuce", "Three", "Four", "Five",<br/>
<span style="white-space:pre "Six", "Seven", "Eight", "Nine", "Ten",<br/>
<span style="white-space:pre "JacK", "Queen", "King"<br/>
<span style="white-space:pre };<br/>
<span style="white-space:pre for(int i = 0; i < 52; i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre deck.face = face[i % 13];<br/>
<span style="white-space:pre //cout<<face[i % 13]<<", ";<br/>
<span style="white-space:pre deck.suit = suit[i / 13];<br/>
<span style="white-space:pre //cout<<suit[i / 13]<<", ";<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre srand(time(0));<br/>
}<br/>
void DeckOfCards::shuffle()<br/>
{<br/>
<span style="white-space:pre cout<<"nn- - - - - - - - - - - - - - - - - - - - - - - - nn";<br/>
<span style="white-space:pre for(int i = 0; i < 52; i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre int j = rand() % 52;<br/>
//cout<<j<<", "<<((i + 1) % 2 ?t : n);<span style="white-space:pre
<br/>
<span style="white-space:pre Card temp = deck;<br/>
<span style="white-space:pre deck = deck[j];<br/>
<span style="white-space:pre deck[j] = temp;<br/>
<span style="white-space:pre }<br/>
}<br/>
void DeckOfCards::deal() const<br/>
{<br/>
<span style="white-space:pre cout<<"nn- - - - - - - - - - - - - - - - - - - - - - - - nn";<br/>
<span style="white-space:pre for(int i = 0; i < 52; i++)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre cout<<setw(8)<<right<<deck.face<<" of "<br/>
<span style="white-space:pre <<left<<setw(8)<<deck.suit<br/>
<span style="white-space:pre << ((i + 1) % 2 ? t : n);<br/>
<span style="white-space:pre }<br/>
}

View the full article
 

Similar threads

D
Replies
0
Views
104
Donald Uko
D
U
Replies
0
Views
206
UnixWolf
U
A
Replies
0
Views
186
Arash_89
A
A
Replies
0
Views
153
abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL
A
Back
Top