Link to pdf version on UVa OJ
As you read the problem statement you will find out this is a problem in the "implementation" category, because there are no tricks or algorithms necessary to solve this problem, you only need to know how to program and know a few data structures like vector, set, map, multiset...
What happened on my mind : since there are two kind of comparisons I decided to find out the highest order one hand has (Straight flush, Four of a kind, Full house, etc)
For this I added an enum that contained all the orders.
Then I wrote a function named getBest(hand) which returned the highest PokerHand this hand could have, starting from 9 to 1.
After having PokerHand for Black player and White player, if they have different PokerHands I can simply compare them and the winner is known, otherwise both Black and White players have the same PokerHand, so I need to write 9 different functions to compare each of them, namely compareStraightFlush(first, second), compareFourOfAKind(first, second), etc.
This way I could break the problem to smaller functions and implement each separately.
If I had to write this solution 10 years ago, I would only have a long main function and maybe a few more utility functions, but in this solution I used more than 25 functions none of them more than 20 lines of code, and because of this modularity my code is very readable and was accepted in the first submission.
For more details refer to my code on github
As you read the problem statement you will find out this is a problem in the "implementation" category, because there are no tricks or algorithms necessary to solve this problem, you only need to know how to program and know a few data structures like vector, set, map, multiset...
What happened on my mind : since there are two kind of comparisons I decided to find out the highest order one hand has (Straight flush, Four of a kind, Full house, etc)
For this I added an enum that contained all the orders.
Then I wrote a function named getBest(hand) which returned the highest PokerHand this hand could have, starting from 9 to 1.
After having PokerHand for Black player and White player, if they have different PokerHands I can simply compare them and the winner is known, otherwise both Black and White players have the same PokerHand, so I need to write 9 different functions to compare each of them, namely compareStraightFlush(first, second), compareFourOfAKind(first, second), etc.
This way I could break the problem to smaller functions and implement each separately.
If I had to write this solution 10 years ago, I would only have a long main function and maybe a few more utility functions, but in this solution I used more than 25 functions none of them more than 20 lines of code, and because of this modularity my code is very readable and was accepted in the first submission.
For more details refer to my code on github
No comments:
Post a Comment