The game of Ghost is played between two players who alternate saying a letter on each turn. At each point, the letters so far must start some valid English word. The loser is the player to complete a full word first. So, for example, if the letters so far are E-A-G-L, then the only valid next letter to say is "E" and so the next player will lose. (Even though there are longer words such as "eaglet".)
The challenge
You are to write a program or function to determine, given the letters so far, who will win assuming two perfect players. The input is a string representing the current state of the game, and a list of strings representing the dictionary of valid words. The output should distinguish whether the next player to go will win or lose.
Details
- The code must handle the case where the current state is empty. However, you may assume no word in the dictionary is empty.
- You may assume that each input string consists only of lowercase ASCII letters, i.e. a-z.
- You may assume the current state and all words in the dictionary have at most 80 characters each.
- The dictionary is guaranteed to be nonempty (to avoid the case where there is no valid first move).
- You may assume the "current state" will be valid: there will necessarily be some word starting with the current state; also, the current state will not be a full word, nor will any prefix of the current state be a full word.
- The dictionary will be prefiltered according to the rules of which "English words" are considered to be valid for the game - so for example, for a variant in which words of three or fewer letters don't end the game yet, the dictionary will be prefiltered to include only the words of four or more letters.
- You may assume the dictionary will be presorted.
Examples
Suppose the dictionary is:
abbot
eager
eagle
eaglet
earful
earring
Then for the following current states, the output should be as follows:
Current state Result
============= ======
loss
a win
eag win
eagl loss
ear win
earf win
earr loss
Likewise, for the word list at https://raw.githubusercontent.com/dschepler/ghost-word-list/master/wordlist.txt (produced on a Debian system using pcregrep '^[a-z]{4,80}$' /usr/share/dict/american-english
) here is a possible session:
Current state Result
============= ======
win
h loss
ho win
hoa loss
hoar win
hoars loss
(And then the next move completes "hoarse".)
Scoring
This is code-golf: Shortest program in bytes for each programming language wins.