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!
#Help needed shuffling with pre-planted number
71 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @rich ore! Please use
/closeor theClose Postbutton 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.
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
As I said, I can only send string values between them. This is an ArrayList object that holds many Card objects
...i don't see how that's an issue
to continue my previous thought; wait, is this a card game as in playing cards
How do I send an ArrayList as a string and than from a string to an object?
Yes
serialization/encoding
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
The Arraylist holds roughly 50 cards. It would be a rather beefy string- are you sure no data would get lost on the way?
All good
you could do it within 100 chars = 800 bits
that's not really beefy
Each card holds it's image in string value and card type in string value
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
Actually I'm making uno lol
oh that wouldn't be playing cards then
for instance, the red 5 card's string value is "r 5"
also uno uses a deck of 100+ cards, btw
I'm aware
iirc it's 110
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?
yeah
the space there isn't really necessary, and if you design an encoding that always uses 2 chars per card, you could achieve the 816 bits i mentioned
not the best solution anymore considering the cards are known
i'm using parse(" ") to separate the many messages i'm sending so for the way i have it set up it is
(i work with what i have stored in my brain)
i was assuming you had sent the deck and then a rng number to shuffle afterwards (as opposed to sending the shuffled deck to begin with), because i had the tcg kind of card game in mind
so yeah your idea works fine here
Ah ok
well i mean not quite
it's the same as the servers (or well they should be), except they're generated instead of received
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
still maintains the important thing, which is the server being the source of truth, so i see no issue with your solution
I may not be doing it in the best way but It's what i have written
Alright. So how would I do it lol
Collections.shuffle has an overload that lets you provide a Random, a source of randomness
https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#shuffle-java.util.List-java.util.Random-
Random can be seeded
https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#Random-long-
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
yep
yeah that's how seeds work
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?
i mean yeah that's how you shuffle a list
the point is that you can get consistent shuffling though
Collections.shuffle(deck, new Random(seed));
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?
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.
/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);
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]
ggwp
Just gonna close this if there's no way to reward you
sec
(i tried it out and it is working perfectly)
there we go
it'll come up when closing the post
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.