Repository navigation
Poker: Update test to exactly match the canonical data and add version file - #1432
Conversation
sjwarner-bp
left a comment
There was a problem hiding this comment.
Hi @katmpatz !
Thanks for helping out with this 🙂 Looks like the build is failing because the example solution no longer passes these tests:
28 tests completed, 9 failed
:test FAILED
You'll need to update it so all of the new tests pass! Let me know if you need any advice!
|
ok!I will try to fix it! 🙂 |
|
I resolved the merge conflicts and I update also the code, in order to pass the tests. 😄 |
FridaTveit
left a comment
There was a problem hiding this comment.
This is looking really good, thanks for putting in all this effort @katmpatz! 😃
I've left a few comments, let me know if you have any questions or if you disagree with any of it! 🙂
| @@ -2,108 +2,129 @@ | |||
| import java.util.stream.Collectors; | |||
|
|
|||
| class Hand { | |||
There was a problem hiding this comment.
It's quite difficult to see what changes you've made in this file since you've changed the indentation. Please change the indentation back to the way it was (indented with 4 spaces). It's good to keep indentation consistent because that should make code easier to read and maintain 🙂
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class PokerTest { | ||
| @Test |
There was a problem hiding this comment.
Again, please change the indentation back to the way it was 🙂
| return 10; | ||
| } | ||
| String s = "..23456789TJQKA"; | ||
| return s.indexOf(card.charAt(0)); |
There was a problem hiding this comment.
The indentation appears to be a bit off here. Please try to make it consistent with the rest of the file 🙂
Also, I think the last two lines could be one line as before:
return "..23456789TJQKA".indexOf(card.charAt(0));
Or if you would rather extract the string as a variable, then it should be given a useful and descriptive name, e.g. possibleRanks 🙂
|
|
||
| private int parseSuit(String card) { | ||
| return ".HSDC".indexOf(card.charAt(1)); | ||
| return ".HSDC".indexOf(card.charAt(card.length()-1)); |
There was a problem hiding this comment.
Please add whitespace around operators: card.length() - 1 🙂
| .unordered() | ||
| .collect(Collectors.toList()); | ||
| private int scoreHand(List<Card> cards) { | ||
| List<Card> cardsByRank = cards.stream().sorted(Comparator.comparing(Card::getRank)).unordered() |
There was a problem hiding this comment.
It's considered good practice to add a linebreak with each full stop here, as it was before. That makes the code easier to read and maintain 🙂
The same goes for all the later changes you've made 🙂
| for (Object key : frequencyMap.keySet()) { | ||
| int fKey = (int) key; | ||
| int fValue = frequencyMap.get(key); | ||
| sum += fKey * fValue; |
There was a problem hiding this comment.
Again, key should probably have type Integer not Object. And try to avoid abbreviations in names, as they often make it harder to understand what the variable is. Instead of fKey and fValue, frequencyKey and frequencyValue (or just frequency) would be more descriptive 🙂
And what is sum a sum over? Try to include that in the name if possible 🙂
| String highest8 = "4D 5S 6S 8D 3C"; | ||
| String highest10 = "2S 4C 7S 9H 10H"; | ||
| String highestJ = "3S 4S 5D 6H JH"; | ||
| assertEquals(Arrays.asList(highestJ), new Poker(Arrays.asList(highest8, highest10, highestJ)).getBestHands()); |
There was a problem hiding this comment.
This line is a bit long so I would add some linebreaks (120 chars is a common limit for how long a line should be). It's considered good practice to put each argument on a new line if an expression is too long to fit on one line. For example, in this case:
assertEquals(
Arrays.asList(highestJ),
new Poker(Arrays.asList(highest8, highest10, highestJ)).getBestHands()
);
| @Test | ||
| public void onePairVsDoublePair() { | ||
| String pairOf8 = "2S 8H 6S 8D JH"; | ||
| String doublePair = "4S 5H 4C 8C 5H"; |
There was a problem hiding this comment.
I think this should be 5C not 5H?
| public void doublePairVsThree() { | ||
| String doublePair2And8 = "2S 8H 2H 8D JH"; | ||
| String threeOf4 = "4S 5H 4C 8S 4H"; | ||
| assertEquals(Arrays.asList(threeOf4), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands()); |
There was a problem hiding this comment.
When there's only one object in the list you can use Collections.singletonList instead of Arrays.asList 🙂
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void twoSquares() { | ||
| String squareOf2 = "2S 2H 2C 8D 2H"; |
There was a problem hiding this comment.
I think this should end in 2D not 2H?
|
I tried to change indentation and to fix the mistakes. I hope all your remarks have been corrected. Let me know if I have to change something else. |
sjwarner-bp
left a comment
There was a problem hiding this comment.
This is looking really good! Tiny bit of indent still needed 🙂
| frequencyMap.put(c.getRank(), frequencyMap.get(c.getRank()) + 1); | ||
| } else { | ||
| frequencyMap.put(c.getRank(), 1); | ||
| frequencyMap.put(c.getRank(), 1); |
There was a problem hiding this comment.
I think this one here is still a tiny bit too far right!
FridaTveit
left a comment
There was a problem hiding this comment.
Looks great, thanks @katmpatz! 😃
A few minor comments, and then it's ready to merge! 🙂
| .distinct() | ||
| .count() == 5 && ranks | ||
| .get(0) - ranks | ||
| .get(4) == 4; |
There was a problem hiding this comment.
In this case I would keep these two as they were because they're not just calling many functions on one object, they're also doing comparisons. This gets a bit lost when the statement is split over many lines, especially for the straight case. It now looks like that's just calling lots of functions on ranks instead of calculating two boolean values and then comparing them. If you did want to split that one over several lines then I would split it on the boolean operator:
boolean straight = ranks.stream().distinct().count() == 5
&& ranks.get(0) - ranks.get(4) == 4;
Does that make sense? Sorry if my previous comment was confusing 🙂
| } | ||
| if (straight) { | ||
| return 400 + highestFrequency; | ||
| return 400 + maxValue; |
There was a problem hiding this comment.
Not very important (so feel free to ignore): from what I can tell maxValue is only used here? In which case I would define it inside this if. It's good practice to limit the scope as much as possible of any variable that you use. That makes it easier to read and understand your code 🙂
| } | ||
| if (rankCounts.equals(Arrays.asList(3, 1, 1))) { | ||
| return 300 + cardsByRank.get(0).getRank(); | ||
| List<Integer> unique_cards = new ArrayList<Integer>(); |
There was a problem hiding this comment.
Please use camel case, not snake case, to be consistent with the rest of the code in the repo 🙂
| assertEquals(Arrays.asList(threeOf4), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands()); | ||
| String doublePair2And8 = "2S 8H 2H 8D JH"; | ||
| String threeOf4 = "4S 5H 4C 8S 4H"; | ||
| assertEquals(Collections.singletonList(threeOf4), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands()); |
There was a problem hiding this comment.
You can do this for all the lists with only one element 🙂 It's not very important though, so feel free to leave it if you want. We can open a good first patch issue for it 🙂
|
I tried to fix the mistakes. Let me know if I have to change something else. 😄 |
FridaTveit
left a comment
There was a problem hiding this comment.
Thanks for making those changes so quickly @katmpatz! 😃
There's one very minor thing left to fix and then it's perfect! 🙂
| return 300 + triplet + Collections.max(uniqueCards); | ||
| } | ||
| if (rankCounts.equals(Arrays.asList(2, 2, 1))) { | ||
| int products_of_frequency_and_value = 0; |
There was a problem hiding this comment.
This one should also be camel case 🙂
| } | ||
| if (rankCounts.equals(Arrays.asList(2, 2, 1))) { | ||
| int products_of_frequency_and_value = 0; | ||
| int ProductsOfFrequencyAndValue = 0; |
There was a problem hiding this comment.
Minor nitpick: in Java it's considered best practice to name variables starting with a lowercase letter and only name classes starting with an uppercase letter 🙂
FridaTveit
left a comment
There was a problem hiding this comment.
Looks great! Thanks for taking the time to make this perfect @katmpatz! 😃
Fix #1171
Reviewer Resources:
Track Policies