2008-08-06 15:26:25 +00:00
|
|
|
-- Hangman in eLua using the 'term' module
|
|
|
|
-- Inspired by the original 'hangman' from the bsdgames package
|
|
|
|
|
2008-08-09 15:36:11 +00:00
|
|
|
-- we need a random function
|
|
|
|
-- using math.random for now, which implies target=lua, not lualong
|
|
|
|
if not math then
|
2008-09-01 20:32:43 +00:00
|
|
|
print "\nError: Hangman needs the math module (disabled when target=lualong) !"
|
2008-08-09 15:36:11 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2009-07-20 19:39:51 +00:00
|
|
|
local h, w = term.getlines(), term.getcols()
|
2008-08-06 15:26:25 +00:00
|
|
|
local tries = 0
|
|
|
|
|
2008-08-09 15:36:11 +00:00
|
|
|
-- "Database" with our words
|
2008-08-06 15:26:25 +00:00
|
|
|
local words = { "simple", "hangman", "guess", "elua", "inane", "serial",
|
|
|
|
"stupenduous", "software" }
|
|
|
|
|
|
|
|
-- Build our hanging site :)
|
|
|
|
function hang()
|
|
|
|
if tries == 0 then
|
|
|
|
-- Build the basic structure
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 5, 1, string.rep( '_', 6 ) )
|
|
|
|
term.print( 5, 2, '| |')
|
2008-08-06 15:26:25 +00:00
|
|
|
for i = 3, 6 do
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 5, i, '|' )
|
2008-08-06 15:26:25 +00:00
|
|
|
end
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 3, 7, '__|_____')
|
|
|
|
term.print( 3, 8, '| |___')
|
|
|
|
term.print( 3, 9, '|__________|')
|
2008-08-06 15:26:25 +00:00
|
|
|
|
|
|
|
elseif tries == 1 then
|
|
|
|
-- Draw the head
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 10, 3, "O" )
|
2008-08-06 15:26:25 +00:00
|
|
|
|
|
|
|
elseif tries == 2 or tries == 3 then
|
|
|
|
-- First or second part of body
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 10, tries + 2, "|" )
|
2008-08-06 15:26:25 +00:00
|
|
|
|
|
|
|
elseif tries == 4 or tries == 5 then
|
|
|
|
-- First leg / first hand
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 9, tries == 4 and 6 or 4, "/" )
|
2008-08-06 15:26:25 +00:00
|
|
|
|
|
|
|
elseif tries == 6 or tries == 7 then
|
|
|
|
-- Second hand / second leg
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 11, tries == 7 and 6 or 4, "\\" )
|
2008-08-06 15:26:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local total, guessed = 0, 0
|
|
|
|
|
|
|
|
-- Show the game statistics
|
|
|
|
function stats()
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( w - 20, 5, "Total words: ", tostring( total ) )
|
|
|
|
term.print( w - 20, 6, "Guessed words: ", tostring( guessed ) )
|
2008-08-06 15:26:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
while true do
|
|
|
|
term.clrscr()
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 3, 12, "eLua hangman" )
|
|
|
|
term.print( 3, 13, "ESC to exit" )
|
2008-08-06 15:26:25 +00:00
|
|
|
stats()
|
|
|
|
|
|
|
|
-- Draw the hanging site
|
|
|
|
tries = 0
|
|
|
|
hang()
|
|
|
|
|
|
|
|
-- Then write the "Guess" line
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 2, h - 3, "Word: " )
|
2008-08-06 15:26:25 +00:00
|
|
|
local lword = words[ math.random( #words ) ]:lower()
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( string.rep( "-", #lword ) )
|
|
|
|
term.print( 2, h - 2, "Guess: " )
|
2008-08-06 15:26:25 +00:00
|
|
|
|
|
|
|
local nguess = 0
|
|
|
|
local tried = {}
|
|
|
|
local key
|
|
|
|
while tries < 7 and nguess < #lword do
|
2009-07-20 19:39:51 +00:00
|
|
|
key = term.getchar()
|
2008-08-07 11:45:26 +00:00
|
|
|
if key == term.KC_ESC then break end
|
|
|
|
if key > 0 and key < 255 then
|
|
|
|
key = string.char( key ):lower()
|
2009-07-20 19:39:51 +00:00
|
|
|
term.moveto( 2, h - 1 )
|
2008-08-07 11:45:26 +00:00
|
|
|
term.clreol()
|
|
|
|
if not key:find( '%l' ) then
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( "Invalid character" )
|
2008-08-06 15:26:25 +00:00
|
|
|
else
|
2008-08-07 11:45:26 +00:00
|
|
|
key = key:byte()
|
|
|
|
if tried[ key ] ~= nil then
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( "Already tried this key" )
|
2008-08-07 11:45:26 +00:00
|
|
|
else
|
|
|
|
tried[ key ] = true
|
|
|
|
local i
|
|
|
|
local ok = false
|
|
|
|
for i = 1, #lword do
|
|
|
|
if key == lword:byte( i ) then
|
|
|
|
ok = true
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 7 + i, h - 3, string.char( key ) )
|
2008-08-07 11:45:26 +00:00
|
|
|
nguess = nguess + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not ok then
|
|
|
|
tries = tries + 1
|
|
|
|
hang()
|
2008-08-06 15:26:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-07-20 19:39:51 +00:00
|
|
|
term.moveto( 9, h - 2 )
|
2008-08-06 15:26:25 +00:00
|
|
|
end
|
|
|
|
end
|
2008-08-07 11:45:26 +00:00
|
|
|
if key == term.KC_ESC then break end
|
2008-08-06 15:26:25 +00:00
|
|
|
|
2009-07-20 19:39:51 +00:00
|
|
|
term.moveto( 2, h - 1 )
|
2008-08-06 15:26:25 +00:00
|
|
|
total = total + 1
|
|
|
|
if nguess == #lword then
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( "Congratulations! Another game? (y/n)" )
|
2008-08-06 15:26:25 +00:00
|
|
|
guessed = guessed + 1
|
|
|
|
else
|
2009-07-20 19:39:51 +00:00
|
|
|
term.print( 8, h - 3, lword )
|
|
|
|
term.print( 2, h - 1, "Game over. Another game? (y/n)" )
|
2008-08-06 15:26:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Show statistics again
|
|
|
|
stats()
|
|
|
|
|
|
|
|
repeat
|
2009-07-20 19:39:51 +00:00
|
|
|
key = string.char( term.getchar() ):lower()
|
2008-08-06 15:26:25 +00:00
|
|
|
until key == 'y' or key == 'n'
|
|
|
|
|
|
|
|
if key == 'n' then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
term.clrscr()
|
2009-07-20 19:39:51 +00:00
|
|
|
term.moveto( 1 , 1 )
|
2008-08-06 15:26:25 +00:00
|
|
|
|