I'm new here, I'm not a programmer and I'm french. So I guess It will be hard for me to understand everything ! If you can't understand one of my sentence, I'll try my best to explain it to you.
Anyway yesterday I started to do a Hangman game with löve, and I was surprised (and proud ) to achieve to do a "working" program. But I can't think of a way to do one thing : the error counter. I tried to increment the error counter when a key is pressed and the character isn't one of the word but it increment the variable too much. I don't know where that come from or how I could resolve this..
Here is the .love :https://www.mediafire.com/?53s7crw5h97999e
And here's my code, that I tried my best to comment :
Code: Select all
--HANGMAN 03-07-2014
function dictionnaire() --Choose a word and return it's length and a string.
local rowIndex =0
local wordList = {}
--The list of words used in the hangman
dictionnaire = [[abricot
absent
boule
boisson
bombardier
copain
captif
constitution
dent
fleurs
gourmand]]
--Selecting a random word
local line = math.random(1,11)
for row in dictionnaire:gmatch ("[^\n]+") do
rowIndex = rowIndex+1
wordList[rowIndex] = row
end
word = wordList[line]
length = string.len(word)
return word, length
end
function love.keypressed(key)
if key and key:match( '%a' ) then character = key end
end
function love.load () --Initialise the variables
character = "*"
victoryCount = 0
characterTable = {}
math.randomseed(os.time())
word, length = dictionnaire()
--CONVERT TABLE INTO _
for i=0,length do
i = i+1
characterTable[i-1] = "_"
end
life = 11
end
function love.update ()
-- Check if the key pressed is in the word
if character ~= "*" then
local i = 0
local o = 0
for letter in word:gmatch ("%a") do
i=i+1
if letter == character then
characterTable[i]= character
elseif letter ~= character and character ~= "*" then ---Here is the problem..
life = life -1
end
end
victoryCount = 0
for letter in word:gmatch ("%a") do --Check the number of correct letters
o = o + 1
if characterTable[o] == letter then
victoryCount = victoryCount+1
end
end
character = "*"
end
--Make the table into a string
characterString = table.concat(characterTable, " ")
function love.draw ()
love.graphics.print (word,10,1)
love.graphics.print (characterString,10,12)
love.graphics.print (victoryCount,10,24)
love.graphics.print (life,300,24)
if victoryCount == #word then
love.graphics.print ("Victory :D",10,34)
end
if life <= 0 then
love.graphics.print ("Defeat",10,44)
end
end