working hangman

This commit is contained in:
Severak 2019-09-14 23:12:53 +02:00
parent ddda1003eb
commit 6782afd9fc
1 changed files with 97 additions and 0 deletions

97
hangman/hangman.lua Normal file
View File

@ -0,0 +1,97 @@
wordlist = {
"Praha",
"Brno",
"Ostrava",
"Plzen",
"Liberec",
"Olomouc",
"Ceske Budejovice",
"Usti nad Labem",
"Hraded Kralove",
"Pardubice",
"Zlin",
"Havirov",
"Kladno",
"Most"
}
math.randomseed (os.time())
score = 0
while true do
round = 1
letters = {}
possible = {}
guessed = false
guessing = wordlist[math.random(1, #wordlist)]
for i=1,string.len(guessing) do
possible[string.lower(string.sub(guessing,i, i))] = true
end
while round<10 do
local censored = {}
for i=1,string.len(guessing) do
local L = string.sub(guessing, i, i)
if L == " " then
censored[#censored+1] = " "
elseif letters[string.lower(L)] then
censored[#censored+1] = L
else
censored[#censored+1] = "*"
end
end
if table.concat(censored, '')==guessing then
print(guessing)
print "You guessed it!"
score = score + 1
guessed = true
break
end
print(table.concat(censored, ''))
io.stdout:write("guess " .. round .. "/9: ")
input = io.read()
if input=="?" or input=="/help" or input=="" then
elseif input=="/q" then
print "Bye"
return
elseif string.len(input)==1 then
if letters[string.lower(input)] then
print "Already tried this letter."
elseif possible[string.lower(input)] then
print "Hit! :-)"
letters[string.lower(input)] = true
else
print "Miss. :-("
round = round + 1
end
else
if string.lower(input)==string.lower(guessing) then
print(guessing)
print "You guessed it!"
score = score + 1
guessed = true
break
end
end
end
if not guessed then
print("It was: ".. guessing)
if score > 0 then
score = score - 1
end
end
print ""
print "Another one:"
end
print("Score" .. score)