Main function (tables) of a Hangman-clone

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Main function (tables) of a Hangman-clone

Post by Petunien »

Hi,

I started creating a little, easily Hangman-"clone", mainly so I expand my Lua scripting knowledge. :)

I have a function called at the beginn of the game, that selects a word of a predefined wordlist, takes each letter of the word and put them in a table.
That table will be put in the main word-table, so I can register user inputs and compare those with the letters.
If the user guessed right, the relating letter appear (therefore the id).

Code of the "function":

Code: Select all

	words = {"House", "Mouse", "Money"}
	wichWord  = math.random(1, 3)
	wichWord  = words[wichWord]
	
	word = {}
	for i = 1, #wichWord do
		_word = {}
		_word.letter = string.byte(wichWord, i)
		_word.letter = string.char(_word.letter)
		_word.id 	 = i
		table.insert(word, _word)
	end
And now my problem. I don't now how to handle that tables so I can use a for-loop to go trough the entries and compare them.

Or more specific, how would look a code, that print a "_" for each letter that the word contains?

It would be nice, if someone help me getting over this. :)

Greets
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Main function (tables) of a Hangman-clone

Post by trubblegum »

Code: Select all

explode = function(str)
  local t = {}
  for i = 1, str:len() do t[i] = str:sub(i, i) end
  return t
end
implode = function(t)
  local str = ''
  for i, v in ipairs(t) do str = str .. v end
  return str
end
newword = function()
  local word = explode(words[math.ceil(math.random() * #words)])
  local guess = {}
  for i = 1, #word do guess[i] = ' _' end
  return word, guess
end

love.load = function()
  words = {'douse', 'house', 'louse', 'mouse', 'rouse', 'souse',}
  word, guess = newword()
end
love.keypressed = function(key, code)
  if key == 'escape' then word, guess = newword()
  elseif code > 96 and code < 123 then -- could be key:find('%l')
    for i, v in ipairs(word) do
      if v == key then guess[i] = ' ' .. v end
    end
  end
end

love.draw = function()
  love.graphics.print(implode(guess), 0, 0)
end
Should find what you need in there.
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Main function (tables) of a Hangman-clone

Post by Petunien »

Thank you very much!

I'm going to understand your code... ^^
Copy alone isn't enough.
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Main function (tables) of a Hangman-clone

Post by Petunien »

Edit: Ohh, sorry for double-post. I hope it is ok, I wrote a lot of new stuff.

So, please let me know if I have understood your code. :)

Code: Select all

explode = function(str)
  local t = {}
  for i = 1, str:len() do t[i] = str:sub(i, i) end
  return t
end
This function gets a string by a parameter. Then it creates a table named "t" and calls a for-loop with the length of the string as "exp2".
In this for-loop it replaces the current index of the table with the current letter .
Finally it returns the table.

Code: Select all

implode = function(t)
  local str = ''
  for i, v in ipairs(t) do str = str .. v end
  return str
end
This function gets a table as parameter. Then it creates a new string named "str" and appends the current value (v?) of the table to the string.
Finally it does the same as explode(), it returns the table.

Code: Select all

newword = function()
  local word = explode(words[math.ceil(math.random() * #words)])
  local guess = {}
  for i = 1, #word do guess[i] = ' _' end
  return word, guess
end
I don't understand the first line, it would be nice if you explain.
I understand that it creates a table and calls a for-loop, that sets a " _" for every index of the guess table.
Then it returns the word and guess-table, so they can be replaced with the empty ones.

Code: Select all

love.load = function()
  words = {'douse', 'house', 'louse', 'mouse', 'rouse', 'souse',}
  word, guess = newword()
end
Sets the wordlist and calls the function newword() once.

Code: Select all

love.keypressed = function(key, code)
  if key == 'escape' then word, guess = newword()
  elseif code > 96 and code < 123 then -- could be key:find('%l')
    for i, v in ipairs(word) do
      if v == key then guess[i] = ' ' .. v end
    end
  end
end
Checks the user keyboard-inputs. If the key is "escape" it calls newword() and so it resets the game.
If the key is letter with a code between 96 and 123 it call a for-loop, that checks if the value of the table "word" is equal to the pressed key.
If it is true, it replace the current index of guess with the key, so the draw function displays the letter.

Code: Select all

love.draw = function()
  love.graphics.print(implode(guess), 0, 0)
end
Calls the function implode() with the table "guess" and displays the returned string.

Also, why "love.draw = function()" instead of "function love.draw()"?

It is very important for me to understand your code. We don't learn with copy and paste.

I hope, I wrote it understandable. Please correct my wrong assumptions. Thank you a lot. :)
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Main function (tables) of a Hangman-clone

Post by Roland_Yonaba »

Code: Select all

newword = function()
  local word = explode(words[math.ceil(math.random() * #words)])
  local guess = {}
  for i = 1, #word do guess[i] = ' _' end
  return word, guess
end
The first line is quite simple...
You already know that table 'words' is a kind of dictionnary, containing a certain number of valid words.

Code: Select all

math.ceil(math.random() * #words)
Then this a tricky way to select a random integer between 1 and N, assuming N the very number of words existing in the dictionnary.
Fact is math.random() return a decimal number between 0 and 1...that you could consider as a percentage. Multiply this by N, round it to the next integer, and you will get another integer but necessily between 1 and N.
Then we pick up the word indexed at that position in the dictionnary, that we explodes into a table (local word variable on the first line).
Then we return that word, plus another table which is the same in terms of length but consists of a collection of empty characters (' _') that will be used a display mask later on to the player's eyes.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Main function (tables) of a Hangman-clone

Post by bartbes »

Or math.random(#words) could be used.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Main function (tables) of a Hangman-clone

Post by Roland_Yonaba »

bartbes wrote:Or math.random(#words) could be used.
Exactly.

@Petunien...

That was yout first code:

Code: Select all

   words = {"House", "Mouse", "Money"}
   wichWord  = math.random(1, 3)
   wichWord  = words[wichWord]
   
   word = {}
   for i = 1, #wichWord do
      _word = {}
      _word.letter = string.byte(wichWord, i)
      _word.letter = string.char(_word.letter)
      _word.id     = i
      table.insert(word, _word)
   end
Something I can't understand...Can u explain the cumulative use of string.byte() and string.char() ?
Seems obscur... to me :P
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Main function (tables) of a Hangman-clone

Post by Petunien »

As first thank you both for the answers! :)

@Roland_Yonaba

Thanks for your explaination.

Regarding your question...
I used string.byte to get the numerical code of a letter(i) in the string .
Then, with string.char I converted the code back to a "regular" letter.
I don't know, how would I go otherwise about getting a char of a string by a position.

I'm sure there are better solutions for that. ^^
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Main function (tables) of a Hangman-clone

Post by trubblegum »

Yeah .. thanks, Roland, but where's your homework? :P
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Main function (tables) of a Hangman-clone

Post by Robin »

Petunien wrote:I don't know, how would I go otherwise about getting a char of a string by a position.
string.sub
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot], slime and 176 guests