• Welcome to the new Internet Infidels Discussion Board, formerly Talk Freethought.

The Programming Thread

If tic-tac-toe is too boring, try this (much harder?) game:

As in tic-tac-toe, players alternate turns choosing from among the digits 1,2,3,4,5,6,7,8,9. Once taken, a digit cannot be taken again. A player wins when exactly three of the digits he's taken sum to 15.

This has an almost astonishing answer. It should be better known. Try playing the game with a friend. Or play with me right now; I'll start by taking 2.

I'll try to do that problem more generally, ....

Spoiler alert: To achieve the astonishment and say "Wow," focus on the specific problem: three of {1,2,3,4,5,6,7,8,9} summing to 15.
 
As in tic-tac-toe, players alternate turns choosing from among the digits 1,2,3,4,5,6,7,8,9. Once taken, a digit cannot be taken again. A player wins when exactly three of the digits he's taken sum to 15 satisfy some specified criterion.

If we generalize from "sum to 15" to "satisfy some specified criterion" we define a class which includes tic-tac-toe AND the new game. To play either game, it may be convenient to write the 9 digits on a piece of paper, perhaps in a 3-by-3 square, and label them with an 'X' or an 'O' as they are taken.

It's arbitrary how we arrange the 9 digits on the piece of paper. Let's pick this arrangement:
2 7 6
9 5 1
4 3 8

Notice anything magical about this arrangement?
 
All rows, columns, and diagonals add up to 15.

Players selecting numbers is equivalent to players doing X's and O's in tic-tac-toe, and the victory condition is equivalent to getting three in a straight line. Something that must be checked is whether the absence of such lines means no summation of three digits to 15.

ETA: I checked out this magic square to see if no nonlinear sums were 15, and I found that that is the case. So the match to tic-tac-toe is exact.
 
Last edited:
Moving on to blackjack.

It doesn't implement all the rules. For two players.

This is what I think of as structured programming. Use of functions, and top down execution in the main() code. Generally the way I coded unless there was a reason to use OOP.

Code:
 string deck[52] = {
 " sk"," sq"," sj","s10"," s9"," s8",
 " s7"," s6"," s5"," s4"," s3"," s2"," sa",
 " hk"," hq"," hj","h10"," h9"," h8",
 " h7"," h6"," h5"," h4"," h3"," h2"," ha",
 " dk"," dq"," dj","d10"," d9"," d8",
 " d7"," d6"," d5"," d4"," d3"," d2"," da",
 " ck"," cq"," cj","c10"," c9"," c8",
 " c7"," c6"," c5"," c4"," c3"," c2"," ca"};

int points[52] = {
    10,10,10,10,9,8,7,6,5,4,3,2,1,
    10,10,10,10,9,8,7,6,5,4,3,2,1,
    10,10,10,10,9,8,7,6,5,4,3,2,1,
    10,10,10,10,9,8,7,6,5,4,3,2,1};


const int game_over = 1;
const int FOREVER = 1;

void shuffle(string *str_ptr, int *points_ptr){

    string str_temp = "   ";
    int x = 0, y = 0, points_temp = 0;
    unsigned seed = unsigned (time(NULL));
    srand(seed);
    for (int i = 0; i < 52; i++){
        y = rand()%52;
        x = rand()%52;
        str_temp = str_ptr[x];
        str_ptr[x] = str_ptr[y];
        str_ptr[y] = str_temp;
        points_temp = points_ptr[x];
        points_ptr[x] = points_ptr[y];
        points_ptr[y] = points_temp;
        }//for
}//shuffle()

int check_win(int player,int dealer){

        if((player < 21 && dealer > 21) || player == 21){
            cout<<"Player Wins"<<endl;
            return(game_over);
            }

        if((dealer < 21 && player > 21) || dealer == 21){
            cout<<"Dealer Wins"<<endl;
            return(game_over);
            }

            return(0);
}//check_win()

int main(){

    int player = 0, card_count = 0,dealer = 0;
    char ch = ' ';
    shuffle(deck,points);

    cout<< "Dealer  "<<"Player"<<endl;
    dealer += points[card_count];
    cout<<deck[card_count++]<<"  "<<dealer<<endl;
    player += points[card_count];
    cout<<"        "<<deck[card_count++]<<"   "<<player<<endl;


    while(FOREVER){

        cout<<"Hit Me , Dealer, or Exit  h,d,x - ";
        cin>>ch;

        switch(ch){

            case 'x':
            return(0);
            break;

            case 'h':
            player += points[card_count];
            cout<<"        "<<deck[card_count++]<<"  "<<player<<endl;
            if(check_win(player,dealer) == game_over)return(0);
            break;

            case 'd':
            dealer += points[card_count];
            cout<<deck[card_count++]<<"  "<<dealer<<endl;
            if(check_win(player,dealer) == game_over)return(0);
            break;

            default:
            cout<<"Ilegal Key";return(0);
            break;


        }//switch

      if(card_count > 51){cout<<"Runaway Loop";break;}

    }//while
    return 0;
}//main()
 
Moving on to blackjack.

It doesn't implement all the rules. For two players.

This is what I think of as structured programming. Use of functions, and top down execution in the main() code. Generally the way I coded unless there was a reason to use OOP.
In code this simple there's little use for OOP.

However, I rarely write methods that long. More levels = lower code complexity = harder for bugs to hide.
 
C++:
void shuffle(string *str_ptr, int *points_ptr){

    string str_temp = "   ";
    int x = 0, y = 0, points_temp = 0;
    unsigned seed = unsigned (time(NULL));
    srand(seed);
    for (int i = 0; i < 52; i++){
        y = rand()%52;
        x = rand()%52;
        str_temp = str_ptr[x];
        str_ptr[x] = str_ptr[y];
        str_ptr[y] = str_temp;
        points_temp = points_ptr[x];
        points_ptr[x] = points_ptr[y];
        points_ptr[y] = points_temp;
        }//for
}//shuffle()
BTW it looks like in C++ you can shuffle an array of cards in one line:
C++:
random_shuffle(&a[0], &a[10]); // end must be 10, not 9
In C++11, you can use std::begin and std::end:
random_shuffle(std::begin(a), std::end(a));
Perhaps this would work: (or use "deck")
C++:
random_shuffle(&str_ptr[0], &str_ptr[52]); // end must be 52, not 51
In C++11, you can use std::begin and std::end:
random_shuffle(std::begin(str_ptr), std::end(str_ptr));
Though that doesn't synchronise the shuffling of the points and the deck.... maybe you could use a struct (suit, value, points, etc)
 
Last edited:
C++:
void shuffle(string *str_ptr, int *points_ptr){

    string str_temp = "   ";
    int x = 0, y = 0, points_temp = 0;
    unsigned seed = unsigned (time(NULL));
    srand(seed);
    for (int i = 0; i < 52; i++){
        y = rand()%52;
        x = rand()%52;
        str_temp = str_ptr[x];
        str_ptr[x] = str_ptr[y];
        str_ptr[y] = str_temp;
        points_temp = points_ptr[x];
        points_ptr[x] = points_ptr[y];
        points_ptr[y] = points_temp;
        }//for
}//shuffle()
BTW it looks like in C++ you can shuffle an array of cards in one line:

You can shuffle an array in one line in almost ANY language if someone (perhaps yourself) has already written a function to do that.

A proper "random" shuffle of a deck of 52 cards should, in principle, choose with equal probability among the 52! orderings. Steve's sort doesn't achieve that. An easy way to see that is to note that the code selects, with equal probability, from among (52*52)^52 orderings. This is NOT a multiple of 52!, but being such a multiple is a necessary condition to being proper. (Prove it.)

Writing a proper shuffle is surprisingly difficult. If you've never explored this issue before, try it yourself before peeking at the answer —  Fisher–Yates shuffle. (That code chooses among exactly 52! orderings. [edited])

I recall reading about an improper shuffle in an on-line casino playing with real money! I don't recall details. but think they were exploited.
 
As Hercule Poirot would say, 'I am exceeding the little grey cells', that is all. Mental exercise.

The efficacy a shuffle I'd think comes down to the quality of the random sequence generator. The test for qualality of a shuffle is statistical.

Run many shuffles and plot the results.

You can get USB random number genertors that use random electronic noise.
 
BTW it looks like in C++ you can shuffle an array of cards in one line:
You can shuffle an array in one line in almost ANY language if someone (perhaps yourself) has already written a function to do that.
It looks like random_shuffle is in the "algorithm" library (I assumed it was in the "random" library) Though at least it seems to be a standard C++ library.
A proper "random" shuffle of a deck of 52 cards should, in principle, choose with equal probability among the 52! orderings. Steve's sort doesn't achieve that. An easy way to see that is to note that the code selects, with equal probability, from among (52*52)^52 orderings. This is NOT a multiple of 52!, but being such a multiple is a necessary condition to being proper. (Prove it.)

Writing a proper shuffle is surprisingly difficult. If you've never explored this issue before, try it yourself before peeking at the answer —  Fisher–Yates shuffle. (That code does exactly 52! swaps.)

I recall reading about an improper shuffle in an on-line casino playing with real money! I don't recall details. but think they were exploited.
What about this method - pick a random card out of the pile, then pick another, etc? (assuming a data type that you can remove things from). Though perhaps that can't be called a "shuffle".
 
Last edited:
Well, the shuffle does not work.

I overlooked the fact that the random number generator can generate more than one of the same number over 52 invocations. The result is that some cards will end up missing from the deck.

I eyeballed the output of the shuffle but did not quantitatively test it. A rookie mistake.

It requires a random sequence of 0-51 without duplication. A little more work.
 
Well, the shuffle does not work.

I overlooked the fact that the random number generator can generate more than one of the same number over 52 invocations. The result is that some cards will end up missing from the deck.

I eyeballed the output of the shuffle but did not quantitatively test it. A rookie mistake.

It requires a random sequence of 0-51 without duplication. A little more work.
I asked an AI for an array shuffling array and it is a bit similar to yours:
C++:
void shuffleArray(int array[], int size)
{
    // Use current time as seed for random generator
    srand(time(0));
 
    for (int i=size-1; i>0; i--)
    {
        // Pick a random index from 0 to i
        int j = rand() % (i+1);
 
        // Swap array[i] with the element at random index
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
My method is to use a data type that you can remove items from anywhere in (e.g. a list?). Then you get a random item then remove it.... then remove an item from the remaining items... though perhaps it can't be called a "shuffle". I think it is similar to the "52 pick up" technique.
 
What about this method - pick a random card out of the pile, then pick another, etc? (assuming a data type that you can remove things from). Though perhaps that can't be called a "shuffle".

Yes! That's it! Note that the first pick is among 52 items, the 2nd among 51, and so on. 52x51x50x49x... = 52! This is Fisher-Yates; only the coding details are left.

I'm sure that Steve is a very expert programmer. A large majority of expert programmers do shuffle wrong the first time.

~ ~ ~ ~ ~ ~ ~ ~

Interesting(?) digression.

A Fisher-Yates shuffle will invoke random() 51 times, each random() returning, say, 32 bits. Most of those bits are unnecessary. For example, picking among 16 cards needs only 4 random bits, not 32. Suppose (counterfactually) that random() is very expensive and you want to limit the invocations.

log2 (52!) = 226 approximately, a number of bits that can be obtained with just 7 invocations of a 32-bit random() on average. Can you write a proper 52-card shuffle which makes do with just 226 random bits on average? It is doable but NOT easy.
 
Here is a way to do it: for N entities, make a list of N calls of a random-number generator, then do an index sort on them. Then rearrange those N entities using that index list.

Pseudocode:

The original entities : Ents

Num = len(Ents)

Rand = new array with size Num
for i in 1 to Num:
. Rand(i) = random-number generator
end for
// Can also do this by appending to an empty array

Ixs = list of 1 to Num

// Sort with a comparison function
sort(Ixs, compare(i,j): return Rand(i) < Rand(j))

// If one's sorter supports sort keys, then
sort(Ixs, key(i): Rand(i))

One now has a list of randomized index values: Ixs

Then create a new list with

Shuf = new array with size Num
for i in 1 to Num:
. Shuf(i) = Ents(Ixs(i))
end for
// Can also do this by appending to an empty array
 
I just invented a new game, I call it 'three in a row'. Two players try to place Xs and Os on a 3 by three grid trying to get three in a line.

Runs as a console app.

The formatting did no work right this time.


Code:
onst int wino = 2;
const int stalemate = 1;
const int nowinner = 0;
const int FOREVER = 1;
const int ERROR = 1;
const int NORMAL = 0;
const int ABORT = 1;

void display_board(int *ptr){
    printf("  1  2  3\n");
    printf("1 %c  %c  %c\n", ptr[0],ptr[1],ptr[2]);
    printf("2 %c  %c  %c\n", ptr[3],ptr[4],ptr[5]);
    printf("3 %c  %c  %c\n", ptr[6],ptr[7],ptr[8]);
    printf("\n\n");
}//display_board()

void init_board(int *ptr){for(int i = 0;i<9;i++)ptr[i] = '_';}


int check_win(int *ptr){

    // check rows, columns, diagonals for three in a row
    // if all cells filled and no match stalemate

    int j = 0;

    if( (ptr[0] == 'x' && ptr[1] == 'x' && ptr[2]  == 'x') ||
        (ptr[3] == 'x' && ptr[4] == 'x' && ptr[5]  == 'x') ||
        (ptr[6] == 'x' && ptr[7] == 'x' && ptr[8]  == 'x') ||
        (ptr[0] == 'x' && ptr[3] == 'x' && ptr[6]  == 'x') ||
        (ptr[1] == 'x' && ptr[4] == 'x' && ptr[7]  == 'x') ||
        (ptr[2] == 'x' && ptr[5] == 'x' && ptr[8]  == 'x') ||
        (ptr[0] == 'x' && ptr[4] == 'x' && ptr[8]  == 'x') ||
        (ptr[2] == 'x' && ptr[4] == 'x' && ptr[6]  == 'x') )return(winx);

    if( (ptr[0] == 'o' && ptr[1] == 'o' && ptr[2]  == 'o') ||
        (ptr[3] == 'o' && ptr[4] == 'o' && ptr[5]  == 'o') ||
        (ptr[6] == 'o' && ptr[7] == 'o' && ptr[8]  == '0') ||
        (ptr[0] == 'o' && ptr[3] == 'o' && ptr[6]  == 'o') ||
        (ptr[1] == 'o' && ptr[4] == 'o' && ptr[7]  == 'o') ||
        (ptr[2] == 'o' && ptr[5] == 'o' && ptr[8]  == 'o') ||
        (ptr[0] == 'o' && ptr[4] == 'o' && ptr[8]  == 'o') ||
        (ptr[2] == 'o' && ptr[4] == 'o' && ptr[6]  == 'o'))return(wino);

    for(j = 0;j < 9;j++)if(ptr[j] == '_')return(nowinner);
    return(stalemate);

}//check_win()


int user_input(int *ptr){

    // enter x/o and update or error checking board
    // no input protection or error checking

    char ch, row,col;

    cout<<"Abort? y/n  -  "<<endl;
    cin>>ch;
    if(ch == 'y') return(ABORT);
    cout<<"Enter Row 1,2,3 - "<<endl;
    cin>>row;
    cout<<"Enter Column 1,2,3 - "<<endl;
    cin>>col;
    cout<<"X or O  x/o - "<<endl;
    cin>>ch;
    if(row == '1' && col == '1')ptr[0] = ch;
    if(row == '1' && col == '2')ptr[1] = ch;
    if(row == '1' && col == '3')ptr[2] = ch;
    if(row == '2' && col == '1')ptr[3] = ch;
    if(row == '2' && col == '2')ptr[4] = ch;
    if(row == '2' && col == '3')ptr[5] = ch;
    if(row == '3' && col == '1')ptr[6] = ch;
    if(row == '3' && col == '2')ptr[7] = ch;
    if(row == '3' && col == '3')ptr[8] = ch;

    return(NORMAL);

}//user_input()

int main(){

    int game_board[9];
    init_board(&game_board[0]);
    display_board(&game_board[0]);

    while(FOREVER){

        if(user_input(&game_board[0])){cout<<"ABORTED"<<endl;return(ABORT);}
        display_board(&game_board[0]);

        switch(check_win(&game_board[0])){
            case winx:           cout<<"X Wins"<<endl;
                                       return(NORMAL);
                                       break;
            case wino:          cout<<"O Wins"<<endl;
                                       return(NORMAL);
                                       break;
            case nowinner:   cout<<"No Winner"<<endl;
                                       break;
            case stalemate:   cout<<"Stalemate"<<endl;
                                       return(NORMAL);
                                      break;
            default:              cout<<"ERROR"<<endl;
                                      return(ERROR);
                                      break;

        }//switch

    }//while

    return (NORMAL);
}//main()

The first shuffle used a vector of 52 random ints from 0-51. The problem with that is within the 52 random numbers there can be multiples of the same same number resulting in picking the same card twice.

The solution I worked out was to iterate until there are the numbers 0-51 in random order. I have no doubt it has been done before.

A random number is generated and checked against previous numbers,If it has already been selected generate another random number.

One problem I had was having to explicitly cast the random() output as an int. Otherwise the comparison proctors would not work right. I have seen it before when comparing signed ints with unsigned ints.

Code:
void shuffle(string *str_ptr, int *points_ptr){

    string str_temp = "   ";
    int flag = 0, j = 0,i = 0, x = 0, points_temp = 0, cnt = 0,r[52];
    string str_copy[52];
    int points_copy[52];
    for(i = 0; i < 52; i++){
        str_copy[i] = "xxx";
        points_copy[i] = 0;
        r[i] = 100;

    }
    unsigned seed = unsigned (time(NULL));
    srand(seed);

    for(i = 0; i < 52; i++){

       cnt = 0;
       while(FOREVER){

            if(cnt++ == 20000){cout<<"Runaway Loop";return;}
            x = int(rand()%52);
            if(i == 0)break;
            flag = 0;
            for(j = 0;j < 52;j++)if(r[j] == x)flag = 1;
            if(!flag)break;
        }//while
        r[i] = x;
        str_copy[i] = str_ptr[x];
        points_copy[i] = points_ptr[x];

    }//i

    for(i = 0; i < 52; i++){
        str_ptr[i] = str_copy[i];
        points_ptr[i] = points_copy[i];
        //cout<<str_ptr[i]<<"   "<<str_copy[i]<<endl;
        }//for

}//shuffle()
 
I may as well show my own code. This is NOT the code I bragged of earlier, which needs less than 230 random bits for the entire 52-card shuffle.
Code:
/* return a uniform variate on {0, 1, 2, 3, ..., siz - 1} */
unsigned int swr_index(unsigned int siz)
{
        unsigned long r, quot, qs;
        unsigned int resi;
        do {
                r = random_32();
                quot = r / siz;
                resi = r % siz;
                qs = quot * siz - 1;
        } while (qs + siz < r);
        return resi;
}


/* Shuffle the m-sized array[]
 * Define 'struct shufflee' to suit.
 * Structure assignments were permitted long before ANSI C.
 * I recommend AGAINST replacing the struct with a char array.
 */
void swr_shuffle(struct shufflee *array, int siz)
{
        int     j;
        struct shufflee t;

        for (; siz > 1; siz--, array++) {
                j = swr_index(siz);
                if (j == 0) continue;
                t = array[j];
                array[j] = array[0];
                array[0] = t;
        }
}

The range of random() is not a multiple of 52 — So there will be a tiny bias toward small numbers. (unsigned)FFFFFFD0 is a multiple of 52 but there is further room only for 0-49. 50 and 51 won't happen. If you code perfectionistically then you might circumvent with a variant of steve's own while, as I have shown above.
 
I may as well show my own code. This is NOT the code I bragged of earlier, which needs less than 230 random bits for the entire 52-card shuffle.
Code:
/* return a uniform variate on {0, 1, 2, 3, ..., siz - 1} */
unsigned int swr_index(unsigned int siz)
{
        unsigned long r, quot, qs;
        unsigned int resi;
        do {
                r = random_32();
                quot = r / siz;
                resi = r % siz;
                qs = quot * siz - 1;
        } while (qs + siz < r);
        return resi;
}


/* Shuffle the m-sized array[]
 * Define 'struct shufflee' to suit.
 * Structure assignments were permitted long before ANSI C.
 * I recommend AGAINST replacing the struct with a char array.
 */
void swr_shuffle(struct shufflee *array, int siz)
{
        int     j;
        struct shufflee t;

        for (; siz > 1; siz--, array++) {
                j = swr_index(siz);
                if (j == 0) continue;
                t = array[j];
                array[j] = array[0];
                array[0] = t;
        }
}

The range of random() is not a multiple of 52 — So there will be a tiny bias toward small numbers. (unsigned)FFFFFFD0 is a multiple of 52 but there is further room only for 0-49. 50 and 51 won't happen. If you code perfectionistically then you might circumvent with a variant of steve's own while, as I have shown above.
If I was till working I'd add that to my toolbox. A much cleaner solution.
 
For sorting itself, numerous algorithms have been devised. Naive ones like bubblesort require O(n2) operations, for a list with length n. The theoretical best case is O(n*log(n)) and some algorithms have similar time dependence.

The plain-C standard library includes a sort function, qsort in stdlib.h

That function has signature

void qsort(void *list, size_t list_length, size_t list_element_size, int (*compare)(const void *, const void *))

The compare function has signature

int compare(const void *x, const void *y)

The comparison function should return these results for comparing dereferenced x and y:
x* < y*: -1
x* ~ y*: 0
x* > y*: 1

That means that if one wants to transmit data to one's compare function, one has to do it in a global varable.

To avoid that problem, some versions of Unix have alternatives that allow one to transmit data to the comparison function.

GNU and Linux have

void qsort_r(void *list, size_t list_length, size_t list_element_size, int (*compare)(const void *, const void *, void *), void *compare_data)

int compare(const void *x, const void *y, void *compare_data)

FreeBSD and macOS ("AppleBSD"?) have

void qsort_r(void *list, size_t list_length, size_t list_element_size, void *compare_data, int (*compare)( void *, const void *, const void *))

void qsort_b(void *list, size_t list_length, size_t list_element_size, int (^compare)(const void *, const void *))

The second one uses an Objective-C feature called a block: an anonymous function or lambda that can capture surrounding data.
 
C++ is not just "C with classes" - it supports generics in the form of template functions and template classes.

It has a Standard Template Library with template classes and functions for a variety of things, like container classes.

In its file "algorithm" are a variety of algorithms, like a sort algorithm: "sort".

template <class RAI> void sort( RAI first, RAI last);
template <class RAI, class Cmpr> void sort (RAI first, RAI last, Cmpr comp);

RAI = RandomAccessIterator, Cmpr = Compare

The arg "first" is the location of the first item to sort and the arg "last" is one ahead of the location of the last item.

In the first function, the comparison function is operator< -- the built-in operator for numeric types, and whatever one might define for whatever object class one uses.

In the second function, one can specify the comparison function explicitly. This function must return something that can be converted to bool, and it ought to work like "<".

The container classes have members that return iterators in them, members like begin() and end()

vector v;
or else vector v(something);

(put some stuff in v)

sort(v.begin(), v.end());
 
Recent versions have an anonymous-function or lambda feature.

[captured variables] (parameters) {body of code}

If one wants to assign an anonymous function to a variable, then an easy way to do it is with the "auto" declaration:

auto lambda_function = [captured variables] (parameters) {body of code};

The "auto" type parameter can be used for anything where the compiler can infer the variable's type.

int x, y;
...
auto z = x*y;
 
Speaking of classes.

A benefit of using classes is that they are a subsystem in themselves. The constructor automatically configures and initializes the class when it is created at run time. It makes coding the rest of app easier. The contructor is a function with the class name.

Consider an online blackjack application with multiple player and multiple simultaneous games. Create a an object for a deck and simply spawn multiple objects to support multiple games.

On a lare app the deck class woud be compiled to an object file and library. If you make a change to the deck object you only have to rebuild the class object not the entire system.

Code:
class Deck{
public:

    int card_count = 0, point = 0;
    string card = "   ";

    void next_card(void); //function prototype
    //contructor
    Deck(){
    int i = 0, j = 0, cnt = 0, flag = 0;
    int x = 0,r[52];
    for(i = 0; i < 52;i++){r[i] = 100;rn[i] = 0;}
    unsigned seed = unsigned (time(NULL));
    srand(seed);

    for(i = 0; i < 52; i++){
        do{
            if(cnt++ == 20000)cout<<"Runaway Loop";
            x = int(rand()%52);
            if(i == 0)break;
            flag = 0;
            for(j = 0;j < 52;j++)if(r[j] == x)flag = 1;
        }while(flag);
        r[i] = x;
        rn[i] = x;
    }//i
    }//Deck()

private:

    int rn[52];

    string cards[52] = {
    " sk"," sq"," sj","s10"," s9"," s8",
    " s7"," s6"," s5"," s4"," s3"," s2"," sa",
    " hk"," hq"," hj","h10"," h9"," h8",
    " h7"," h6"," h5"," h4"," h3"," h2"," ha",
    " dk"," dq"," dj","d10"," d9"," d8",
    " d7"," d6"," d5"," d4"," d3"," d2"," da",
    " ck"," cq"," cj","c10"," c9"," c8",
    " c7"," c6"," c5"," c4"," c3"," c2"," ca"};

    int points[52] = {
    10,10,10,10,9,8,7,6,5,4,3,2,1,
    10,10,10,10,9,8,7,6,5,4,3,2,1,
    10,10,10,10,9,8,7,6,5,4,3,2,1,
    10,10,10,10,9,8,7,6,5,4,3,2,1};

};//Deck

 void Deck::next_card(void){
    card = cards[rn[card_count]];
    point = points[rn[card_count++]];
}//next_card()




int main(){

    int i = 0, j = 0, n_players = 3;
    Deck deck[n_players];  //an array of casses

    for(i = 0; i < 52; i++){
        deck[0].next_card();
        deck[1].next_card();
        deck[2].next_card();
        cout<<deck[0].card_count
        <<"   "<<deck[0].card
        <<"   "<<deck[0].point
        <<"   "<<deck[1].card
        <<"   "<<deck[1].point
        <<"   "<<deck[2].card
        <<"   "<<deck[2].point
        <<endl;
        }
    return(0);
}//main()
 
Back
Top Bottom