Elegant/easy code to trigger depending on number differences in a sequence?

Started by KyriakosCH, Mon 02/01/2017 22:15:28

Previous topic - Next topic

KyriakosCH

I want a code to trigger different outcomes depending on which three (out of 9, repeated only one step after the immediately next one) numbers the player chose.

More clearly: You can choose 3 numbers. They all have to be out of the group (1,2,3,4,5,6,7,8,9). But while the first number can be any of those 9, the second can be only the 8 which aren't the previous choice (choice #1), and the third can be only the 8 which aren't your second choice (choice #2).
Eg if you choose "1" for the first position, you can choose 2,3... up to 9 for the second, etc.

While mathematically this is a variation of probability for a set of 3 out of 9 numbers repeated with a small exception, i am wondering if there is an elegant way to code it without having to use object binary (true or false) (which i can code, cause it is basic := ). I think the full basic object thing would go on for up to 9 x 8 x 8 different outcomes, which i would want to avoid even if many lead to the same next screen/sequence.

Thanks in advance for any help! This is for an AGS game i am creating.



Yes, you can't hop onto the square you already are on (nod)
This is the Way - A dark allegory. My Twitter!  My Youtube!

Snarky

You haven't really explained what you want to do. You are right that there are 9*8*8=576 different possible number combinations... If you don't care about all of those so that you have to treat them all differently, what is the key thing you actually care about, that should affect the outcome?

If you just want to detect that the sequence is valid (no jumping to the same square), that's trivial:

Code: ags
bool numbersOk(int num1, int num2, int num3)
{
  if(num1==num2 || num2==num3)
    return false;
  else
    return true;
}

KyriakosCH

^Nice :)

Well, out of the 9x8x8 possible results, i want to tie them to 5 or 6 possible triggered sequences (those will just be in other rooms). So depending on some specific combination, you either go to the "bad" result/room, or to the actual next stage of the adventure.

I am not yet decided on whether there will be a very small number (or even just one) of combinations leading to the next phase. But the issue is more what to do with the rest leading to 5-6 different sequences.
To be more precise (without giving too much away) each number is a variable, but of different types of things. Maybe the most economical way to do this would be to have preloaded states in each of the 5-6 bad rooms, so if the first of the three numbers chosen is "1", you get stageA, and so on? (StageA being a TypeA thing) And then just two more similar events for typeB and typeC?
I suppose this can be done with a rather crude code of if...etc, else...etc...else...etc?
This is the Way - A dark allegory. My Twitter!  My Youtube!

Snarky

Quote from: KyriakosCH on Mon 02/01/2017 23:42:15But the issue is more what to do with the rest leading to 5-6 different sequences.
To be more precise (without giving too much away) each number is a variable, but of different types of things. Maybe the most economical way to do this would be to have preloaded states in each of the 5-6 bad rooms, so if the first of the three numbers chosen is "1", you get stageA, and so on? (StageA being a TypeA thing) And then just two more similar events for typeB and typeC?
I suppose this can be done with a rather crude code of if...etc, else...etc...else...etc?

Again, you're not properly describing the logic or behavior you're trying to achieve. What does StageA and "TypeA thing" refer to?
If there are only 5-6 bad sequences, which you reach by going to 5-6 bad rooms... then you shouldn't need any particular code to interpret the numbers in those rooms, since by going to the room you've already determined which sequence to play. If the sequences play out in the same actual location, you shouldn't need different rooms, just 5-6 different functions, each playing a sequence. And if the sequences are basically similar just with some variation, you can probably use a single function that takes an argument (an enum or int 1-5 or 6), which then allows you to vary key points in the sequence.

As for how to determine which of the 5-6 sequences to play, that depends ENTIRELY on the precise rules you want to implement. If you want it to depend on particular numbers appearing or not appearing in the three squares chosen, you should do it one way. If you want it to depend on the relationships between the three numbers (e.g. whether it's an up-up, down-down, up-down or down-up sequence), there's another way. If you want to treat some numbers as a group, there's a third way, etc., etc.

You say "each number is a variable, but of different types of things". If I'm interpreting that correctly, it means that the first number controls one thing, the second another, and the third yet another, completely independent of each other? Isn't the answer then to... just set each variable according to its corresponding number? Again, how to do that comes totally down to things you haven't explained: What kind of variable is it? How many states does it have? How should the mapping from number to variable state work? Of course, in the simplest case the state is simply the number. Or you could have an array and just look up the value. Or do some calculation on the number to work out the right value. Or a series of if-else statements (or, if you're using an AGS version that supports it, switch-case statements). IT DEPENDS.

But this still does not address how you go from a certain combination of those three variables to one of the 5-6 sequences, and I guess I'm starting to doubt that you have a clear idea of what behavior you want there. Programming is all about telling a computer exactly what it needs to do. In order to do that, you need to be able to express exactly what you want to happen. And to do that, you need to get it clear in your own mind. If you're not sure how you want it to work, there's no way to program it.

KyriakosCH

Thanks for the final snarky comment, yet i already told you i don't plan to give away what the relation between the numbers is, probably cause that is what the game is about, Snarky. They build off each other, yet still don't lead to 9x8x8 different cases cause many there are tied to the same development. And while i would use an elegant set-up (which is why i asked for one) i probably can do one with mostly the same effect. Sorry if you interpret that as negative enough that you have to be like this :)

Don't worry, you aren't the only person here familiar enough with math to ponder the logic of things, i was just interested in the AGS code (nod)
This is the Way - A dark allegory. My Twitter!  My Youtube!

Snarky

If you don't want to explain what you want the code to do or what logic it is supposed to implement, we can't possibly help you figure out the best way to write it. Hence your question is silly, hence my tone.

KyriakosCH

{
cSnarky.say

was not the code i was after. Happy new year, and thanks for the first part (about the difference in number chosen :) i do need that very much).

Anyway, sorry for being secretive about the specifics there; i will have a look at the options you briefly alluded to and make something myself for the specifics. Peace (nod)
This is the Way - A dark allegory. My Twitter!  My Youtube!


Matti

You can still put the question/explanation/code in spoiler tags or make it clear to people that when they follow the whole thread they might not have much fun in case they want to play your game. I don't think that too many people follow this thread anyway, so I don't really see the problem.

If you don't want to spoil at all, then the question is indeed silly ;)

Monsieur OUXX

a wild guess :

(please note I wrote this code directly in the forum, did not compile it or test it)
Code: ags


bool numbersOk(int num0, int num1, int num2)
{
  if(num0==num2 || num1==num2)
    return false;
  else
    return true;
}

int numbers[3];

void drawNumbers()
{
    numbers[0] = Random(9); //first number can be anything we want

    numbers[1] = Random(9); //we take a second number randomly...
    while (numbers[0] == numbers[1]) { //but if it's the same as the first one we need to draw a new one. We continue until it's correct
          numbers[1] = Random(9);
    }
    numbers[2] = Random(9);//we take a third number randomly...
    while (!numbersOk(numbers[0], numbers[1], numbers[2])) { //but if it's the same as the first or second one we need to draw a new one. We continue until it's correct
          numbers[2] = Random(9);
    }
    //at this stage we picked 3 different numbers
}

//this function is not really useful, it's just for convenience
bool isSequence(int num0, int num1, int num2)
{
  if(numbers[0] == num0 && numbers[1] == num1 && numbers[2] == num2) 
    return true;
  else
    return false;
}

function game_start()
{
    drawNumbers();
    Display(String.Format("the 3 numbers are : %d, %d, %d", number[0], number[1], number[2]));

    //now for your favorite combinations
   if (isSequence(1,2,3))  //1 2 3
       Display("ABC baby you and me");
   else if (isSequence(3,2,1)) // 3 2 1
       Display("That's a countdown");
   else if (isSequence(0,1,2)) // 0 1 2
       Display("you're counting in base zero, you big nerd");
   //etc.
}
 

KyriakosCH

This is the Way - A dark allegory. My Twitter!  My Youtube!

SMF spam blocked by CleanTalk