Problem Statement
Yahtzee is a game played with 5 dice and a score card. The object of the game is to get the best score. A turn consists of the following:
- Roll all of the dice.
- Keep as many of the dice as you want.
- Re-roll the dice you decide not to keep. Note that at any time if you like your hand you can stop rolling.
- Repeat step two and three. Note that you can throw away some of the dice that you previously kept.
- Now that you've had up to three rolls, record the resulting roll in a single place on your score card (scoring only applies to the final hand you keep).
Your score card is divided up as follows:
- 4 of a kind - 4 or more dice the same, scores the face value of ALL the dice.
- full house - 3 dice equal to each other with the other 2 dice equal to each other as well. Scores 25 points.
- small straight - Any 4 of the dice in consecutive order, scores 30 points.
- large straight - All 5 dice in consecutive order, scores 40 points.
- yahtzee - All 5 dice equal to each other, scores 50 points.
You are given a
Definition
- Class:
- BestYahtzeeScore
- Method:
- bestScore
- Parameters:
- String
- Returns:
- int
- Method signature:
- int bestScore(String rolls)
- (be sure your method is public)
Notes
- You can choose only 1 category. That is, you can not get points for 2 categories if your hand satisfies requirements for both.
- Straights cannot wrap around. "12346" would count as a small straight, but not a large straight.
- You can reorder your dice as you want to fit into a category. That is, "64523" can be reordered as "23456" and scored as a large straight.
Constraints
- rolls will contain exactly 15 characters.
- Each character in rolls will be between '1' and '6', inclusive.
Examples
"354621111111111"
Returns: 50
You get 3, 5, 4, 6 and 2 after the first roll. You should re-roll all dice and get with 1 on all five faces.
"666663213214444"
Returns: 50
Here you get Yahtzee after the first roll. You should keep all the dice.
"666111333222555"
Returns: 25
"566111333222555"
Returns: 25
"666111555333222"
Returns: 25
"566111555333222"
Returns: 26
"652353235164412"
Returns: 40
After the first roll you get "65235" and throw away all the dice. The second roll gives you "32351". You should keep "235" and re-roll the other two dice. You finish with "23564", which is a large straight.
"642123416566163"
Returns: 40
"122364354212453"
Returns: 40
"234356262414315"
Returns: 40
"551643562364522"
Returns: 40
"662112146254615"
Returns: 25
"554333626161461"
Returns: 40
"142635155126515"
Returns: 40
"354161641111434"
Returns: 50
"211355524631226"
Returns: 40
"122331562663452"
Returns: 40
"211351564326565"
Returns: 40
"241356131451654"
Returns: 40
"436235364232152"
Returns: 40
"265241155222313"
Returns: 25
"113522363523416"
Returns: 40
"621165364262131"
Returns: 40
"661432152641433"
Returns: 40
"441212656511344"
Returns: 30
"425156462151225"
Returns: 0
"661523213563112"
Returns: 0
"361263512613631"
Returns: 0
"142652614564164"
Returns: 0
"525362311651213"
Returns: 0
"144165526421151"
Returns: 0
You can't satisfy the requirements for any category.
"321563233141345"
Returns: 40
"344623441514333"
Returns: 40
"615535222334622"
Returns: 40
"566234636121354"
Returns: 40
"145534642556131"
Returns: 40
"465444366222353"
Returns: 40
"462616344636465"
Returns: 30
"645312462663533"
Returns: 40
"213211514343243"
Returns: 40
"525566164122521"
Returns: 25
"232544254356114"
Returns: 40
"322566141534452"
Returns: 40
"144356243244615"
Returns: 40
"513214421553254"
Returns: 40
"155563634314361"
Returns: 30
"241326356445421"
Returns: 40
"516231244624551"
Returns: 40
"344351222564215"
Returns: 40
"426464366523253"
Returns: 40
"444416251625162"
Returns: 22
"112245555465424"
Returns: 26
"666123123123166"
Returns: 25
"245265526464431"
Returns: 30
"114121526421151"
Returns: 25
"161613526655534"
Returns: 29
"133662515565242"
Returns: 26
"122342323232323"
Returns: 30
"666635554444111"
Returns: 30
"666655526421151"
Returns: 29
"133155313561411"
Returns: 30
"666612351235123"
Returns: 27
"555564446665555"
Returns: 29
"555512631125536"
Returns: 26
"112462534162351"
Returns: 40
"112231666651223"
Returns: 29
"233453344433442"
Returns: 30
"123432323232323"
Returns: 30
"666612312312312"
Returns: 27
"666622211545433"
Returns: 30
"122342233322333"
Returns: 30
"333355554446661"
Returns: 30
"122342233326655"
Returns: 30
"666653333111122"
Returns: 29
"111123562356235"
Returns: 9
"222333134223322"
Returns: 30
"222444455542213"
Returns: 30
"123561212121212"
Returns: 25
"235442352354235"
Returns: 30