Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
"smart" way to find "patterns"? — Gideros Forum

"smart" way to find "patterns"?

piepie Member
edited August 2016 in Game & application design
Hi, I was wondering about how to implement this, I'm sorry if the title is not perfect, I don't know how to call this operation: :)

my player can collect various items (start with 3, maybe more in the future) each item have a value in points.
When collected, the items are saved in a table with 6 slots (maybe more later)
when the table is "full" its content should be processed:
to make an example I will name those items with letters and give them values (A = 1,B = 3,C = 5)
tableOfCollectedItems = {
{A,1},{A,1},{B,3},{B,3},{C,5},{C,5}
}
resultOfAddition= 18
Each object in ipairs have its item name and its value in points, so I can easily add all the values and get a result.
This is where I'm stuck: I'd like to give bonus points based on the "composition" of the table.

- if all the "slots" are filled with the same item ,
- if 3 "slots" are filled with one item and 3 are filled with another,
- if there are 2*3 "slots" filled with the same item (as in tableOfCollectedItems before ) .

I can only think of a complex if/then table parsing to do this, which would most likely prevent me to easily add slots later in the game.
Do you have any idea to do it better?
It feels like something that bit operations can handle, but I don't know where to start.

Thank you

Comments

  • n1cken1cke Maintainer
    edited August 2016 Accepted Answer
    If items order doesn't matter then this is solution for six slots:
    local A, B, C = 1, 3, 5
     
    local tableOfCollectedItems = {
    	{A,1},{A,1},{B,3},{B,3},{C,5},{C,5}
    }
     
    local resultOfAddition = 18
     
    local t = {}
    for k,v in ipairs(tableOfCollectedItems) do t[k] = v[1] end
    table.sort(t) --> {1, 1, 3, 3, 5, 5}
     
    if t[1] == t[2] == t[3] == t[4] == t[5] == t[6] then print "1 * 6"
    elseif t[1] == t[2] == t[3] and t[4] == t[5] == t[6] then print "2 * 3"
    elseif t[1] == t[2] and t[3] == t[4] and t[5] == t[6] then print "3 * 2" end
    How do you want to add slots? For example if you will add 7th slot then which combinations will you check then? Because it will only have "1*7" combo.

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    Thank you, nice sorting trick! :)
    The order doesn't matter, I will go for this.
  • talistalis Guru
    edited August 2016
    edit reason = there seems like an error in the code will look and update later.

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    edited August 2016
    >How do you want to add slots? For example if you will add 7th slot then which combinations will you check then? Because it will only have "1*7" combo.

    I'm sorry I didn't notice this yesterday :) I thought to add slots in pairs and check for "proportional" combinations.
    I am thinking about counting item types and item occurrency in collectedTab, I believe that knowing the number of slots and those infos I could get my combos dynamically:

    ie:
    1 item type in 6 slots = combo 1
    2 item types in 3+3 slots = combo 2
    3 item types in 2+2+2 slots = combo 3

    These could be easily translated to 8 slots and more, assuming that I provide at least max_slots/2 items to collect.

    1 item type in 8 slots = combo 1
    2 item types in 4+4 slots = combo 2
    4 item types in 2+2+2+2 slots = combo 3

    I am still thinking how to write it clean though :D
    There is also an issue I didn't thought before: it should be a lot easier to get those combos with more slots available, so I don't know if it's worth doing it :)
  • n1cken1cke Maintainer
    Accepted Answer
    local function checkCombo(t)
    	local r = {}
    	for k,v in ipairs(t) do r[v[1]] = (r[v[1]] or 0) + 1 end
    	local c = {}
    	for k,v in pairs(r) do c[#c+1] = v end
    	local l = #c
    	if     l == 1 then return 1
    	elseif l == 2 and c[1] == c[2] then return 2
    	elseif l == 3 and c[1] == c[2] and c[2] == c[3] then return 3
    	else return 0 end
    end

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.