Skip to content

Poker: Update test to exactly match the canonical data and add version file - #1432

Merged
FridaTveit merged 9 commits into
exercism:masterfrom
katmpatz:poker
May 15, 2018
Merged

FridaTveit merged 9 commits into
exercism:masterfrom
katmpatz:poker

Conversation

@katmpatz

Copy link
Copy Markdown
Contributor

Fix #1171


Reviewer Resources:

Track Policies

@sjwarner-bp sjwarner-bp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@sjwarner-bp

Copy link
Copy Markdown
Contributor

@katmpatz since #1430 has been merged in, you'll see the message:

"This branch has conflicts that must be resolved"

If you need a hand fixing this up, let me know 🙂

@katmpatz

Copy link
Copy Markdown
Contributor Author

ok!I will try to fix it! 🙂

@katmpatz

katmpatz commented May 7, 2018

Copy link
Copy Markdown
Contributor Author

I resolved the merge conflicts and I update also the code, in order to pass the tests. 😄

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, please change the indentation back to the way it was 🙂

return 10;
}
String s = "..23456789TJQKA";
return s.indexOf(card.charAt(0));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should end in 2D not 2H?

@katmpatz

Copy link
Copy Markdown
Contributor Author

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 sjwarner-bp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one here is still a tiny bit too far right!

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

@katmpatz

Copy link
Copy Markdown
Contributor Author

I tried to fix the mistakes. Let me know if I have to change something else. 😄

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should also be camel case 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! 😃

}
if (rankCounts.equals(Arrays.asList(2, 2, 1))) {
int products_of_frequency_and_value = 0;
int ProductsOfFrequencyAndValue = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for taking the time to make this perfect @katmpatz! 😃

@FridaTveit
FridaTveit merged commit c1f4630 into exercism:master May 15, 2018
@katmpatz
katmpatz deleted the poker branch June 3, 2018 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants