Need some help to create a difficult algorithm

0 favourites
From the Asset Store
Awardkit
$5 USD
Create custom awards and achievement or use the many templates and create your own.
  • 1) I have two sprites (sprite1 and sprite2) with the same animations (10 in total) with different amount of frames each:

    anima1 = pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8, pic9, pic10

    anima2 = pic3, pic5, pic7, pic8, pic9, pic10, pic12, pic15, pic16, pic17, pic18, pic19, pic20

    anima3 = pic1, pic3, pic5, pic7, pic9, pic11, pic13, pic15, pic17, pic19, pic21

    .

    .

    .

    anima10 = pic2, pic3, pic4, pic12, pic13, pic14

    2) The game will have 6 categories and each category will have references for some animations of the sprites I have cited:

    category1 = anima1, anima3, anima5

    category2 = anima2, anima4

    category3 = anima1, anima2, anima3, anima4, anima5, anima6, anima9, anima10

    .

    .

    category6 = anima1, anima2, anima5, anima6

    3) The player will choose between numbers 1 and 6 and the system needs to get the categories of the respective group and show a randomly frame of a randomly animation of the sprite1 and a different randomly frame of a randomly animation of the sprite2:

    Player choose number 3

    sprite1 = anima2 / pic4

    sprite2 = anima10 / pic14

    4) The player now needs to chose one of these two pictures and each will have a different value that I need to add to a score that represents the animation (each frame of each animation has a different value):

    Player choose pic14

    Add 5 to the scoreAnima10

    The last process (4) needs to repeat 10 times.

    At the end, I need to show the total score and what animation wins. If we have a tie, the system needs to show images of the respective animations to tie up.

    I need to understand what I need to do for each stage of the script.

  • 2. You need to decide how you want to store these lists of categories/animations - you can read them from a text file (csv, xml etc) or load as JSON into an array or dictionary.

    Or you can simply create 6 text variables:

    category1 = "anima1,anima3,anima5"

    category2 = "anima2,anima4"

    ...

    3. When player selects a category, you can choose a random animation from a comma-separated string of animations using this expression:

    animationsList = category3 // for example

    animationName = tokenat(animationsList, floor(random(tokencount(animationsList, ","))), ",")

    Then set this animation to a sprite and choose a random frame:

    Sprite1 set animation to animationName

    Sprite1 set frame to floor(random(Sprite1.AnimationFrameCount))

    Repeat the same steps for the seconds sprite.

    4. How do you assign a value to selected frame I don't know, you need to give more information about these values. What are they? Are they different for each frame? Where are you planning to get these values from?

  • Hi dop2000

    Thanks for replying.

    2. You need to decide how you want to store these lists of categories/animations - you can read them from a text file (csv, xml etc) or load as JSON into an array or dictionary.

    Or you can simply create 6 text variables:

    category1 = "anima1,anima3,anima5"

    category2 = "anima2,anima4"

    ...

    I will use simple variables for this 1st test but I intend to use a DB to store and load informations.

    [quote:2m2n2ohh]3. When player selects a category, you can choose a random animation from a comma-separated string of animations using this expression:

    animationsList = category3 // for example

    animationName = tokenat(animationsList, floor(random(tokencount(animationsList, ","))), ",")

    Is this animation list will be filled by the string variable "category3" and the tokencount will be separate this information and randomize them? Is that correct?

    [quote:2m2n2ohh]

    Then set this animation to a sprite and choose a random frame:

    Sprite1 set animation to animationName

    Sprite1 set frame to floor(random(Sprite1.AnimationFrameCount))

    Repeat the same steps for the seconds sprite.

    I think I got it (but I am not sure) but anyway, I don't know how to avoid the animation/frame repetition (frames because a same frames could be found in more than an animation)

    [quote:2m2n2ohh]4. How do you assign a value to selected frame I don't know, you need to give more information about these values. What are they? Are they different for each frame? Where are you planning to get these values from?

    Simple points. For example: frame 1 worth 3 points, frame 2 worth 1 point, 3 worth 7 points...

  • [quote:1zukx38c]3. When player selects a category, you can choose a random animation from a comma-separated string of animations using this expression:

    animationsList = category3 // for example

    animationName = tokenat(animationsList, floor(random(tokencount(animationsList, ","))), ",")

    Is this animation list will be filled by the string variable "category3" and the tokencount will be separate this information and randomize them? Is that correct?

    animationsList is a text variable, you can fill it from Category3 variable or in the future from your DB.

    That long formula selects a random animation name from a list of names separated by commas. See tokenat() and tokencount() expressions in the manual.

    [quote:1zukx38c]

    Simple points. For example: frame 1 worth 3 points, frame 2 worth 1 point, 3 worth 7 points...

    Ok, but you will have at least 100-200 different frames. How your game will know how much points each frame worth? Where will you get this information from?

  • >

    > Simple points. For example: frame 1 worth 3 points, frame 2 worth 1 point, 3 worth 7 points...

    >

    Ok, but you will have at least 100-200 different frames. How your game will know how much points each frame worth? Where will you get this information from?

    May I could use arrays to store these points, one for each animation. What do you think about this? Do you know a more practical solution?

    And about the repetition issue? What can I do to avoid the animation and frame (image) repetition?

  • Ok, and where will you get the data to put into these arrays?

    You need to read this data from somewhere - a text file with JSON strings, or CSV file, or XML, or maybe request it from a server etc.

    I would probably use CSV - you can create a table with all values in Excel, save as CSV and import to C2. You can do the same with the lists of categories/animations.

    Also, you should probably re-think the whole idea of how you store all these pictures.

    10 animations with lots of frames, different values for each frame, plus all of them divided into categories - this mess will be very difficult to manage.

    Maybe instead of animation and frames, consider importing all images as files into your project.

    So you'll have a bunch of files (001.jpg - 199.jpg for example) and two empty sprites.

    In your CSV file you'll have the list of all images (001 - 199), list of categories for each image, points value for each image:

      001, Animals, Birds, , 7 002, People, Professions, Celebrities , 12 etc.

    Load data from CSV into an array or dictionary. When user selects a category, find a random image in the array from this category, load its picture into your sprite using this action:

    Sprite1 load image from URL "123.jpg"

    Check tutorials, I think I seen something similar there, some picture quiz game.

    https://www.scirra.com/tutorials/all

    As for the repetition issue, you can simply delete images from the array after they have been used.

  • Sorry about the delay.

    I just would like to know if I also can use the token to store the points, associanting the animation frame to the points order in the variable categoryX_points. If so, could you please check the example below and tell me where I need to include the animation in the tokenat expression to get the frame that is showing on the screen? Please, confirm if the example is correct.

    Something like this:

    animationList = category1

    anima1_points = 5,2,3,1,2,4,1,2,3,2

    add tokenat(animationsList&"_points", floor(tokencount(animationsList.animationFrame, ","))), ",")to totalPoints

  • You can't do this - animationsList&"_points" - with variable names.

    You'll need a temporary variable s and a bunch of events like:

    If CategoryNumber=1 Set s to Category1_points

    If CategoryNumber=2 Set s to Category2_points

    Or you can store your lists of points in a dictionary, under keys "Category1_points", "Category2_points" and do this:

    s=PointsDictionary.Get("Category" & CategoryNumber & "_points")

    Add int(tokenat(s, Sprite1.animationFrame, ",")) to totalPoints

  • You can't do this - animationsList&"_points" - with variable names.

    You'll need a temporary variable s and a bunch of events like:

    If CategoryNumber=1 Set s to Category1_points

    If CategoryNumber=2 Set s to Category2_points

    Or you can store your lists of points in a dictionary, under keys "Category1_points", "Category2_points" and do this:

    s=PointsDictionary.Get("Category" & CategoryNumber & "_points")

    Add tokenat(s, Sprite1.animationFrame, ",") to totalPoints

    I think I understood your suggestion. I will give it a try and after that, I will return here to leave a feedback.

    Thanks

  • I forgot to say that I need to calculate the points separately for each animation. I think to use a dictionary to store the total points of each one (anima1/anima2/anima3...). When the game finishes, I will have to check which animation has the highest points and show the image of the winner animation then. I would like to know if I can do it with the dictionary (or if I will need to use an array) and how to compare the values in the dictionary (is it possible?) and get the name of the winner.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I notice that you have identically named pictures for separate animations, are they meant to be identical pictures?

  • Yes. But depending on which group this image is, it worth more or less value.

  • I got bored of finding a simple solution. You could just use one sprite with on animation with all the frames. The second object would just be a separate instance. The animations are just lists of frames, and categories are just lists of animations. The frame value, if different per animation could be included in the animation list.

    The lists could be done with arrays, text variables or whatever. Getting the data into the arrays isn't too bad. Dop listed a few solutions but there are more.

    The algorithm itself is straightforward imo. Just implement it as you say.

  • Actually, I just need to know if I can use a dictionary to store the total points of each animation and how can I check which animation has the highest points when the game finishes.

  • R0J0hound

    Maybe I will have to use an array instead of a dictionary. I saw your suggestion in this thread:

    I am wondering if is possible to also know the index that stores the highest value, using this same code:

    global number highest=0
    
    +----------------------+
    |                           | set highest to array.at(0)
    +----------------------+
       +-------------------+
       | array: for each x | set highest to max(highest, array.curValue)
       +-------------------+[/code:1sjoo0i4]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)