#Help needed shuffling with pre-planted number

71 messages · Page 1 of 1 (latest)

rich ore
#

Making card game with clients and a server. Only form of communication between them are strings. To shuffle the deck and have it be the same amongst all clients, I have determined that the server needs to make a random number, than pass this number to all the clients. The clients, using the same number the other clients are using, will shuffle the ArrayList that holds the deck of card objects. This way, all clients will have the same exact deck afterwards. How would I go about making a randomizer that uses a single number to control the randomization? Thanks for your time (and possibly) your help!

blissful jettyBOT
#

This post has been reserved for your question.

Hey @rich ore! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

spark barn
#

why not just shuffle the deck on the server and send that data?

#

much less data duplication and you maintain the server as the source of truth

rich ore
spark barn
#

...i don't see how that's an issue

#

to continue my previous thought; wait, is this a card game as in playing cards

rich ore
#

How do I send an ArrayList as a string and than from a string to an object?

spark barn
spark barn
# rich ore Yes

i was initially understanding it as like a tcg kind of card game, where the server would also have to send what cards were in the deck to begin with, this makes more sense mb

rich ore
spark barn
#

that's not really beefy

rich ore
#

Each card holds it's image in string value and card type in string value

spark barn
#

you don't need to send the entire card, every ace of spades is equal, you only need to send the value and suit of the card

spark barn
#

oh that wouldn't be playing cards then

rich ore
#

for instance, the red 5 card's string value is "r 5"

spark barn
#

also uno uses a deck of 100+ cards, btw

rich ore
#

I'm aware

spark barn
#

iirc it's 110

rich ore
#

Simplifying it for myself lmao

#

So you're saying my best solution would just to use Collections.shuffle and than pass this as a string, than back into an object again?

spark barn
spark barn
rich ore
#

(i work with what i have stored in my brain)

spark barn
rich ore
#

ah no

#

each client holds their own deck

spark barn
#

so yeah your idea works fine here

rich ore
#

Ah ok

spark barn
#

it's the same as the servers (or well they should be), except they're generated instead of received

rich ore
# spark barn well i mean not quite

Yes quite, each client holds their own deck and send messages to the server telling each other client what each client is doing. If a client draws a card, that client gets the card but all the other clients get rid of the top card in the deck

spark barn
#

still maintains the important thing, which is the server being the source of truth, so i see no issue with your solution

rich ore
#

I may not be doing it in the best way but It's what i have written

rich ore
spark barn
rich ore
#

So by "random can be seeded", you mean I can have the same "random" number across all clients?

#

Because the issue is figuring out how to randomize with just one number

#

I can easily have the server just make up a number and pass it to the clients

spark barn
rich ore
#

OOH

#

So you're saying it'll be just like Collections.shuffle(insert here)

#

but the same across all clients

#

or have I completely missed the point?

spark barn
#

the point is that you can get consistent shuffling though
Collections.shuffle(deck, new Random(seed));

rich ore
#

with "seed" being just a number (EX: 23737)?

#

(I read the docs, just a sanity check for me from mr pro himself)

#

Just checked, you are completely right

#

Thank you very much, I joined this server just for this- is there a way to give you points for helping me or something?

blissful jettyBOT
spark barn
#

/run

import java.util.*;
import java.util.stream.*;
Random r1 = new Random(1);
Random r2 = new Random(1);
List<Integer> l1 = new ArrayList<>(List.of(0, 1, 2, 3, 4));
List<String> l2 = new ArrayList<>(List.of("a", "b", "c", "d", "e"));
System.out.println(IntStream.range(0, 5).mapToObj((i) -> String.valueOf(r1.nextInt())).collect(Collectors.joining(" ")));
System.out.println(IntStream.range(0, 5).mapToObj((i) -> String.valueOf(r2.nextInt())).collect(Collectors.joining(" ")));
Collections.shuffle(l1, r1);
Collections.shuffle(l2, r2);
System.out.println(l1);
System.out.println(l2);
inner scrollBOT
#

Here is your java(15.0.2) output @spark barn

-1155869325 431529176 1761283695 1749940626 892128508
-1155869325 431529176 1761283695 1749940626 892128508
[0, 2, 3, 1, 4]
[a, c, d, b, e]
rich ore
#

ggwp

spark barn
#

mobile dev moment

#

fml

rich ore
#

Just gonna close this if there's no way to reward you

spark barn
#

sec

rich ore
#

(i tried it out and it is working perfectly)

spark barn
#

there we go

spark barn
rich ore
#

Thank you very much!

blissful jettyBOT
# rich ore Thank you very much!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.