lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


4x4 grid = 16 positions,
each position is perhaps 2 states, placed or not placed?

Consider using tables:

game_state = {[1]={[2]=true,[3]=true}} -- player currently has blocks
in (1,2) and (1,3)
goal_state = {{[2]={[3]=true,[4]=true}}  -- the goal state for this
level requires blocks in (2,3) and (2,4)

function check(game, goal)
  for x, row in pairs(goal) do
    for y, state in pairs(row) do
      if game[x][y] ~= state then return false end -- return false if
game state does not match goal state
    end
  end
  return true
end

check(game_state, goal_state)

This might require some adapting to fit your game logic, but would
allow you to declare future arbitrary goal patterns by simply
declaring a new table.
- David Hollander

On Sat, Jul 23, 2011 at 8:04 AM, Michelle Jenkins
<ultraluxurylife@gmail.com> wrote:
> Hello Everyone,
> I am trying to write a type of combination If Then statement and it doesn't
> seem to be working.  I want to write a statement where i can ensure a
> pattern of placing a gamepiece is being done correctly.  To do that I need a
> statement like below.  For instance the statement below is making sure the
> pieces in block "1,1" and "1,2" is correct.  I have 16 different blocks and
> depending on the game played in one level a person could be in 9 spots so I
> would need to check those spots.  Lets just say the spots are "1,1",  "1,2",
>  "1,3", "1,4",  "2,3",  "2,4",  "3,1",  "3,2",  "3,4"
> How would I write an if then statement with those combinations in the code I
> have below?  Nothing I do works, so I know I'm doing something wrong.
>
> checkAllBlocks = function()
> for i=1,#blocks do
> if (blocks[i].x < 130 or blocks[i].y < 90) then
> --DO STUFF HERE WHEN LEVEL ISN'T COMPLETED, PROBABLY NOTHING
> return false
> end
> end
> if (findBlock(1,1) == findBlock(1,2)) then
> native.showAlert("Uh-oh","You can't do that.",{"Try Again"});
> --this stops our alert box from showing
> return false
> end
> --DO STUFF HERE WHEN LEVEL IS COMPLETED
> native.showAlert("level complete","success",{"Awesome!"});
> return true
> end