patternjavaMinor
Scrabble Tile Counter
Viewed 0 times
scrabblecountertile
Problem
Introduction
Inspired by this r/dailyprogrammer problem, I've written a Scrabble tile counter. Given a
Description
Scrabble is a popular word game where players remove tiles with
letters on them from a bag and use them to create words on a board.
The total number of tiles as well as the frequency of each letter does
not change between games.
For this challenge we will be using the tile set from the English
edition, which has 100 tiles total. Here's a reference for the
distribution and point value of each
tile.
Each tile will be represented by the letter that appears on it, with
the exception that blank tiles are represented by underscores _.
Input Description
The tiles already in play are inputted as an uppercase string. For
example, if 14 tiles have been removed from the bag and are in play,
you would be given an input like this:
Output Description
You should output the tiles that are left in the bag. The list should
be in descending order of the quantity of each tile left in the bag,
skipping over amounts that have no tiles.
In cases where more than one letter has the same quantity remaining,
output those letters in alphabetical order, with blank tiles at the
end.
Feedback
Inspired by this r/dailyprogrammer problem, I've written a Scrabble tile counter. Given a
Collection of Scrabble tiles known to be in play, it can output a Map of the remaining unused tiles and their counts, which can be converted into a Map containing counts and a Set of tiles with that count.Description
Scrabble is a popular word game where players remove tiles with
letters on them from a bag and use them to create words on a board.
The total number of tiles as well as the frequency of each letter does
not change between games.
For this challenge we will be using the tile set from the English
edition, which has 100 tiles total. Here's a reference for the
distribution and point value of each
tile.
Each tile will be represented by the letter that appears on it, with
the exception that blank tiles are represented by underscores _.
Input Description
The tiles already in play are inputted as an uppercase string. For
example, if 14 tiles have been removed from the bag and are in play,
you would be given an input like this:
AEERTYOXMCNB_SOutput Description
You should output the tiles that are left in the bag. The list should
be in descending order of the quantity of each tile left in the bag,
skipping over amounts that have no tiles.
In cases where more than one letter has the same quantity remaining,
output those letters in alphabetical order, with blank tiles at the
end.
10: E
9: I
8: A
7: O
5: N, R, T
4: D, L, U
3: G, S
2: F, H, P, V, W
1: B, C, J, K, M, Q, Y, Z, _
0: XFeedback
- I'm a pretty new to Java so general pointers around design and implementation would be much appreciated.
- My biggest design qualm was around the implementation of the
ScrabbleTileenum.ScrabbleTiles feel like they should be an enum, with thecharacterand starting count that the tile is associated with.
- The
isScrabbleTileandvalueOfmethods are fairly inef
Solution
I think that the
It's much easier just to work with
Your
I don't recommend marking function parameters as
I don't think that you need to define a
ScrabbleTile enum is overkill. The tiles are mostly in consecutive ASCIIbetical order (except for the characters [, \, ], and ^ that we would consider as junk between Z and _). A simple array, serving as a lookup table, would do the job:private static final int[] START_COUNTS = {
/* A */ 9, /* B */ 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, /* M */ 2,
/* N */ 6, /* O */ 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, /* Z */ 1,
0, 0, 0, 0, /* _ */ 2
};It's much easier just to work with
chars and ints directly.Your
calculateTileCount() function appears to implement the bulk of the counting work. I find it to be an unnatural interface design, though. The ScrabbleTileCounter doesn't act like an object, the way its name would suggest. It's not obvious what "calculate" means, or that the tiles is a list of tiles to be removed from the initial set. I would find an interface like this more intuitive:ScrabbleTileCounter scrabble = new ScrabbleTileCounter();
for (char c : tiles) {
scrabble.remove(c);
}
/* something here about scrabble.tileCount() */I don't recommend marking function parameters as
final. Functions are usually short enough that it's obvious you haven't reassigned the variable. The final adds noise, and it gives the false impression that the object is immutable, when in fact there is no such guarantee.I don't think that you need to define a
NoRemainingTilesException. The standard NoSuchElementException is close enough to do the job.Code Snippets
private static final int[] START_COUNTS = {
/* A */ 9, /* B */ 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, /* M */ 2,
/* N */ 6, /* O */ 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, /* Z */ 1,
0, 0, 0, 0, /* _ */ 2
};ScrabbleTileCounter scrabble = new ScrabbleTileCounter();
for (char c : tiles) {
scrabble.remove(c);
}
/* something here about scrabble.tileCount() */Context
StackExchange Code Review Q#132698, answer score: 3
Revisions (0)
No revisions yet.