First game help with logic

Hey Corona Fans,
Hoping the forum will help me a bit further in my first try out for a game.

First about me:
Can code in: C#,PHP,Objective-C. Made apps and websites. But never a game. So I learned a bit about LUA and Corona SDK with the excellent tutorials from Jay. (Maker of outlaw).

But now I want to make a game and I am stuck because I can’t figure the simple logic. (Is this normal?)

The game goes like this:

You have a game board. In the centre of the screen you have 6 icons that has a square overlay over that you move with swipe. Above and down the icons are other icons. You can move them up and down into the overlay field (you can move only left or right with the overlay) with the square. I want to match the icons from up and down in the middle.

Problem:
This is my logic. I make an array for the centre field to check if any of the 6 icons are a match (3 or higher). But how should I code the column that is moving up and down into the matching field. Push the icon in the array and removing the one that is being pushed down or up ?

How would I know which one I just pushed in the centre field?

Bare with me it’s my first game and game logic is much harder than programming non-games.

Without knowing more it sounds like you need an array that contains both x/y for example.

local grid = {} for x = 1, 5 do --Lets say 5 rows across grid[x] = {} --Create empty table for columns. for y = 1, 5 do --Lets say 5 columns down. grid[x][y] = {iconKey = "fred", pushed = false, center = false, iconObject = someImageObj} --fill slot with info end end

So if you add your icon “image” to the iconObject = of the table you will now have access to it by simply saying

grid[2][1].iconObject -> this would be the second row and the first column icon.

as you move stuff around you can simply either destroy the iconObject or swap it using that object in the correct position.

Matching would be simple too, just say something along the lines of…

if (grid[1][1].iconKey == grid[1][2].iconKey and grid[1][2].iconKey == grid[1][3].iconKey) then --its a match, continue checking more or return true/false end

Granted above is pretty simplistic but gives you the general idea.

[quote=“Christopher_Bishop,post:2,topic:323818”]

Without knowing more it sounds like you need an array that contains both x/y for example.

local grid = {} for x = 1, 5 do --Lets say 5 rows across grid[x] = {} --Create empty table for columns. for y = 1, 5 do --Lets say 5 columns down. grid[x][y] = {iconKey = "fred", pushed = false, center = false, iconObject = someImageObj} --fill slot with info end end

So if you add your icon “image” to the iconObject = of the table you will now have access to it by simply saying

grid[2][1].iconObject -> this would be the second row and the first column icon.

as you move stuff around you can simply either destroy the iconObject or swap it using that object in the correct position.

Matching would be simple too, just say something along the lines of…

if (grid[1][1].iconKey == grid[1][2].iconKey and grid[1][2].iconKey == grid[1][3].iconKey) then --its a match, continue checking more or return true/false end

Granted above is pretty simplistic but gives you the general idea. [/quote]
So in essence you say do all in one 2d array instead of one array for where the matching happens and one for the icons top or down ?

Btw this is the game I want to make… I am not going to clone it but I thought its a good first game…

http://cj-pixel.com/move-your-marbles/

Just downloaded the game to check it out however it makes no sense to me a slid the middle row left and right but nothing happen lol…

But given that yes, that is how I would do it it looks like there is 7 rows and 9 columns and the ones sliding in from the top and bottom are simple transitions.to so for example build your grid layout and inside the table do something like if (y == 5) then isCenter = true end

then it is a simple if y <= 4 then it is the top row if y >5 then it is the bottom row.

if you expand this out a little bit and do a newRect at each grid location with a 0 or alpha fill 0 then when you are dropping from the top or shooting up from the bottom you simply say transition.to(newBallIMage, {y = grid[1][4].y}) etc. and so forth.

But i am still half a sleep :slight_smile:

Haha I wonder if game design logic is always so hard. I can’t wrap my head around something that looks so easy.

So this is the idea:
Make a function that generates a grid for all columns and rows. Making sure that each object has a place for a object name, x and y.

Make a function that pushes some random objects into the generated grid (updating the object Name and x and y)

Make a function that goes through the grid and draws all ? Is this a good idea or should I fill the table with display objects instead of names ?

Am I going the right direction so far?

I won’t be back at computer until tomorrow but I think I have most of the functions you are looking for on my old dev box that I could shoot up here for ya to get you a head start :slight_smile:

Wow I am curious :slight_smile: just want to finish a game from beginning to end so I can understand the logic and process much better. In tutorials you say easy and oke I can do that. But the minute you have something you can’t figure it out lol

No worries, it just takes a bit to kind of figure out how the flow works and if you are coming from C#, PHP you should be able to grasp it pretty quick you just need a basic this is how the flow works etc. and so forth… I found some code on one of my backups that will help you out just need to put it all together into one file and comment it so you can see it… 

Once you see it I am pretty sure the light will go off and you will just get it :slight_smile:

I have to finish putting up my fence (weekend project) so more than likely wont be able to put it all together until tonight or tomorrow am :slight_smile:

Hey khawtf, sorry it took me so long ended up just writing it from scratch as the old code i had was all G1.0 stuff and thought it would be easier just to write it up and had some other things going on so was doing it in between :slight_smile:

Ok couple things.

1: If you are like me, it might be better to understand the code by just removing the comments lol.

2: I did this in under 3 hours so it is kind of ugly but you will get the idea.

3: Didn’t put a whole lot of game logic in because to be honest i have no idea how that game works.

4: The swipe for the middle row is “real basic” and not doing any loading from left right.

5: And of course do whatever you want with it :slight_smile:

I tried to put a few different things in just so you could get a head start on how things all come together so I ended up just making main.lua just that a main entry point and then used composer so you could see how scene stuff is put together.

Screenshot is here:  example.png

Link to zip file is here: https://s3.amazonaws.com/mclss/Grid.zip

Enjoy and let me know if you need help with anything else :slight_smile:

Wow this is awsome!!! Let me dive in the code.
I really appreciate this !

No problem, just keep in mind some of it is spaghetti code as i was distracted and sleep deprived lol

I can follow what you did very good. Really like it. I have a question about the grids. So you divided it up into 3 different grids. Isn’t that a bit harder to move a column down into the middle field ?
Then I would have to check where I swiped up or down and shift the icon that is moved up into the column and the one that comes from beneath into the middle column… Is that the best way to do this ?

Yeah, I split it up because I really didn’t understand the game and given that I didn’t understand the game I made 3 different functions so if need be you could knock it down to one function and just increase the row/column in the for x and for y :slight_smile:

So what you could do is remove the Middle and Bottom functions (move the bottom rect up to the build top function) then just increase the x/y values in the for loop and modify the drop function to check for < 6 = Top and > 6 = Bottom and 6 = Middle :slight_smile:

Make sense?

If I had a better idea of how the game should actually work i could modify it fairly quickly, it sounds kind of like candy crush with a twist of a sliding middle piece?

Hehe its rather simple…the icons drop from the top and bottom and you can move the columns up and down you have to match them in the middle…its rather simple game :slight_smile:

So basicly i have to change the first function and add to the y and remove the functions for middle and down. Change the function for swiping to move only the middle.

Right ?

having a hard time changing the function to 1. I will try it from the bottom up

Flip the showDebug at the top to true so you can see the grid lines being draw, I will have a look at it in morning as you should be able to comment out Middle, Bottom and just increase the y in the top function to for y = 1, 11 (as 11 would be the new value) 

Yes i see it i am changing the code. Dropping the icons from both sides is kind of tricky and btw why are you using the 

pcall function for… 

like this one:

pcall(function() table.insert(self.imageCache, iconObj) end)

lol, because at one point in my sleep deprivation I had gotten to the point of not really caring if it inserted into the image cache or not lol

disclaimer: I don’t really recommend what I did but there are times when it has its uses inside of lua.

pcall is a really good way to try something without error-ing back to the app

try{ //do something } catch(e){ //do nothing on error just keep moving along //because we don't care if it throws an error }

Hehe So i did what you said

its all one array now, i made a function that adds icons in the middle indexes for all columns because i dont want that empty.

The problem is in another function i made a function that handles the touch,

If i move left:

if gEvent.xStart > gEvent.x and swipeLength > 10 then 

                print(“swipe left”)

                        for columnX=1,6 do

                        local tmpObj = self.topGrid[columnX][6].gridObj

                            if columnX == 1 then

                            table.insert(self.topGrid[7],6,tmpObj)

                            table.remove(self.topGrid[7],6)

                            transition.to(tmpObj, {transition = easing.outBack, time = 200,  x = 722, onComplete = function(iObj) end})

                            else

                            table.insert(self.topGrid[columnX - 1],6,tmpObj)

                            table.remove(self.topGrid[columnX - 1],6)

                            transition.to(tmpObj, {transition = easing.outBack, time = 200,  x = tmpObj.x - 107, onComplete = function(iObj) end})   

                        end 

                        end

How do you move the icons…they are not stored in the array lol and is this the best way to do it:

array = 1,2,3,4,5,6,7

if move left it should look like this array = 2,3,4,5,6,7,1

the numbers are icons 

No real need to add/remove like that ie: table.insert, table.remove you can simply just add a index value to the icon like index = 1, index = 2 etc. and then just get the index from grid table for its x/y etc. and then just transition that way.

You are on the right path but a lot of the answer to the question “is this the best way” really depends on other factors like for example if a match is found do you destroy the match and load in more icons or do they lock or remain in the grid etc. 

The grid itself should always remain intact and only the actual value of what is in the grid position should be changed, the rest should be just moving to those grid positions and updating data.

But if i want to “match” the icons (3 or more) i should know where they are in the grid…and moving the x value is just the icon it self…  i cant match based on that…right ??

My biggest problem stays i dont know how to move the icons they are not inside the self.topGrid[x][y].gridObj its another table in it

Just so its clear the top and bottom grids can be shoved into the middle pushing the other away (if there is room)

  |F|


A|B|C


D|  |E

swipe up from the first index:

A|F|


D|B|C


   |  |E