playMaker

Author Topic: Check power of 2 ?  (Read 943 times)

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Check power of 2 ?
« on: July 31, 2018, 06:24:59 AM »
Hello,

So i don't know if it's the right "name" but what i mean when i say "Power of 2" is a serie of number : 1 2 4 8 16 32 64 etc... (1 isn't a power of two obviously so i don't know the right term)

What i want to do :

I'm making a tower of hanoi game, you have to move rings from the first pillar to the third pillar without having a small ring under a large one.

What i want to check is, when i'm moving a small ring to a new pillar is there a larger ring already on the pillar. If yes, then i can't move it.

And i want to do it this way :

Each ring has a int value, let's say
Ring1 = 1
Ring2 = 2
Ring3 = 4
Ring4 = 8
etc..

If a pillar has the ring 3 and 4, the int value returned is 12. So by checking the int value i know that ring 1 and 2 aren't there. Does that makes sense ?

Is there an action in playmaker that could work ? Or a simple function in js/c# that could help me ?

Or maybe there is a complete other way to do it ? (Probably)
« Last Edit: July 31, 2018, 06:35:19 AM by Thiaz »

Plancksize

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 62
    • View Profile
Re: Check power of 2 ?
« Reply #1 on: July 31, 2018, 06:52:57 AM »
Well, that's way more "mathy" than what I would do if my intention was just to avoid moving a larger piece on top of a smaller.

The way I'd do it would be to just record the last (top)  disk in place (without doing the sums).
That way you'd only need to check the value of the disk to be moved against the top disk on the target "stick". (easy to do by, i.e., adding a var to each "stick" that gets updated with the disk size/value when a successful move is done on it. (by considering that an empty "stick" has a "top disk value" of 0(zero).

I realize I'm not answering your question at all but hope that this alternative way can help ;)
« Last Edit: July 31, 2018, 06:56:21 AM by Plancksize »

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Check power of 2 ?
« Reply #2 on: July 31, 2018, 05:09:14 PM »
Seems like a really good idea actually, i didn't thought of that. I'll try that, thanks :)

I found my answer in javascript, but playmaker probably won't help with this kind of things :

https://stackoverflow.com/questions/28298374/decomposing-a-value-into-results-of-powers-of-two

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15371
  • Official Playmaker Support
    • View Profile
Re: Check power of 2 ?
« Reply #3 on: July 31, 2018, 11:50:03 PM »
Hi,

 why not? you have all the actions to do this, you just build an fsm with several states that represent this algorythm.


however, could you not do that with triggers? you could have your rings with carefully places box colliders and you check their collision which will help deciding what can be moved or not .

for example if each ring as a box tangent to their outer circle on one axis, you can then check if it collides or not with the ring above or below and deduce what can be done with it.


basically, what I am trying to say is you could traverse the rings on the pillar from bottom to top in a loop and check the current one against the one below and stop where it's wrong, this will be easier than trying to have an overview of the game with one number ( provided you are limited with scripting or algorythm experience)


Bye,

 Jean

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Check power of 2 ?
« Reply #4 on: August 01, 2018, 02:49:08 AM »
Hi,

 why not? you have all the actions to do this, you just build an fsm with several states that represent this algorythm.

I did, but i feel like it's messy, probably because i'm not good enough yet haha
To check if multiple rings are there i've a lot of int switches to fill, i'll try again today to find a better way to do it.

Quote
however, could you not do that with triggers? you could have your rings with carefully places box colliders and you check their collision which will help deciding what can be moved or not .

for example if each ring as a box tangent to their outer circle on one axis, you can then check if it collides or not with the ring above or below and deduce what can be done with it.


basically, what I am trying to say is you could traverse the rings on the pillar from bottom to top in a loop and check the current one against the one below and stop where it's wrong, this will be easier than trying to have an overview of the game with one number ( provided you are limited with scripting or algorythm experience)


Bye,

 Jean

But how do i know if the ring can move to another pillar ? I've to check the rings on the "destination" pillar to the actual pillar the ring in on to allow the ring to move. By the way, the overview of the game is done with 3 numbers, a count of rings on each pillar.

At the moment i use triggers on rings and on pillars. I use int add every time a ring enter a pillar to know which rings are there when i'm moving a new one.
But when there are like 6 rings on the same pillar it's becoming kind of a headache to know what can move.

So i think Plancksize's method is also good when a ring arrive (setting the int value to the ring on top), but when one ring leaves i subtract the int value of the ring and if there are other rings remaining on the pillar, i don't know which one is still there because the int value is 0. So at the moment i don't know if i can only refer to the ring on top.

Anyway, i need some time to think about all of that.

But i'll definitely try both of your methods, thanks guys :)
« Last Edit: August 01, 2018, 02:51:09 AM by Thiaz »