Skip to content

poker : add to track - #384

Merged
FridaTveit merged 7 commits into
exercism:masterfrom
Zaldrick:poker
Jun 30, 2017
Merged

FridaTveit merged 7 commits into
exercism:masterfrom
Zaldrick:poker

Conversation

@Zaldrick

Copy link
Copy Markdown
Contributor

Inspired by Poker C# exercice, I made a Java version.

@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 adding this exercise @Zaldrick! :) I've posted a few comments, mostly about java best practices when it comes to naming etc. If you could have a look at the comments when you have time that would be great! :)

}
}

public static List<String> BestHands(List<String> hands) {

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.

Methods in java normally start with a lowercase letter :)

public class PokerTest
{
@Test
public void One_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.

Test names in java are normally written in camel case, so oneHand() in this case :)

.collect(Collectors.toList());
}

public static List<Card> ParseCards(String 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.

From what I can tell, the only method that needs to be public is bestHands as that's the only method which is called outside of this class. The other methods can all be private. That makes it more clear how the class is meant to be used :)


public Card(String card) {
rank = Parserank(card);
Suit = ParseSuit(card);

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.

If parseRank and parseSuit are only used in this class, then they should be moved inside this class. That way each class is responsible for its own behaviour :)

public class Poker {
private static class Card {
private int rank;
private int Suit;

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.

Variables normally start with a lowercase letter in java, so suit instead of Suit :)


public Hand(String hand) {
this.input = hand;
this.score = ScoreHand(ParseCards(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.

Same goes for these two methods. scoreHand and parseCards are only used in this class therefore their implementations should be moved inside this class.

@Zaldrick

Zaldrick commented Mar 16, 2017 •

Copy link
Copy Markdown
Contributor Author

Thank you for your review , I made indeed some newby errors.
Change will be done this evening.

[edit]
Done :)

@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.

Thank you for updating your PR @Zaldrick, and for doing it so quickly! :) It's looking great! :) I just have a few comments you can look at when you have time. Let me know if you have any questions or if any of it doesn't make sense :)

Comment thread config.json~ Outdated
@@ -0,0 +1,346 @@
{

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 don't think you meant to add this file :)

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 still need to remove this file :)

public class PokerTest
{
@Test
public void OneHand()

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.

These method names should also start with a lowercase letter, only class names start with a capital letter in java :)

import java.util.Map;
import java.util.stream.Collectors;

public class Poker {

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 class is looking a lot better now! :) But it is quite long with the two nested classes, Card and Hand, in it. I would suggest extracting these into their own files. That would make each file more readable :)

}

private static List<Card> parseCards(String hand) {
ArrayList<Card> parseCards = new ArrayList<Card>();

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.

To avoid confusion with the method name, maybe name this variable parsedCards instead?


private static int scoreHand(List<Card> cards) {

List<Card> cardsByrank = new ArrayList<Card>();

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.

Remember to use camelCase for variable names as well, so cardsByRank in this case :)

public class Poker {

public static List<String> bestHands(List<String> hands) {
{

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 don't need these extra curly brackets :)

@Test
@Ignore
public void NothingVsOnePair()
{

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.

To be consistent, maybe move this curly bracket to after the signature? So it becomes
public void NothingVsOnePair() { instead?

@Zaldrick

Copy link
Copy Markdown
Contributor Author

Thanks you again for your comments ! Also I take the time to think about some improvements on my side too.
About making Card and Hand classes, I was thinking about it at the beginning, but as far as I saw in others exercices, the solution was always on one file. So basically I tried to put everything in one file even if I know it's cleary against the Java policy haha.

Also I really think that my method Hand::scoreHand is too big. I tried to reduce it a little but now I don't really know how to reduce it more. Maybe by adding some attributs to the class making able to divide into more method ? I don't know but if you can give me some tips, it would be great :)

@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.

Thank you for updating your changes again, this is looking really great now! :D

Yes, I understand why you wanted to keep the solution in one file. There are some other solutions that use more than one file so it should be fine :)

You're right, the scoreHand method is a bit long, good spot! I've tried to make some suggestions for how you can make it shorter :)

Another thing you could do if you have time and feel like it is try to use instance methods instead of static methods. We've decided to have that as a policy for the java track (see issue #177 for the discussion about it) so if you feel like it, it would be great to make this exercise compy with that policy :)

Thank you again for taking the time to make these changes. It's turning into a really great exercise! :)

And of course, if you have any questions or if something doesn't make sense, just let me know :)

}

private static int scoreHand(List<Card> cards) {
List<Card> cardsByRank = new ArrayList<Card>();

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 don't need this line. You can just do List<Card> cardsByRank = cards.stream().sorted(... :)


frequencymap = frequencymap.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors
.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

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.

Do you need this to be a LinkedHashMap? If not you can just do .toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1). And I think you are guaranteed to have no duplicates? If so you could use the even shorter method .toMap(Map.Entry::getKey, Map.Entry::getValue) :)

And just to check, do you need the frequency map to be in descending order for both this and to get the ranks? If so, could just just make getFrequencyMap return the map in descending order (if you do that I would also change the name to getFrequencyMapInDescendingOrder to make it more clear)?

@Zaldrick Zaldrick Mar 23, 2017 •

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.

I use a LinkedHash because it maintains insertion order of elements. I know that it requires more memory, but I tried to use HashMap and TreeMap. Both didn't give me the right order.

I also tried your suggestions but i've go the same probleme that I've got with HashMap.
Sometime i've got the right order, sometime not. The twoSquares test won't work with Hash or your suggestions.
Example :
"2S 2H 2S 8D 2H";
"4S 5H 5S 5D 5H";

Before ordering the frequencyMap :
{2=4, 8=1}
{4=1, 5=4}
After :
{2=4, 8=1}
{4=1, 5=4}

Nothing change ...

What i want is :
{2=4, 8=1}
{5=4, 4=1}

Do you have any suggestions ?

About getFrequencyMapInDescendingOrder, I had a reason for not doing this initially, but can't remember. Anyway I tried to change it and some tests don't work. I'm on it

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.

If there's a reason you're doing it then that's fine, I just wanted to check :)

Great! :)

.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

List<Integer> rankCounts = frequencymap.entrySet().stream().map(x -> x.getValue())
.collect(Collectors.toList());

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.

To simplify it, you could do List<Integer> rankCounts = new ArrayList<>(frequencyMap.values()) I think :)

return 800 + frequencymap.keySet().iterator().next();
}
if (rankCounts.equals(Arrays.asList(4, 1))) {
return (700 + (cardsByRank.get(0)).getRank());

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 have some extra brackets here that you don't need. It should just be
return 700 + cardsByRank.get(0).getRank() :)

boolean flush = suits.stream().distinct().count() == 1;
boolean straight = ranks.stream().distinct().count() == 5 && ranks.get(0) - ranks.get(4) == 4;
Iterator<Integer> itr = frequencymap.keySet().iterator();
int pairA = itr.next();

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.

If you only need the first two keys in the frequency map, then I would probably just do:

int highestFrequency = itr.next();
int secondHighestFrequency = itr.next();

That way you don't have to use two iterators. And it's more clear what your variables represent :)

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.

Damn why I created a iterator and didn't use it after.
I fix it by remplacing every "frequencymap.keySet().iterator().next()" with pairA.
I renamed pairA, it wasn't a explicite name.
Not sure about using a another int for stocking secondHighestFrequency because I will use only if I got two pairs. Can't I just use itr.next() ?
I understand that using two variables will make the code more clear anyway.

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.

Sure, using itr.next() would also work fine :) The reason I suggested you use would be that it's clearer what it is. But I agree that if you're only using it in one case then it might not be worth it. Maybe renaming itr to iteratorOverFrequencies would have the same effect and make it more clear? What do you think? :)

if (ranks.equals(Arrays.asList(14, 5, 4, 3, 2))) {
ranks = Arrays.asList(5, 4, 3, 2, 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.

If you want to make this method shorter I would maybe pull everything from here onwards out into a separate method called something like getScore.

@Zaldrick Zaldrick Mar 23, 2017 •

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.

working on it. But I also tried to make more instance method, so I need time to figure out how to implements that because I need a lot of variables depending of every case.

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.

That's fine, take your time, there's no rush :)

ArrayList<Hand> scoredHands = new ArrayList<Hand>();

for (String s : hands) {
for (String s : hands)

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 best practice to keep the curly brackets in this case. Otherwise it's easy to add another line to the for loop and forget to add the curly brackets then :)

Comment thread config.json~ Outdated
@@ -0,0 +1,346 @@
{

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 still need to remove this file :)

import org.junit.Test;

public class PokerTest
{

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 class is looking really good now! :) I would just move this curly bracket to be consistent, so that it's public class PokerTest {. If you're using Intellij you could do ctrl + alt + L (on Windows, cmd + alt + L on a mac) which will reformat your code for you. That should fix spacing and formatting issues like this, and you can tell it to optimise your imports as well. This page explains it more. I use it all the time because I can never remember to do stuff like remove unused imports manually :)

@Zaldrick Zaldrick Mar 23, 2017 •

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.

Thank you again for your suggestions. Sorry for taking so long time to answer, I wasn't available and It's a little bit harder to fix so I need more time haha. I started but I think it's not enough for making a commit. I guess I will finish that this weekend.
Also i'm working on eclipse so your shortcut is not available. But I'll will take the time and make an effort for fixing every issues about curly bracket and spacing. Thank you for the website, i didn't know Intellij !

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.

No problem, that's completely understandable! We all have other committments as well so it's fine to need some time before answering comments and updating reviews. This is meant to be fun to do so don't feel pressured to do everything really fast if you're busy. It's better to do it well and have fun while doing it, than doing it super fast :)

According to this page the shortcut on eclipse is ctrl + shift + F :)

@stale

stale Bot commented May 22, 2017

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@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.

Hi @Zaldrick, have you had a chance to look at this? If you're very busy that's completely fine :) The only change that really needs to be made is to remove the config.json~ file and you need to add this exercise to settings.gradle. Apart from that this is ready to merge! :) Let me know if you feel you have time to do those two things, otherwise I can do it. And let me know if you have any questions about it :) This exercise is looking really good and I think it would be a great addition to the java track :)

@FridaTveit FridaTveit removed the stale label Jun 11, 2017
@Zaldrick

Copy link
Copy Markdown
Contributor Author

Hi !
Yeah sorry for putting aside this PR.
I think it will be possible for me to finish the job next week. So if it's okay for you, I'll find the time for it :)

@FridaTveit

Copy link
Copy Markdown
Contributor

Sure, sounds great! :)

@Zaldrick

Copy link
Copy Markdown
Contributor Author

Hey, I tried to take your old suggestions (about instance functions and some other little things) into account. I guess it's pretty decent now. What do you think ? Is there anything else that could improve the code ?

@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 great @Zaldrick! :) I only have a couple of small comments and apart from that it's ready to merge! Thanks for all your great work! :)

Comment thread config.json
},
{
"slug": "poker",
"difficulty": 7,

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.

At the moment, none of the other exercises have topics (see issue #523). But letting people add topics to new exercises if they want to might be a good idea. Thoughts @exercism/java?

@@ -0,0 +1,12 @@
import java.util.List;

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.

According to the POLICIES doc, since this exercise is after the first 20 exercises it shouldn't have any starter implementation. Instead there should just be a .keep file here :)

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.

I remove function declarations and just left class declaration as I saw in another exercice that I take for example. Is this okay ?


import org.junit.Test;

import jdk.nashorn.internal.ir.annotations.Ignore;

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 should be import org.junit.Ignore; :)

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.

Of course ! My bad for this careless mistake !

}

@Test
@Ignore

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.

Sorry, I just noticied one more thing from the POLICIES doc. The @Ignores should have a hint: @Ignore("Remove to run test"). This is to make it more clear to the users that they have to remove that line to get the test to run :)

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.

You don't have to be sorry, it's my fault for not paying attention to POLICIES doc. Thanks for the comment :)

@Zaldrick

Copy link
Copy Markdown
Contributor Author

Changes done :)

@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.

Great, thanks @Zaldrick! :) This is a great new exercise, thanks for all the effort you've put in! :)

@FridaTveit
FridaTveit merged commit f63f64d into exercism:master Jun 30, 2017
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.

2 participants