Repository navigation
poker : add to track - #384
Conversation
FridaTveit
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Methods in java normally start with a lowercase letter :)
| public class PokerTest | ||
| { | ||
| @Test | ||
| public void One_hand() |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
Same goes for these two methods. scoreHand and parseCards are only used in this class therefore their implementations should be moved inside this class.
|
Thank you for your review , I made indeed some newby errors. [edit] |
FridaTveit
left a comment
There was a problem hiding this comment.
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 :)
| @@ -0,0 +1,346 @@ | |||
| { | |||
There was a problem hiding this comment.
I don't think you meant to add this file :)
There was a problem hiding this comment.
You still need to remove this file :)
| public class PokerTest | ||
| { | ||
| @Test | ||
| public void OneHand() |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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) { | ||
| { |
There was a problem hiding this comment.
You don't need these extra curly brackets :)
| @Test | ||
| @Ignore | ||
| public void NothingVsOnePair() | ||
| { |
There was a problem hiding this comment.
To be consistent, maybe move this curly bracket to after the signature? So it becomes
public void NothingVsOnePair() { instead?
|
Thanks you again for your comments ! Also I take the time to think about some improvements on my side too. 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 :) |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); | ||
| } | ||
|
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That's fine, take your time, there's no rush :)
| ArrayList<Hand> scoredHands = new ArrayList<Hand>(); | ||
|
|
||
| for (String s : hands) { | ||
| for (String s : hands) |
There was a problem hiding this comment.
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 :)
| @@ -0,0 +1,346 @@ | |||
| { | |||
There was a problem hiding this comment.
You still need to remove this file :)
| import org.junit.Test; | ||
|
|
||
| public class PokerTest | ||
| { |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 !
There was a problem hiding this comment.
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 :)
|
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
left a comment
There was a problem hiding this comment.
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 :)
|
Hi ! |
|
Sure, sounds great! :) |
|
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
left a comment
There was a problem hiding this comment.
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! :)
| }, | ||
| { | ||
| "slug": "poker", | ||
| "difficulty": 7, |
There was a problem hiding this comment.
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; | |||
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
This should be import org.junit.Ignore; :)
There was a problem hiding this comment.
Of course ! My bad for this careless mistake !
| } | ||
|
|
||
| @Test | ||
| @Ignore |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
You don't have to be sorry, it's my fault for not paying attention to POLICIES doc. Thanks for the comment :)
|
Changes done :) |
FridaTveit
left a comment
There was a problem hiding this comment.
Great, thanks @Zaldrick! :) This is a great new exercise, thanks for all the effort you've put in! :)
Inspired by Poker C# exercice, I made a Java version.