Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- limit = {10,true,8}
- 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]"'}
- function newWord()
- print('Enter the new word between 1 and ' .. limit[1] .. ' characters below or type "exit" to exit the game:')
- prefix('newWord> ')
- local word = io.read()
- if not limit[2] then
- for i=1,#word do
- if word:sub(i,i) == " " then
- return false
- end
- end
- end
- if #word <= limit[1] then
- return word:lower()
- else
- return false
- end
- end
- function prefix(prefix)
- io.write(prefix)
- end
- function wordSplit(string)
- local out = {}
- for word in string:gmatch("%S+") do
- table.insert(out, word)
- end
- return out
- end
- print('Welcome to my hangman program. Type in a command you want to run or type "help" for options')
- while true do
- prefix('> ')
- local input = wordSplit(io.read())
- if input[1] == 'help' then
- for i=1,#help do
- print(help[i].. '\n')
- end
- elseif input[1] == 'exit' then
- print('Exiting the program')
- break
- elseif input[1] == 'changewordlimit' then
- if tonumber(input[2]) then
- limit[1] = input[2]
- print('New word limit set to ' .. limit[1] .. '.')
- else
- print('Enter the new limit below or leave blank to not set:')
- prefix('changeWordLimit> ')
- newLimit = io.read()
- if tonumber(newLimit) then
- limit[1] = newLimit
- print('New word limit set to ' .. limit[1] .. '.')
- else
- print('Word limit not changed.')
- end
- end
- elseif input[1] == 'changespace' then
- if input[2] == 'true' then
- limit[2] = true
- print('Enabled spaces in words')
- elseif input[2] == 'false' then
- limit[2] = false
- print('Disabled spaces in words')
- else
- print('Enter true to enable spaces or false to disable them or leave blank to not change.')
- prefix('changeSpace> ')
- space = io.read()
- if space == 'true' then
- limit[2] = true
- print('Enabled spaces in words')
- elseif space == 'false' then
- limit[2] = false
- print('Disabled spaces in words')
- else
- print('Spaces unchanged.')
- end
- end
- elseif input[1] == 'changelives' then
- if tonumber(input[2]) then
- limit[3] = input[2]
- print('New max lives set to ' .. limit[3] .. '.')
- else
- print('Enter the new lives max below or leave blank to not set:')
- prefix('changeLives> ')
- newLimit = io.read()
- if tonumber(newLimit) then
- limit[3] = newLimit
- print('New lives max set to ' .. limit[3] .. '.')
- else
- print('Lives max not changed.')
- end
- end
- elseif input[1] == 'start' then
- while true do
- word = newWord()
- if word == 'exit' then
- print('Exiting the game')
- break
- elseif word then
- lives = limit[3]
- guess = false
- chars = {}
- rightGuess = true
- win = false
- for i=1,#word do
- if word:sub(i,i) == ' ' then
- table.insert(chars, {' ',true})
- else
- table.insert(chars,{word:sub(i,i),false})
- end
- end
- while lives > 1 do
- os.execute('cls')
- win = true
- rightGuess = false
- for i=1,#chars do
- if guess == false then
- rightGuess = true
- elseif chars[i][1] == guess and not chars[i][2] then
- rightGuess = true
- chars[i][2] = true
- end
- end
- if not rightGuess then
- lives = lives-1
- end
- for i=1,#chars do
- if not chars[i][2] then
- win = false
- end
- end
- if guess == word then
- win = true
- end
- if win then break end
- print('Guess a letter in the word below or guess the entire word, you have ' .. lives .. ' lives left.')
- for i=1,#chars do
- if chars[i][1] == ' ' then
- io.write(' ')
- elseif chars[i][2] then
- io.write(' ' .. chars[i][1])
- else
- io.write(' _')
- end
- end
- print('\n')
- prefix('guess> ')
- guess = string.lower(io.read())
- end
- os.execute('cls')
- if win then
- print('Congratulations you guessed it! The word was "' .. word .. '"\n')
- else
- print('Game over. The word was "' .. word .. '".\n')
- end
- else
- os.execute('cls')
- print('The word was bigger than ' .. limit[1] .. ' characters.\n')
- end
- end
- else
- print('Unknown command. Type "help" for options.')
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment