Wordiff: Difference between revisions

2,816 bytes added ,  2 years ago
julia example
m (→‎{{header|REXX}}: shown a different game.)
(julia example)
Line 31:
:* That last player must have entered a wordiff or loses.
:* If the game is timed-out, the loser is the person who took the longest `average` time to answer in their rounds.
 
 
 
=={{header|Julia}}==
<lang julia>
 
isoneless(nw, ow) = any(i -> nw == ow[begin:i-1] * ow[i+1:end], eachindex(ow))
isonemore(nw, ow) = isoneless(ow, nw)
isonechanged(x, y) = length(x) == length(y) && count(i -> x[i] != y[i], eachindex(y)) == 1
onefrom(nw, ow) = isoneless(nw, ow) || isonemore(nw, ow ) || isonechanged(nw, ow)
 
function askprompt(prompt)
ans = ""
while isempty(ans)
print(prompt)
ans = strip(readline())
println()
end
return ans
end
 
function wordiff(dictfile = "unixdict.txt")
wordlist = [w for w in split(read(dictfile, String), r"\s+") if !occursin(r"\W", w)]
starters = [w for w in wordlist if 3 <= length(w) <= 4]
 
timelimit = something(tryparse(Float64, askprompt("Time limit (min) or 0 for none: ")), 0)
 
players = split(askprompt("Enter players' names. Separate by commas: "), r"\s*,\s*")
times, word = Dict(player => Float32[] for player in players), rand(starters)
used = [word]
totalsecs, timestart = timelimit * 60, time()
while length(players) > 1
player = popfirst!(players)
playertimestart = time()
newword = askprompt("$player, your move. The current word is $word. Your worddiff? ")
if timestart + totalsecs > time()
if onefrom(newword, word) && !(newword in used) && lowercase(newword) in wordlist
println("Correct.")
push!(players, player)
word = newword
push!(used, newword)
push!(times[player], time() - playertimestart)
else
println("Wordiff choice incorrent. Player $player exits game.")
end
else # out of time
println("Sorry, time was up. Timing ranks for remaining players:")
avtimes = Dict(p => isempty(times[p]) ? NaN : sum(times[p]) / length(times[p])
for p in players)
sort!(players, lt = (x, y) -> avtimes[x] < avtimes[y])
foreach(p -> println(" $p:", lpad(avtimes[p], 10), " seconds average"), players)
break
end
sleep(rand() * 10)
end
length(players) < 2 && println("Player $(first(players)) is the only one left, and wins the game.")
end
 
wordiff()
</lang>{{out}}
<pre>
Time limit (min) or 0 for none: 0.4
 
Enter players' names. Separate by commas: sam,mary,ann,ron
 
sam, your move. The current word is shod. Your worddiff? shot
 
Correct.
mary, your move. The current word is shot. Your worddiff? hot
 
Correct.
ann, your move. The current word is hot. Your worddiff? hog
 
Correct.
ron, your move. The current word is hog. Your worddiff? log
 
Sorry, time was up. Timing ranks for remaining players:
ann: 3.1 seconds average
mary: 4.573 seconds average
sam: 6.447 seconds average
</pre>
 
 
=={{header|Nim}}==
4,106

edits