eniallator

Hangman

Oct 26th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | None | 0 0
  1. limit = {10,true,8}
  2. help = {'start - starts the game of hangman and prompts with enter a word.','exit - exit the program.','changewordlimit - allows you to change the limit of the word input. usage: "changewordlimit [integer value]"','changespace - change whether you can use spaces or not. usage: "changespace [boolean value]"','changelives - change the amount of lives you start with. usage: "changelives [integer value]"'}
  3.  
  4. function newWord()
  5.    print('Enter the new word between 1 and ' .. limit[1] .. ' characters below or type "exit" to exit the game:')
  6.    prefix('newWord> ')
  7.    local word = io.read()
  8.  
  9.    if not limit[2] then
  10.       for i=1,#word do
  11.          if word:sub(i,i) == " " then
  12.             return false
  13.          end
  14.       end
  15.    end
  16.  
  17.    if #word <= limit[1] then
  18.  
  19.       return word:lower()
  20.    else
  21.  
  22.       return false
  23.    end
  24. end
  25.  
  26. function prefix(prefix)
  27.    io.write(prefix)
  28. end
  29.  
  30. function wordSplit(string)
  31.    local out = {}
  32.  
  33.    for word in string:gmatch("%S+") do
  34.       table.insert(out, word)
  35.    end
  36.  
  37.    return out
  38. end
  39.  
  40. print('Welcome to my hangman program. Type in a command you want to run or type "help" for options')
  41.  
  42. while true do
  43.    prefix('> ')
  44.    local input = wordSplit(io.read())
  45.  
  46.    if input[1] == 'help' then
  47.       for i=1,#help do
  48.  
  49.          print(help[i].. '\n')
  50.       end
  51.    elseif input[1] == 'exit' then
  52.  
  53.       print('Exiting the program')
  54.       break
  55.    elseif input[1] == 'changewordlimit' then
  56.       if tonumber(input[2]) then
  57.  
  58.          limit[1] = input[2]
  59.          print('New word limit set to ' .. limit[1] .. '.')
  60.       else
  61.  
  62.          print('Enter the new limit below or leave blank to not set:')
  63.          prefix('changeWordLimit> ')
  64.          newLimit = io.read()
  65.  
  66.          if tonumber(newLimit) then
  67.  
  68.             limit[1] = newLimit
  69.             print('New word limit set to ' .. limit[1] .. '.')
  70.          else
  71.             print('Word limit not changed.')
  72.          end
  73.       end
  74.    elseif input[1] == 'changespace' then
  75.  
  76.       if input[2] == 'true' then
  77.  
  78.          limit[2] = true
  79.          print('Enabled spaces in words')
  80.       elseif input[2] == 'false' then
  81.  
  82.          limit[2] = false
  83.          print('Disabled spaces in words')
  84.       else
  85.  
  86.          print('Enter true to enable spaces or false to disable them or leave blank to not change.')
  87.          prefix('changeSpace> ')
  88.          space = io.read()
  89.  
  90.          if space == 'true' then
  91.  
  92.             limit[2] = true
  93.             print('Enabled spaces in words')
  94.          elseif space == 'false' then
  95.  
  96.             limit[2] = false
  97.             print('Disabled spaces in words')
  98.          else
  99.  
  100.             print('Spaces unchanged.')
  101.          end
  102.       end
  103.    elseif input[1] == 'changelives' then
  104.       if tonumber(input[2]) then
  105.  
  106.          limit[3] = input[2]
  107.          print('New max lives set to ' .. limit[3] .. '.')
  108.       else
  109.  
  110.          print('Enter the new lives max below or leave blank to not set:')
  111.          prefix('changeLives> ')
  112.          newLimit = io.read()
  113.  
  114.          if tonumber(newLimit) then
  115.  
  116.             limit[3] = newLimit
  117.             print('New lives max set to ' .. limit[3] .. '.')
  118.          else
  119.             print('Lives max not changed.')
  120.          end
  121.       end
  122.  
  123.    elseif input[1] == 'start' then
  124.       while true do
  125.  
  126.          word = newWord()
  127.  
  128.          if word == 'exit' then
  129.  
  130.             print('Exiting the game')
  131.             break
  132.          elseif word then
  133.  
  134.             lives = limit[3]
  135.             guess = false
  136.             chars = {}
  137.             rightGuess = true
  138.             win = false
  139.  
  140.             for i=1,#word do
  141.                if word:sub(i,i) == ' ' then
  142.  
  143.                   table.insert(chars, {' ',true})
  144.                else
  145.  
  146.                   table.insert(chars,{word:sub(i,i),false})
  147.                end
  148.             end
  149.  
  150.             while lives > 1 do
  151.  
  152.                os.execute('cls')
  153.                win = true
  154.                rightGuess = false
  155.  
  156.                for i=1,#chars do
  157.                   if guess == false then
  158.  
  159.                      rightGuess = true
  160.                   elseif chars[i][1] == guess and not chars[i][2] then
  161.  
  162.                      rightGuess = true
  163.                      chars[i][2] = true
  164.                   end
  165.                end
  166.  
  167.                if not rightGuess then
  168.  
  169.                   lives = lives-1
  170.                end
  171.  
  172.                for i=1,#chars do
  173.                   if not chars[i][2] then
  174.  
  175.                      win = false
  176.                   end
  177.                end
  178.  
  179.                if guess == word then
  180.  
  181.                   win = true
  182.                end
  183.  
  184.                if win then break end
  185.  
  186.                print('Guess a letter in the word below or guess the entire word, you have ' .. lives .. ' lives left.')
  187.  
  188.                for i=1,#chars do
  189.                   if chars[i][1] == ' ' then
  190.  
  191.                      io.write('  ')
  192.                   elseif chars[i][2] then
  193.  
  194.                      io.write(' ' .. chars[i][1])
  195.                   else
  196.  
  197.                      io.write(' _')
  198.                   end
  199.                end
  200.  
  201.                print('\n')
  202.                prefix('guess> ')
  203.                guess = string.lower(io.read())
  204.             end
  205.  
  206.             os.execute('cls')
  207.  
  208.             if win then
  209.  
  210.                print('Congratulations you guessed it! The word was "' .. word .. '"\n')
  211.             else
  212.  
  213.                print('Game over. The word was "' .. word .. '".\n')
  214.             end
  215.          else
  216.  
  217.             os.execute('cls')
  218.             print('The word was bigger than ' .. limit[1] .. ' characters.\n')
  219.          end
  220.       end
  221.    else
  222.  
  223.       print('Unknown command. Type "help" for options.')
  224.    end
  225. end
Advertisement
Add Comment
Please, Sign In to add comment