How to determine Lowest Card by Suit and Number?

Feb 24, 2017 at 8:51pm
I'm writing a game and im trying to determine who has the lowest card. I have written a function which determines every players (2-4) lowest card. Now I'm writing a function to see which one of the player's lowest card is the lowest card in the deck. My issue is how would i code this is only two players are playing? I still need to pass in four variable or is there a better way to do this?

Do determine who has the lowest card i need to compare the suit strength and the cardNumber
1
2
3
4
5
6
7
8
9
//
//*****************************
//DetermineLowestCard Function*
//*****************************
//
CardTemplate determineLowestCard(CardTemplate pOneLowest, CardTemplate pTwoLowest, CardTemplate pThreeLowest, CardTemplate pFourLowest)
{
    //Determine how to set default values of players 3 and four arent playing
}
Feb 25, 2017 at 4:23am
I have written a function which determines every players (2-4) lowest card

This suggests you've already overloaded operator < for the Card class, in that case you could use a variadic template solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

struct Person {int m_age;};
bool operator < (const Person& lhs, const Person& rhs){return lhs.m_age < rhs.m_age;}

template<typename T>
T vmin(const T& val)
{
    return val;
}
template<typename T, typename... Args>
T vmin(const T& val1, const T& val2, const Args&... vs)
{
    return (val1 < val2) ?
      vmin(val1, vs ...) :
      vmin(val2, vs...);
}

int main()
{
    Person a{10}, b{43}, c{7}, d{36}, e{21};
    std::cout << vmin(a, b, c, d, e).m_age;
}

edit: const qualifying the functions
Last edited on Feb 25, 2017 at 4:27am
Feb 25, 2017 at 5:40am
> My issue is how would i code this is only two players are playing?
> I still need to pass in four variable or is there a better way to do this?

One possibility is to provide defaults for the last two arguments:
1
2
3
4
5
6
7
const CardTemplate highest_possible { /* ... */ }; // initialise with highest possible value

CardTemplate determineLowestCard( CardTemplate a, CardTemplate b,
                                  CardTemplate c = highest_possible, CardTemplate d = highest_possible )
{
    // ...
}


Another way is to overload the function (leads to somewhat cleaner code).
1
2
3
4
5
6
7
8
CardTemplate lowest( CardTemplate a, CardTemplate b )
{ /* return the lower of the two */ }

CardTemplate lowest( CardTemplate a, CardTemplate b, CardTemplate c )
{ return lowest( lowest(a,b), c ) ; }

CardTemplate lowest( CardTemplate a, CardTemplate b, CardTemplate c, CardTemplate d  )
{ return lowest( lowest(a,b), lowest(c,d) ) ; }


A third way (arguments have to be placed within {} ) is to use std::initialiser_list a la std::max
http://en.cppreference.com/w/cpp/algorithm/max

In general, for a function which can accept an unlimited number of arguments:
if we want all parameters to be of the same type, favour std::initialiser_list
if we want the type of each parameter to be arbitrary, use a variadic template.
Feb 25, 2017 at 6:39pm
I didnt overload the operator. I was just working with int so i just used an if statement
if(playerHand[i].cardNum < lowestCard.cardNum && playerHand[i].suitStrength < lowestCard.suitStrength)

How would i place defaults for the last two arguments? because each one needs two pieces of info to determine if its smaller or not. Which method would be the most efficient? I was thinking of attempting the overload method. Completely forgot about that.
Topic archived. No new replies allowed.