problem populating a table with random numbers

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.
Post Reply
namelis
Prole
Posts: 2
Joined: Mon Dec 17, 2012 7:53 am

problem populating a table with random numbers

Post by namelis »

to start with i don't have much programming experience but i do have at least a general understanding of the basics also i haven't really done any programming for like 10ish years

i have a vague idea of a game i want to make and i figured no better way to learn then by doing so i spent some time reading the wiki and started working adding features as i got the prior ones working but i just hit a point where as far as i understand what i did should work but it does not so there must be something i don't understand

this is where i think my problem is cause before i added it everything worked

Code: Select all

  puzzle = {                                                  --builds a random puzzle into a table
	      for hight = 1, 8 do    --i want my puzzle to have 8 horazontal rows 
		{ for width = 1, 12 do  -- i need the { to begin each block of columns that will give me my rows and i want each row in my puzzle to have 12 vertical columns 
		    math.random(#blockTypes) --i want to fill my puzzle with the same number of block types that i have image's loaded in the table blockTypes
		    if width ~= 12 then , end -- i need a , between each randomly assigned block for each new column in the row im building but not after the last one 
		} if hight ~=8 then , end  -- i need the } to end the block of columns that i am building compleating a row and i nead a , between each new row but not after the last one 
	      end                          -- im done building rows 
	    }                              -- i close my table that holds my randomly filled puzzle 
  
this is the error message that i get
Error: Syntax error: main.lua:16: unexpected symbol near 'for'

stack traceback:
[C]: ?
[C]: in function 'require'
[string "boot.lua"]:331: in function <[string "boot.lua"]:227>
[C]: in function 'xpcall'

attached is the .love both what i have that works and what i have that i don't understand why it doesn't work

any help you can give is appreciated
Attachments
game.love
this is the .love of what i have that works so far
(8.05 KiB) Downloaded 107 times
gameTest.love
this is the .love of what i have so that i don't understand why it doesn't work
(8.56 KiB) Downloaded 103 times
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: problem populating a table with random numbers

Post by Wojak »

this is not a lua syntax... ('{' is the unexpected symbol debuger is referring to)

Code: Select all

 blockTypes = {5,6,8,9} -- just a example, I hope you have similar syntax
  puzzle = {} --start with a empty table   
             for hight = 1, 8 do    
               puzzle[hight]={} -- create a row in each column
                 for width = 1, 12 do 
                   puzzle[hight][width] = blockTypes[math.random(1,#blockTypes)] -- now you set a random id from the blockTypes table to the puzzle table at hight, width coordinates
                 end
             end    
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: problem populating a table with random numbers

Post by Roland_Yonaba »

Hi,

Well this due to the syntax, which is wrong. You don't need those brackets.

A working version would be:

Code: Select all

  puzzle = {}
   for y = 1, 8 do 
     puzzle[y] = {}
     for x= 1, 12 do
       local aBlockTypeIndex = math.random(1,#blockTypes)
       local theRandomBlockType = blockTypes[aBlockTypeIndex]
       puzzle[y][x] = theRandomBlockType
     end
   end
Actually, it seems you're influenced by another programming language you were used to...Anyway, I suggest you to go through the first chapters of PiL, which will help you get the basics with Lua, in terms of syntax.

EDIT: I've just been Wojak'ed :)
namelis
Prole
Posts: 2
Joined: Mon Dec 17, 2012 7:53 am

Re: problem populating a table with random numbers

Post by namelis »

i wrote a whole post where i explained the way i thought lua worked and what i needed to do different to get it to work but when i clicked preview it maid me login again and now its gone and i don't feel like typing it all in again so im just going to say that what i needed was this

Code: Select all

  puzzle = {}                                                  --empty table to hold my random puzzle 
	      for hight = 1, 8 do                              --i want my puzzle to have 8 horazontal rows 
		puzzle [hight] ={}                            --empty table to hold each row of columns
		  for width = 1, 12 do  -- i want my puzzle to have 12 vertical columns 
		    puzzle [hight][width] = math.random(#blockTypes) --i want to fill my puzzle with the same number of block types that i have image's loaded in the table blockTypes
		  end   
	      end   
now im going to sleep
tomorrow il figure out how to remove the blocks if there is 3 or more of the same type in a row
thanks for all the help id have figured it out eventually but it would have taken much longer
o and if you want to know what i was going for here is a .love with a working puzzle table
Attachments
game.love
press scroll lock to release mouse so you can exit game
(8.25 KiB) Downloaded 104 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests