Card game ranking
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
Badugi is a 4 card low hand poker game.
i / 13 is suit and i % 13 is rank. Cards from 0 to 51 inclusive.
Straights and flushes do not count. The best hand is A234. The worst hand is KKKK.
If you match on either a suit or rank you take out a card. Any 4 card hand beats a 3 card hand. Any 3 card hand beats a 2 card hand....
The way I approached was to start with a score of 4 and take 1 away on a match. Need to match on the or (rank or suite) as a card that matches on both is still just one card eliminated.
Would use this to mark how many hands you are ahead and behind.
Please comment on style and speed.
public static int BadugiStrength(int cards)
int score = 4;
int i = cards[0];
for(int j = 1; j <= 3; j ++)
i = cards[1];
for (int j = 2; j <= 3; j++)
i / 13 == cards[j] / 13)
score--;
break;
if (cards[2] % 13 == cards[3] % 13
public static int Badugi()
int counter = 0;
int hand = new int[4];
int scores = new int[5];
for (int i = 51; i >=3; i--)
hand[0] = i;
for (int j = i - 1; j >= 2; j--)
hand[1] = j;
for (int k = j - 1; k >= 1; k--)
hand[2] = k;
for (int m = k - 1; m >= 0; m--)
int score = BadugiStrength(hand);
scores[score]++;
counter++;
Debug.WriteLine($"4 card scores[4] 3 card scores[3] 2 card scores[2] 1 card scores[1] total scores[4] + scores[3] + scores[2] + scores[1] counter");
return counter;
c# playing-cards
add a comment |Â
up vote
5
down vote
favorite
Badugi is a 4 card low hand poker game.
i / 13 is suit and i % 13 is rank. Cards from 0 to 51 inclusive.
Straights and flushes do not count. The best hand is A234. The worst hand is KKKK.
If you match on either a suit or rank you take out a card. Any 4 card hand beats a 3 card hand. Any 3 card hand beats a 2 card hand....
The way I approached was to start with a score of 4 and take 1 away on a match. Need to match on the or (rank or suite) as a card that matches on both is still just one card eliminated.
Would use this to mark how many hands you are ahead and behind.
Please comment on style and speed.
public static int BadugiStrength(int cards)
int score = 4;
int i = cards[0];
for(int j = 1; j <= 3; j ++)
i = cards[1];
for (int j = 2; j <= 3; j++)
i / 13 == cards[j] / 13)
score--;
break;
if (cards[2] % 13 == cards[3] % 13
public static int Badugi()
int counter = 0;
int hand = new int[4];
int scores = new int[5];
for (int i = 51; i >=3; i--)
hand[0] = i;
for (int j = i - 1; j >= 2; j--)
hand[1] = j;
for (int k = j - 1; k >= 1; k--)
hand[2] = k;
for (int m = k - 1; m >= 0; m--)
int score = BadugiStrength(hand);
scores[score]++;
counter++;
Debug.WriteLine($"4 card scores[4] 3 card scores[3] 2 card scores[2] 1 card scores[1] total scores[4] + scores[3] + scores[2] + scores[1] counter");
return counter;
c# playing-cards
@BrunoCosta i / 13 is wuit and i % 13 is rank. From what I can tell it is working.
â paparazzo
Apr 16 at 19:21
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
Badugi is a 4 card low hand poker game.
i / 13 is suit and i % 13 is rank. Cards from 0 to 51 inclusive.
Straights and flushes do not count. The best hand is A234. The worst hand is KKKK.
If you match on either a suit or rank you take out a card. Any 4 card hand beats a 3 card hand. Any 3 card hand beats a 2 card hand....
The way I approached was to start with a score of 4 and take 1 away on a match. Need to match on the or (rank or suite) as a card that matches on both is still just one card eliminated.
Would use this to mark how many hands you are ahead and behind.
Please comment on style and speed.
public static int BadugiStrength(int cards)
int score = 4;
int i = cards[0];
for(int j = 1; j <= 3; j ++)
i = cards[1];
for (int j = 2; j <= 3; j++)
i / 13 == cards[j] / 13)
score--;
break;
if (cards[2] % 13 == cards[3] % 13
public static int Badugi()
int counter = 0;
int hand = new int[4];
int scores = new int[5];
for (int i = 51; i >=3; i--)
hand[0] = i;
for (int j = i - 1; j >= 2; j--)
hand[1] = j;
for (int k = j - 1; k >= 1; k--)
hand[2] = k;
for (int m = k - 1; m >= 0; m--)
int score = BadugiStrength(hand);
scores[score]++;
counter++;
Debug.WriteLine($"4 card scores[4] 3 card scores[3] 2 card scores[2] 1 card scores[1] total scores[4] + scores[3] + scores[2] + scores[1] counter");
return counter;
c# playing-cards
Badugi is a 4 card low hand poker game.
i / 13 is suit and i % 13 is rank. Cards from 0 to 51 inclusive.
Straights and flushes do not count. The best hand is A234. The worst hand is KKKK.
If you match on either a suit or rank you take out a card. Any 4 card hand beats a 3 card hand. Any 3 card hand beats a 2 card hand....
The way I approached was to start with a score of 4 and take 1 away on a match. Need to match on the or (rank or suite) as a card that matches on both is still just one card eliminated.
Would use this to mark how many hands you are ahead and behind.
Please comment on style and speed.
public static int BadugiStrength(int cards)
int score = 4;
int i = cards[0];
for(int j = 1; j <= 3; j ++)
i = cards[1];
for (int j = 2; j <= 3; j++)
i / 13 == cards[j] / 13)
score--;
break;
if (cards[2] % 13 == cards[3] % 13
public static int Badugi()
int counter = 0;
int hand = new int[4];
int scores = new int[5];
for (int i = 51; i >=3; i--)
hand[0] = i;
for (int j = i - 1; j >= 2; j--)
hand[1] = j;
for (int k = j - 1; k >= 1; k--)
hand[2] = k;
for (int m = k - 1; m >= 0; m--)
int score = BadugiStrength(hand);
scores[score]++;
counter++;
Debug.WriteLine($"4 card scores[4] 3 card scores[3] 2 card scores[2] 1 card scores[1] total scores[4] + scores[3] + scores[2] + scores[1] counter");
return counter;
c# playing-cards
edited Apr 16 at 19:22
asked Apr 15 at 16:16
paparazzo
4,8131730
4,8131730
@BrunoCosta i / 13 is wuit and i % 13 is rank. From what I can tell it is working.
â paparazzo
Apr 16 at 19:21
add a comment |Â
@BrunoCosta i / 13 is wuit and i % 13 is rank. From what I can tell it is working.
â paparazzo
Apr 16 at 19:21
@BrunoCosta i / 13 is wuit and i % 13 is rank. From what I can tell it is working.
â paparazzo
Apr 16 at 19:21
@BrunoCosta i / 13 is wuit and i % 13 is rank. From what I can tell it is working.
â paparazzo
Apr 16 at 19:21
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
I'm focusing on the BadugiStrength
function.
I like that BadugiStrength
is a separate function for calculating the score of a single hand. That makes it easy to reuse and test individually. Any refactoring you make can easily be verified through tests.
It seems a little cumbersome to store two kinds of information about the card (suit and rank) into a single int
variable. Since this is C#, I encourage either a class or struct. That way you can have two separate properties for suit and rank. (You can use integers or make enumerations for the suit and rank).
If you stick with a single int
for each card, I recommend pulling the suit and rank comparisons into separate functions (though this may be useful even if you switch away from using a single int
).
For example, you an IsSameSuit
function would handle just the % 13
checks.
private static bool IsSameSuit(int card1, int card2)
return (card1 % 13) == (card2 % 13)
And similarly for an IsSameRank
function.
(I find the extra parentheses helpful because I don't have to remember precedence rules, but that's a matter of opinion. Feel free to leave them out.)
Then your if blocks can be rewritten as
if(IsSameSuit(i, cards[j]) || IsSameRank(i, cards[j]))
score--;
break;
You have a loop for the second card of each comparison (the j
loops). I'd like to see the same type of loop for the first card (the i
variable).
public static int BadugiStrength(int cards)
int score = 4;
for (int i = 0; i <= 2; i++)
for (int j = i + 1; j <= 3; j++)
cards[i] / 13 == cards[j] / 13)
score--;
break;
return score;
There are a lot of magic numbers in the code (int score = 4
, j <= 3
). I've never heard of Badugi, so I do not know if there are variants with a different hand size. If you declare a constant for the hand size, passing a five card hand would be an easy change to support. A constant for max rank (13) can handle smaller/larger deck sizes. It also means later when searching the code base, you can look for the 13 that means "card rank" and keep it different from another type of 13 (such as shoe size, player count, game length, etc).
Even better than a constant, you could use the built in length property of the array. Start your score
at cards.Length
, and use the length to set your array indices.
Putting it all together:
public struct Card
public int Suit;
public int Rank;
public static int BadugiStrength(card cards)
int score = cards.Length;
for (int i = 0; i < cards.Length; i++)
for (int j = i + 1; j < cards.Length; j++)
if(IsSameSuit(cards[i], cards[j])
return score;
private static bool IsSameSuit(card card1, card card2)
return card1.Suit == card2.Suit
private static bool IsSameRank(card card1, card card2)
return card1.Rank == card2.Rank
If the hand size changed then the number of loops would need to change. Or go with a radical different approach that is not apparent to me.
â paparazzo
Apr 16 at 20:12
I added a couple more examples, including a final example that accumulates my suggestions. Does that help you understand my example of the hand size changing?
â Brian J
Apr 16 at 20:25
1
I got the same answer. I see this just blows over the J loop on the last pass. I like this.
â paparazzo
Apr 16 at 20:58
I forgot to commend you on having a separate, testable function!
â Brian J
Apr 16 at 22:57
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
I'm focusing on the BadugiStrength
function.
I like that BadugiStrength
is a separate function for calculating the score of a single hand. That makes it easy to reuse and test individually. Any refactoring you make can easily be verified through tests.
It seems a little cumbersome to store two kinds of information about the card (suit and rank) into a single int
variable. Since this is C#, I encourage either a class or struct. That way you can have two separate properties for suit and rank. (You can use integers or make enumerations for the suit and rank).
If you stick with a single int
for each card, I recommend pulling the suit and rank comparisons into separate functions (though this may be useful even if you switch away from using a single int
).
For example, you an IsSameSuit
function would handle just the % 13
checks.
private static bool IsSameSuit(int card1, int card2)
return (card1 % 13) == (card2 % 13)
And similarly for an IsSameRank
function.
(I find the extra parentheses helpful because I don't have to remember precedence rules, but that's a matter of opinion. Feel free to leave them out.)
Then your if blocks can be rewritten as
if(IsSameSuit(i, cards[j]) || IsSameRank(i, cards[j]))
score--;
break;
You have a loop for the second card of each comparison (the j
loops). I'd like to see the same type of loop for the first card (the i
variable).
public static int BadugiStrength(int cards)
int score = 4;
for (int i = 0; i <= 2; i++)
for (int j = i + 1; j <= 3; j++)
cards[i] / 13 == cards[j] / 13)
score--;
break;
return score;
There are a lot of magic numbers in the code (int score = 4
, j <= 3
). I've never heard of Badugi, so I do not know if there are variants with a different hand size. If you declare a constant for the hand size, passing a five card hand would be an easy change to support. A constant for max rank (13) can handle smaller/larger deck sizes. It also means later when searching the code base, you can look for the 13 that means "card rank" and keep it different from another type of 13 (such as shoe size, player count, game length, etc).
Even better than a constant, you could use the built in length property of the array. Start your score
at cards.Length
, and use the length to set your array indices.
Putting it all together:
public struct Card
public int Suit;
public int Rank;
public static int BadugiStrength(card cards)
int score = cards.Length;
for (int i = 0; i < cards.Length; i++)
for (int j = i + 1; j < cards.Length; j++)
if(IsSameSuit(cards[i], cards[j])
return score;
private static bool IsSameSuit(card card1, card card2)
return card1.Suit == card2.Suit
private static bool IsSameRank(card card1, card card2)
return card1.Rank == card2.Rank
If the hand size changed then the number of loops would need to change. Or go with a radical different approach that is not apparent to me.
â paparazzo
Apr 16 at 20:12
I added a couple more examples, including a final example that accumulates my suggestions. Does that help you understand my example of the hand size changing?
â Brian J
Apr 16 at 20:25
1
I got the same answer. I see this just blows over the J loop on the last pass. I like this.
â paparazzo
Apr 16 at 20:58
I forgot to commend you on having a separate, testable function!
â Brian J
Apr 16 at 22:57
add a comment |Â
up vote
4
down vote
accepted
I'm focusing on the BadugiStrength
function.
I like that BadugiStrength
is a separate function for calculating the score of a single hand. That makes it easy to reuse and test individually. Any refactoring you make can easily be verified through tests.
It seems a little cumbersome to store two kinds of information about the card (suit and rank) into a single int
variable. Since this is C#, I encourage either a class or struct. That way you can have two separate properties for suit and rank. (You can use integers or make enumerations for the suit and rank).
If you stick with a single int
for each card, I recommend pulling the suit and rank comparisons into separate functions (though this may be useful even if you switch away from using a single int
).
For example, you an IsSameSuit
function would handle just the % 13
checks.
private static bool IsSameSuit(int card1, int card2)
return (card1 % 13) == (card2 % 13)
And similarly for an IsSameRank
function.
(I find the extra parentheses helpful because I don't have to remember precedence rules, but that's a matter of opinion. Feel free to leave them out.)
Then your if blocks can be rewritten as
if(IsSameSuit(i, cards[j]) || IsSameRank(i, cards[j]))
score--;
break;
You have a loop for the second card of each comparison (the j
loops). I'd like to see the same type of loop for the first card (the i
variable).
public static int BadugiStrength(int cards)
int score = 4;
for (int i = 0; i <= 2; i++)
for (int j = i + 1; j <= 3; j++)
cards[i] / 13 == cards[j] / 13)
score--;
break;
return score;
There are a lot of magic numbers in the code (int score = 4
, j <= 3
). I've never heard of Badugi, so I do not know if there are variants with a different hand size. If you declare a constant for the hand size, passing a five card hand would be an easy change to support. A constant for max rank (13) can handle smaller/larger deck sizes. It also means later when searching the code base, you can look for the 13 that means "card rank" and keep it different from another type of 13 (such as shoe size, player count, game length, etc).
Even better than a constant, you could use the built in length property of the array. Start your score
at cards.Length
, and use the length to set your array indices.
Putting it all together:
public struct Card
public int Suit;
public int Rank;
public static int BadugiStrength(card cards)
int score = cards.Length;
for (int i = 0; i < cards.Length; i++)
for (int j = i + 1; j < cards.Length; j++)
if(IsSameSuit(cards[i], cards[j])
return score;
private static bool IsSameSuit(card card1, card card2)
return card1.Suit == card2.Suit
private static bool IsSameRank(card card1, card card2)
return card1.Rank == card2.Rank
If the hand size changed then the number of loops would need to change. Or go with a radical different approach that is not apparent to me.
â paparazzo
Apr 16 at 20:12
I added a couple more examples, including a final example that accumulates my suggestions. Does that help you understand my example of the hand size changing?
â Brian J
Apr 16 at 20:25
1
I got the same answer. I see this just blows over the J loop on the last pass. I like this.
â paparazzo
Apr 16 at 20:58
I forgot to commend you on having a separate, testable function!
â Brian J
Apr 16 at 22:57
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
I'm focusing on the BadugiStrength
function.
I like that BadugiStrength
is a separate function for calculating the score of a single hand. That makes it easy to reuse and test individually. Any refactoring you make can easily be verified through tests.
It seems a little cumbersome to store two kinds of information about the card (suit and rank) into a single int
variable. Since this is C#, I encourage either a class or struct. That way you can have two separate properties for suit and rank. (You can use integers or make enumerations for the suit and rank).
If you stick with a single int
for each card, I recommend pulling the suit and rank comparisons into separate functions (though this may be useful even if you switch away from using a single int
).
For example, you an IsSameSuit
function would handle just the % 13
checks.
private static bool IsSameSuit(int card1, int card2)
return (card1 % 13) == (card2 % 13)
And similarly for an IsSameRank
function.
(I find the extra parentheses helpful because I don't have to remember precedence rules, but that's a matter of opinion. Feel free to leave them out.)
Then your if blocks can be rewritten as
if(IsSameSuit(i, cards[j]) || IsSameRank(i, cards[j]))
score--;
break;
You have a loop for the second card of each comparison (the j
loops). I'd like to see the same type of loop for the first card (the i
variable).
public static int BadugiStrength(int cards)
int score = 4;
for (int i = 0; i <= 2; i++)
for (int j = i + 1; j <= 3; j++)
cards[i] / 13 == cards[j] / 13)
score--;
break;
return score;
There are a lot of magic numbers in the code (int score = 4
, j <= 3
). I've never heard of Badugi, so I do not know if there are variants with a different hand size. If you declare a constant for the hand size, passing a five card hand would be an easy change to support. A constant for max rank (13) can handle smaller/larger deck sizes. It also means later when searching the code base, you can look for the 13 that means "card rank" and keep it different from another type of 13 (such as shoe size, player count, game length, etc).
Even better than a constant, you could use the built in length property of the array. Start your score
at cards.Length
, and use the length to set your array indices.
Putting it all together:
public struct Card
public int Suit;
public int Rank;
public static int BadugiStrength(card cards)
int score = cards.Length;
for (int i = 0; i < cards.Length; i++)
for (int j = i + 1; j < cards.Length; j++)
if(IsSameSuit(cards[i], cards[j])
return score;
private static bool IsSameSuit(card card1, card card2)
return card1.Suit == card2.Suit
private static bool IsSameRank(card card1, card card2)
return card1.Rank == card2.Rank
I'm focusing on the BadugiStrength
function.
I like that BadugiStrength
is a separate function for calculating the score of a single hand. That makes it easy to reuse and test individually. Any refactoring you make can easily be verified through tests.
It seems a little cumbersome to store two kinds of information about the card (suit and rank) into a single int
variable. Since this is C#, I encourage either a class or struct. That way you can have two separate properties for suit and rank. (You can use integers or make enumerations for the suit and rank).
If you stick with a single int
for each card, I recommend pulling the suit and rank comparisons into separate functions (though this may be useful even if you switch away from using a single int
).
For example, you an IsSameSuit
function would handle just the % 13
checks.
private static bool IsSameSuit(int card1, int card2)
return (card1 % 13) == (card2 % 13)
And similarly for an IsSameRank
function.
(I find the extra parentheses helpful because I don't have to remember precedence rules, but that's a matter of opinion. Feel free to leave them out.)
Then your if blocks can be rewritten as
if(IsSameSuit(i, cards[j]) || IsSameRank(i, cards[j]))
score--;
break;
You have a loop for the second card of each comparison (the j
loops). I'd like to see the same type of loop for the first card (the i
variable).
public static int BadugiStrength(int cards)
int score = 4;
for (int i = 0; i <= 2; i++)
for (int j = i + 1; j <= 3; j++)
cards[i] / 13 == cards[j] / 13)
score--;
break;
return score;
There are a lot of magic numbers in the code (int score = 4
, j <= 3
). I've never heard of Badugi, so I do not know if there are variants with a different hand size. If you declare a constant for the hand size, passing a five card hand would be an easy change to support. A constant for max rank (13) can handle smaller/larger deck sizes. It also means later when searching the code base, you can look for the 13 that means "card rank" and keep it different from another type of 13 (such as shoe size, player count, game length, etc).
Even better than a constant, you could use the built in length property of the array. Start your score
at cards.Length
, and use the length to set your array indices.
Putting it all together:
public struct Card
public int Suit;
public int Rank;
public static int BadugiStrength(card cards)
int score = cards.Length;
for (int i = 0; i < cards.Length; i++)
for (int j = i + 1; j < cards.Length; j++)
if(IsSameSuit(cards[i], cards[j])
return score;
private static bool IsSameSuit(card card1, card card2)
return card1.Suit == card2.Suit
private static bool IsSameRank(card card1, card card2)
return card1.Rank == card2.Rank
edited Apr 16 at 22:57
answered Apr 16 at 20:03
Brian J
26618
26618
If the hand size changed then the number of loops would need to change. Or go with a radical different approach that is not apparent to me.
â paparazzo
Apr 16 at 20:12
I added a couple more examples, including a final example that accumulates my suggestions. Does that help you understand my example of the hand size changing?
â Brian J
Apr 16 at 20:25
1
I got the same answer. I see this just blows over the J loop on the last pass. I like this.
â paparazzo
Apr 16 at 20:58
I forgot to commend you on having a separate, testable function!
â Brian J
Apr 16 at 22:57
add a comment |Â
If the hand size changed then the number of loops would need to change. Or go with a radical different approach that is not apparent to me.
â paparazzo
Apr 16 at 20:12
I added a couple more examples, including a final example that accumulates my suggestions. Does that help you understand my example of the hand size changing?
â Brian J
Apr 16 at 20:25
1
I got the same answer. I see this just blows over the J loop on the last pass. I like this.
â paparazzo
Apr 16 at 20:58
I forgot to commend you on having a separate, testable function!
â Brian J
Apr 16 at 22:57
If the hand size changed then the number of loops would need to change. Or go with a radical different approach that is not apparent to me.
â paparazzo
Apr 16 at 20:12
If the hand size changed then the number of loops would need to change. Or go with a radical different approach that is not apparent to me.
â paparazzo
Apr 16 at 20:12
I added a couple more examples, including a final example that accumulates my suggestions. Does that help you understand my example of the hand size changing?
â Brian J
Apr 16 at 20:25
I added a couple more examples, including a final example that accumulates my suggestions. Does that help you understand my example of the hand size changing?
â Brian J
Apr 16 at 20:25
1
1
I got the same answer. I see this just blows over the J loop on the last pass. I like this.
â paparazzo
Apr 16 at 20:58
I got the same answer. I see this just blows over the J loop on the last pass. I like this.
â paparazzo
Apr 16 at 20:58
I forgot to commend you on having a separate, testable function!
â Brian J
Apr 16 at 22:57
I forgot to commend you on having a separate, testable function!
â Brian J
Apr 16 at 22:57
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192124%2fcard-game-ranking%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
@BrunoCosta i / 13 is wuit and i % 13 is rank. From what I can tell it is working.
â paparazzo
Apr 16 at 19:21