I'm having some issues with the following part of a game that I am working on.
I have spent hours trying to get my head around this and I think I've just hit a brick wall and could use some guidance, so thanks in advance for any help.
Output: Is 12 random letters.
Input: make a word using these 12 random letters only.
Processing: Check if the input word contains ONLY the random letters (no other characters).
I have tried a number of different things to try and implement this validation such as for loops & traversals etc... I'm now onto trying to use the Arrays.equals function to validate the input. It works But only if the arrays are the same length. Is there a way I can ignore length? Or am I going about this in a totally wrong way?
Create two arrays with those twelve letters in and the letters in the word you are testing.
Sort the two arrays.
Create Strings with those letters in.
Test whether the smaller is a prefix of the larger.
There are methods in String which should be able to do those things for you.
But a truly random choice allows repetition; your original text had two Es in.
Actually, on second thoughts, I suspect my suggestion about prefixes won't work. Try putting all the letters into List<Character>s. Try the containsAll() method. Try removing letters in the smaller List from the larger List(from both Lists in turn) and see what the remove() method returns. The links should give you more information. Remember you can easily try removal on JShell.
Thanks for that. I've actually almost solved this now just by using for loops and CharAt. Still working on the repetition aspect by using deleteCharAt. Sometimes you just need to try and explain to someone else what the problem is for the solution to come to you. Thanks again for your help.
Campbell Ritchie wrote:But a truly random choice allows repetition; your original text had two Es in.
I think the idea was that the output can have repetition, as shown. But the input (the response to the output) cannot use more of each letter than were present in the outout. So if the output was "bb", the input (response) could be "b" or "bb", but not "bbb". Robin, is that correct?