Two Players Card Game

Two players are playing a card game. Each card consists of a rank (from 1 to 13) and a suit (S, H, C, or D). For any two cards, the card with a higher rank is considered of higher value. If they have the same rank, the card with a better suit is considered of higher value. Suit 'S' is better than suit 'H', which is better than suit 'C', which is better than suit 'D'. The input will never contain two cards with the same rank and suit. The output array must contain the card indexes sorted from a higher value to a lower value.
I was able to output the indexes but I can't implement the suit array to the code.
The final output should be {0,4,2,1,3,5}.

P.S. I'm not allowed to include any libraries other than <iostream>.
Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;

int main()
{
    int n = 6;
    int output[n];
    int count = 0, max = 0, index;
    int rank[] = {10,7,7,4,10,4};
    char suit[] = {'S','D','C','C','H','D'};
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < n ; j++)
        {
            if (rank[j] > max)
            {
                max = rank[j];
                index = j;
            }
        }
        output[count]= index;
        count++;
        rank[index]=0;
        max=0;
    }
    for(int i=0 ; i<n ; i++)
        cout<<output[i] << " ";
}
Last edited on
Create a function that can score a suit so you can compare in an ordering.
1
2
3
4
5
6
7
int valueOfSuit(char suit) {
  if ( suit == 'S' ) return 3;
  if ( suit == 'H' ) return 2;
  if ( suit == 'C' ) return 1;
  if ( suit == 'D' ) return 0;
  return -1;  // error
}


Then it would be something like this at a suitable point in the code.
 
if ( valueOfSuit(suit[j]) > valueOfSuit(suit[index]) )

The way that you store the card data doesn't have to be the same as the way you present the data to the user. So you can store the suit as a number from 0-3 (or an enum if you've learned about those), and then convert them to the appropriate characters when you print the values:

1
2
3
4
5
6
7
struct Card {
    int suit;    // 0=D, 1=C, 2=H, 3=S
    int value;
    int index; // index original index.
    bool lessThan(Card &other);   // return true if this card is "less than" the other card  
    void print(ostream &os);   // Print the card to the stream, converting the suit to a char
};


Once you have the lessThan and print() methods, the rest of the code is a pretty easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>

enum suit { diamond, club, heart, spade };
enum rank
{
    two, three, four, five, six, seven, eight, nine,
    ten, jack, queen, king, ace
};

char suit_name[][10] = {"diamonds", "clubs", "hearts", "spades"};

char rank_name[][10] =
{"two", "three", "four", "five", "six", "seven", "eight", "nine",
    "ten", "jack", "queen", "king", "ace"
};

struct Card
{
    int suit;
    int rank;
    
    Card(int aSuit, int aRank)
    {
        suit = aSuit;
        rank = aRank;
    }
    
    int value()
    {
        return suit * 13 + rank;
    }
    
    void print()
    {
        std::cout
        << rank << '\t' << suit << '\t' << rank_name[rank]
        << " of " << suit_name[suit]
        <<  "\tvalue: " << value() << '\n';
    }
    
};

int main()
{
    // A COUPLE OF TRIAL CARDS
    Card deal_1(diamond, ace);
    deal_1.print();
    
    Card deal_2(heart, ace);
    deal_2.print();
    
    Card deal_3(heart, king);
    deal_3.print();
    
    std::cout << '\n';
    
    // SHOW A WHOLE DECK
    Card temp(0,0);
    for(int sut = diamond; sut <=  spade; sut++)
    {
        temp.suit = sut;
        for (int rnk = two; rnk <= ace; rnk++)
        {
            temp.rank = rnk;
            temp.print();
        }
        std::cout << '\n';
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.