This site requires JavaScript, please enable it in your browser!
Greenfoot back
TrippSci
TrippSci wrote ...

2021/10/15

Making non-repearing numbers?

TrippSci TrippSci

2021/10/15

#
Alrighty, So in this circumstance I have 5 different categories and i need a random number from each to be chosen that number correlates to a certain image to be placed on the screen. the catch is, I need every possible combination without repeats. Say like a "put the face together" game of sorts I have 5 different eyes, 5 different mouths, 5 different hairs, 5 different ears. And I'm trying to find a way to get every combination of those without repeat. Sorry I sound like a broken record lol just trying to explain the best I can! Maybe if I can press space to initiate the next face? I genuinely don't know where to start but definitely could use help! Thanks.
danpost danpost

2021/10/15

#
Think of it this way. In your example (5 of each -- eyes, mouths, hairs, and ears). That is 5*5*5*5 = 625 combinations. So, 0 thru 624 could be used to represent each combination. With n being a combination: n%5 = index of a pair of ears (n/5)%5 = index of hair (n/25)%5 = index of mouth (n/125)%5 = index of a pair of eyes Example n=297; n%5 = 2; pair of ears #3 (counting starts at zero) (n/5)%5 = 59%5 = 4; hair #5 (n/25)%5 = 11%5 = 1; mouth #2 (n/125)%5 = 2%5 = 2; eyes #3
danpost danpost

2021/10/15

#
The following will show that every combination is acquired:
String[] combos = new String[625];
for (int i=0; i<625; i++)
{
    combos[i] = ""+((i/125)%5)+((i/25)%5)+((i/5)%5)+(i%5);
    System.out.println(combos[i]);
}
TrippSci TrippSci

2021/10/15

#
Hey yah awesome thats a really handy way to think of it so an idea outline could it look like this? i also have each image named ex: eyes1, eyes2 ect cant i shorten that to like eyes"x" I genuinely don't even know where to start on this haha
public void Mouth()
    {
      # 1-5 correlates to
          Mouth img 1-5

    }

    public void Eyes()
    {
     # 1-5 correlates to
          eyes img 1-5
    }

    public void Hair()
    {
    # 1-5 correlates to
          hair img 1-5
    }

    public void Clothes()
    {
     # 1-5 correlates to
          clothes img 1-5
    }

    public void Ears()
    {
    # 1-5 correlates to
          mouth img 1-5
    }
danpost danpost

2021/10/15

#
TrippSci wrote...
so an idea outline could it look like this?
There is no need for methods like that.
i also have each image named ex: eyes1, eyes2 ect cant i shorten that to like eyes"x"
The image names you currently have are fine. It might help to set things up a little:
String[] features = { "mouth", "eyes", "hair", "clothes", "ears" };
Actor[] actors = new Actor[5]; // actor to portray images of features
int[] partNums = new int[5]; // for feature indexes

public MyWorld()
{
    super(400, 600, 1);
    int[] ys = { 200, 150, 100, 400, 175 }; // y-coordinates of features (adjust as needed
    for (int i=0; i<5; i++)
    {
        actors[i] = new Actor(){};
        actors[i].setImage(new GreenfootImage(1, 1));
        addObject(actors[i], 200, ys[i]);
    }
    setPartNumbers(Greenfoot.getRandomNumber(3125));
    setCombination();
}

private void setPartNumbers(int comboNum)
{
    for (int i=0, n=5; i<5; i++, n*=5) partNums[i] = (comboNum/n)%5;
}

public void setCombination()
{
    for (int i=0; i<5; i++)
    {
        String part = features[i]+(partNums[i]+1)+".png";
        actors[i].setImage(new GreenfootImage(part));
    }
}
You can then have a method to change the parts:
public void nextLook(int featureIndex)
{
    partNums[featureIndex] = (partNums[featureIndex]+1)%5;
    setCombination();
}
TrippSci TrippSci

2021/10/17

#
Alrighty that makes sense to simplify things as well, and would the Actor create the needed actors for me then change their image based on the combination? and for the y axis' all my images are set to plug into the same place, as in my mouth will fit into the right place at (25, 29) same as the rest of my parts so should I just change the ys to all 29? Thanks for helping me along this I really appreciate it!
danpost danpost

2021/10/18

#
TrippSci wrote...
Alrighty that makes sense to simplify things as well, and would the Actor create the needed actors for me then change their image based on the combination?
Line 2 declares an array of Actor objects and assigns an empty array to it. In the loop starting at line 9, we first have the actors being created on line 11, Line 12 assigns a one-pixel transparent image to the new actors (so the greenfoot logo image is not shown). Line 13 adds those actors into the world.
and for the y axis' all my images are set to plug into the same place, as in my mouth will fit into the right place at (25, 29) same as the rest of my parts so should I just change the ys to all 29?
I would think the x-coordinates would be the same (which would put all actors along the same vertical), not the y-coordinates, which means you would adjust the '200' on line 13. Still, where along that vertical would need adjusting in the int array of ys. Actually, line 12 can be removed as calling setCombination at the end of the constructor will assign appropriate images to the actors.
TrippSci TrippSci

2021/10/19

#
Sweet thanks I've got it down pretty good with my own images and stuff, out of curiosity how easy would it be to add in a new "actor" such as background where I have a random flat color image put behind the made character. Basically what I'm asking is what I would have to change and account for? Also is there any way to have it save a picture of sorts of what the world looks like at that specific moment or would I have to manually like screenshot it if I wanted a picture of the different combinations that were made?
TrippSci TrippSci

2021/10/19

#
Adding onto my question how would I possibly activate the new combination via say "space" rather than needing to restart?
danpost danpost

2021/10/19

#
TrippSci wrote...
Adding onto my question how would I possibly activate the new combination via say "space" rather than needing to restart?
Add into MyWorld:
public void act()
{
    String key = Greenfoot.getKey();
    if (key == null) return;
    if ("space".equals(key))
    {
        Greenfoot.setWorld(new MyWorld());
    }
}
TrippSci wrote...
how easy would it be to add in a new "actor" such as background where I have a random flat color image put behind the made character.
Actually, you do not need an actor for that. Just change the background color of the world.
what I would have to change and account for?
Best would probably be to build on the existing random value.
is there any way to have it save a picture of sorts of what the world looks like at that specific moment or would I have to manually like screenshot it if I wanted a picture of the different combinations that were made?
"save" -- in what context? ? saving one or multiple ? saving only during a program session or permanently Another question is will you be uploading the project onto a website?
TrippSci TrippSci

2021/10/19

#
danpost wrote...
Actually, you do not need an actor for that. Just change the background color of the world.
By actor I mean like the "
String[] features = { "mouth", "eyes", "hair", "clothes", "ears" };
Actor[] actors = new Actor[5]; // actor to portray images of features
" Like if i added in another variable like "Background"
danpost wrote...
Best would probably be to build on the existing random value.
Makes sense was hoping i could make it run through all the combos 1 by 1 sort of thing as a example for just how many there are as you can "endlessly" click away as they change slowly
danpost wrote...
"save" -- in what context? ? saving one or multiple ? saving only during a program session or permanently
and I meant like if it could like take a screenshot of how the world looks and save it to my computer
danpost wrote...
Another question is will you be uploading the project onto a website?
Probably not, honestly didn't know what was possible haha, Why?
TrippSci TrippSci

2021/10/20

#
Also just realized my "ears" part isn't actually randomizing its just only choosing "ears1"
TrippSci TrippSci

2021/10/20

#
Nevermind, seems the random number "    
setPartNumbers(Greenfoot.getRandomNumber(3125));
" was too small to let in factors of the ears, how would I do the math on that i completely forgot if it was a combination or permutation
Spock47 Spock47

2021/10/20

#
It is "n-tuples" (i.e. list of numbers, ordered, with repetition), so the formula is k^n with k is the number of possibilities per feature and n is the number of features. So, in your case, you have 5^5 = 3125, so you already have the right number. I guess, there was a small typo in one of the lines in the method setPartNumbers:
for (int i=0, n=1; i<5; i++, n*=5) partNums[i] = (comboNum/n)%5;
(n should start at 1, not at 5) Live long and prosper, Spock47
TrippSci TrippSci

2021/10/20

#
Spock47 wrote...
It is "n-tuples" (i.e. list of numbers, ordered, with repetition), so the formula is k^n with k is the number of possibilities per feature and n is the number of features. So, in your case, you have 5^5 = 3125, so you already have the right number. I guess, there was a small typo in one of the lines in the method setPartNumbers:
(int i=0, n=1; i<5; i++, n*=5) partNums[i] = (comboNum/n)%5;
(n should start at 1, not at 5)
Thank you so much now its making much more sense!
You need to login to post a reply.